use of com.okta.idx.sdk.api.exception.ProcessingException 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;
}
use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.
the class IDXAuthenticationWrapper method verifyAuthenticator.
/**
* Verify Authenticator with the supplied authenticator options.
*
* @param proceedContext the ProceedContext
* @param verifyAuthenticatorOptions the verify Authenticator options
* @return the Authentication response
*/
public AuthenticationResponse verifyAuthenticator(ProceedContext proceedContext, VerifyAuthenticatorOptions verifyAuthenticatorOptions) {
try {
Credentials credentials = new Credentials();
credentials.setPasscode(verifyAuthenticatorOptions.getCode().toCharArray());
// build answer password authenticator challenge request
AnswerChallengeRequest challengeAuthenticatorRequest = AnswerChallengeRequestBuilder.builder().withStateHandle(proceedContext.getStateHandle()).withCredentials(credentials).build();
return AuthenticationTransaction.proceed(client, proceedContext, () -> client.answerChallenge(challengeAuthenticatorRequest, proceedContext.getHref())).asAuthenticationResponse(AuthenticationStatus.AWAITING_PASSWORD_RESET);
} catch (ProcessingException e) {
return handleProcessingException(e);
} catch (IllegalArgumentException e) {
return handleIllegalArgumentException(e);
}
}
use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.
the class IDXAuthenticationWrapper method submitPhoneAuthenticator.
/**
* Submit phone authenticator enrollment with the provided phone number.
*
* @param proceedContext the ProceedContext
* @param phone the phone number
* @param factor factor
* @return the Authentication response
*/
public AuthenticationResponse submitPhoneAuthenticator(ProceedContext proceedContext, String phone, com.okta.idx.sdk.api.client.Authenticator.Factor factor) {
try {
Assert.notNull(proceedContext, "proceed context cannot be null");
Authenticator phoneAuthenticator = new Authenticator();
phoneAuthenticator.setId(factor.getId());
phoneAuthenticator.setMethodType(factor.getMethod());
phoneAuthenticator.setPhoneNumber(phone);
EnrollRequest enrollRequest = EnrollRequestBuilder.builder().withAuthenticator(phoneAuthenticator).withStateHandle(proceedContext.getStateHandle()).build();
return AuthenticationTransaction.proceed(client, proceedContext, () -> client.enroll(enrollRequest, proceedContext.getHref())).asAuthenticationResponse();
} catch (ProcessingException e) {
return handleProcessingException(e);
} catch (IllegalArgumentException e) {
return handleIllegalArgumentException(e);
}
}
use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.
the class IDXAuthenticationWrapper method enrollAuthenticator.
public AuthenticationResponse enrollAuthenticator(ProceedContext proceedContext, String authenticatorId) {
try {
AuthenticationResponse authenticationResponse = AuthenticationTransaction.proceed(client, proceedContext, () -> {
Authenticator authenticator = new Authenticator();
authenticator.setId(authenticatorId);
EnrollRequest enrollRequest = EnrollRequestBuilder.builder().withAuthenticator(authenticator).withStateHandle(proceedContext.getStateHandle()).build();
return client.enroll(enrollRequest, proceedContext.getHref());
}).asAuthenticationResponse();
if (authenticationResponse.getWebAuthnParams() != null) {
AuthenticatorEnrollments authenticatorEnrollments = authenticationResponse.getAuthenticatorEnrollments();
Optional<AuthenticatorEnrollment> authenticatorEnrollmentOptional = authenticatorEnrollments.stream().filter(x -> "security_key".equals(x.getType())).findAny();
authenticatorEnrollmentOptional.ifPresent(authenticatorEnrollment -> authenticationResponse.getWebAuthnParams().setWebauthnCredentialId(authenticatorEnrollment.getCredentialId()));
}
return authenticationResponse;
} catch (ProcessingException e) {
return handleProcessingException(e);
} catch (IllegalArgumentException e) {
return handleIllegalArgumentException(e);
}
}
use of com.okta.idx.sdk.api.exception.ProcessingException in project okta-idx-java by okta.
the class IDXAuthenticationWrapper method recoverPassword.
/**
* Recover Password with the supplied username.
*
* @param username the username
* @return the Authentication response
*/
public AuthenticationResponse recoverPassword(String username, ProceedContext proceedContext) {
try {
boolean isIdentifyInOneStep = proceedContext.isIdentifyInOneStep();
if (isIdentifyInOneStep) {
// recover
AuthenticationTransaction recoverTransaction = AuthenticationTransaction.proceed(client, proceedContext, () -> {
RecoverRequest recoverRequest = RecoverRequestBuilder.builder().withStateHandle(proceedContext.getStateHandle()).build();
return client.recover(recoverRequest, null);
});
RemediationOption remediationOption = recoverTransaction.getRemediationOption(RemediationType.IDENTIFY_RECOVERY);
IdentifyRequest identifyRequest = IdentifyRequestBuilder.builder().withIdentifier(username).withStateHandle(proceedContext.getStateHandle()).build();
// identify user
return recoverTransaction.proceed(() -> remediationOption.proceed(client, identifyRequest)).asAuthenticationResponse(AuthenticationStatus.AWAITING_AUTHENTICATOR_SELECTION);
} else {
// identify user
AuthenticationTransaction identifyTransaction = AuthenticationTransaction.proceed(client, proceedContext, () -> {
IdentifyRequest identifyRequest = IdentifyRequestBuilder.builder().withIdentifier(username).withStateHandle(proceedContext.getStateHandle()).build();
return client.identify(identifyRequest, proceedContext.getHref());
});
IDXResponse identifyResponse = identifyTransaction.getResponse();
if (identifyResponse.getMessages() != null) {
return identifyTransaction.asAuthenticationResponse(AuthenticationStatus.AWAITING_USER_EMAIL_ACTIVATION);
}
// Check if instead of password, user is being prompted for list of authenticators to select
if (identifyResponse.getCurrentAuthenticatorEnrollment() == null) {
identifyTransaction = selectPasswordAuthenticatorIfNeeded(identifyTransaction);
}
Recover recover = identifyTransaction.getResponse().getCurrentAuthenticatorEnrollment().getValue().getRecover();
AuthenticationTransaction recoverTransaction = identifyTransaction.proceed(() -> {
// recover password
RecoverRequest recoverRequest = RecoverRequestBuilder.builder().withStateHandle(proceedContext.getStateHandle()).build();
return recover.proceed(client, recoverRequest);
});
return recoverTransaction.asAuthenticationResponse(AuthenticationStatus.AWAITING_AUTHENTICATOR_SELECTION);
}
} catch (ProcessingException e) {
return handleProcessingException(e);
} catch (IllegalArgumentException e) {
return handleIllegalArgumentException(e);
}
}
Aggregations