Improve error handling in verification
This commit is contained in:
parent
a1777f7d7e
commit
28576f6846
|
@ -9,6 +9,7 @@ import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.RequiresApi;
|
import android.support.annotation.RequiresApi;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.webkit.WebResourceError;
|
||||||
import android.webkit.WebResourceRequest;
|
import android.webkit.WebResourceRequest;
|
||||||
import android.webkit.WebSettings;
|
import android.webkit.WebSettings;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
|
@ -40,17 +41,24 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity {
|
||||||
WebView webview = findViewById(R.id.webview);
|
WebView webview = findViewById(R.id.webview);
|
||||||
WebSettings webSettings = webview.getSettings();
|
WebSettings webSettings = webview.getSettings();
|
||||||
webSettings.setJavaScriptEnabled(true);
|
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", "https://stuifzand.eu/micropub-auth")
|
.setQueryParameter("redirect_uri", "https://stuifzand.eu/micropub-auth")
|
||||||
.setQueryParameter("response_type", "code")
|
.setQueryParameter("response_type", "code")
|
||||||
.setQueryParameter("state", "1234")
|
.setQueryParameter("state", "1234") // @TODO use random states, check the state later
|
||||||
.setQueryParameter("scope", "create edit update post delete");
|
.setQueryParameter("scope", "create edit update post delete"); // @TODO use different scope
|
||||||
Log.i("micropub", builder.toString());
|
Log.i("micropub", "LoadUrl: " + builder.toString());
|
||||||
webview.loadUrl(builder.toString());
|
webview.loadUrl(builder.toString());
|
||||||
webview.setWebViewClient(new WebViewClient() {
|
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)
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||||
public boolean shouldOverrideUrlLoading(WebView viewx, WebResourceRequest request) {
|
public boolean shouldOverrideUrlLoading(WebView viewx, WebResourceRequest request) {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
@ -93,12 +94,14 @@ public class Authenticator extends AbstractAccountAuthenticator {
|
||||||
Response tokenResponse = null;
|
Response tokenResponse = null;
|
||||||
try {
|
try {
|
||||||
tokenResponse = call.execute();
|
tokenResponse = call.execute();
|
||||||
ResponseBody body = tokenResponse.body();
|
if (tokenResponse.isSuccessful()) {
|
||||||
JsonParser parser = new JsonParser();
|
ResponseBody body = tokenResponse.body();
|
||||||
JsonObject element = parser.parse(body.string()).getAsJsonObject();
|
JsonParser parser = new JsonParser();
|
||||||
authToken = element.get("access_token").getAsString();
|
JsonObject element = parser.parse(body.string()).getAsJsonObject();
|
||||||
|
authToken = element.get("access_token").getAsString();
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
Log.e("micropub", "Failed getting token response", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (tokenResponse != null) {
|
if (tokenResponse != null) {
|
||||||
tokenResponse.close();
|
tokenResponse.close();
|
||||||
|
|
|
@ -6,7 +6,9 @@ import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -25,15 +27,32 @@ public class VerifyAuthenticationTask extends AsyncTask<String, Void, VerifyAuth
|
||||||
private final AuthenticationActivity activity;
|
private final AuthenticationActivity activity;
|
||||||
|
|
||||||
public class AuthenticationResult {
|
public class AuthenticationResult {
|
||||||
|
private boolean success;
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
public String me;
|
public String me;
|
||||||
public String scope;
|
public String scope;
|
||||||
public String code;
|
public String code;
|
||||||
|
|
||||||
|
public AuthenticationResult(String errorMessage) {
|
||||||
|
this.success = false;
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
public AuthenticationResult(String me, String scope, String code) {
|
public AuthenticationResult(String me, String scope, String code) {
|
||||||
|
this.success = true;
|
||||||
this.me = me;
|
this.me = me;
|
||||||
this.scope = scope;
|
this.scope = scope;
|
||||||
this.code = code;
|
this.code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isSuccessful() {
|
||||||
|
return this.success;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public VerifyAuthenticationTask(AccountAuthenticatorResponse response, AuthenticationActivity activity) {
|
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("redirect_uri", "https://stuifzand.eu/micropub-auth")
|
||||||
.add("client_id", "https://stuifzand.eu/micropub")
|
.add("client_id", "https://stuifzand.eu/micropub")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.addHeader("Accept", "application/json")
|
.addHeader("Accept", "application/json")
|
||||||
.url(args[0])
|
.url(endpoint)
|
||||||
.method("POST", formBody)
|
.method("POST", formBody)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
String msg;
|
|
||||||
Call call = client.newCall(request);
|
Call call = client.newCall(request);
|
||||||
Response response = null;
|
Response response = null;
|
||||||
try {
|
try {
|
||||||
response = call.execute();
|
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();
|
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();
|
JsonParser parser = new JsonParser();
|
||||||
JsonObject element = parser.parse(body.string()).getAsJsonObject();
|
try {
|
||||||
return new AuthenticationResult(element.get("me").getAsString(), element.get("scope").getAsString(), code);
|
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) {
|
} catch (IOException e) {
|
||||||
return null;
|
return new AuthenticationResult("Could not get the response from the endpoint");
|
||||||
} finally {
|
} finally {
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
response.close();
|
response.close();
|
||||||
|
@ -78,13 +120,17 @@ public class VerifyAuthenticationTask extends AsyncTask<String, Void, VerifyAuth
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onPostExecute(AuthenticationResult message) {
|
protected void onPostExecute(AuthenticationResult message) {
|
||||||
Bundle bundle = new Bundle();
|
if (message.isSuccessful()) {
|
||||||
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, message.me);
|
Bundle bundle = new Bundle();
|
||||||
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, "Indieauth");
|
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, message.me);
|
||||||
bundle.putString(AuthenticationActivity.PARAM_USER_PASS, message.code);
|
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, "Indieauth");
|
||||||
Intent intent = new Intent();
|
bundle.putString(AuthenticationActivity.PARAM_USER_PASS, message.code);
|
||||||
intent.putExtras(bundle);
|
Intent intent = new Intent();
|
||||||
this.activity.finishLogin(intent);
|
intent.putExtras(bundle);
|
||||||
this.response.onResult(bundle);
|
this.activity.finishLogin(intent);
|
||||||
|
this.response.onResult(bundle);
|
||||||
|
} else {
|
||||||
|
this.response.onError(1, "Could not verify authorization: " + message.getErrorMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,4 +10,5 @@
|
||||||
<string name="categories">Categories (space separated)</string>
|
<string name="categories">Categories (space separated)</string>
|
||||||
<string name="post">Post</string>
|
<string name="post">Post</string>
|
||||||
<string name="syndication">Syndicate to</string>
|
<string name="syndication">Syndicate to</string>
|
||||||
|
<string name="user_agent">Wrimini</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user