Cleanup of VerifyAuthenticationTask

This commit is contained in:
Peter Stuifzand 2018-04-22 00:32:35 +02:00
parent a009adddc9
commit bdee44ab8e

View File

@ -1,6 +1,5 @@
package eu.stuifzand.micropub.auth; package eu.stuifzand.micropub.auth;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse; import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.content.Intent; import android.content.Intent;
@ -97,24 +96,23 @@ public class VerifyAuthenticationTask extends AsyncTask<String, Void, VerifyAuth
return new AuthenticationResult("Unsuccessful response from authorization_endpoint: HTTP status code is " + String.valueOf(response.code())); 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")) { 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();
} try {
JsonElement jsonElement = parser.parse(body.string());
JsonObject element = jsonElement.getAsJsonObject();
JsonParser parser = new JsonParser(); JsonElement meElement = element.get("me");
try { if (meElement == null) {
JsonElement jsonElement = parser.parse(body.string()); return new AuthenticationResult("Missing element \"me\" in authorization_endpoint response");
JsonObject element = jsonElement.getAsJsonObject(); }
String resultMe = meElement.getAsString();
JsonElement meElement = element.get("me"); return new AuthenticationResult(resultMe, code);
if (meElement == null) { } catch (JsonParseException e) {
return new AuthenticationResult("Missing element \"me\" in authorization_endpoint response"); return new AuthenticationResult("Could not parse json response from authorization_endpoint");
} }
String resultMe = meElement.getAsString();
return new AuthenticationResult(resultMe, code);
} catch (JsonParseException e) {
return new AuthenticationResult("Could not parse json response from authorization_endpoint");
} }
return new AuthenticationResult("Unsupported content type of authorization_endpoint response: " + response.header("Content-Type"));
} catch (IOException e) { } catch (IOException e) {
return new AuthenticationResult("Could not get the response from the endpoint"); return new AuthenticationResult("Could not get the response from the endpoint");
} finally { } finally {