如何从 aws cognito - android 获取密码?

How to get password from aws cognito - android?

所以我正在使用 aws cognito,我对如何获取密码有点困惑?

如果用户在编辑文本中输入密码,我如何获取用户在注册时输入的密码,以便我可以将他们登录时使用的密码与注册时使用的密码进行比较?

这是我必须注册用户的代码:

userPool.signUpInBackground(username_ET.getText().toString(), password_ET.getText().toString(), userAttributes, null, signupCallback);

这是我用来登录的代码:

 private AuthenticationHandler authenticationHandler = new AuthenticationHandler()
    {
        @Override
        public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice)
        {
            Log.d(COGNITO_LOGIN,"Login success I think?!");
            cognitoUser.getDetailsInBackground(getDetailsHandler);
            //Here i need to compare passwords before i can move on.
        }
        @Override
        public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId)
        {
            Log.d(COGNITO_LOGIN,passwordET.getText().toString());
            // The API needs user sign-in credentials to continue
            AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, passwordET.getText().toString(), null);

            // Pass the user sign-in credentials to the continuation
            authenticationContinuation.setAuthenticationDetails(authenticationDetails);

            // Allow the sign-in to continue
            authenticationContinuation.continueTask();
        }
        @Override
        public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) {
            // Multi-factor authentication is required; get the verification code from user
            multiFactorAuthenticationContinuation.setMfaCode("verificationCode");
            // Allow the sign-in process to continue
            multiFactorAuthenticationContinuation.continueTask();
        }
        @Override
        public void authenticationChallenge(ChallengeContinuation continuation) {
        }
        @Override
        public void onFailure(Exception exception)
        {
            // Sign-in failed, check exception for the cause
            Log.d(COGNITO_LOGIN,"Login failed!");
            Log.d(COGNITO_LOGIN,exception.getMessage());
            exceptionMessage(exception.getMessage());
        }
    };



cognitoUser.getSessionInBackground(authenticationHandler);

使用身份验证处理程序,我只需要传入正确的用户名(或用户 ID)即可使 onSuccess 运行。甚至不需要密码。所以我很困惑,用户还必须输入正确的密码才能登录。

您不需要比较密码。当您注册时,Cognito 会为您注册时使用的密码存储盐分和验证程序。 Cognito 不会以您输入的形式存储您的密码,而只会存储盐和验证器。当您使用以下代码时,Cognito 使用安全远程密码协议来匹配内部存储的验证程序。由于我们使用您提供的密码进行计算,因此您无法找回它。请注意,在 onSuccess 回调中,如果调用成功,您将获得令牌,如下所述。

// Callback handler for the sign-in process 
AuthenticationHandler authenticationHandler = new AuthenticationHandler() { 

    @Override 
    public void onSuccess(CognitoUserSession cognitoUserSession) { 
        // Sign-in was successful, cognitoUserSession will contain tokens for the user   
    }

    @Override
    public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) { 
        // The API needs user sign-in credentials to continue
        AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, password, null);

        // Pass the user sign-in credentials to the continuation
        authenticationContinuation.setAuthenticationDetails(authenticationDetails);

        // Allow the sign-in to continue
        authenticationContinuation.continueTask();
    }

    @Override
    public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) { 
        // Multi-factor authentication is required, get the verification code from user
        multiFactorAuthenticationContinuation.setMfaCode(mfaVerificationCode);
        // Allow the sign-in process to continue
        multiFactorAuthenticationContinuation.continueTask();
    }

    @Override
    public void onFailure(Exception exception) {
        // Sign-in failed, check exception for the cause
    } 
};

// Sign-in the user 
cognitoUser.getSessionInBackground(authenticationHandler);