I\'m building a Webapp that uses OpenId to authenticate users, like Stackoverlfow does. There will be a mobile App too, e.g. Android or iPhone. These Apps have to authenticate o
I have done the following to achieve this:
The mobile application extracts the token from the URL and uses it for authentication when talking to the server.
The WebLogin Activity looks like this: (note: you have to override "shouldOverrideUrlLoading" to stay in the same WebView. Otherwise, a new browser is open when you receive some redirect)
public class WebLogin extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if(StringUtils.contains(url, "?auth_token=")){
// extract and save token here
setResult(RESULT_OK);
finish();
}
}
});
webview.loadUrl("https://www.yourdomain.com/authapp");
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
} }
Note, I use https to make this save. If you use plain http, one could read and steal the token of a user.
[1]: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)