Search in sources :

Example 6 with AnalyticsMetadataType

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

the class CognitoUser method userSrpAuthRequest.

/**
 * @param challengeParameters returned by service
 * @param password maintained locally
 * @param challengeName returned by service
 * @param session returned by service
 * @param authenticationHelper
 * @return
 */
private RespondToAuthChallengeRequest userSrpAuthRequest(final Map<String, String> clientMetadata, final Map<String, String> challengeParameters, final String password, final String challengeName, final String session, final AuthenticationHelper authenticationHelper) {
    final String userId = challengeParameters.get(CognitoServiceConstants.CHLG_PARAM_USERNAME);
    final String userIdForSRP = challengeParameters.get(CognitoServiceConstants.CHLG_PARAM_USER_ID_FOR_SRP);
    final String srpBString = challengeParameters.get(CognitoServiceConstants.CHLG_PARAM_SRP_B);
    final String saltString = challengeParameters.get(CognitoServiceConstants.CHLG_PARAM_SALT);
    final String secretBlockString = challengeParameters.get(CognitoServiceConstants.CHLG_PARAM_SECRET_BLOCK);
    this.usernameInternal = userId;
    this.deviceKey = CognitoDeviceHelper.getDeviceKey(usernameInternal, pool.getUserPoolId(), context);
    secretHash = CognitoSecretHash.getSecretHash(usernameInternal, clientId, clientSecret);
    final BigInteger srpB = new BigInteger(srpBString, 16);
    if (srpB.mod(AuthenticationHelper.N).equals(BigInteger.ZERO)) {
        throw new CognitoInternalErrorException("SRP error, B cannot be zero");
    }
    final BigInteger salt = new BigInteger(saltString, 16);
    final byte[] key = authenticationHelper.getPasswordAuthenticationKey(userIdForSRP, password, srpB, salt);
    final Date timestamp = new Date();
    byte[] hmac;
    String dateString;
    try {
        final Mac mac = Mac.getInstance("HmacSHA256");
        final SecretKeySpec keySpec = new SecretKeySpec(key, "HmacSHA256");
        mac.init(keySpec);
        mac.update(pool.getUserPoolId().split("_", 2)[1].getBytes(StringUtils.UTF8));
        mac.update(userIdForSRP.getBytes(StringUtils.UTF8));
        final byte[] secretBlock = Base64.decode(secretBlockString);
        mac.update(secretBlock);
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        dateString = simpleDateFormat.format(timestamp);
        final byte[] dateBytes = dateString.getBytes(StringUtils.UTF8);
        hmac = mac.doFinal(dateBytes);
    } catch (final Exception e) {
        throw new CognitoInternalErrorException("SRP error", e);
    }
    final Map<String, String> srpAuthResponses = new HashMap<String, String>();
    srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_PASSWORD_CLAIM_SECRET_BLOCK, secretBlockString);
    srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_PASSWORD_CLAIM_SIGNATURE, new String(Base64.encode(hmac), StringUtils.UTF8));
    srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_TIMESTAMP, dateString);
    srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_USERNAME, usernameInternal);
    srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_DEVICE_KEY, deviceKey);
    srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_SECRET_HASH, secretHash);
    final RespondToAuthChallengeRequest authChallengeRequest = new RespondToAuthChallengeRequest();
    authChallengeRequest.setChallengeName(challengeName);
    authChallengeRequest.setClientId(clientId);
    authChallengeRequest.setSession(session);
    authChallengeRequest.setChallengeResponses(srpAuthResponses);
    authChallengeRequest.setClientMetadata(clientMetadata);
    final String pinpointEndpointId = pool.getPinpointEndpointId();
    if (pinpointEndpointId != null) {
        AnalyticsMetadataType amd = new AnalyticsMetadataType();
        amd.setAnalyticsEndpointId(pinpointEndpointId);
        authChallengeRequest.setAnalyticsMetadata(amd);
    }
    authChallengeRequest.setUserContextData(getUserContextData());
    return authChallengeRequest;
}
Also used : HashMap(java.util.HashMap) CognitoInternalErrorException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException) Date(java.util.Date) Mac(javax.crypto.Mac) 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) AnalyticsMetadataType(com.amazonaws.services.cognitoidentityprovider.model.AnalyticsMetadataType) SecretKeySpec(javax.crypto.spec.SecretKeySpec) RespondToAuthChallengeRequest(com.amazonaws.services.cognitoidentityprovider.model.RespondToAuthChallengeRequest) BigInteger(java.math.BigInteger) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with AnalyticsMetadataType

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

the class CognitoUser method confirmSignUpInternal.

/**
 * Internal method to Confirm Registration.
 *
 * @param confirmationCode REQUIRED: Code to confirm this user.
 * @param forcedAliasCreation REQUIRED: If set over-rides parameter
 *            contentions
 * @param clientMetadata A map of custom key-value pairs that is passed to the lambda function for
 *                       custom workflow.
 */
