Search in sources :

Example 1 with AssociateSoftwareTokenResult

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

the class CognitoUser method associateSoftwareToken.

/**
 * Registers an MFA based on Time-based One-time Password, run on current thread.
 * @param sessionToken Optional: If a session token has to be used to register the MFA.
 * @param callback Required: Callback handler {@link VerifyMfaContinuation}.
 */
public void associateSoftwareToken(final String sessionToken, final RegisterMfaHandler callback) {
    if (callback == null) {
        throw new CognitoParameterInvalidException("callback is null");
    }
    final CognitoUser user = this;
    boolean useSessionToken;
    try {
        final CognitoUserSession cognitoTokens = user.getCachedSession();
        AssociateSoftwareTokenResult result;
        if (!StringUtils.isBlank(sessionToken)) {
            result = associateTotpMfaInternalWithSession(sessionToken);
            useSessionToken = true;
        } else {
            result = associateTotpMfaInternalWithTokens(cognitoTokens);
            useSessionToken = false;
        }
        final String nextSessionToken = result.getSession();
        final Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("type", CognitoServiceConstants.CHLG_TYPE_SOFTWARE_TOKEN_MFA);
        parameters.put("secretKey", result.getSecretCode());
        callback.onVerify(new VerifyMfaContinuation(context, clientId, user, callback, parameters, useSessionToken, nextSessionToken, VerifyMfaContinuation.RUN_IN_CURRENT));
    } catch (Exception e) {
        callback.onFailure(e);
    }
}
Also used : CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) HashMap(java.util.HashMap) AssociateSoftwareTokenResult(com.amazonaws.services.cognitoidentityprovider.model.AssociateSoftwareTokenResult) VerifyMfaContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.VerifyMfaContinuation) 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 AssociateSoftwareTokenResult

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

the class CognitoUser method associateSoftwareTokenInBackground.

/**
 * Registers an MFA based on Time-based One-time Password.
 * @param sessionToken Optional: If a session token has to be used to register the MFA.
 * @param callback Required: Callback handler {@link VerifyMfaContinuation}.
 */
public void associateSoftwareTokenInBackground(final String sessionToken, 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;
            boolean useSessionToken;
            try {
                final CognitoUserSession cognitoTokens = user.getCachedSession();
                AssociateSoftwareTokenResult result;
                if (!StringUtils.isBlank(sessionToken)) {
                    result = associateTotpMfaInternalWithSession(sessionToken);
                    useSessionToken = true;
                } else {
                    result = associateTotpMfaInternalWithTokens(cognitoTokens);
                    useSessionToken = false;
                }
                final String nextSessionToken = result.getSession();
                final Map<String, String> parameters = new HashMap<String, String>();
                parameters.put("type", CognitoServiceConstants.CHLG_TYPE_SOFTWARE_TOKEN_MFA);
                parameters.put("secretKey", result.getSecretCode());
                if (useSessionToken) {
                    returnCallback = new Runnable() {

                        @Override
                        public void run() {
                            callback.onVerify(new VerifyMfaContinuation(context, clientId, user, callback, parameters, true, nextSessionToken, VerifyMfaContinuation.RUN_IN_BACKGROUND));
                        }
                    };
                } else {
                    returnCallback = new Runnable() {

                        @Override
                        public void run() {
                            callback.onVerify(new VerifyMfaContinuation(context, clientId, user, callback, parameters, false, nextSessionToken, VerifyMfaContinuation.RUN_IN_BACKGROUND));
                        }
                    };
                }
            } catch (final Exception e) {
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onFailure(e);
                    }
                };
            }
            handler.post(returnCallback);
        }
    }).start();
}
Also used : CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) HashMap(java.util.HashMap) 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) VerifyMfaContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.VerifyMfaContinuation) 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) AssociateSoftwareTokenResult(com.amazonaws.services.cognitoidentityprovider.model.AssociateSoftwareTokenResult)

Aggregations

VerifyMfaContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.VerifyMfaContinuation)2 CognitoInternalErrorException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException)2 CognitoNotAuthorizedException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)2 CognitoParameterInvalidException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException)2 AssociateSoftwareTokenResult (com.amazonaws.services.cognitoidentityprovider.model.AssociateSoftwareTokenResult)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 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 HashMap (java.util.HashMap)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