How to check permissions of an entity on create in appsync

旧街凉风 提交于 2020-04-30 16:32:12

问题


Sorry for the unspecific title. However, I am having a hard time to describe it.

I am using aws-appsync with aws cognito for authentication.

I've followed the amplify docs about the @auth annotation to handle permissions for mutations and queries.

Here is an example of my schema. A user can create an entry and share it with others. However, they should only read the entry and should not have permissions to edit it.

An entry also has multiple notes. (And some more fields)

type Entry @model @versioned @auth (rules: [
  { allow: owner },
  { allow: owner, ownerField: "shared", queries: [get, list], mutations: []}
])  @searchable {
  id: ID!
  date: AWSDate
  updated_at: AWSDateTime
  text: String
  notes: [Note] @connection(name: "EntryNotes")
  shared: [String]!
}

And here is the note

type Note @model @versioned @auth (rules: [{ allow: owner }]) {
  id: ID!
  text: String
  track: Track!
  diary: DiaryEntry @connection(name: "EntryNotes")
}

This works fine so far. But the problem is the Note connection. Because if you create a note you would create it like this:

mutation makeNote {
  createNote (input: {
    text: "Hello there!"
    noteEntryId: "444c80ee-6fd9-4267-b371-c2ed4a3ccda4"
  }) {
    id
    text
  }
}

The problem is now, that you can create notes for entries that you do not have access to. If you somehow find out which id they have.

Is there a way to check if you have permissions to the entry before creating the note?


回答1:


Currently, the best way to do this is via custom resolvers within the Amplify CLI. Specifically, you are able to use AppSync pipeline resolvers to perform the authorization check before creating the note. Your pipeline resolver would contain two functions. The first would look up the entry and compare the owner to the $ctx.identity. The second function would handle writing the record to DynamoDB. You can use the same logic found in build/resolvers/Mutation.createNote.re(q|s).vtl to implement the second function by copying it into the top level resolvers/ directory and then referencing it from your custom resource. After copying the logic, you will want to disable the default createNote mutation by changing @model to @model(mutations: { update: "updateNote", delete: "deleteNote" }).

For more information on how to setup custom resolvers see https://aws-amplify.github.io/docs/cli/graphql#add-a-custom-resolver-that-targets-a-dynamodb-table-from-model. For more information on pipeline resolvers (slightly different than the example in the amplify docs) see https://docs.aws.amazon.com/appsync/latest/devguide/pipeline-resolvers.html. Also see the CloudFormation reference docs for AppSync https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-reference-appsync.html.

Looking towards the future, we are working on a design that would allow you to define auth rules that span @connections. When this is done, it will automatically configure this pattern but there is not yet a set release date.



来源:https://stackoverflow.com/questions/54767325/how-to-check-permissions-of-an-entity-on-create-in-appsync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!