Search in sources :

Example 1 with IDXClientContext

use of com.okta.idx.sdk.api.model.IDXClientContext in project okta-idx-java by okta.

the class BaseIDXClient method interact.

@Override
public IDXClientContext interact(String token, EmailTokenType tokenType) throws ProcessingException {
    InteractResponse interactResponse;
    String codeVerifier, codeChallenge, state;
    try {
        codeVerifier = PkceUtil.generateCodeVerifier();
        codeChallenge = PkceUtil.generateCodeChallenge(codeVerifier);
        state = UUID.randomUUID().toString();
        StringBuilder urlParameters = new StringBuilder().append("client_id=").append(clientConfiguration.getClientId()).append("&client_secret=").append(clientConfiguration.getClientSecret()).append("&scope=").append(clientConfiguration.getScopes().stream().map(Object::toString).collect(Collectors.joining(" "))).append("&code_challenge=").append(codeChallenge).append("&code_challenge_method=").append(PkceUtil.CODE_CHALLENGE_METHOD).append("&redirect_uri=").append(clientConfiguration.getRedirectUri()).append("&state=").append(state);
        if (Strings.hasText(token) && !Strings.isEmpty(tokenType)) {
            if (tokenType == EmailTokenType.ACTIVATION_TOKEN) {
                urlParameters.append("&activation_token=").append(token);
            } else if (tokenType == EmailTokenType.RECOVERY_TOKEN) {
                urlParameters.append("&recovery_token=").append(token);
            }
        }
        HttpHeaders httpHeaders = getHttpHeaders(true);
        if (clientConfiguration.getDeviceContext() != null) {
            httpHeaders.setAll(clientConfiguration.getDeviceContext().getAll());
        }
        Request request = new DefaultRequest(HttpMethod.POST, normalizedIssuerUri(clientConfiguration.getIssuer(), "/v1/interact"), null, httpHeaders, new ByteArrayInputStream(urlParameters.toString().getBytes(StandardCharsets.UTF_8)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        interactResponse = objectMapper.convertValue(responseJsonNode, InteractResponse.class);
        Assert.notNull(interactResponse, "interact response cannot be null");
        Assert.notNull(interactResponse.getInteractionHandle(), "interactionHandle cannot be null");
    } catch (IOException | IllegalArgumentException | HttpException | NoSuchAlgorithmException e) {
        throw new ProcessingException(e);
    }
    return new IDXClientContext(codeVerifier, codeChallenge, interactResponse.getInteractionHandle(), state);
}
Also used : HttpHeaders(com.okta.commons.http.HttpHeaders) DefaultRequest(com.okta.commons.http.DefaultRequest) IDXClientContext(com.okta.idx.sdk.api.model.IDXClientContext) 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) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) 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) ByteArrayInputStream(java.io.ByteArrayInputStream) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) HttpException(com.okta.commons.http.HttpException) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 2 with IDXClientContext

use of com.okta.idx.sdk.api.model.IDXClientContext in project okta-idx-java by okta.

the class AuthenticationTransaction method create.

static AuthenticationTransaction create(IDXClient client, String token, EmailTokenType tokenType) throws ProcessingException {
    IDXClientContext idxClientContext;
    if (token == null) {
        idxClientContext = client.interact();
    } else {
        Assert.notNull(tokenType, "token type may not be null");
        idxClientContext = client.interact(token, tokenType);
    }
    Assert.notNull(idxClientContext, "IDX client context may not be null");
    IDXResponse introspectResponse = client.introspect(idxClientContext);
    String stateHandle = introspectResponse.getStateHandle();
    Assert.hasText(stateHandle, "State handle may not be null");
    WrapperUtil.printRemediationOptions(introspectResponse);
    return new AuthenticationTransaction(client, idxClientContext, introspectResponse);
}
Also used : IDXClientContext(com.okta.idx.sdk.api.model.IDXClientContext) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse)

Aggregations

IDXClientContext (com.okta.idx.sdk.api.model.IDXClientContext)2 IDXResponse (com.okta.idx.sdk.api.response.IDXResponse)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 DefaultRequest (com.okta.commons.http.DefaultRequest)1 HttpException (com.okta.commons.http.HttpException)1 HttpHeaders (com.okta.commons.http.HttpHeaders)1 Request (com.okta.commons.http.Request)1 Response (com.okta.commons.http.Response)1 ProcessingException (com.okta.idx.sdk.api.exception.ProcessingException)1 AnswerChallengeRequest (com.okta.idx.sdk.api.request.AnswerChallengeRequest)1 CancelRequest (com.okta.idx.sdk.api.request.CancelRequest)1 ChallengeRequest (com.okta.idx.sdk.api.request.ChallengeRequest)1 EnrollRequest (com.okta.idx.sdk.api.request.EnrollRequest)1 EnrollUserProfileUpdateRequest (com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest)1 IdentifyRequest (com.okta.idx.sdk.api.request.IdentifyRequest)1 IntrospectRequest (com.okta.idx.sdk.api.request.IntrospectRequest)1 PollRequest (com.okta.idx.sdk.api.request.PollRequest)1 RecoverRequest (com.okta.idx.sdk.api.request.RecoverRequest)1 SkipAuthenticatorEnrollmentRequest (com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest)1 ErrorResponse (com.okta.idx.sdk.api.response.ErrorResponse)1