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

8
我已经创建了一个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);
        },

    });

1
看起来你正在使用 JavaScript - 这不是 Java。 - ecoe
3个回答

14
AWS Java SDK 包含用于在用户池中验证用户的 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;
    }
}

这很有帮助,那么在获得AdminInitiateAuthResult之后,如何获取用户数据呢? - gremwell
啊,搞定了。GetUserRequest getUserRequest = new GetUserRequest().withAccessToken(initiateAuthResult.getAuthenticationResult().getAccessToken()); GetUserResult getUserResult = client.getUser(getUserRequest); - gremwell
2
然而,adminInitiateAuth需要AWS凭证,因此这种方法仅适用于拥有该特权信息的用户。 - ecoe

3

目前仅支持使用JavaScript、iOS和Android进行身份验证。在测试期间,身份验证所需的API不包含在服务端SDK(如java、Python等)中。建议从登录页面使用JavaScript SDK进行身份验证。


我一定很接近了,按照JavaScript的所有指示操作,但现在卡在了“ConfigError: Missing credentials in config”这里,遵循了使用案例4。 - user1432403
这是 JavaScript SDK 生成方式的一个产物。我们的 JavaScript SDK 假定所有 AWS 服务都需要凭据,但有些服务具有未经身份验证的 API,不需要它们。如果您为凭据设置一些任意值,例如 ACCESS_KEY 和 SECRET_KEY,它应该可以工作。 - perpil
搞定了,我最终重新阅读了说明并创建了一个应用程序,没有勾选生成客户端密钥框。 - user1432403
1
JavaScript SDK 似乎无法从 AWS Lambda 中使用。而且,尽管 OP 可能已经说了“Java SDK”,但他们显然已经在使用 JavaScript SDK。 - TiggerToo

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接