Search in sources :

Example 1 with OktaIdToken

use of com.okta.oidc.OktaIdToken in project okta-oidc-android by okta.

the class TokenRequest method executeRequest.

@Override
public TokenResponse executeRequest(OktaHttpClient client) throws AuthorizationException {
    HttpResponse response = null;
    TokenResponse tokenResponse;
    try {
        response = openConnection(client);
        JSONObject json = response.asJsonWithErrorDescription();
        if (json.has(AuthorizationException.PARAM_ERROR)) {
            try {
                final String error = json.getString(AuthorizationException.PARAM_ERROR);
                throw AuthorizationException.fromOAuthTemplate(AuthorizationException.TokenRequestErrors.byString(error), error, json.optString(AuthorizationException.PARAM_ERROR_DESCRIPTION, null), UriUtil.parseUriIfAvailable(json.optString(AuthorizationException.PARAM_ERROR_URI)));
            } catch (JSONException jsonEx) {
                throw AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, jsonEx);
            }
        }
        tokenResponse = new Gson().fromJson(json.toString(), TokenResponse.class);
        tokenResponse.setCreationTime(System.currentTimeMillis());
        if (tokenResponse.getIdToken() != null) {
            OktaIdToken idToken;
            try {
                idToken = OktaIdToken.parseIdToken(tokenResponse.getIdToken());
            } catch (IllegalArgumentException | JsonIOException ex) {
                Log.e(TAG, "", ex);
                throw AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.ID_TOKEN_PARSING_ERROR, ex);
            }
            idToken.validate(this, mConfig.getIdTokenValidator());
        }
        return tokenResponse;
    } catch (IOException ex) {
        throw new AuthorizationException(ex.getMessage(), ex);
    } catch (JSONException ex) {
        throw AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, ex);
    } catch (AuthorizationException ae) {
        throw ae;
    } catch (Exception e) {
        throw AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.NETWORK_ERROR, e);
    } finally {
        if (response != null) {
            response.disconnect();
        }
    }
}
Also used : AuthorizationException(com.okta.oidc.util.AuthorizationException) HttpResponse(com.okta.oidc.net.HttpResponse) JSONException(org.json.JSONException) Gson(com.google.gson.Gson) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) OktaIdToken(com.okta.oidc.OktaIdToken) IOException(java.io.IOException) AuthorizationException(com.okta.oidc.util.AuthorizationException) JSONException(org.json.JSONException) JsonIOException(com.google.gson.JsonIOException) TokenResponse(com.okta.oidc.net.response.TokenResponse) JSONObject(org.json.JSONObject) JsonIOException(com.google.gson.JsonIOException)

Aggregations

Gson (com.google.gson.Gson)1 JsonIOException (com.google.gson.JsonIOException)1 OktaIdToken (com.okta.oidc.OktaIdToken)1 HttpResponse (com.okta.oidc.net.HttpResponse)1 TokenResponse (com.okta.oidc.net.response.TokenResponse)1 AuthorizationException (com.okta.oidc.util.AuthorizationException)1 IOException (java.io.IOException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1