use of com.okta.idx.sdk.api.request.IdentifyRequest 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);
}
}
use of com.okta.idx.sdk.api.request.IdentifyRequest 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);
}
}
use of com.okta.idx.sdk.api.request.IdentifyRequest in project okta-idx-java by okta.
the class BaseIDXClient method identify.
@Override
public IDXResponse identify(IdentifyRequest identifyRequest, String href) throws ProcessingException {
IDXResponse idxResponse;
try {
Request request = new DefaultRequest(HttpMethod.POST, href, null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(identifyRequest)), -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;
}
Aggregations