具有OpenID提供商和有条件DynamoDB策略的Cognito身份



我使用自定义的打开ID提供商,AWS接受我的令牌,我能够使用Coginto身份池来限制对DynamoDB数据库的访问。我的问题是,当我将coginito Identity与DynamoDB:LeadingKeys-> $ {cognito-nidentity.amazonaws.com:sub}'始终在我的用户ID之前预处理AWS-RIGION:EU-Central-1:eu-Central-1:1234567

在我的数据库设计中,我只需要用户ID(没有区域(作为主要。是否有可能从子中删除该区域的可能性。我已经试图将条件更改为我的自定义开放式提供商(如描述的文档( - 但这始终不起作用

我的政策:

- Effect: Allow
  Action: 
    - dynamodb:DescribeTable
    - dynamodb:Query
    - dynamodb:GetItem
    - dynamodb:PutItem
    - dynamodb:UpdateItem
    - dynamodb:DeleteItem
    - dynamodb:BatchWriteItem
  Resource:
    - arn:aws:dynamodb:eu-central-1:123:table/table
  Condition:
    ForAllValues:StringEquals:
      dynamodb:LeadingKeys: ${custom.open.id/openid:sub}

我的JavaScript代码看起来像这样:

const Logins = {}
  Logins['custom.open.id'] = accessToken; // my own openId access JWT Token
  // Add the User's Id Token to the Cognito credentials login map.
  const credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: identityPoolId,
    Logins: Logins
  });
  return credentials.getPromise()
    .then( error => {
      if (error) {
        return Promise.reject(error)
      }
      if (credentials.needsRefresh() === true) {
        return credentials.refreshPromise()
      }
      return null;
    })
    .then(error => {
      if (error) {
         return Promise.reject(error)
      }
      console.log('Successfully logged!', credentials.data);
      const dynamoDb = new AWS.DynamoDB.DocumentClient({
        credentials: credentials,
        region: region,
      });
      const params = {
        TableName: "table",
        KeyConditionExpression: "#userId = :userId",
        ExpressionAttributeNames: {
          "#userId": "userId"
        },
        ExpressionAttributeValues: {
          ":userId": currentUserId
        }
      }
      return dynamoDb.query(params).promise();
    })
    .then(data => {
      console.log("DynamoDb", data)
    })

和我的JWT访问令牌的内容是:

{
  "sub": "123",
  "nonce": "VRN3voQIhS6Nb6AzSsv907GxPnKc0szo",
  "sid": "4fd7fd36-5a36-40e6-b381-b004a52be473",
  "at_hash": "wF9BX32r6yQqvV5j0QGj8g",
  "s_hash": "NzqHNkNF7FfCJa1tKudjTg",
  "aud": "test_client",
  "exp": 1556560701,
  "iat": 1556557101,
  "iss": "https://custom.open.id/openid"
}

此IAM JSON中的子是身份ID。身份ID定义为每个尝试通过Cognito Identity池获取凭据的用户的唯一身份。

无法更改身份ID的模式,因为格式由Cognito的架构预定。有关Cognito Identity池的角色和政策的更多详细信息,请参考此官方AWS博客文章。

最新更新