When implementing a custom account type in AccountManager in Android I have the following problem for the sign in flow:
The sign in should happen through an OAuth provider. Therefore I have created a SignInActivity which launches a WebView and starts an OAuth flow. This works fine, when the callback is received to my-custom-scheme://callback the WebView detects it, receives the code querystring parameter and completes the flow. The disadvantage with using a WebView is that even though the user might already have an active session in the browser, this session is not used in the WebView so the user will have to login again in the WebView.
To solve this, I tried switching to using an intent-filter in AndroidManifest.xml, like this:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="my-custom-scheme" android:path="callback"/>
</intent-filter>
Instead of opening a WebView in the SignInActivity, I then launch a browser intent and wait for the browser to hit my-custom-scheme://callback.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, "http://oauth2provider/authorize");
startActivity(browserIntent);
finish();
In my SignInActivity I have the following code to handle the callback:
if (intent != null && intent.getData() != null && getString("my-custom-scheme").equals(intent.getData().getScheme())) {
String code = getIntent().getData().getQueryParameter("code");
// complete oauth flow
}
This works. But, to the problem (finally!):
- If the user is not signed in, the browser intent will display the sign in page for the oauth provider. After the user has signed in, Chrome will redirect to my-custom-scheme://callback and
SignInActivitywill launch to handle the intent. As this activity is invisible, the browser will stay open on the sign in page, and to the user it will look like nothing happened. The browser never closes. - If the user is already signed in, the oauth provider will redirect directly to my-custom-scheme://callback. In this case, the browser tab is closed automatically but the browser itself remains open (with no tabs visible).
So my question is: is there anyway to make the browser behave differently after having redirected to my-custom-scheme://callback? Ideally, I would like it to simply close after having redirected to the callback, and to return to the previous activity in the activity stack (i.e. to the activity that started the SignInActivity from the beginning).
0 comments:
Post a Comment