Search in sources :

Example 6 with ProcessingException

use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.

the class BaseIDXClient method handleErrorResponse.

private void handleErrorResponse(Request request, Response response) throws IOException, ProcessingException {
    int httpStatus = response.getHttpStatus();
    String errorMsg = "Request to " + request.getResourceUrl() + " failed.";
    JsonNode errorResponseJson;
    if (response.getHeaders().getContentType() != null && response.getHeaders().getContentType().toString().contains("application/json") || response.getHeaders().getContentType().toString().contains("application/ion+json")) {
        errorResponseJson = objectMapper.readTree(response.getBody());
        ErrorResponse errorResponseDetails = objectMapper.convertValue(errorResponseJson, ErrorResponse.class);
        if (errorResponseDetails.getError() == null && errorResponseDetails.getMessages() == null) {
            getErrorsFromRemediationOptions(errorResponseDetails, errorResponseJson);
        }
        throw new ProcessingException(httpStatus, errorMsg, errorResponseDetails);
    } else {
        throw new ProcessingException(httpStatus, errorMsg);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 7 with ProcessingException

use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.

the class BaseIDXClient method introspect.

@Override
public IDXResponse introspect(IDXClientContext idxClientContext) throws ProcessingException {
    IDXResponse idxResponse;
    IntrospectRequest introspectRequest = new IntrospectRequest(idxClientContext.getInteractionHandle());
    try {
        Request request = new DefaultRequest(HttpMethod.POST, clientConfiguration.getBaseUrl() + "/idp/idx/introspect", null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(introspectRequest)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        idxResponse = objectMapper.convertValue(responseJsonNode, IDXResponse.class);
    } catch (IOException | HttpException e) {
        throw new ProcessingException(e);
    }
    return idxResponse;
}
Also used : ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) Response(com.okta.commons.http.Response) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) DefaultRequest(com.okta.commons.http.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(com.okta.commons.http.HttpException) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) IOException(java.io.IOException) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 8 with ProcessingException

use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.

the class BaseIDXClient method poll.

@Override
public IDXResponse poll(PollRequest pollRequest, String href) throws ProcessingException {
    IDXResponse idxResponse;
    try {
        Request request = new DefaultRequest(HttpMethod.POST, Strings.hasText(href) ? href : clientConfiguration.getBaseUrl() + "/idp/idx/challenge/poll", null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(pollRequest)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        idxResponse = objectMapper.convertValue(responseJsonNode, IDXResponse.class);
    } catch (IOException | HttpException e) {
        throw new ProcessingException(e);
    }
    return idxResponse;
}
Also used : ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) Response(com.okta.commons.http.Response) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) DefaultRequest(com.okta.commons.http.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(com.okta.commons.http.HttpException) IOException(java.io.IOException) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 9 with ProcessingException

use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.

the class BaseIDXClient method revokeToken.

@Override
public void revokeToken(String tokenType, String token) throws ProcessingException {
    StringBuilder urlParameters = new StringBuilder();
    urlParameters.append("client_id=").append(clientConfiguration.getClientId());
    if (Strings.hasText(clientConfiguration.getClientSecret())) {
        urlParameters.append("&client_secret=").append(clientConfiguration.getClientSecret());
    }
    urlParameters.append("&token_type_hint=").append(tokenType);
    urlParameters.append("&token=").append(token);
    try {
        Request request = new DefaultRequest(HttpMethod.POST, normalizedIssuerUri(clientConfiguration.getIssuer(), "/v1/revoke"), null, getHttpHeaders(true), new ByteArrayInputStream(urlParameters.toString().getBytes(StandardCharsets.UTF_8)), -1L);
        requestExecutor.executeRequest(request);
    } catch (HttpException e) {
        throw new ProcessingException(e);
    }
}
Also used : DefaultRequest(com.okta.commons.http.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) HttpException(com.okta.commons.http.HttpException) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 10 with ProcessingException

use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.

the class IDXAuthenticationWrapper method authenticate.

/**
 * Authenticate user with the supplied Authentication options (username and password) and
 * returns the Authentication response object that contains:
 * - IDX Client context
 * - Token (access_token/id_token/refresh_token) object
 * - Authentication status
 * <p>
 * Note: This requires 'Password' as the ONLY required factor in app Sign-on policy configuration.
 *
 * @param authenticationOptions the Authenticator options
 * @return the Authentication response
 */
public AuthenticationResponse authenticate(AuthenticationOptions authenticationOptions, ProceedContext proceedContext) {
    try {
        // Check if identify flow needs to include credentials
        boolean isIdentifyInOneStep = proceedContext.isIdentifyInOneStep();
        AuthenticationTransaction identifyTransaction = AuthenticationTransaction.proceed(client, proceedContext, () -> {
            IdentifyRequest identifyRequest;
            if (isIdentifyInOneStep) {
                Credentials credentials = new Credentials();
                credentials.setPasscode(authenticationOptions.getPassword());
                identifyRequest = IdentifyRequestBuilder.builder().withIdentifier(authenticationOptions.getUsername()).withCredentials(credentials).withStateHandle(proceedContext.getStateHandle()).build();
            } else {
                identifyRequest = IdentifyRequestBuilder.builder().withIdentifier(authenticationOptions.getUsername()).withStateHandle(proceedContext.getStateHandle()).build();
            }
            // identify user
            return client.identify(identifyRequest, proceedContext.getHref());
        });
        AuthenticationResponse identifyResponse = identifyTransaction.asAuthenticationResponse();
        if (isIdentifyInOneStep || identifyResponse.getErrors() != null && !identifyResponse.getErrors().isEmpty()) {
            return identifyResponse;
        }
        AuthenticationTransaction passwordTransaction = selectPasswordAuthenticatorIfNeeded(identifyTransaction);
        AuthenticationTransaction answerTransaction = passwordTransaction.proceed(() -> {
            // answer password authenticator challenge
            Credentials credentials = new Credentials();
            credentials.setPasscode(authenticationOptions.getPassword());
            // build answer password authenticator challenge request
            AnswerChallengeRequest passwordAuthenticatorAnswerChallengeRequest = AnswerChallengeRequestBuilder.builder().withStateHandle(passwordTransaction.getStateHandle()).withCredentials(credentials).build();
            return passwordTransaction.getRemediationOption(RemediationType.CHALLENGE_AUTHENTICATOR).proceed(client, passwordAuthenticatorAnswerChallengeRequest);
        });
        return answerTransaction.asAuthenticationResponse();
    } catch (ProcessingException e) {
        return handleProcessingException(e);
    } catch (IllegalArgumentException e) {
        return handleIllegalArgumentException(e);
    }
}
Also used : AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) Credentials(com.okta.idx.sdk.api.model.Credentials) WrapperUtil.handleIllegalArgumentException(com.okta.idx.sdk.api.client.WrapperUtil.handleIllegalArgumentException) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException) WrapperUtil.handleProcessingException(com.okta.idx.sdk.api.client.WrapperUtil.handleProcessingException)

