公共查询和突变(无身份验证)

Jul*_*hon 9 amazon-web-services aws-appsync

文档我们可以通过3种方式授权应用程序与API进行交互,但看起来并不存在拥有公共端点的方法.

例如,如果我希望任何人查询待办事项列表,但只有经过身份验证的用户可以向该列表添加待办事项,我该如何实现?

或者,如果我想允许任何人进行模式内省,但是将所有其他查询限制为经过身份验证的用户,是否可能?

我正在使用cognito进行身份验证.我注意到有一个AppId client regex字段说(Optional) Type a regular expression to allow or block requests to this API.但不幸的是我找不到任何例子.也许这就是我要找的东西?

谢谢

朱利安

Kar*_*hik 7

有几种方法可以根据身份验证机制执行此操作.

假设您正在使用Cognito Identity并使用AWS IAM流进行身份验证.然后,您将有2个策略,一个用于Authenticated User,另一个用于Unauthenticated User.

给定GraphQL架构

schema{
   query:Query
   mutation:Mutation
}

type Query{
   listTodo(count:Int, paginationToken:String):[TodoConnection];

}

type Mutation{
   addTodo(input:TodoInput):Todo
}
Run Code Online (Sandbox Code Playgroud)

您的未经身份验证的政策看起来像

{
  "Version": "2012-10-17",
  "Statement": [
  {
     "Effect": "Allow",
     "Action": [
        "appsync:GraphQL"
     ],
     "Resource": [
        "arn:aws:appsync:us-west-2:<account-id>:apis/<api-id>/types/Query/fields/listTodo", 
        //-> below is for schema introspection
        "arn:aws:appsync:us-west-2:<account-id>:apis/<api-id>/types/Query/fields/__schema" 
     ]
    ]
   }
}
Run Code Online (Sandbox Code Playgroud)

您的身份验证用户策略看起来像

{
  "Version": "2012-10-17",
  "Statement": [
  {
     "Effect": "Allow",
     "Action": [
        "appsync:GraphQL"
     ],
     "Resource": [
        "arn:aws:appsync:us-west-2:<account-id>:apis/<api-id>/types/Mutation/fields/addTodo",
        "arn:aws:appsync:us-west-2:<account-id>:apis/<api-id>/types/Query/fields/listTodo", 
        //-> below is for schema introspection
        "arn:aws:appsync:us-west-2:<account-id>:apis/<api-id>/types/Query/fields/__schema"
     ]
    ]
   }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是JWT令牌,则必须将每个Cognito用户池用户与一个组关联(如"Admin","Users"等).然后,您必须将每个查询/突变与可以使用AWS AppSync auth指令执行操作的Cognito Groups相关联.为此,您只需要更新下面的架构:

schema{
   query:Query
   mutation:Mutation
}

type Query{
   listTodo(count:Int, paginationToken:String):[TodoConnection];
     @aws_auth(cognito_groups:["Users", "Admin"])
}

type Mutation{
   addTodo(input:TodoInput):Todo
     @aws_auth(cognito_groups:["Admin"])
}
Run Code Online (Sandbox Code Playgroud)

基于API密钥的身份验证,无法控制操作.