Authentication 使用cognito验证用户身份
我正在创建一个后端服务并使用AWS Cognito。 现在,我正在使用我的开发者身份验证。 因此,我的用户可以使用我自己的系统登录。这部分已经完成 但是,我在登录后对用户进行身份验证时遇到问题。假设用户向我发送一个访问API的某个私有函数的请求。如何识别和验证此用户 如果我使用facebook作为登录提供者,我可以使用他的Cognito ID和他的facebook访问令牌来完成http头。然后,我可以对他进行身份验证,尝试使用函数GetOpenIdTokenRequest获取Cognito令牌 我要一份类似的Authentication 使用cognito验证用户身份,authentication,amazon-cognito,Authentication,Amazon Cognito,我正在创建一个后端服务并使用AWS Cognito。 现在,我正在使用我的开发者身份验证。 因此,我的用户可以使用我自己的系统登录。这部分已经完成 但是,我在登录后对用户进行身份验证时遇到问题。假设用户向我发送一个访问API的某个私有函数的请求。如何识别和验证此用户 如果我使用facebook作为登录提供者,我可以使用他的Cognito ID和他的facebook访问令牌来完成http头。然后,我可以对他进行身份验证,尝试使用函数GetOpenIdTokenRequest获取Cognito令牌
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的东西,在oauth2中我接收到一个令牌,可以检查用户身份
使用Cognito的正确方法是什么?服务器端的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令牌,那么您应该能够获得标识并进行身份验证。如果令牌无效,则不验证用户
如果令牌无效,Amazon将引发异常,因此您可以捕获并返回404错误。是的,使用标识ID和Cognito令牌,您可以调用getCredentialsForIdentity并确定令牌有效且属于该标识 然后,您可以使用这些凭据作为该用户调用不同的服务 另一种选择是将后端服务器逻辑置于API网关之后:
然后,您的用户将从客户端获取凭据,并使用这些凭据调用面向服务器端逻辑的API网关。如何验证getCredentialsForIdentity的令牌?对象是“凭证”:{“AccessKeyId”:“string”,“Expiration”:number,“SecretKey”:“string”,“SessionToken”:“string”},但我找不到如何验证它的信息。