Aggregations

ProcessingException (com.okta.idx.sdk.api.exception.ProcessingException)28 AnswerChallengeRequest (com.okta.idx.sdk.api.request.AnswerChallengeRequest)21 IdentifyRequest (com.okta.idx.sdk.api.request.IdentifyRequest)18 EnrollRequest (com.okta.idx.sdk.api.request.EnrollRequest)17 EnrollUserProfileUpdateRequest (com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest)17 RecoverRequest (com.okta.idx.sdk.api.request.RecoverRequest)17 ErrorResponse (com.okta.idx.sdk.api.response.ErrorResponse)17 ChallengeRequest (com.okta.idx.sdk.api.request.ChallengeRequest)16 PollRequest (com.okta.idx.sdk.api.request.PollRequest)16 SkipAuthenticatorEnrollmentRequest (com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest)16 IDXResponse (com.okta.idx.sdk.api.response.IDXResponse)15 TokenResponse (com.okta.idx.sdk.api.response.TokenResponse)15 DefaultRequest (com.okta.commons.http.DefaultRequest)14 HttpException (com.okta.commons.http.HttpException)14 Request (com.okta.commons.http.Request)14 Response (com.okta.commons.http.Response)14 CancelRequest (com.okta.idx.sdk.api.request.CancelRequest)14 IntrospectRequest (com.okta.idx.sdk.api.request.IntrospectRequest)14 ByteArrayInputStream (java.io.ByteArrayInputStream)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)13