|
| 1 | +[[subscriptions-authorization]] |
| 2 | +:description: This page describes how to set up authorization features for subscriptions in the Neo4j GraphQL Library. |
| 3 | += Subscriptions authorization |
| 4 | + |
| 5 | +Subscriptions require their own authorization rules, which are configured with the `@subscriptionsAuthorization` directive. |
| 6 | +These rules are different to normal authorization rules because only filtering rules are available for subscriptions events and subscriptions events have more limitations in how they can be filtered. |
| 7 | + |
| 8 | +All subscriptions authorization rules have an implied requirement for authentication, given that the rules are normally evaluated against values in the JWT payload. |
| 9 | + |
| 10 | +== Rules |
| 11 | + |
| 12 | +=== Filtering |
| 13 | + |
| 14 | +Filtering rules prevent events which contain information that users don't have access to from reaching them - they will receive no indication that this is the case. |
| 15 | +These rules are evaluated when the events are returned from the database, before they are broadcasted out to subscribing GraphQL clients. |
| 16 | + |
| 17 | +For instance, here is how to filter out `User` events which don't match the JWT of the user listening for events: |
| 18 | + |
| 19 | +[source, graphql, indent=0] |
| 20 | +---- |
| 21 | +type User @subscriptionsAuthorization(filter: [ |
| 22 | + { where: { node: { id: "$jwt.sub" } } } |
| 23 | +]) { |
| 24 | + id: ID! |
| 25 | +} |
| 26 | +---- |
| 27 | + |
| 28 | +==== Events |
| 29 | + |
| 30 | +Filtering can be configured to only be performed on certain events: |
| 31 | + |
| 32 | +* `CREATED` |
| 33 | +* `UPDATED` |
| 34 | +* `DELETED` |
| 35 | +* `RELATIONSHIP_CREATED` |
| 36 | +* `RELATIONSHIP_DELETED` |
| 37 | + |
| 38 | +For instance, to only require filtering for mutations to a type itself and not its relationships: |
| 39 | + |
| 40 | +[source, graphql, indent=0] |
| 41 | +---- |
| 42 | +type User @subscriptionsAuthorization(filter: [ |
| 43 | + { events: [CREATED, UPDATED, DELETED], where: { node: { id: "$jwt.sub" } } } |
| 44 | +]) { |
| 45 | + id: ID! |
| 46 | +} |
| 47 | +---- |
| 48 | + |
| 49 | +== Authorization without authentication |
| 50 | + |
| 51 | +Authentication is implicitly required for every authorization check by default, but this can be disabled on a per-rule basis. |
| 52 | +This could be the case, for instance, when a node has a property which flags whether the node should be public or not. |
| 53 | + |
| 54 | +For instance, in the case where some `Post` nodes are private whilst other `Post` nodes are public, here is how to set this up: |
| 55 | + |
| 56 | +[source, graphql, indent=0] |
| 57 | +---- |
| 58 | +type Post @subscriptionsAuthorization(filter: [ |
| 59 | + { requireAuthentication: false, where: { node: { public: true } } } |
| 60 | +]) { |
| 61 | + title: String! |
| 62 | + content: String! |
| 63 | + public: Boolean! |
| 64 | +} |
| 65 | +---- |
0 commit comments