如何针对 AWS Cognito 用户池进行身份验证

新手上路,请多包涵

我创建了一个 Cognito 用户池。我可以使用 Java AWS SDK 中的 AWSCognitoIdentityProviderClient 列出用户并添加用户。

但是,我有一个自定义登录页面,我希望使用输入的用户名和密码对我的用户池进行身份验证。我在 Java AWS SDK 中看不到任何可以传递凭据并从中获取身份验证结果的地方。

编辑:我无法克服这个错误:

NotAuthorizedException:配置中缺少凭据

相关代码:

     AWS.config.region = 'us-east-1';
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'us-east-1:087a3210-64f8-4dae-9e3c...' // your identity pool id here
    });

    AWSCognito.config.region = 'us-east-1';
    AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'us-east-1:087a3210-64f8-4dae-9e3c...' // your identity pool id here
    });

    var poolData = {
        UserPoolId: 'us-east-1_39RP...',
        ClientId: 'ttsj9j5...',
        ClientSecret: 'bkvkj9r8kl2ujrlu41c7krsb6r7nub2kb260gj3mgi...'
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

    var authenticationData = {
        Username: 'test@foo.com',
        Password: 'foobarfoo',
    };
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var userData = {
        Username: 'test@foo.com',
        Pool: userPool
    };
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
        },

        onFailure: function (err) {
            alert(err);
        },

    });

原文由 user1432403 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 497
1 个回答

AWS Java 开发工具包包括用于对用户池中的用户进行身份验证的 API。您可以使用 AWSCognitoIdentityProviderClient 类的 InitiateAuth api 或 AdminInitiateAuth api 对用户进行身份验证。文档中解释了这两个 API 之间的区别。总之,对于InitiateAuth,需要进行SRP计算,然后传递给API,而在AdminInitiateAuth中,可以直接传递用户名和密码。您可以阅读这两种情况下的安全隐患并决定使用哪一种。

文档: https ://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html

API 参考: https ://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html

https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html

我的工作示例(Groovy):

 def login() {
    AWSCognitoIdentityProviderClient client = new AWSCognitoIdentityProviderClient()
    println("Provider client: " + client)
    client.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1))

    HashMap authParams = new HashMap<>()
    authParams.put("USERNAME", "User1")
    authParams.put("PASSWORD", "a*123")
    AdminInitiateAuthRequest adminInitiateAuthRequest = new AdminInitiateAuthRequest()
            .withClientId(<YOUR_CLIENT_ID>)
            .withUserPoolId(<YOUR_USER_POOL_ID>)
            .withAuthFlow(AuthFlowType.ADMIN_NO_SRP_AUTH )
            .withAuthParameters(authParams)
    AdminInitiateAuthResult result = client.adminInitiateAuth(adminInitiateAuthRequest);
    if (result != null) {
        System.out.println("AdminInitiateAuthResult:");
        System.out.println(result.toString());
    } else {
        System.out.println("No result available");
        return;
    }
}

原文由 neel 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题