【问题标题】:Verify user identity using cognito使用 cognito 验证用户身份
【发布时间】:2016-07-24 16:07:36
【问题描述】:

我正在创建后端服务,并且正在使用 AWS Cognito。 现在,我正在使用我的 Developer Authenticated Identities。 所以,我的用户可以使用我自己的系统登录。这部分完成了。

但是,我在登录后对用户进行身份验证时遇到了问题。假设用户向我发送了访问我的 API 的某些私有功能的请求。如何识别和验证此用户?

如果我使用 facebook 作为登录提供程序,我可以使用他的 Cognito ID 和他的 facebook 访问令牌来完成 http 标头。然后,我可以对他进行身份验证,尝试使用 GetOpenIdTokenRequest 函数获取 Cognito Token。

我会有类似的东西

providerTokens.put("graph.facebook.com", "xxxxxxxxxxxx");
tokenRequest.setLogins(providerTokens);

AmazonCognitoIdentityClient identityClient = new AmazonCognitoIdentityClient();
        identityClient.setRegion(RegionUtils.getRegion(Configuration.REGION));
        GetOpenIdTokenResult tokenResp = identityClient.getOpenIdToken(tokenRequest);

但是,我没有使用 Facebook。

所以,我尝试了一些类似的方法

providerTokens.put("customdeveloper.authentication.com", "xxxxxxxxxxxx");
tokenRequest.setLogins(providerTokens);

收到我的开发者身份验证不是公共提供者的错误。

如果我朝着正确的方向前进,我会很困惑。我基本上想验证我的用户。类似于我收到令牌的 oauth2 的东西可以检查用户身份。

使用 Cognito 的正确方法是什么?

【问题讨论】:

    标签: authentication amazon-cognito


    【解决方案1】:

    是的,使用身份 ID 和 Cognito Token,您可以调用 getCredentialsForIdentity 并确定该令牌有效并属于该身份。

    然后,您可以使用这些凭据以该用户的身份调用不同的服务。

    另一种选择是将后端服务器逻辑放在 API Gateway 后面: http://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html

    然后您的用户将在客户端获取凭据,并使用这些凭据向位于您的服务器端逻辑前端的 API 网关进行调用。

    【讨论】:

    • 如何验证 getCredentialsForIdentity 的令牌?对象是 "Credentials": { "AccessKeyId": "string","Expiration": number,"SecretKey": "string", "SessionToken": "string" } 但我找不到如何验证它的信息。
    【解决方案2】:

    Cognito 文档在服务器端相当模糊,但我想出了一种方法。

    所以基本上,您需要将身份 ID 和 Cognito 令牌传递给服务器。然后,在服务器上执行以下操作:

    // Create the request object            
            Map providerTokens = new HashMap();
            providerTokens.put("cognito-identity.amazonaws.com", "auidhashaisdhals");
            tokenRequest.setLogins(providerTokens);
    
            AmazonCognitoIdentityClient identityClient = new AmazonCognitoIdentityClient();
            identityClient.setRegion(RegionUtils.getRegion(Configuration.REGION));
            GetCredentialsForIdentityRequest request = new GetCredentialsForIdentityRequest();
            request.withLogins(providerTokens);
            request.setIdentityId("us-east-1:XXXXX-9ac6-YYYY-ac07-ZZZZZZZZZZZZ");
            GetCredentialsForIdentityResult tokenResp = identityClient.getCredentialsForIdentity(request);
    

    如果您拥有正确的 Cognito Token,那么您应该能够获得身份并进行身份验证。如果令牌无效,则您不验证您的用户。

    如果令牌无效,亚马逊会抛出异常,因此您可以捕获并返回 404 错误。

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 1970-01-01
      • 2022-06-19
      • 1970-01-01
      • 2016-01-17
      • 2018-12-19
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      相关资源
      最近更新 更多