Search in sources :

Example 1 with Recover

use of com.okta.idx.sdk.api.model.Recover 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);
    }
}
Also used : IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) RemediationOption(com.okta.idx.sdk.api.model.RemediationOption) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) Recover(com.okta.idx.sdk.api.model.Recover) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) WrapperUtil.handleIllegalArgumentException(com.okta.idx.sdk.api.client.WrapperUtil.handleIllegalArgumentException) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException) WrapperUtil.handleProcessingException(com.okta.idx.sdk.api.client.WrapperUtil.handleProcessingException)

Aggregations

WrapperUtil.handleIllegalArgumentException (com.okta.idx.sdk.api.client.WrapperUtil.handleIllegalArgumentException)1 WrapperUtil.handleProcessingException (com.okta.idx.sdk.api.client.WrapperUtil.handleProcessingException)1 ProcessingException (com.okta.idx.sdk.api.exception.ProcessingException)1 Recover (com.okta.idx.sdk.api.model.Recover)1 RemediationOption (com.okta.idx.sdk.api.model.RemediationOption)1 IdentifyRequest (com.okta.idx.sdk.api.request.IdentifyRequest)1 RecoverRequest (com.okta.idx.sdk.api.request.RecoverRequest)1 IDXResponse (com.okta.idx.sdk.api.response.IDXResponse)1