@graphql-yoga/plugin-jwt 1.0.0-alpha-20221021134149-437795dc
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/LICENSE +23 -0
- package/cjs/index.js +97 -0
- package/cjs/package.json +1 -0
- package/esm/index.js +93 -0
- package/package.json +42 -0
- package/typings/index.d.cts +46 -0
- package/typings/index.d.ts +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-2020 Graphcool
|
|
4
|
+
Copyright (c) 2020-2021 Prisma
|
|
5
|
+
Copyright (c) 2021- The Guild
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useJwt = void 0;
|
|
4
|
+
const jwks_rsa_1 = require("jwks-rsa");
|
|
5
|
+
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
6
|
+
const graphql_1 = require("graphql");
|
|
7
|
+
function useJwt(options) {
|
|
8
|
+
let jwksClient;
|
|
9
|
+
if (options.jwksUri) {
|
|
10
|
+
jwksClient = new jwks_rsa_1.JwksClient({
|
|
11
|
+
cache: true,
|
|
12
|
+
rateLimit: true,
|
|
13
|
+
jwksRequestsPerMinute: 5,
|
|
14
|
+
jwksUri: options.jwksUri,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const headerName = options.headerName || 'authorization';
|
|
18
|
+
const headerType = options.headerType || 'Bearer ';
|
|
19
|
+
const contextFieldName = options.extendContextField || '_jwt';
|
|
20
|
+
const payloadByRequest = new WeakMap();
|
|
21
|
+
return {
|
|
22
|
+
async onRequest({ request }) {
|
|
23
|
+
const header = request.headers.get(headerName);
|
|
24
|
+
if (header != null) {
|
|
25
|
+
const [type, token] = header.split(' ');
|
|
26
|
+
if (type !== headerType) {
|
|
27
|
+
throw new graphql_1.GraphQLError(`Unsupported token type provided: "${type}"!`, {
|
|
28
|
+
extensions: {
|
|
29
|
+
http: {
|
|
30
|
+
status: 401,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
const decodedToken = (0, jsonwebtoken_1.decode)(token, { complete: true });
|
|
36
|
+
if (decodedToken?.header?.kid == null) {
|
|
37
|
+
throw new graphql_1.GraphQLError(`Failed to decode authentication token!`, {
|
|
38
|
+
extensions: {
|
|
39
|
+
http: {
|
|
40
|
+
status: 401,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
let signingKey;
|
|
46
|
+
if (options.signingKey) {
|
|
47
|
+
signingKey = options.signingKey;
|
|
48
|
+
}
|
|
49
|
+
else if (options.jwksUri) {
|
|
50
|
+
const secret = await jwksClient.getSigningKey(decodedToken.header.kid);
|
|
51
|
+
signingKey = secret.getPublicKey();
|
|
52
|
+
}
|
|
53
|
+
if (signingKey) {
|
|
54
|
+
const verified = await new Promise((resolve, reject) => {
|
|
55
|
+
(0, jsonwebtoken_1.verify)(token, signingKey, {
|
|
56
|
+
algorithms: options.algorithms || ['RS256'],
|
|
57
|
+
audience: options.audience,
|
|
58
|
+
issuer: options.issuer,
|
|
59
|
+
}, (err, result) => {
|
|
60
|
+
if (err) {
|
|
61
|
+
reject(err);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
resolve(result);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
if (!verified) {
|
|
69
|
+
throw new graphql_1.GraphQLError(`Unauthenticated!`, {
|
|
70
|
+
extensions: {
|
|
71
|
+
http: {
|
|
72
|
+
status: 401,
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
payloadByRequest.set(request, verified);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
payloadByRequest.set(request, decodedToken.payload);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
onContextBuilding({ context, extendContext }) {
|
|
85
|
+
if (context.request == null) {
|
|
86
|
+
throw new Error('Request is not available on context! Make sure you use this plugin with GraphQL Yoga.');
|
|
87
|
+
}
|
|
88
|
+
const payload = payloadByRequest.get(context.request);
|
|
89
|
+
if (payload != null) {
|
|
90
|
+
extendContext({
|
|
91
|
+
[contextFieldName]: payload,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
exports.useJwt = useJwt;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { JwksClient } from 'jwks-rsa';
|
|
2
|
+
import { decode, verify } from 'jsonwebtoken';
|
|
3
|
+
import { GraphQLError } from 'graphql';
|
|
4
|
+
export function useJwt(options) {
|
|
5
|
+
let jwksClient;
|
|
6
|
+
if (options.jwksUri) {
|
|
7
|
+
jwksClient = new JwksClient({
|
|
8
|
+
cache: true,
|
|
9
|
+
rateLimit: true,
|
|
10
|
+
jwksRequestsPerMinute: 5,
|
|
11
|
+
jwksUri: options.jwksUri,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
const headerName = options.headerName || 'authorization';
|
|
15
|
+
const headerType = options.headerType || 'Bearer ';
|
|
16
|
+
const contextFieldName = options.extendContextField || '_jwt';
|
|
17
|
+
const payloadByRequest = new WeakMap();
|
|
18
|
+
return {
|
|
19
|
+
async onRequest({ request }) {
|
|
20
|
+
const header = request.headers.get(headerName);
|
|
21
|
+
if (header != null) {
|
|
22
|
+
const [type, token] = header.split(' ');
|
|
23
|
+
if (type !== headerType) {
|
|
24
|
+
throw new GraphQLError(`Unsupported token type provided: "${type}"!`, {
|
|
25
|
+
extensions: {
|
|
26
|
+
http: {
|
|
27
|
+
status: 401,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const decodedToken = decode(token, { complete: true });
|
|
33
|
+
if (decodedToken?.header?.kid == null) {
|
|
34
|
+
throw new GraphQLError(`Failed to decode authentication token!`, {
|
|
35
|
+
extensions: {
|
|
36
|
+
http: {
|
|
37
|
+
status: 401,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
let signingKey;
|
|
43
|
+
if (options.signingKey) {
|
|
44
|
+
signingKey = options.signingKey;
|
|
45
|
+
}
|
|
46
|
+
else if (options.jwksUri) {
|
|
47
|
+
const secret = await jwksClient.getSigningKey(decodedToken.header.kid);
|
|
48
|
+
signingKey = secret.getPublicKey();
|
|
49
|
+
}
|
|
50
|
+
if (signingKey) {
|
|
51
|
+
const verified = await new Promise((resolve, reject) => {
|
|
52
|
+
verify(token, signingKey, {
|
|
53
|
+
algorithms: options.algorithms || ['RS256'],
|
|
54
|
+
audience: options.audience,
|
|
55
|
+
issuer: options.issuer,
|
|
56
|
+
}, (err, result) => {
|
|
57
|
+
if (err) {
|
|
58
|
+
reject(err);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
resolve(result);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
if (!verified) {
|
|
66
|
+
throw new GraphQLError(`Unauthenticated!`, {
|
|
67
|
+
extensions: {
|
|
68
|
+
http: {
|
|
69
|
+
status: 401,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
payloadByRequest.set(request, verified);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
payloadByRequest.set(request, decodedToken.payload);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
onContextBuilding({ context, extendContext }) {
|
|
82
|
+
if (context.request == null) {
|
|
83
|
+
throw new Error('Request is not available on context! Make sure you use this plugin with GraphQL Yoga.');
|
|
84
|
+
}
|
|
85
|
+
const payload = payloadByRequest.get(context.request);
|
|
86
|
+
if (payload != null) {
|
|
87
|
+
extendContext({
|
|
88
|
+
[contextFieldName]: payload,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphql-yoga/plugin-jwt",
|
|
3
|
+
"version": "1.0.0-alpha-20221021134149-437795dc",
|
|
4
|
+
"description": "jwt plugin for GraphQL Yoga.",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@graphql-tools/utils": "8.12.0",
|
|
7
|
+
"jsonwebtoken": "8.5.1",
|
|
8
|
+
"jwks-rsa": "2.1.5",
|
|
9
|
+
"tslib": "^2.4.0"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/dotansimha/graphql-yoga.git",
|
|
14
|
+
"directory": "packages/plugins/jwt"
|
|
15
|
+
},
|
|
16
|
+
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"main": "cjs/index.js",
|
|
19
|
+
"module": "esm/index.js",
|
|
20
|
+
"typings": "typings/index.d.ts",
|
|
21
|
+
"typescript": {
|
|
22
|
+
"definition": "typings/index.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./typings/index.d.cts",
|
|
29
|
+
"default": "./cjs/index.js"
|
|
30
|
+
},
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./typings/index.d.ts",
|
|
33
|
+
"default": "./esm/index.js"
|
|
34
|
+
},
|
|
35
|
+
"default": {
|
|
36
|
+
"types": "./typings/index.d.ts",
|
|
37
|
+
"default": "./esm/index.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Algorithm } from 'jsonwebtoken';
|
|
2
|
+
import { Plugin } from 'graphql-yoga';
|
|
3
|
+
export interface JwtPluginOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The endpoint to fetch keys from.
|
|
6
|
+
*
|
|
7
|
+
* For example: https://example.com/.well-known/jwks.json
|
|
8
|
+
*/
|
|
9
|
+
jwksUri?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Signing key to be used to verify the token
|
|
12
|
+
* You can also use the jwks option to fetch the key from a JWKS endpoint
|
|
13
|
+
*/
|
|
14
|
+
signingKey?: string;
|
|
15
|
+
/**
|
|
16
|
+
* List of the algorithms used to verify the token
|
|
17
|
+
*
|
|
18
|
+
* Default: ["RS256"] (Allowed values: HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512, PS256, PS384, PS512, none)
|
|
19
|
+
*/
|
|
20
|
+
algorithms?: Algorithm[];
|
|
21
|
+
/**
|
|
22
|
+
* The audience is the identifier of the API and is forwarded to your Auth API in order to specify for which API we are trying to authenticate our user for. E.g. if our API is hosted on `http://localhost:3000/graphql`, we would pass that value.
|
|
23
|
+
*/
|
|
24
|
+
audience?: string;
|
|
25
|
+
/**
|
|
26
|
+
* For example: `https://myapp.auth0.com/`
|
|
27
|
+
*/
|
|
28
|
+
issuer: string;
|
|
29
|
+
/**
|
|
30
|
+
* Once a user got successfully authenticated the authentication information is added on the context object under this field. In our resolvers we can then access the authentication information via `context._jwt.sub`.
|
|
31
|
+
*/
|
|
32
|
+
extendContextField?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The name of the header to be used to extract the token from.
|
|
35
|
+
*
|
|
36
|
+
* Default: "Authorization"
|
|
37
|
+
*/
|
|
38
|
+
headerName?: string;
|
|
39
|
+
/**
|
|
40
|
+
* The type of the header
|
|
41
|
+
*
|
|
42
|
+
* Default: Bearer
|
|
43
|
+
*/
|
|
44
|
+
headerType?: string;
|
|
45
|
+
}
|
|
46
|
+
export declare function useJwt(options: JwtPluginOptions): Plugin;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Algorithm } from 'jsonwebtoken';
|
|
2
|
+
import { Plugin } from 'graphql-yoga';
|
|
3
|
+
export interface JwtPluginOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The endpoint to fetch keys from.
|
|
6
|
+
*
|
|
7
|
+
* For example: https://example.com/.well-known/jwks.json
|
|
8
|
+
*/
|
|
9
|
+
jwksUri?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Signing key to be used to verify the token
|
|
12
|
+
* You can also use the jwks option to fetch the key from a JWKS endpoint
|
|
13
|
+
*/
|
|
14
|
+
signingKey?: string;
|
|
15
|
+
/**
|
|
16
|
+
* List of the algorithms used to verify the token
|
|
17
|
+
*
|
|
18
|
+
* Default: ["RS256"] (Allowed values: HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512, PS256, PS384, PS512, none)
|
|
19
|
+
*/
|
|
20
|
+
algorithms?: Algorithm[];
|
|
21
|
+
/**
|
|
22
|
+
* The audience is the identifier of the API and is forwarded to your Auth API in order to specify for which API we are trying to authenticate our user for. E.g. if our API is hosted on `http://localhost:3000/graphql`, we would pass that value.
|
|
23
|
+
*/
|
|
24
|
+
audience?: string;
|
|
25
|
+
/**
|
|
26
|
+
* For example: `https://myapp.auth0.com/`
|
|
27
|
+
*/
|
|
28
|
+
issuer: string;
|
|
29
|
+
/**
|
|
30
|
+
* Once a user got successfully authenticated the authentication information is added on the context object under this field. In our resolvers we can then access the authentication information via `context._jwt.sub`.
|
|
31
|
+
*/
|
|
32
|
+
extendContextField?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The name of the header to be used to extract the token from.
|
|
35
|
+
*
|
|
36
|
+
* Default: "Authorization"
|
|
37
|
+
*/
|
|
38
|
+
headerName?: string;
|
|
39
|
+
/**
|
|
40
|
+
* The type of the header
|
|
41
|
+
*
|
|
42
|
+
* Default: Bearer
|
|
43
|
+
*/
|
|
44
|
+
headerType?: string;
|
|
45
|
+
}
|
|
46
|
+
export declare function useJwt(options: JwtPluginOptions): Plugin;
|