Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
5661f5aa
Commit
5661f5aa
authored
Dec 07, 2016
by
Yusuke Iwaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Separate common logic of {GitHub/Twitter}OAuthFragment into AbstractOAuthFragment.
parent
98db9f97
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
137 additions
and
232 deletions
+137
-232
AbstractOAuthFragment.java
.../rocket/android/fragment/oauth/AbstractOAuthFragment.java
+128
-0
GitHubOAuthFragment.java
...at/rocket/android/fragment/oauth/GitHubOAuthFragment.java
+5
-115
TwitterOAuthFragment.java
...t/rocket/android/fragment/oauth/TwitterOAuthFragment.java
+4
-117
No files found.
app/src/main/java/chat/rocket/android/fragment/oauth/AbstractOAuthFragment.java
0 → 100644
View file @
5661f5aa
package
chat
.
rocket
.
android
.
fragment
.
oauth
;
import
android.os.Bundle
;
import
android.support.annotation.Nullable
;
import
android.text.TextUtils
;
import
android.webkit.JavascriptInterface
;
import
android.webkit.WebView
;
import
chat.rocket.android.api.MethodCallHelper
;
import
chat.rocket.android.fragment.AbstractWebViewFragment
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.model.ServerConfig
;
import
chat.rocket.android.model.ddp.MeteorLoginServiceConfiguration
;
import
chat.rocket.android.realm_helper.RealmStore
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
timber.log.Timber
;
abstract
class
AbstractOAuthFragment
extends
AbstractWebViewFragment
{
protected
String
serverConfigId
;
protected
String
hostname
;
private
String
url
;
private
boolean
resultOK
;
protected
abstract
String
getOAuthServiceName
();
protected
abstract
String
generateURL
(
MeteorLoginServiceConfiguration
oauthConfig
);
private
boolean
hasValidArgs
(
Bundle
args
)
{
return
args
!=
null
&&
args
.
containsKey
(
"serverConfigId"
);
}
@Override
public
void
onCreate
(
@Nullable
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
Bundle
args
=
getArguments
();
if
(!
hasValidArgs
(
args
))
{
throw
new
IllegalArgumentException
(
"serverConfigId required"
);
}
serverConfigId
=
args
.
getString
(
"serverConfigId"
);
ServerConfig
serverConfig
=
RealmStore
.
getDefault
().
executeTransactionForRead
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
equalTo
(
"serverConfigId"
,
serverConfigId
).
findFirst
());
MeteorLoginServiceConfiguration
oauthConfig
=
RealmStore
.
get
(
serverConfigId
).
executeTransactionForRead
(
realm
->
realm
.
where
(
MeteorLoginServiceConfiguration
.
class
)
.
equalTo
(
"service"
,
getOAuthServiceName
())
.
findFirst
());
if
(
serverConfig
==
null
||
oauthConfig
==
null
)
{
throw
new
IllegalArgumentException
(
"Invalid serverConfigId given,"
);
}
hostname
=
serverConfig
.
getHostname
();
url
=
generateURL
(
oauthConfig
);
}
@Override
protected
void
navigateToInitialPage
(
WebView
webview
)
{
if
(
TextUtils
.
isEmpty
(
url
))
{
finish
();
return
;
}
resultOK
=
false
;
webview
.
loadUrl
(
url
);
webview
.
addJavascriptInterface
(
new
JSInterface
(
result
->
{
// onPageFinish is called twice... Should ignore latter one.
if
(
resultOK
)
{
return
;
}
if
(
result
!=
null
&&
result
.
optBoolean
(
"setCredentialToken"
,
false
))
{
try
{
final
String
credentialToken
=
result
.
getString
(
"credentialToken"
);
final
String
credentialSecret
=
result
.
getString
(
"credentialSecret"
);
handleOAuthCallback
(
credentialToken
,
credentialSecret
);
resultOK
=
true
;
}
catch
(
JSONException
exception
)
{
Timber
.
e
(
exception
,
"failed to parse OAuth result."
);
}
}
onOAuthCompleted
();
}),
"_rocketchet_hook"
);
}
@Override
protected
void
onPageLoaded
(
WebView
webview
,
String
url
)
{
super
.
onPageLoaded
(
webview
,
url
);
if
(
url
.
contains
(
hostname
)
&&
url
.
contains
(
"_oauth/"
+
getOAuthServiceName
()
+
"?close"
))
{
final
String
jsHookUrl
=
"javascript:"
+
"window._rocketchet_hook.handleConfig(document.getElementById('config').innerText);"
;
webview
.
loadUrl
(
jsHookUrl
);
}
}
private
interface
JSInterfaceCallback
{
void
hanldeResult
(
@Nullable
JSONObject
result
);
}
private
static
final
class
JSInterface
{
private
final
JSInterfaceCallback
jsInterfaceCallback
;
JSInterface
(
JSInterfaceCallback
callback
)
{
jsInterfaceCallback
=
callback
;
}
@JavascriptInterface
public
void
handleConfig
(
String
config
)
{
try
{
jsInterfaceCallback
.
hanldeResult
(
new
JSONObject
(
config
));
}
catch
(
Exception
exception
)
{
jsInterfaceCallback
.
hanldeResult
(
null
);
}
}
}
private
void
handleOAuthCallback
(
final
String
credentialToken
,
final
String
credentialSecret
)
{
new
MethodCallHelper
(
getContext
(),
serverConfigId
)
.
loginWithOAuth
(
credentialToken
,
credentialSecret
)
.
continueWith
(
new
LogcatIfError
());
}
protected
void
onOAuthCompleted
()
{
}
}
app/src/main/java/chat/rocket/android/fragment/oauth/GitHubOAuthFragment.java
View file @
5661f5aa
package
chat
.
rocket
.
android
.
fragment
.
oauth
;
import
android.os.Bundle
;
import
android.support.annotation.Nullable
;
import
android.text.TextUtils
;
import
android.util.Base64
;
import
android.webkit.JavascriptInterface
;
import
android.webkit.WebView
;
import
chat.rocket.android.fragment.AbstractWebViewFragment
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.api.MethodCallHelper
;
import
chat.rocket.android.model.ServerConfig
;
import
chat.rocket.android.model.ddp.MeteorLoginServiceConfiguration
;
import
chat.rocket.android.realm_helper.RealmStore
;
import
java.nio.charset.Charset
;
import
okhttp3.HttpUrl
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
timber.log.Timber
;
public
class
GitHubOAuthFragment
extends
AbstractWebViewFragment
{
private
String
serverConfigId
;
private
String
hostname
;
private
String
url
;
private
boolean
resultOK
;
public
class
GitHubOAuthFragment
extends
AbstractOAuthFragment
{
/**
* create new Fragment with ServerConfig-ID.
...
...
@@ -36,36 +21,12 @@ public class GitHubOAuthFragment extends AbstractWebViewFragment {
return
fragment
;
}
private
boolean
hasValidArgs
(
Bundle
args
)
{
return
args
!=
null
&&
args
.
containsKey
(
"serverConfigId"
);
}
@Override
public
void
onCreate
(
@Nullable
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
Bundle
args
=
getArguments
();
if
(!
hasValidArgs
(
args
))
{
throw
new
IllegalArgumentException
(
"serverConfigId required"
);
}
serverConfigId
=
args
.
getString
(
"serverConfigId"
);
ServerConfig
serverConfig
=
RealmStore
.
getDefault
().
executeTransactionForRead
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
equalTo
(
"serverConfigId"
,
serverConfigId
).
findFirst
());
MeteorLoginServiceConfiguration
oauthConfig
=
RealmStore
.
get
(
serverConfigId
).
executeTransactionForRead
(
realm
->
realm
.
where
(
MeteorLoginServiceConfiguration
.
class
)
.
equalTo
(
"service"
,
"github"
)
.
findFirst
());
if
(
serverConfig
==
null
||
oauthConfig
==
null
)
{
throw
new
IllegalArgumentException
(
"Invalid serverConfigId given,"
);
}
hostname
=
serverConfig
.
getHostname
();
url
=
generateURL
(
oauthConfig
.
getClientId
());
@Override
protected
String
getOAuthServiceName
()
{
return
"github"
;
}
private
String
generateURL
(
String
clientId
)
{
@Override
protected
String
generateURL
(
MeteorLoginServiceConfiguration
oauthConfig
)
{
final
String
clientId
=
oauthConfig
.
getClientId
();
try
{
String
state
=
Base64
.
encodeToString
(
new
JSONObject
().
put
(
"loginStyle"
,
"popup"
)
.
put
(
"credentialToken"
,
"github"
+
System
.
currentTimeMillis
())
...
...
@@ -88,75 +49,4 @@ public class GitHubOAuthFragment extends AbstractWebViewFragment {
}
return
null
;
}
@Override
protected
void
navigateToInitialPage
(
WebView
webview
)
{
if
(
TextUtils
.
isEmpty
(
url
))
{
finish
();
return
;
}
resultOK
=
false
;
webview
.
loadUrl
(
url
);
webview
.
addJavascriptInterface
(
new
JSInterface
(
result
->
{
// onPageFinish is called twice... Should ignore latter one.
if
(
resultOK
)
{
return
;
}
if
(
result
!=
null
&&
result
.
optBoolean
(
"setCredentialToken"
,
false
))
{
try
{
final
String
credentialToken
=
result
.
getString
(
"credentialToken"
);
final
String
credentialSecret
=
result
.
getString
(
"credentialSecret"
);
handleOAuthCallback
(
credentialToken
,
credentialSecret
);
resultOK
=
true
;
}
catch
(
JSONException
exception
)
{
Timber
.
e
(
exception
,
"failed to parse OAuth result."
);
}
}
onOAuthCompleted
();
}),
"_rocketchet_hook"
);
}
@Override
protected
void
onPageLoaded
(
WebView
webview
,
String
url
)
{
super
.
onPageLoaded
(
webview
,
url
);
if
(
url
.
contains
(
hostname
)
&&
url
.
contains
(
"_oauth/github?close"
))
{
final
String
jsHookUrl
=
"javascript:"
+
"window._rocketchet_hook.handleConfig(document.getElementById('config').innerText);"
;
webview
.
loadUrl
(
jsHookUrl
);
}
}
private
interface
JSInterfaceCallback
{
void
hanldeResult
(
@Nullable
JSONObject
result
);
}
private
static
final
class
JSInterface
{
private
final
JSInterfaceCallback
jsInterfaceCallback
;
JSInterface
(
JSInterfaceCallback
callback
)
{
jsInterfaceCallback
=
callback
;
}
@JavascriptInterface
public
void
handleConfig
(
String
config
)
{
try
{
jsInterfaceCallback
.
hanldeResult
(
new
JSONObject
(
config
));
}
catch
(
Exception
exception
)
{
jsInterfaceCallback
.
hanldeResult
(
null
);
}
}
}
private
void
handleOAuthCallback
(
final
String
credentialToken
,
final
String
credentialSecret
)
{
new
MethodCallHelper
(
getContext
(),
serverConfigId
)
.
loginWithOAuth
(
credentialToken
,
credentialSecret
)
.
continueWith
(
new
LogcatIfError
());
}
private
void
onOAuthCompleted
()
{
}
}
app/src/main/java/chat/rocket/android/fragment/oauth/TwitterOAuthFragment.java
View file @
5661f5aa
package
chat
.
rocket
.
android
.
fragment
.
oauth
;
import
android.os.Bundle
;
import
android.support.annotation.Nullable
;
import
android.text.TextUtils
;
import
android.util.Base64
;
import
android.webkit.JavascriptInterface
;
import
android.webkit.WebView
;
import
chat.rocket.android.api.MethodCallHelper
;
import
chat.rocket.android.fragment.AbstractWebViewFragment
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.model.ServerConfig
;
import
chat.rocket.android.model.ddp.MeteorLoginServiceConfiguration
;
import
chat.rocket.android.realm_helper.RealmStore
;
import
hugo.weaving.DebugLog
;
import
java.nio.charset.Charset
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
timber.log.Timber
;
public
class
TwitterOAuthFragment
extends
AbstractWebViewFragment
{
private
String
serverConfigId
;
private
String
hostname
;
private
String
url
;
private
boolean
resultOK
;
public
class
TwitterOAuthFragment
extends
AbstractOAuthFragment
{
/**
* create new Fragment with ServerConfig-ID.
...
...
@@ -36,36 +20,11 @@ public class TwitterOAuthFragment extends AbstractWebViewFragment {
return
fragment
;
}
private
boolean
hasValidArgs
(
Bundle
args
)
{
return
args
!=
null
&&
args
.
containsKey
(
"serverConfigId"
);
}
@Override
public
void
onCreate
(
@Nullable
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
Bundle
args
=
getArguments
();
if
(!
hasValidArgs
(
args
))
{
throw
new
IllegalArgumentException
(
"serverConfigId required"
);
}
serverConfigId
=
args
.
getString
(
"serverConfigId"
);
ServerConfig
serverConfig
=
RealmStore
.
getDefault
().
executeTransactionForRead
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
equalTo
(
"serverConfigId"
,
serverConfigId
).
findFirst
());
MeteorLoginServiceConfiguration
oauthConfig
=
RealmStore
.
get
(
serverConfigId
).
executeTransactionForRead
(
realm
->
realm
.
where
(
MeteorLoginServiceConfiguration
.
class
)
.
equalTo
(
"service"
,
"twitter"
)
.
findFirst
());
if
(
serverConfig
==
null
||
oauthConfig
==
null
)
{
throw
new
IllegalArgumentException
(
"Invalid serverConfigId given,"
);
}
hostname
=
serverConfig
.
getHostname
();
url
=
generateURL
();
@Override
protected
String
getOAuthServiceName
()
{
return
"twitter"
;
}
private
String
generateURL
(
)
{
@Override
protected
String
generateURL
(
MeteorLoginServiceConfiguration
oauthConfig
)
{
try
{
String
state
=
Base64
.
encodeToString
(
new
JSONObject
().
put
(
"loginStyle"
,
"popup"
)
.
put
(
"credentialToken"
,
"twitter"
+
System
.
currentTimeMillis
())
...
...
@@ -79,76 +38,4 @@ public class TwitterOAuthFragment extends AbstractWebViewFragment {
}
return
null
;
}
@Override
protected
void
navigateToInitialPage
(
WebView
webview
)
{
if
(
TextUtils
.
isEmpty
(
url
))
{
finish
();
return
;
}
resultOK
=
false
;
webview
.
loadUrl
(
url
);
webview
.
addJavascriptInterface
(
new
JSInterface
(
result
->
{
// onPageFinish is called twice... Should ignore latter one.
if
(
resultOK
)
{
return
;
}
if
(
result
!=
null
&&
result
.
optBoolean
(
"setCredentialToken"
,
false
))
{
try
{
final
String
credentialToken
=
result
.
getString
(
"credentialToken"
);
final
String
credentialSecret
=
result
.
getString
(
"credentialSecret"
);
handleOAuthCallback
(
credentialToken
,
credentialSecret
);
resultOK
=
true
;
}
catch
(
JSONException
exception
)
{
Timber
.
e
(
exception
,
"failed to parse OAuth result."
);
}
}
onOAuthCompleted
();
}),
"_rocketchet_hook"
);
}
@DebugLog
@Override
protected
void
onPageLoaded
(
WebView
webview
,
String
url
)
{
super
.
onPageLoaded
(
webview
,
url
);
if
(
url
.
contains
(
hostname
)
&&
url
.
contains
(
"_oauth/twitter?close"
))
{
final
String
jsHookUrl
=
"javascript:"
+
"window._rocketchet_hook.handleConfig(document.getElementById('config').innerText);"
;
webview
.
loadUrl
(
jsHookUrl
);
}
}
private
interface
JSInterfaceCallback
{
void
hanldeResult
(
@Nullable
JSONObject
result
);
}
private
static
final
class
JSInterface
{
private
final
JSInterfaceCallback
jsInterfaceCallback
;
JSInterface
(
JSInterfaceCallback
callback
)
{
jsInterfaceCallback
=
callback
;
}
@JavascriptInterface
public
void
handleConfig
(
String
config
)
{
try
{
jsInterfaceCallback
.
hanldeResult
(
new
JSONObject
(
config
));
}
catch
(
Exception
exception
)
{
jsInterfaceCallback
.
hanldeResult
(
null
);
}
}
}
private
void
handleOAuthCallback
(
final
String
credentialToken
,
final
String
credentialSecret
)
{
new
MethodCallHelper
(
getContext
(),
serverConfigId
)
.
loginWithOAuth
(
credentialToken
,
credentialSecret
)
.
continueWith
(
new
LogcatIfError
());
}
private
void
onOAuthCompleted
()
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment