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()));
}
}
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);
}
}
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);
}
}
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();
}
}
}
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();
}
}
Aggregations