Search in sources :

Example 1 with VerifySoftwareTokenResult

use of com.amazonaws.services.cognitoidentityprovider.model.VerifySoftwareTokenResult in project aws-sdk-android by aws-amplify.

the class CognitoUser method verifySoftwareToken.

/**
 * Verify the Time-based One-time Password based MFA tpo complete registration, in current thread.
 * @param sessionToken Optional: If a session token has to be used to register the MFA.
 * @param totpCode Required: The TOTP code.
 * @param friendlyName Required: Friendly name to be associated with this MFA.
 * @param callback Required: Callback handler {@link VerifyMfaContinuation}.
 */
public void verifySoftwareToken(final String sessionToken, final String totpCode, final String friendlyName, final RegisterMfaHandler callback) {
    if (callback == null) {
        throw new CognitoParameterInvalidException("callback is null");
    }
    final CognitoUser user = this;
    boolean useSessionToken;
    try {
        final CognitoUserSession cognitoTokens = user.getCachedSession();
        VerifySoftwareTokenResult result;
        if (!StringUtils.isBlank(sessionToken)) {
            result = verifyTotpAssociationWithSession(sessionToken, totpCode, friendlyName);
            useSessionToken = true;
        } else {
            result = verifyTotpAssociationWithTokens(cognitoTokens, totpCode, friendlyName);
            useSessionToken = false;
        }
        final String newSessionToken = result.getSession();
        if (VerifySoftwareTokenResponseType.ERROR.equals(result.getStatus())) {
            throw new CognitoInternalErrorException("verification failed");
        }
        if (useSessionToken) {
            callback.onSuccess(newSessionToken);
        } else {
            callback.onSuccess(null);
        }
    } catch (Exception e) {
        callback.onFailure(e);
    }
}
Also used : VerifySoftwareTokenResult(com.amazonaws.services.cognitoidentityprovider.model.VerifySoftwareTokenResult) CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) CognitoInternalErrorException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CognitoInternalErrorException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException) UserNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException) CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoNotAuthorizedException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)

Example 2 with VerifySoftwareTokenResult

use of com.amazonaws.services.cognitoidentityprovider.model.VerifySoftwareTokenResult in project aws-sdk-android by aws-amplify.

the class CognitoUser method verifySoftwareTokenInBackground.

/**
 * Verify the Time-based One-time Password based MFA tpo complete registration.
 * @param sessionToken Optional: If a session token has to be used to register the MFA.
 * @param totpCode Required: The TOTP code.
 * @param friendlyName Required: Friendly name to be associated with this MFA.
 * @param callback Required: Callback handler {@link VerifyMfaContinuation}.
 */
public void verifySoftwareTokenInBackground(final String sessionToken, final String totpCode, final String friendlyName, final RegisterMfaHandler callback) {
    if (callback == null) {
        throw new CognitoParameterInvalidException("callback is null");
    }
    final CognitoUser user = this;
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Handler handler = new Handler(context.getMainLooper());
            Runnable returnCallback;
            try {
                final CognitoUserSession cognitoTokens = user.getCachedSession();
                VerifySoftwareTokenResult result;
                boolean useSessionToken;
                if (!StringUtils.isBlank(sessionToken)) {
                    result = verifyTotpAssociationWithSession(sessionToken, totpCode, friendlyName);
                    useSessionToken = true;
                } else {
                    result = verifyTotpAssociationWithTokens(cognitoTokens, totpCode, friendlyName);
                    useSessionToken = false;
                }
                final String newSessionToken = result.getSession();
                if (VerifySoftwareTokenResponseType.ERROR.equals(result.getStatus())) {
                    throw new CognitoInternalErrorException("verification failed");
                }
                if (useSessionToken) {
                    returnCallback = new Runnable() {

                        @Override
                        public void run() {
                            callback.onSuccess(newSessionToken);
                        }
                    };
                } else {
                    returnCallback = new Runnable() {

                        @Override
                        public void run() {
                            callback.onSuccess(null);
                        }
                    };
                }
            } catch (final Exception e) {
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onFailure(e);
                    }
                };
            }
            handler.post(returnCallback);
        }
    }).start();
}
Also used : VerifySoftwareTokenResult(com.amazonaws.services.cognitoidentityprovider.model.VerifySoftwareTokenResult) CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) Handler(android.os.Handler) GetDetailsHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) VerificationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.VerificationHandler) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) ForgotPasswordHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.ForgotPasswordHandler) RegisterMfaHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.RegisterMfaHandler) DevicesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.DevicesHandler) CognitoInternalErrorException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CognitoInternalErrorException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException) UserNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException) CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoNotAuthorizedException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)

Aggregations

CognitoInternalErrorException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException)2 CognitoNotAuthorizedException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)2 CognitoParameterInvalidException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException)2 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)2 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)2 ResourceNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException)2 UserNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException)2 VerifySoftwareTokenResult (com.amazonaws.services.cognitoidentityprovider.model.VerifySoftwareTokenResult)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Handler (android.os.Handler)1 AuthenticationHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler)1 DevicesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.DevicesHandler)1 ForgotPasswordHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.ForgotPasswordHandler)1 GenericHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler)1 GetDetailsHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler)1 RegisterMfaHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.RegisterMfaHandler)1 UpdateAttributesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler)1 VerificationHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.VerificationHandler)1