private void confirmSignUpInternal(final String confirmationCode, final boolean forcedAliasCreation, final Map<String, String> clientMetadata) {
    final ConfirmSignUpRequest confirmUserRegistrationRequest = new ConfirmSignUpRequest().withClientId(clientId).withSecretHash(secretHash).withUsername(userId).withConfirmationCode(confirmationCode).withForceAliasCreation(forcedAliasCreation).withClientMetadata(clientMetadata).withUserContextData(getUserContextData());
    final String pinpointEndpointId = pool.getPinpointEndpointId();
    if (pinpointEndpointId != null) {
        final AnalyticsMetadataType amd = new AnalyticsMetadataType();
        amd.setAnalyticsEndpointId(pinpointEndpointId);
        confirmUserRegistrationRequest.setAnalyticsMetadata(amd);
    }
    cognitoIdentityProviderClient.confirmSignUp(confirmUserRegistrationRequest);
}
Also used : AnalyticsMetadataType(com.amazonaws.services.cognitoidentityprovider.model.AnalyticsMetadataType) ConfirmSignUpRequest(com.amazonaws.services.cognitoidentityprovider.model.ConfirmSignUpRequest)

Example 8 with AnalyticsMetadataType

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

the class CognitoUser method initiateRefreshTokenAuthRequest.

/**
 * Creates a request to refresh tokens.
 *
 * @param currSession REQUIRED: Refresh token.
 * @return {@link InitiateAuthRequest}, request to refresh tokens.
 */
private InitiateAuthRequest initiateRefreshTokenAuthRequest(CognitoUserSession currSession) {
    final InitiateAuthRequest initiateAuthRequest = new InitiateAuthRequest();
    initiateAuthRequest.addAuthParametersEntry(CognitoServiceConstants.AUTH_PARAM_REFRESH_TOKEN, currSession.getRefreshToken().getToken());
    if (deviceKey == null) {
        if (usernameInternal != null) {
            deviceKey = CognitoDeviceHelper.getDeviceKey(usernameInternal, pool.getUserPoolId(), context);
        } else {
            deviceKey = CognitoDeviceHelper.getDeviceKey(currSession.getUsername(), pool.getUserPoolId(), context);
        }
    }
    initiateAuthRequest.addAuthParametersEntry(CognitoServiceConstants.AUTH_PARAM_DEVICE_KEY, deviceKey);
    initiateAuthRequest.addAuthParametersEntry(CognitoServiceConstants.AUTH_PARAM_SECRET_HASH, clientSecret);
    initiateAuthRequest.setClientId(clientId);
    initiateAuthRequest.setAuthFlow(CognitoServiceConstants.AUTH_TYPE_REFRESH_TOKEN);
    final String pinpointEndpointId = pool.getPinpointEndpointId();
    if (pinpointEndpointId != null) {
        AnalyticsMetadataType amd = new AnalyticsMetadataType();
        amd.setAnalyticsEndpointId(pinpointEndpointId);
        initiateAuthRequest.setAnalyticsMetadata(amd);
    }
    initiateAuthRequest.setUserContextData(getUserContextData());
    return initiateAuthRequest;
}
Also used : AnalyticsMetadataType(com.amazonaws.services.cognitoidentityprovider.model.AnalyticsMetadataType) InitiateAuthRequest(com.amazonaws.services.cognitoidentityprovider.model.InitiateAuthRequest)

Aggregations

AnalyticsMetadataType (com.amazonaws.services.cognitoidentityprovider.model.AnalyticsMetadataType)8 AttributeType (com.amazonaws.services.cognitoidentityprovider.model.AttributeType)2 ConfirmForgotPasswordRequest (com.amazonaws.services.cognitoidentityprovider.model.ConfirmForgotPasswordRequest)2 InitiateAuthRequest (com.amazonaws.services.cognitoidentityprovider.model.InitiateAuthRequest)2 HashMap (java.util.HashMap)2 CognitoInternalErrorException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException)1 CognitoNotAuthorizedException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)1 CognitoParameterInvalidException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException)1 ConfirmSignUpRequest (com.amazonaws.services.cognitoidentityprovider.model.ConfirmSignUpRequest)1 ForgotPasswordRequest (com.amazonaws.services.cognitoidentityprovider.model.ForgotPasswordRequest)1 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)1 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)1 ResendConfirmationCodeRequest (com.amazonaws.services.cognitoidentityprovider.model.ResendConfirmationCodeRequest)1 ResourceNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException)1 RespondToAuthChallengeRequest (com.amazonaws.services.cognitoidentityprovider.model.RespondToAuthChallengeRequest)1 SignUpRequest (com.amazonaws.services.cognitoidentityprovider.model.SignUpRequest)1 UserNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException)1 BigInteger (java.math.BigInteger)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SimpleDateFormat (java.text.SimpleDateFormat)1