@envelop/auth0 3.4.0-alpha-26c4ae2.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
## `@envelop/auth0`
|
|
2
|
+
|
|
3
|
+
This plugin validates an JWT token created by [Auth0](https://auth0.com/), and injects the Auth0 user properties into your GraphQL context. With this plugin, you can implement authentication and authorization in a simple way.
|
|
4
|
+
|
|
5
|
+
> The plugins is using [JWKS](https://auth0.com/docs/tokens/json-web-tokens/json-web-key-sets) standard in order to validate the token.
|
|
6
|
+
|
|
7
|
+
## Getting Started
|
|
8
|
+
|
|
9
|
+
We recommend using the [Adding Authentication with Auth0 guide](https://www.envelop.dev/docs/guides/adding-authentication-with-auth0) if this is your first time using this plugin!
|
|
10
|
+
|
|
11
|
+
1. Sign up for [Auth0](https://auth0.com/), create a tenant based on your needs, and then create an Auth0 Application (https://auth0.com/docs/applications).
|
|
12
|
+
2. Setup Auth0 client based on your client app. You should be able to login on your app, and get a JWT token from Auth0. Make sure to pass that token in your GraphQL requests sent to your server, using headers (for example: `Authorization: Bearer XYZ`). You can find more info here: https://auth0.com/docs/quickstart/spa
|
|
13
|
+
3. From your tenant configuration screen, find your `audience` and `domain` configurations.
|
|
14
|
+
4. Setup Envelop with that plugin:
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { envelop } from '@envelop/core';
|
|
18
|
+
import { useAuth0 } from '@envelop/auth0';
|
|
19
|
+
|
|
20
|
+
const getEnveloped = envelop({
|
|
21
|
+
plugins: [
|
|
22
|
+
// ... other plugins ...
|
|
23
|
+
useAuth0({
|
|
24
|
+
onError: e => { ... }, // In case of an error, you can override it and customize the error your client will get.
|
|
25
|
+
domain: 'YOUR_AUTH0_DOMAIN_HERE',
|
|
26
|
+
audience: 'YOUR_AUTH0_AUDIENCE_HERE',
|
|
27
|
+
headerName: 'authorization', // Name of the header
|
|
28
|
+
preventUnauthenticatedAccess: true, // If you need to have unauthenticated parts on your schema, make sure to disable that by setting it to `false` and the check it in your resolvers.
|
|
29
|
+
extendContextField: 'auth0', // The name of the field injected to your `context`
|
|
30
|
+
tokenType: 'Bearer', // Type of token to expect in the header
|
|
31
|
+
}),
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
5. Make sure to pass your request as part of the context building:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
myHttpServer.on('request', async req => {
|
|
40
|
+
const { contextFactory } = getEnveloped({ req });
|
|
41
|
+
const contextValue = await contextFactory({ req }); // Make sure to pass it here
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
> By default, this plugins looks for `req` or `request` properties in your base context. If you need to override it, please use `extractTokenFn` and you can customize it.
|
|
46
|
+
|
|
47
|
+
6. You should now be able to validate user tokens, and if a user is valid, you can get the Auth0 user id (called `sub`) as part of your `context` during execution:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const myResolvers = {
|
|
51
|
+
Query: {
|
|
52
|
+
me: (root, args, context, info) => {
|
|
53
|
+
const auth0UserId = context.auth0.sub;
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### API Reference
|
|
60
|
+
|
|
61
|
+
#### `jwksClientOptions`
|
|
62
|
+
|
|
63
|
+
Pass this to customize the JWKS client creation. See: https://github.com/auth0/node-jwks-rsa
|
|
64
|
+
|
|
65
|
+
> Setting this will override any other options defined by this plugin.
|
|
66
|
+
|
|
67
|
+
#### `jwtDecodeOptions`
|
|
68
|
+
|
|
69
|
+
Pass this to customize the JWT `decode` phase. See: https://www.npmjs.com/package/jws#jwsdecodesignature
|
|
70
|
+
|
|
71
|
+
#### `jwtVerifyOptions`
|
|
72
|
+
|
|
73
|
+
Pass this to customize the JWT `verify` phase. See: https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback
|
|
74
|
+
|
|
75
|
+
#### `onError(e: Error)`
|
|
76
|
+
|
|
77
|
+
By default, this library will throw an error during context building if an error has happened.
|
|
78
|
+
|
|
79
|
+
If you wish to customize the error, you can add `onError` callback and throw a custom error based on your needs.
|
|
80
|
+
|
|
81
|
+
#### `preventUnauthenticatedAccess`
|
|
82
|
+
|
|
83
|
+
By default, this library will prevent execution flow and throw an error in case of an authentication error.
|
|
84
|
+
|
|
85
|
+
Setting this to `false` will lead to a `null` value in case of authentication issue (and `onError` will still get called).
|
|
86
|
+
|
|
87
|
+
#### `domain`
|
|
88
|
+
|
|
89
|
+
Specifies the Auth0 domain, please note that you need to specify that field with a protocol, for example: `my-domain.us.auth0.com`
|
|
90
|
+
|
|
91
|
+
#### `audience`
|
|
92
|
+
|
|
93
|
+
Specifies the Auth0 audience.
|
|
94
|
+
|
|
95
|
+
#### `extractTokenFn(context: any)`
|
|
96
|
+
|
|
97
|
+
If you wish to customize the token extraction from your HTTP request, override this function. It gets the `context` built so far as an argument, and you can extract your auth token based on your setup.
|
|
98
|
+
|
|
99
|
+
#### `headerName` + `tokenType`
|
|
100
|
+
|
|
101
|
+
If `extractTokenFn` is not set, the default behavior of this plugin is to look for `req` and `request` in the context, then look for `headers` and look for `authentication` header (you can customize it with `headerName`). Then, it validates that the token is of type `Bearer` (you can customize it with `tokenType` option).
|
|
102
|
+
|
|
103
|
+
#### `extendContextField`
|
|
104
|
+
|
|
105
|
+
The name of the field to inject to your `context`. When the user is valid, the decoded and verified payload of the JWT is injected. In most cases, the field that you need is `sub` (which refers to the internal Auth0 user identifier).
|
|
106
|
+
|
|
107
|
+
You can read more about the token structure here: https://auth0.com/docs/tokens/json-web-tokens/json-web-token-structure
|
|
108
|
+
|
|
109
|
+
By default, the `auth0` value is used.
|
|
110
|
+
|
|
111
|
+
## Notes
|
|
112
|
+
|
|
113
|
+
> Make sure to specify `audience` field in the client, otherwise you'll get an opaque token instead of a JWT token.
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/auth0",
|
|
3
|
-
"version": "3.4.0
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "2.4.0
|
|
6
|
+
"@envelop/core": "^2.4.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|