Improve error handling in verification

This commit is contained in:
Peter Stuifzand 2018-03-01 20:10:53 +01:00
parent a1777f7d7e
commit 28576f6846
4 changed files with 80 additions and 22 deletions

View File

@ -9,6 +9,7 @@ import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
@ -40,17 +41,24 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity {
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();
builder.setQueryParameter("me", me)
.setQueryParameter("client_id", "https://stuifzand.eu/micropub")
.setQueryParameter("redirect_uri", "https://stuifzand.eu/micropub-auth")
.setQueryParameter("response_type", "code")
.setQueryParameter("state", "1234")
.setQueryParameter("scope", "create edit update post delete");
Log.i("micropub", builder.toString());
.setQueryParameter("state", "1234") // @TODO use random states, check the state later
.setQueryParameter("scope", "create edit update post delete"); // @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)
public boolean shouldOverrideUrlLoading(WebView viewx, WebResourceRequest request) {

View File

@ -9,6 +9,7 @@ import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@ -93,12 +94,14 @@ public class Authenticator extends AbstractAccountAuthenticator {
Response tokenResponse = null;
try {
tokenResponse = call.execute();
if (tokenResponse.isSuccessful()) {
ResponseBody body = tokenResponse.body();
JsonParser parser = new JsonParser();
JsonObject element = parser.parse(body.string()).getAsJsonObject();
authToken = element.get("access_token").getAsString();
}
} catch (IOException e) {
Log.e("micropub", "Failed getting token response", e);
} finally {
if (tokenResponse != null) {
tokenResponse.close();

View File

@ -6,7 +6,9 @@ import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import java.io.IOException;
@ -25,15 +27,32 @@ public class VerifyAuthenticationTask extends AsyncTask<String, Void, VerifyAuth
private final AuthenticationActivity activity;
public class AuthenticationResult {
private boolean success;
private String errorMessage;
public String me;
public String scope;
public String code;
public AuthenticationResult(String errorMessage) {
this.success = false;
this.errorMessage = errorMessage;
}
public AuthenticationResult(String me, String scope, String code) {
this.success = true;
this.me = me;
this.scope = scope;
this.code = code;
}
public boolean isSuccessful() {
return this.success;
}
public String getErrorMessage() {
return errorMessage;
}
}
public VerifyAuthenticationTask(AccountAuthenticatorResponse response, AuthenticationActivity activity) {
@ -52,24 +71,47 @@ public class VerifyAuthenticationTask extends AsyncTask<String, Void, VerifyAuth
.add("redirect_uri", "https://stuifzand.eu/micropub-auth")
.add("client_id", "https://stuifzand.eu/micropub")
.build();
Request request = new Request.Builder()
.addHeader("Accept", "application/json")
.url(args[0])
.url(endpoint)
.method("POST", formBody)
.build();
OkHttpClient client = new OkHttpClient();
String msg;
Call call = client.newCall(request);
Response response = null;
try {
response = call.execute();
if (!response.isSuccessful()) {
return new AuthenticationResult("Unsuccessful response from authorization_endpoint: HTTP status code is " + String.valueOf(response.code()));
}
ResponseBody body = response.body();
if (!response.header("Content-Type").contains("application/json")) {
return new AuthenticationResult("Unsupported content type of authorization_endpoint response: " + response.header("Content-Type"));
}
JsonParser parser = new JsonParser();
JsonObject element = parser.parse(body.string()).getAsJsonObject();
return new AuthenticationResult(element.get("me").getAsString(), element.get("scope").getAsString(), code);
try {
JsonElement jsonElement = parser.parse(body.string());
JsonObject element = jsonElement.getAsJsonObject();
JsonElement meElement = element.get("me");
if (meElement == null) {
return new AuthenticationResult("Missing element \"me\" in authorization_endpoint response");
}
String resultMe = meElement.getAsString();
JsonElement scopeElement = element.get("scope");
if (scopeElement == null) {
return new AuthenticationResult("Missing element \"scope\" in authorization_endpoint response");
}
String resultScope = scopeElement.getAsString();
return new AuthenticationResult(resultMe, resultScope, code);
} catch (JsonParseException e) {
return new AuthenticationResult("Could not parse json response from authorization_endpoint");
}
} catch (IOException e) {
return null;
return new AuthenticationResult("Could not get the response from the endpoint");
} finally {
if (response != null) {
response.close();
@ -78,6 +120,7 @@ public class VerifyAuthenticationTask extends AsyncTask<String, Void, VerifyAuth
}
protected void onPostExecute(AuthenticationResult message) {
if (message.isSuccessful()) {
Bundle bundle = new Bundle();
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, message.me);
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, "Indieauth");
@ -86,5 +129,8 @@ public class VerifyAuthenticationTask extends AsyncTask<String, Void, VerifyAuth
intent.putExtras(bundle);
this.activity.finishLogin(intent);
this.response.onResult(bundle);
} else {
this.response.onError(1, "Could not verify authorization: " + message.getErrorMessage());
}
}
}

View File

@ -10,4 +10,5 @@
<string name="categories">Categories (space separated)</string>
<string name="post">Post</string>
<string name="syndication">Syndicate to</string>
<string name="user_agent">Wrimini</string>
</resources>