通过身份池联合的开发人员身份验证身份无法在 Amplify 项目中进行 AppSync 调用

Developer authenticated identities federated via an Identity Pool cannot make AppSync calls in Amplify project

目前,我正在对 Linkedin 用户进行身份验证并在 Lambda 中调用 GetOpenIdTokenForDeveloperIdentityGetCredentialsForIdentity,并使用 Auth.federatedSignIn() 登录这些用户,我能够检索当前经过身份验证的用户和凭据。

使用 Amplify 的 APIClass 或 AWSAppSyncClient,但是,我无法让这些用户通过 AppSync 进行 GraphQL 调用,AppSync 配置为授权 Cognito 用户池和 AWS IAM 用户。当身份验证类型相应更改时,Cognito 用户池用户可以毫无问题地进行 AppSync 调用。

我已经为联合用户尝试了以下各项:

API.graphql({
  query: queries.getUserProfile,
  variables: {
    input: {
      email,
    }
  },
  authMode: 'AWS_IAM'
})

const client = new AWSAppSyncClient({
  url: process.env.GRAPHQL_ENDPOINT,
  region: process.env.AWS_REGION,
  auth: {
    type: AUTH_TYPE.AWS_IAM,
    credentials: async () => Auth.currentCredentials(),
  },
  disableOffline: true,
});

client.query({
  query: gql(queries.getUserProfile),
  variables: {
    input: {
      email,
    },
  },
  fetchPolicy: 'no-cache',
})

身份池是通过无服务器 resources/CloudFormation 模板提供的,我已经设置了信任关系和经过身份验证的角色,如下所示:

  ProjectAuthRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Federated: "cognito-identity.amazonaws.com"
            Action:
              - "sts:AssumeRoleWithWebIdentity"
            Condition:
              StringEquals:
                "cognito-identity.amazonaws.com:aud":
                  - Ref: ProjectIdentityPool
              ForAnyValue:StringLike:
                "cognito-identity.amazonaws.com:amr": authenticated
  AuthPolicy:
    Type: AWS::IAM::Policy
    Properties:
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - mobileanalytics:PutEvents
              - cognito-sync:*
              - cognito-identity:*
            Resource:
              - "*"
          - Effect: Allow
            Action:
              - appsync:GraphQL
            Resource:
              - "*"
      Roles:
        - Ref: ProjectAuthRole

我已经能够通过将 @aws_iam 模式指令添加到每个类型和输入定义来进行 GraphQL 调用,但听起来这甚至没有必要,因为授权应该发生在 AWS AppSync GraphQL API 级别。

还有什么我可能忘记配置的地方吗?

看起来问题出在我们的 serverless-appsync-plugin YAML 文件中的 additionalAuthenticationProviders

我的理解是,如果您希望其他身份验证提供程序(在我们的例子中,AWS_IAM)与默认身份验证提供程序(在我们的例子中)一起访问,则必须将模式级指令应用于一个人的模式中的类型定义例,AMAZON_COGNITO_USER_POOLS).

更多详细信息来源: https://docs.aws.amazon.com/appsync/latest/devguide/security.html#using-additional-authorization-modes