|
| 1 | +--- |
| 2 | +title: 'authorization' |
| 3 | +--- |
| 4 | + |
| 5 | +# authorization |
| 6 | + |
| 7 | +The `authorization` configuration lets you control fine-grained access to your GraphQL schema using |
| 8 | +directives like `@authenticated` and `@requiresScopes`. This allows you to restrict which fields |
| 9 | +authenticated users can access based on their authentication status or specific scopes. |
| 10 | + |
| 11 | +For practical examples and a guide to authorization concepts, see |
| 12 | +**["Authorization"](../security/authorization)** in the documentation. |
| 13 | + |
| 14 | +## Directives |
| 15 | + |
| 16 | +Access control is defined within the supergraph schema using the following directives: |
| 17 | + |
| 18 | +- `@authenticated` - restricts access to authenticated users only |
| 19 | +- `@requiresScopes(scopes: [[String]])` - restricts access based on specific scopes |
| 20 | + |
| 21 | +## Configuration Options |
| 22 | + |
| 23 | +### `enabled` |
| 24 | + |
| 25 | +- **Type:** `boolean` |
| 26 | +- **Default:** `true` |
| 27 | + |
| 28 | +Whether to enable authorization directives processing. Set to `false` to disable authorization |
| 29 | +checks entirely. |
| 30 | + |
| 31 | +### `unauthorized.mode` |
| 32 | + |
| 33 | +- **Type:** `string` |
| 34 | +- **Allowed values:** `"filter"` or `"reject"` |
| 35 | +- **Default:** `"filter"` |
| 36 | + |
| 37 | +Controls how the router handles unauthorized field access: |
| 38 | + |
| 39 | +- `"filter"`: Remove unauthorized fields and continue processing (returns errors for removed fields) |
| 40 | +- `"reject"`: Reject the entire request if any unauthorized fields are accessed |
| 41 | + |
| 42 | +## Examples |
| 43 | + |
| 44 | +### Filter Mode (Default) |
| 45 | + |
| 46 | +With `filter` mode, unauthorized fields are silently removed from the operation, but the query |
| 47 | +continues to execute and return the data the user can access: |
| 48 | + |
| 49 | +```yaml filename="router.config.yaml" |
| 50 | +authorization: |
| 51 | + directives: |
| 52 | + enabled: true |
| 53 | + unauthorized: |
| 54 | + mode: filter |
| 55 | +``` |
| 56 | +
|
| 57 | +**Request:** |
| 58 | +
|
| 59 | +```graphql |
| 60 | +query { |
| 61 | + publicData |
| 62 | + me { |
| 63 | + name |
| 64 | + email |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +If the user is not authenticated, the response might look like: |
| 70 | + |
| 71 | +```json |
| 72 | +{ |
| 73 | + "data": { |
| 74 | + "publicData": "available", |
| 75 | + "me": null |
| 76 | + }, |
| 77 | + "errors": [ |
| 78 | + { |
| 79 | + "message": "Unauthorized field or type", |
| 80 | + "extensions": { |
| 81 | + "code": "UNAUTHORIZED_FIELD_OR_TYPE", |
| 82 | + "affectedPath": "me" |
| 83 | + } |
| 84 | + } |
| 85 | + ] |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Reject Mode |
| 90 | + |
| 91 | +With `reject` mode, if any field is unauthorized, the entire request is rejected: |
| 92 | + |
| 93 | +```yaml filename="router.config.yaml" |
| 94 | +authorization: |
| 95 | + directives: |
| 96 | + enabled: true |
| 97 | + unauthorized: |
| 98 | + mode: reject |
| 99 | +``` |
| 100 | +
|
| 101 | +**Request (same as above):** |
| 102 | +
|
| 103 | +```graphql |
| 104 | +query { |
| 105 | + publicData |
| 106 | + me { |
| 107 | + name |
| 108 | + email |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +**Response:** |
| 114 | + |
| 115 | +```json |
| 116 | +{ |
| 117 | + "data": null, |
| 118 | + "errors": [ |
| 119 | + { |
| 120 | + "message": "Unauthorized field or type", |
| 121 | + "extensions": { |
| 122 | + "code": "UNAUTHORIZED_FIELD_OR_TYPE" |
| 123 | + } |
| 124 | + } |
| 125 | + ] |
| 126 | +} |
| 127 | +``` |
0 commit comments