Search in sources :

Example 1 with ProcessingException

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

the class LoginController method handleMagicLinkCallback.

@GetMapping("/magic-link/callback")
public ModelAndView handleMagicLinkCallback(HttpServletRequest request, @RequestParam(name = "state") String state, @RequestParam(name = "otp") String otp, HttpSession session) throws MalformedURLException {
    logger.info("Handling Magic link callback with state: {}, otp {}", state, otp);
    if (session.getAttribute(IDX_CLIENT_CONTEXT) == null) {
        try {
            idxClientContext = idxAuthenticationWrapper.getClientContext();
        } catch (ProcessingException e) {
            ModelAndView modelAndView = new ModelAndView("error");
            ErrorResponse errorResponse = e.getErrorResponse();
            if (errorResponse != null) {
                modelAndView.addObject("errorDetails", errorResponse.getError() + "," + errorResponse.getErrorDescription());
            } else {
                modelAndView.addObject("errorDetails", "Unknown error");
            }
            return modelAndView;
        }
        session.setAttribute(IDX_CLIENT_CONTEXT, idxClientContext);
    }
    if (idxClientContext == null) {
        ModelAndView modelAndView = new ModelAndView("error");
        modelAndView.addObject("error_details", "Unknown error");
        return modelAndView;
    }
    // if we don't have the state parameter redirect
    if (state == null) {
        return new ModelAndView("redirect:" + oktaOAuth2Properties.getRedirectUri());
    }
    String issuer = oktaOAuth2Properties.getIssuer();
    // the widget needs the base url, just grab the root of the issuer
    String orgUrl = new URL(new URL(issuer), "/").toString();
    ModelAndView mav = new ModelAndView("login");
    mav.addObject(STATE, state);
    mav.addObject(OTP, otp);
    mav.addObject(SCOPES, oktaOAuth2Properties.getScopes());
    mav.addObject(OKTA_BASE_URL, orgUrl);
    mav.addObject(OKTA_CLIENT_ID, oktaOAuth2Properties.getClientId());
    mav.addObject(INTERACTION_HANDLE, idxClientContext.getInteractionHandle());
    mav.addObject(CODE_VERIFIER, idxClientContext.getCodeVerifier());
    mav.addObject(CODE_CHALLENGE, idxClientContext.getCodeChallenge());
    mav.addObject(CODE_CHALLENGE_METHOD, CODE_CHALLENGE_METHOD_VALUE);
    // from ClientRegistration.redirectUriTemplate, if the template is change you must update this
    mav.addObject(REDIRECT_URI, request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/authorization-code/callback");
    mav.addObject(ISSUER_URI, issuer);
    session.setAttribute(CODE_VERIFIER, idxClientContext.getCodeVerifier());
    return mav;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) URL(java.net.URL) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException) ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with ProcessingException

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

the class BaseIDXClient method answerChallenge.

@Override
public IDXResponse answerChallenge(AnswerChallengeRequest answerChallengeRequest, String href) throws ProcessingException {
    IDXResponse idxResponse;
    try {
        Request request = new DefaultRequest(HttpMethod.POST, href, null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(answerChallengeRequest)), -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 3 with ProcessingException

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

the class BaseIDXClient method token.

@Override
public TokenResponse token(String url, String grantType, String interactionCode, IDXClientContext idxClientContext) throws ProcessingException {
    TokenResponse tokenResponse;
    StringBuilder urlParameters = new StringBuilder();
    urlParameters.append("grant_type=").append(grantType);
    urlParameters.append("&client_id=").append(clientConfiguration.getClientId());
    if (Strings.hasText(clientConfiguration.getClientSecret())) {
        urlParameters.append("&client_secret=").append(clientConfiguration.getClientSecret());
    }
    urlParameters.append("&interaction_code=").append(interactionCode);
    urlParameters.append("&code_verifier=").append(idxClientContext.getCodeVerifier());
    try {
        Request request = new DefaultRequest(HttpMethod.POST, url, null, getHttpHeaders(true), 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());
        tokenResponse = objectMapper.convertValue(responseJsonNode, TokenResponse.class);
    } catch (IOException | HttpException e) {
        throw new ProcessingException(e);
    }
    return tokenResponse;
}
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) 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) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 4 with ProcessingException

use of com.okta.idx.sdk.api.exception.ProcessingException 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 5 with ProcessingException

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

the class BaseIDXClient method cancel.

@Override
public IDXResponse cancel(String stateHandle) throws ProcessingException {
    IDXResponse idxResponse;
    CancelRequest cancelRequest = CancelRequestBuilder.builder().withStateHandle(stateHandle).build();
    try {
        Request request = new DefaultRequest(HttpMethod.POST, clientConfiguration.getBaseUrl() + "/idp/idx/cancel", null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(cancelRequest)), -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) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) 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)

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