Search in sources :

Example 11 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SyncWebAuthClientImpl method processSignInResult.

@NonNull
private Result processSignInResult(StateResult result) {
    if (result == null) {
        return Result.error(new AuthorizationException("Result is empty", new NullPointerException()));
    }
    switch(result.getStatus()) {
        case CANCELED:
            return Result.cancel();
        case ERROR:
            return Result.error(result.getException());
        case AUTHORIZED:
            mOktaState.setCurrentState(State.TOKEN_EXCHANGE);
            TokenResponse response;
            try {
                WebRequest authorizedRequest = mOktaState.getAuthorizeRequest();
                ProviderConfiguration providerConfiguration = mOktaState.getProviderConfiguration();
                AuthorizeResponse authResponse = (AuthorizeResponse) result.getAuthorizationResponse();
                if (isVerificationFlow((authResponse))) {
                    return processEmailVerification(authResponse);
                }
                validateResult(result.getAuthorizationResponse(), authorizedRequest);
                TokenRequest request = tokenExchange((AuthorizeResponse) result.getAuthorizationResponse(), providerConfiguration, (AuthorizeRequest) authorizedRequest);
                mCurrentRequest.set(new WeakReference<>(request));
                response = request.executeRequest(mHttpClient);
                mOktaState.save(response);
            } catch (OktaRepository.EncryptionException e) {
                return Result.error(EncryptionErrors.byEncryptionException(e));
            } catch (AuthorizationException e) {
                return Result.error(e);
            }
            return Result.success();
        default:
            return Result.error(new AuthorizationException("StateResult with invalid status: " + result.getStatus().name(), new IllegalStateException()));
    }
}
Also used : AuthorizeResponse(com.okta.oidc.net.response.web.AuthorizeResponse) TokenResponse(com.okta.oidc.net.response.TokenResponse) WebRequest(com.okta.oidc.net.request.web.WebRequest) AuthorizationException(com.okta.oidc.util.AuthorizationException) OktaRepository(com.okta.oidc.storage.OktaRepository) TokenRequest(com.okta.oidc.net.request.TokenRequest) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration) NonNull(androidx.annotation.NonNull)

Example 12 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SyncSessionClientImpl method getUserProfile.

@Override
public UserInfo getUserProfile() throws AuthorizationException {
    try {
        ProviderConfiguration providerConfiguration = mOktaState.getProviderConfiguration();
        TokenResponse tokenResponse = mOktaState.getTokenResponse();
        AuthorizedRequest request = userProfileRequest(providerConfiguration, tokenResponse);
        JSONObject userInfo = request.executeRequest(mHttpClient);
        mCurrentRequest.set(new WeakReference<>(request));
        return new UserInfo(userInfo);
    } catch (OktaRepository.EncryptionException e) {
        throw AuthorizationException.EncryptionErrors.byEncryptionException(e);
    }
}
Also used : AuthorizedRequest(com.okta.oidc.net.request.AuthorizedRequest) TokenResponse(com.okta.oidc.net.response.TokenResponse) JSONObject(org.json.JSONObject) OktaRepository(com.okta.oidc.storage.OktaRepository) UserInfo(com.okta.oidc.net.response.UserInfo) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration)

Example 13 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SyncSessionClientImpl method authorizedRequest.

public JSONObject authorizedRequest(@NonNull Uri uri, @Nullable Map<String, String> properties, @Nullable Map<String, String> postParameters, @NonNull ConnectionParameters.RequestMethod method) throws AuthorizationException {
    try {
        ProviderConfiguration providerConfiguration = mOktaState.getProviderConfiguration();
        TokenResponse tokenResponse = mOktaState.getTokenResponse();
        AuthorizedRequest request = createAuthorizedRequest(uri, properties, postParameters, method, providerConfiguration, tokenResponse);
        mCurrentRequest.set(new WeakReference<>(request));
        return request.executeRequest(mHttpClient);
    } catch (OktaRepository.EncryptionException e) {
        throw AuthorizationException.EncryptionErrors.byEncryptionException(e);
    }
}
Also used : AuthorizedRequest(com.okta.oidc.net.request.AuthorizedRequest) TokenResponse(com.okta.oidc.net.response.TokenResponse) OktaRepository(com.okta.oidc.storage.OktaRepository) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration)

