Example usage for com.amazonaws.services.cognitoidp.model NewDeviceMetadataType getDeviceKey

List of usage examples for com.amazonaws.services.cognitoidp.model NewDeviceMetadataType getDeviceKey

Introduction

In this page you can find the example usage for com.amazonaws.services.cognitoidp.model NewDeviceMetadataType getDeviceKey.

Prototype


public String getDeviceKey() 

Source Link

Document

The device key.

Usage

From source file:io.fineo.client.auth.cognito.CognitoUser.java

License:Open Source License

/**
 * Find the next step from the challenge.
 * This is an important step in the generic authentication flow. After the responding to a challenge,
 * the results are analyzed here to determine the next step in the authentication process.
 * Like all other methods in this SDK, this is designed to work with Continuation objects.
 * This method returns a {@link Runnable} with the code to be executed, for the next step, to the invoking Continuation.
 * The possible steps are/*from ww  w .jav  a2 s . c  o  m*/
 *  1) Authentication was successful and we have the tokens, in this case we call {@code onSuccess()} to return the tokens.
 *  2) User password is required, an AuthenticationContinuation is created.
 *  3) MFA validation is required, a MultiFactorAuthenticationContinuation object is created.
 *  4) Other generic challenge, the challenge details are passed to the user.
 *
 * @param challenge                 REQUIRED: Current challenge details, {@link RespondToAuthChallengeResult}.
 * @param callback                  REQUIRED: {@link AuthenticationDetails} callback.
 * @param runInBackground           REQUIRED: Boolean to indicate the current threading.
 * @return {@link Runnable} for the next step in user authentication.
 */
private Runnable handleChallenge(final RespondToAuthChallengeResult challenge,
        final AuthenticationHandler callback, final boolean runInBackground) {
    Runnable nextTask;
    final CognitoUser cognitoUser = this;
    nextTask = new Runnable() {
        @Override
        public void run() {
            callback.onFailure(
                    new CognitoInternalErrorException("Authentication failed due to an internal error"));
        }
    };

    if (challenge == null) {
        return nextTask;
    }

    updateInternalUsername(challenge.getChallengeParameters());
    String challengeName = challenge.getChallengeName();
    if (challengeName == null) {
        LOG.debug("Got challenge: {}", challenge);
        final CognitoUserSession cognitoUserSession = getCognitoUserSession(
                challenge.getAuthenticationResult());
        cacheTokens(cognitoUserSession);
        NewDeviceMetadataType newDeviceMetadata = challenge.getAuthenticationResult().getNewDeviceMetadata();
        if (newDeviceMetadata == null) {
            nextTask = new Runnable() {
                @Override
                public void run() {
                    callback.onSuccess(cognitoUserSession, null);
                }
            };
        } else {
            ConfirmDeviceResult confirmDeviceResult = confirmDevice(newDeviceMetadata);
            if (confirmDeviceResult != null && confirmDeviceResult.isUserConfirmationNecessary()) {
                final CognitoDevice newDevice = new CognitoDevice(newDeviceMetadata.getDeviceKey(), null, null,
                        null, null, cognitoUser);
                nextTask = new Runnable() {
                    @Override
                    public void run() {
                        callback.onSuccess(cognitoUserSession, newDevice);
                    }
                };
            } else {
                nextTask = new Runnable() {
                    @Override
                    public void run() {
                        callback.onSuccess(cognitoUserSession, null);
                    }
                };
            }
        }
    } else if (challengeName.equals("PASSWORD_VERIFIER")) {
        return nextTask;
    } else if (challengeName.equals("SMS_MFA")) {
        throw new UnsupportedOperationException("MFA challenges not supported!");
    } else if (challengeName.equals("DEVICE_SRP_AUTH")) {
        nextTask = deviceSrpAuthentication(challenge, callback, runInBackground);
    } else {
        throw new RuntimeException("Generic challenge continuation no supported!");
        //            final ChallengeContinuation challengeContinuation =
        //                    new ChallengeContinuation(cognitoUser, context, clientId, challenge, runInBackground, callback);
        //            nextTask = new Runnable() {
        //                @Override
        //                public void run() {
        //                    callback.authenticationChallenge(challengeContinuation);
        //                }
        //            };
    }
    return nextTask;
}

From source file:io.fineo.client.auth.cognito.CognitoUser.java

License:Open Source License

/**
 * The method confirms a device. If this device can be remembered and if this is a new device,
 * a new device key is generated at the end of a successful authentication. SRP verification is
 * performed by the service, during the next authentication attempts, to identify this device.
 * This method generates the necessary tokens to enable the device SRP verification.
 *
 * @param deviceMetadata        REQUIRED: Metadata for the new device.
 *//*from ww  w  . j  a  va  2 s  . c o  m*/
private ConfirmDeviceResult confirmDevice(final NewDeviceMetadataType deviceMetadata) {
    Map<String, String> deviceSrpVerifiers = CognitoDeviceHelper.generateVerificationParameters(deviceKey,
            deviceMetadata.getDeviceGroupKey());

    ConfirmDeviceResult confirmDeviceResult = new ConfirmDeviceResult();
    confirmDeviceResult.setUserConfirmationNecessary(false);
    try {
        confirmDeviceResult = confirmDeviceInternal(getCachedSession(), deviceMetadata.getDeviceKey(),
                deviceSrpVerifiers.get("verifier"), deviceSrpVerifiers.get("salt"), "Fineo.JDBC");
    } catch (Exception e) {
        LOG.error(TAG, "Device confirmation failed: " + e.getMessage());
        return null;
    }
    this.devices.cacheDevice(usernameInternal, getUserPoolId(), deviceMetadata);
    return confirmDeviceResult;
}