Authenticate through VIEW intent

This commit is contained in:
Peter Stuifzand 2018-03-23 23:56:59 +01:00
parent ac41090fdb
commit 0632bd3076
2 changed files with 66 additions and 51 deletions

View File

@ -33,14 +33,7 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<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="wrimini" />
</intent-filter>
<intent-filter> <intent-filter>
<action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND" />
@ -51,7 +44,18 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".auth.WebSigninActivity" /> <activity android:name=".auth.WebSigninActivity" />
<activity android:name=".auth.AuthenticationActivity" /> <activity android:name=".auth.AuthenticationActivity"
android:exported="true"
android:launchMode="singleTop">
<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="wrimini" />
</intent-filter>
</activity>
<activity android:name=".auth.AccountsActivity" /> <activity android:name=".auth.AccountsActivity" />
<service <service

View File

@ -30,65 +30,76 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity {
private static final String TAG = "AuthenticationActivity"; private static final String TAG = "AuthenticationActivity";
public static final String PARAM_USER_PASS = "eu.stuifzand.micropub.UserPass"; public static final String PARAM_USER_PASS = "eu.stuifzand.micropub.UserPass";
private Bundle bundle;
private Intent newIntent;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authentication); setContentView(R.layout.activity_authentication);
Intent intent = getIntent(); if (savedInstanceState != null) {
final String endpoint = intent.getStringExtra("authorization_endpoint"); bundle = savedInstanceState.getBundle("response");
final String me = intent.getStringExtra(WebsigninTask.ME); return;
final AccountAuthenticatorResponse response = intent.getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE); }
Intent intent = getIntent();
bundle = intent.getExtras();
String endpoint = bundle.getString("authorization_endpoint");
String me = bundle.getString(WebsigninTask.ME);
AccountAuthenticatorResponse response = bundle.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
WebView webview = findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setUserAgentString(getString(R.string.user_agent));
HttpUrl.Builder builder = HttpUrl.parse(endpoint).newBuilder(); HttpUrl.Builder builder = HttpUrl.parse(endpoint).newBuilder();
builder.setQueryParameter("me", me) builder.setQueryParameter("me", me)
.setQueryParameter("client_id", "https://stuifzand.eu/micropub") .setQueryParameter("client_id", "https://stuifzand.eu/micropub")
.setQueryParameter("redirect_uri", "wrimini://oauth") .setQueryParameter("redirect_uri", "wrimini://oauth")
.setQueryParameter("response_type", "code") .setQueryParameter("response_type", "code")
.setQueryParameter("state", "1234") // @TODO use random states, check the state later .setQueryParameter("state", "1234") // @TODO use random states, check the state later
.setQueryParameter("scope", "create edit update post delete"); // @TODO use different scope .setQueryParameter("scope", "create"); // @TODO use different scope
Log.i("micropub", "LoadUrl: " + builder.toString());
webview.loadUrl(builder.toString());
webview.setWebViewClient(new WebViewClient() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Log.i("micropub", request.getMethod() + " " + request.getUrl());
Log.i("micropub", error.toString());
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) String url = builder.toString();
public boolean shouldOverrideUrlLoading(WebView viewx, WebResourceRequest request) { Log.i("micropub", "LoadUrl: " + url);
Log.i("micropub", "New API: " + request.getUrl().toString()); Intent webIntent = new Intent(Intent.ACTION_VIEW);
String url = request.getUrl().toString(); webIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
if (url.startsWith("wrimini://oauth")) { webIntent.setData(Uri.parse(url));
Uri uri = Uri.parse(url); startActivity(webIntent);
String code = uri.getQueryParameter("code"); }
//String state = httpUrl.queryParameter("state");
new VerifyAuthenticationTask(response, AuthenticationActivity.this).execute(endpoint, me, code); @Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBundle("response", bundle);
super.onSaveInstanceState(outState);
}
return true; @Override
} protected void onResume() {
viewx.loadUrl(request.getUrl().toString()); super.onResume();
return false; if (getIntent() != null) {
} bundle = getIntent().getExtras();
}
}
public boolean shouldOverrideUrlLoading(WebView viewx, String url) { @Override
// TODO: fix for older versions protected void onNewIntent(Intent intent) {
Log.i("micropub", "Old API: " + url); super.onNewIntent(intent);
if (url.startsWith("http://") || url.startsWith("https://")) { Log.i("micropub", "onCreate action: " + intent.getAction());
viewx.loadUrl(url); if ("android.intent.action.VIEW".equals(intent.getAction())) {
return false; Log.i("micropub", intent.toString());
} Uri uri = intent.getData();
return true; String code = uri.getQueryParameter("code");
} String state = uri.getQueryParameter("state");
}); Bundle response = bundle;
new VerifyAuthenticationTask(
response.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE),
AuthenticationActivity.this
).execute(
response.getString("authorization_endpoint"),
response.getString(WebsigninTask.ME),
code
);
return;
}
} }
void finishLogin(Intent intent) { void finishLogin(Intent intent) {