【问题标题】:Manual Authentication with Amazon Cognito使用 Amazon Cognito 进行手动身份验证
【发布时间】:2020-08-15 03:29:05
【问题描述】:

我知道以用户身份进行身份验证并获得access token 的两种方法,一种是通过Hosted UI,另一种是通过various provided SDKs

我正在寻找的是一个端点直接使用用户凭据获取access token

POST https://that-special-endpoint.com/login
{
 username: "example@email.com",
 password: "Abc123456",
 ...client ID, etc.
}

我已经搜索了一段时间,但找不到如何执行此操作。由于一些我不知道的安全问题,这不可能吗?

我确实考虑过创建一个 Lambda API 并使用 Cognito SDK 来满足我的用例,但我不确定这是否可取...

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-cognito


    【解决方案1】:

    回答了类似的问题here。您可以访问https://cognito-idp.[region].amazonaws.com/ 调用InitiateAuthRespondToAuthChallenge API。


    启动验证


    1. 创建一个json文件,aws-auth-data.json
    {
        "AuthParameters": {
            "USERNAME": "your-email@example.com",
            "PASSWORD": "your-first-password",
            "SECRET_HASH": "......(required if the app client is configured with a client secret)"
        },
        "AuthFlow": "USER_PASSWORD_AUTH",
        "ClientId": "5m........................"
    }
    
    1. https://cognito-idp.us-east-2.amazonaws.com/(如果用户池位于us-east-2 区域)上发送请求以调用InitiateAuth API 并启动身份验证流程。
    curl -X POST --data @aws-auth-data.json \
    -H 'X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth' \
    -H 'Content-Type: application/x-amz-json-1.1' \
    https://cognito-idp.us-east-2.amazonaws.com/
    
    1. 然后你会得到用户的令牌。
    {
        "AuthenticationResult": {
            "AccessToken": "eyJra........",
            "ExpiresIn": 3600,
            "IdToken": "eyJra........",
            "RefreshToken": "eyJjd........",
            "TokenType": "Bearer"
        },
        "ChallengeParameters": {}
    }
    

    RespondToAuthChallenge


    您可能会收到InitiateAuth 回复的挑战。例如,当您第一次尝试“InitiateAuth”时,系统会要求您更改密码:

    {
        "ChallengeName": "NEW_PASSWORD_REQUIRED",
        "ChallengeParameters": {
            "USER_ID_FOR_SRP": "abababab-......",
            "requiredAttributes": "[]",
            "userAttributes": "{\"email_verified\":\"true\",\"email\":\"your-email@example.com\"}"
        },
        "Session": "DNdY......"
    }
    

    在这种情况下,使用RespondToAuthChallenge 更改密码,您将获得令牌。

    {
        "ChallengeName": "NEW_PASSWORD_REQUIRED",
        "ChallengeResponses": {
            "USERNAME": "your-email@example.com",
            "NEW_PASSWORD": "your-second-password"
        },
        "ClientId": "5m........................",
        "Session": "DNdYN...(what you got in the preceding response)"
    }
    
    curl -X POST --data @aws-change-password.json \
    -H 'X-Amz-Target: AWSCognitoIdentityProviderService.RespondToAuthChallenge' \
    -H 'Content-Type: application/x-amz-json-1.1' \
    https://cognito-idp.us-east-2.amazonaws.com/
    

    另见:

    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_RespondToAuthChallenge.html

    https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html#amazon-cognito-user-pools-client-side-authentication-flow

    【讨论】:

    • 引用文档,...客户端凭据授予通常旨在向应用程序提供凭据,以便授权机器对机器的请求。身体,似乎没有任何输入与最终用户相关联。那么令牌应该如何识别用户呢?
    • @daisura99 我说“尝试在 TOKEN 端点上授予客户端凭据”但很抱歉,我对您的问题有一个错误的想法。让我重写整个答案。
    • 谢谢!你救了我的命。我之前确实浏览过这个文档,但我认为它只是另一个 SDK,不知道实际上可以将这个 API 与 HTTP REST 一起使用
    • 进一步补充,如果API Reference for Cognito 有类似CloudWatch 的东西会很有帮助!
    • @shoek :你是救生员。我试图从 2 天开始访问,但没有任何文档有效。你是宝石..
    猜你喜欢
    • 2019-06-29
    • 2018-11-20
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2017-01-13
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多