Skip to content

Commit 4e5f7e2

Browse files
darrellwardemjfwebbrsill-neo4j
authored
Add documentation for @subscriptionsAuthorization (#165)
* Add documentation for `@subscriptionsAuthorization` * Update modules/ROOT/pages/security/authorization.adoc Co-authored-by: Michael Webb <[email protected]> * Apply suggestions from code review Co-authored-by: Richard Sill <[email protected]> --------- Co-authored-by: Michael Webb <[email protected]> Co-authored-by: Richard Sill <[email protected]>
1 parent 74cdc67 commit 4e5f7e2

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

modules/ROOT/pages/directives/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ a| Required to differentiate interfaces that are used for relationship propertie
4747
| Used in combination with `@jwt`.
4848
Configures the JWT authentication and authorization filters to include an additional JWT claim which is either nested or using special characters not supported by GraphQL.
4949

50-
| `@subscriptionsAuthorization`
50+
| xref::/security/subscriptions-authorization.adoc[`@subscriptionsAuthorization`]
5151
| Specifies authorization rules for subscriptions on the type.
5252

5353
|===

modules/ROOT/pages/security/authorization.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ All authorization rules have an implied requirement for authentication, given th
1010
In the case of explicit authentication, configured using the `@authentication` directive, it is only ever evaluated during Cypher translation time.
1111
Unauthenticated requests with queries requiring authentication never reach the database.
1212

13+
[WARNING]
14+
====
15+
The `@authorization` directive does not apply to subscriptions, it only applies to queries and mutations.
16+
Instead, use xref::/security/subscriptions-authorization.adoc[`@subscriptionsAuthorization`] to configure the authorization for subscriptions if you intend to use subscriptions in your API and want the events protected.
17+
====
18+
1319
== Rules
1420

1521
=== Filtering

modules/ROOT/pages/security/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ auth/authorization/where.adoc, authentication-and-authorization/index.adoc
88

99
* xref::/security/authentication.adoc[Authentication] - Explicit authentication, configured using the `@authentication` directive.
1010
* xref::/security/authorization.adoc[Authorization] - Authorization rules set using the `@authorization` directive.
11+
* xref::/security/subscriptions-authorization.adoc[Subscriptions authorization] - Authorization rules for subscriptions set using the `@subscriptionsAuthorization` directive.
1112
* xref::/security/configuration.adoc[Configuration] - Instructions to set up instantiation.
1213
* xref::/security/impersonation-and-user-switching.adoc[Impersonation and user switching] - How to set up impersonation and user switching features.
1314
* xref::/security/operations.adoc[Operations] - Reference on GraphQL queries and how each location in each query triggers the evaluation of different authentication/authorization rules.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)