1

我有一个 Web 应用程序,它使用户能够轻松地管理 AWS 资源,例如使用 GUI 启动 Ec2 实例。不同的用户可以使用此 Web 应用程序。我已经实现了自己的用户管理。有些用户可能不想为我的网络应用程序创建一个帐户并存储他们的凭证以向 AWS 资源发出请求。对他们来说可能不安全。因此,我想使用以下流程构建一种机制。

  1. 用户使用登录亚马逊登录我的网络应用程序
  2. 使用亚马逊登录将向我提供访问令牌
  3. 使用此令牌获取临时凭据
  4. 将这些凭据用于将来对 AW 资源的请求

这样,我的网络应用程序中就不会存储永久凭据。

我将 java 用于我的后端服务。那么是否有任何 AWS APi 可以这样做?

我尝试了以下代码:

 val client = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials())
    val idRequest = new GetIdRequest();
   idRequest.setAccountId(account-id);
 idRequest.setIdentityPoolId("us-east-1:xxxx");
   // If you are authenticating your users through an identity provider
    // then you can set the Map of tokens in the request
 val logins = new HashMap[String, String]()
  providerTokens+=("www.amazon.com"->"Atza|IwE-xxxxxxxxxxxxxxxxxxx")

  idRequest.setLogins(logins);

  val idResp = client.getId(idRequest)

 val identityId = idResp.getIdentityId()


    // Create the request object
    val tokenRequest = new GetOpenIdTokenRequest();
    tokenRequest.setIdentityId("us-east-1:xxxxxx");

//
    val tokenResp = client.getOpenIdToken(tokenRequest);
//     get the OpenID token from the response
    val openIdToken = tokenResp.getToken()

//     you can now create a set of temporary, limited-privilege credentials to access
//     your AWS resources through the Security Token Service utilizing the OpenID
//     token returned by the previous API call. The IAM Role ARN passed to this call
//     will be applied to the temporary credentials returned


    val stsClient = new AWSSecurityTokenServiceClient(new AnonymousAWSCredentials());
    val stsReq = new AssumeRoleWithWebIdentityRequest();
    stsReq.setRoleArn("arn:aws:iam::account-id:role/Cognito_Auth_Role");
    stsReq.setWebIdentityToken(openIdToken);
    stsReq.setRoleSessionName("AppTestSession");
    val stsResp = stsClient.assumeRoleWithWebIdentity(stsReq);
    val stsCredentials = stsResp.getCredentials();

我收到以下错误:

禁止访问身份“us-east-1:xxxxxxxxxxxxxxxxxx”。(服务:AmazonCognitoIdentity;状态代码:400;错误代码:NotAuthorizedException

以下是 IAM 角色中的 Cognito_Auth_Role 角色策略:

{
  "Version": "2012-10-17",
  "Statement":[{
      "Effect":"Allow",
      "Action":"cognito-sync:*",
      "Resource":["arn:aws:cognito-sync:us-east-1:xxxxxxx:identitypool/${cognito-identity.amazonaws.com:aud}/identity/${cognito-identity.amazonaws.com:sub}/*"]
      }]
  }

以下是此角色的信任关系:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Federated": "cognito-identity.amazonaws.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "cognito-identity.amazonaws.com:aud": "us-east-1:xxxxx"
        },
        "ForAnyValue:StringLike": {
          "cognito-identity.amazonaws.com:amr": "www.amazon.com"
        }
      }
    }
  ]
}

请指出我,这里有什么问题?

谢谢

4

1 回答 1

0

您可以使用 AWS Cognito 处理大部分管道,或者您可以连接您自己的 OpenId 身份提供商或您订阅的提供商服务。

这是身份和访问管理 (IAM) 的文档: https ://aws.amazon.com/documentation/iam/

或者您可以让他们使用 Cognito 为您做一些工作:http: //docs.aws.amazon.com/cognito/latest/developerguide/what-is-amazon-cognito.html

虽然看起来这仅适用于移动设备,但他们有在浏览器中使用 JavaScript SDK 的文档:http: //docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-intro.html

于 2016-09-04T20:46:23.467 回答