Example 14 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse 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)

Example 15 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SyncAuthClientImpl method signIn.

@WorkerThread
@Override
public Result signIn(String sessionToken, @Nullable AuthenticationPayload payload) {
    try {
        mCancel.set(false);
        ProviderConfiguration providerConfiguration = obtainNewConfiguration();
        checkIfCanceled();
        mOktaState.setCurrentState(State.SIGN_IN_REQUEST);
        NativeAuthorizeRequest request = nativeAuthorizeRequest(sessionToken, providerConfiguration, payload);
        mCurrentRequest.set(new WeakReference<>(request));
        // Save the nativeAuth request in a AuthRequest because it is needed to verify results.
        AuthorizeRequest authRequest = new AuthorizeRequest(request.getParameters());
        mOktaState.save(authRequest);
        AuthorizeResponse authResponse = request.executeRequest(mHttpClient);
        checkIfCanceled();
        // This flow should never happen but if it does throw a exception.
        if (isVerificationFlow(authResponse)) {
            return Result.error(new AuthorizationException("Email verification required. Session: " + authResponse.getSessionHint(), null));
        }
        validateResult(authResponse, authRequest);
        mOktaState.setCurrentState(State.TOKEN_EXCHANGE);
        TokenRequest requestToken = tokenExchange(authResponse, providerConfiguration, authRequest);
        mCurrentRequest.set(new WeakReference<>(requestToken));
        TokenResponse tokenResponse = requestToken.executeRequest(mHttpClient);
        mOktaState.save(tokenResponse);
        return Result.success();
    } catch (AuthorizationException e) {
        return Result.error(e);
    } catch (IOException e) {
        return Result.cancel();
    } catch (Exception e) {
        return Result.error(new AuthorizationException(OTHER.code, e.getMessage(), e));
    } finally {
        resetCurrentState();
    }
}
Also used : AuthorizeResponse(com.okta.oidc.net.response.web.AuthorizeResponse) TokenResponse(com.okta.oidc.net.response.TokenResponse) AuthorizeRequest(com.okta.oidc.net.request.web.AuthorizeRequest) NativeAuthorizeRequest(com.okta.oidc.net.request.NativeAuthorizeRequest) AuthorizationException(com.okta.oidc.util.AuthorizationException) TokenRequest(com.okta.oidc.net.request.TokenRequest) IOException(java.io.IOException) NativeAuthorizeRequest(com.okta.oidc.net.request.NativeAuthorizeRequest) IOException(java.io.IOException) AuthorizationException(com.okta.oidc.util.AuthorizationException) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

TokenResponse (com.okta.oidc.net.response.TokenResponse)25 Test (org.junit.Test)16 ProviderConfiguration (com.okta.oidc.net.request.ProviderConfiguration)7 AuthorizationException (com.okta.oidc.util.AuthorizationException)6 OktaRepository (com.okta.oidc.storage.OktaRepository)5 Gson (com.google.gson.Gson)4 Tokens (com.okta.oidc.Tokens)4 TokenRequest (com.okta.oidc.net.request.TokenRequest)4 AuthorizeResponse (com.okta.oidc.net.response.web.AuthorizeResponse)4 RefreshTokenRequest (com.okta.oidc.net.request.RefreshTokenRequest)3 AuthorizeRequest (com.okta.oidc.net.request.web.AuthorizeRequest)3 MockRequestCallback (com.okta.oidc.util.MockRequestCallback)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 JSONObject (org.json.JSONObject)3 OIDCConfig (com.okta.oidc.OIDCConfig)2 AuthorizedRequest (com.okta.oidc.net.request.AuthorizedRequest)2 RevokeTokenRequest (com.okta.oidc.net.request.RevokeTokenRequest)2 WebRequest (com.okta.oidc.net.request.web.WebRequest)2 UserInfo (com.okta.oidc.net.response.UserInfo)2