Search in sources :

Example 1 with ErrorResponse

use of com.okta.idx.sdk.api.response.ErrorResponse 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 ErrorResponse

use of com.okta.idx.sdk.api.response.ErrorResponse 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 3 with ErrorResponse

use of com.okta.idx.sdk.api.response.ErrorResponse in project okta-idx-java by okta.

the class LoginController method handleLogin.

@GetMapping(value = "/custom-login")
public ModelAndView handleLogin(HttpServletRequest request, @RequestParam(name = "state", required = false) String state, @RequestParam(name = "nonce") String nonce, HttpSession session) throws MalformedURLException {
    logger.info("Handling login with state: {}, nonce {}", state, nonce);
    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(NONCE, nonce);
    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 4 with ErrorResponse

use of com.okta.idx.sdk.api.response.ErrorResponse in project okta-idx-java by okta.

the class WrapperUtil method handleProcessingException.

/**
 * Helper to parse {@link ProcessingException} and populate {@link AuthenticationResponse}
 * with appropriate error messages.
 *
 * @param e the {@link ProcessingException} reference
 */
static AuthenticationResponse handleProcessingException(ProcessingException e) {
    logger.error("Exception occurred", e);
    AuthenticationResponse authenticationResponse = new AuthenticationResponse();
    ErrorResponse errorResponse = e.getErrorResponse();
    if (errorResponse != null) {
        if (errorResponse.getMessages() != null) {
            Arrays.stream(errorResponse.getMessages().getValue()).forEach(msg -> authenticationResponse.addError(msg.getMessage()));
        } else {
            authenticationResponse.addError(errorResponse.getError() + ":" + errorResponse.getErrorDescription());
        }
    } else {
        authenticationResponse.addError(e.getMessage());
    }
    logger.error("Error Detail: {}", authenticationResponse.getErrors());
    return authenticationResponse;
}
Also used : AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse)

Aggregations

ErrorResponse (com.okta.idx.sdk.api.response.ErrorResponse)4 ProcessingException (com.okta.idx.sdk.api.exception.ProcessingException)3 URL (java.net.URL)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 AuthenticationResponse (com.okta.idx.sdk.api.response.AuthenticationResponse)1