【问题标题】:Authenticating AppSync mutation using AWS Cognito User pool on Graphql playground在 Graphql 操场上使用 AWS Cognito 用户池对 AppSync 突变进行身份验证
【发布时间】:2020-09-30 07:46:01
【问题描述】:

一个非常基本的场景,我想在 Graphql Playground 上测试 AppSync 突变,该突变在 API 密钥身份验证中运行良好。

除了 API 密钥身份验证之外,我还附加了一个额外的授权提供程序。

变异:

type Mutation {
  createPitch(gameID: ID!, pitchID: Int!, pitchEvent: PitchInput!): Pitch
    @aws_api_key
  predictPitch(userID: String!, gamePitchID: String!, prediction: PredictionInput): Prediction
    @aws_cognito_user_pools
}

在 graphql 操场上调用 predictPitch 突变:

mutation PredictPitch($prediction:PredictionInput) {
  predictPitch(userID: "12345", gamePitchID: "29fb2xx-xxxxx-xxxxx-1", 
  prediction: $prediction ) {
    gameID
    gamePitchID
  }
}

查询变量:

{
  "prediction": {
    "gameID": "29",
    "hitterGuess": "Miss",
    "pitcherGuess": "Fastball"
  }
}

标题:


{
  "X-API-KEY": "da2-o6fs2lq47vbehexxxxxxxx",
  "Authorization": "Bearer xxxx-the-pretty-long-jwt-token-from-cognito login"
}

我已经尝试单独使用Authorization 标头并与x-api-key 结合使用。 到目前为止没有任何效果。我很确定我错过了一点点。

{
  "error": {
    "errors": [
      {
        "errorType": "UnauthorizedException",
        "message": "Valid authorization header not provided."
      }
    ]
  }
}

注意: JWT 令牌 AccessToken 是通过 aws-cli 生成的 aws cognito-idp admin-initiate-auth.

【问题讨论】:

  • 在只有 Authorization 标头的情况下,我还使用 Cognito 用户池对查询进行身份验证,除了查询定义中的指令之外,我对所有对象(类型)也有相同的指令查询正在访问。在这两种情况下(授权和 API 密钥),我尝试过,如果我用查询和查询试图访问的对象定义两个指令(aws_cognito_user_pools 和 aws_api_key),它就可以工作。因此,对于您的情况,您可以尝试使用“Pitch”和“Pridiction”添加指令。也许这可以给你一些提示。

标签: graphql aws-appsync


【解决方案1】:

我必须在 Prediction 类型上添加 @aws_cognito_user_pools 以及我的突变。

type Prediction @aws_cognito_user_pools {
   gameID
   gamePitchID
}

另外,从 Cognito 我不得不像这样使用 idToken:

{
   "Authorization": "xxxxxxxxxx"
}

请注意Bearer 不见了。

【讨论】:

  • 看来我的评论对你来说很方便。我很高兴它有帮助!
  • 这很有帮助,谢谢。我还发现我没有在 AppSync 中启用“Amazon Cognito 用户池”作为授权设置,因此出现“UnauthorizedException”错误。
猜你喜欢
  • 2021-03-07
  • 2016-08-18
  • 2021-06-23
  • 2019-10-15
  • 2018-09-03
  • 2020-07-31
  • 2019-09-30
  • 2018-02-23
  • 2016-09-27
相关资源
最近更新 更多