@graphql-mesh/plugin-jwt-auth 0.0.2-alpha-20240717092838-06347d9cc3b6c01e65cedffa4f27922bda71cf0f
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/cjs/index.js +65 -0
- package/cjs/package.json +1 -0
- package/esm/index.js +60 -0
- package/package.json +47 -0
- package/typings/index.d.cts +13 -0
- package/typings/index.d.ts +13 -0
package/cjs/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useForwardedJWTClaims = void 0;
|
|
4
|
+
exports.default = useJWTAuth;
|
|
5
|
+
const plugin_jwt_1 = require("@graphql-yoga/plugin-jwt");
|
|
6
|
+
function useJWTAuth(options) {
|
|
7
|
+
const shouldPassthroughIndicators = new WeakSet();
|
|
8
|
+
return {
|
|
9
|
+
onPluginInit({ addPlugin }) {
|
|
10
|
+
// TODO: fix useYogaJWT typings to inherit the context
|
|
11
|
+
addPlugin((0, plugin_jwt_1.useJWT)(options));
|
|
12
|
+
},
|
|
13
|
+
// When a subgraph is about to be executed, we check if the request has a JWT token
|
|
14
|
+
// that needs to be passed. At the moment, only subgraphs will have the option to forward tokens/claims.
|
|
15
|
+
onSubgraphExecute({ executionRequest, setExecutionRequest }) {
|
|
16
|
+
if (executionRequest.context.jwt) {
|
|
17
|
+
console.log('got jwt', executionRequest.context.jwt);
|
|
18
|
+
console.log('current extensions', executionRequest.extensions);
|
|
19
|
+
setExecutionRequest({
|
|
20
|
+
...executionRequest,
|
|
21
|
+
extensions: {
|
|
22
|
+
...executionRequest.extensions,
|
|
23
|
+
jwt: executionRequest.context.jwt,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
shouldPassthroughIndicators.add(executionRequest.context.request);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
// When an upstream HTTP call is made, we check if the incoming request has a JWT token from previous stages,
|
|
30
|
+
// and if the concerete upstream HTTP request should have the token passed.
|
|
31
|
+
onFetch({ context, setOptions, options: fetchOptions }) {
|
|
32
|
+
const shouldForward = options.forward.claims || options.forward.token;
|
|
33
|
+
if (shouldForward && context.jwt && shouldPassthroughIndicators.has(context.request)) {
|
|
34
|
+
const headers = {};
|
|
35
|
+
if (options.forward.token && context.jwt.token) {
|
|
36
|
+
const headerName = typeof options.forward.token === 'string' ? options.forward.token : 'Authorization';
|
|
37
|
+
const headerValue = [context.jwt.token.prefix, context.jwt.token.value]
|
|
38
|
+
.filter(r => !!r)
|
|
39
|
+
.join(' ');
|
|
40
|
+
headers[headerName] = headerValue;
|
|
41
|
+
}
|
|
42
|
+
if (options.forward.claims && context.jwt.payload) {
|
|
43
|
+
const headerName = typeof options.forward.claims === 'string'
|
|
44
|
+
? options.forward.claims
|
|
45
|
+
: 'X-JWT-Auth-Claims';
|
|
46
|
+
const headerValue = JSON.stringify(context.jwt.payload);
|
|
47
|
+
headers[headerName] = headerValue;
|
|
48
|
+
}
|
|
49
|
+
if (Object.keys(headers).length > 0) {
|
|
50
|
+
setOptions({
|
|
51
|
+
...fetchOptions,
|
|
52
|
+
headers: {
|
|
53
|
+
...fetchOptions.headers,
|
|
54
|
+
...headers,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const useForwardedJWTClaims = () => {
|
|
63
|
+
return {};
|
|
64
|
+
};
|
|
65
|
+
exports.useForwardedJWTClaims = useForwardedJWTClaims;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useJWT as useYogaJWT, } from '@graphql-yoga/plugin-jwt';
|
|
2
|
+
export default function useJWTAuth(options) {
|
|
3
|
+
const shouldPassthroughIndicators = new WeakSet();
|
|
4
|
+
return {
|
|
5
|
+
onPluginInit({ addPlugin }) {
|
|
6
|
+
// TODO: fix useYogaJWT typings to inherit the context
|
|
7
|
+
addPlugin(useYogaJWT(options));
|
|
8
|
+
},
|
|
9
|
+
// When a subgraph is about to be executed, we check if the request has a JWT token
|
|
10
|
+
// that needs to be passed. At the moment, only subgraphs will have the option to forward tokens/claims.
|
|
11
|
+
onSubgraphExecute({ executionRequest, setExecutionRequest }) {
|
|
12
|
+
if (executionRequest.context.jwt) {
|
|
13
|
+
console.log('got jwt', executionRequest.context.jwt);
|
|
14
|
+
console.log('current extensions', executionRequest.extensions);
|
|
15
|
+
setExecutionRequest({
|
|
16
|
+
...executionRequest,
|
|
17
|
+
extensions: {
|
|
18
|
+
...executionRequest.extensions,
|
|
19
|
+
jwt: executionRequest.context.jwt,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
shouldPassthroughIndicators.add(executionRequest.context.request);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
// When an upstream HTTP call is made, we check if the incoming request has a JWT token from previous stages,
|
|
26
|
+
// and if the concerete upstream HTTP request should have the token passed.
|
|
27
|
+
onFetch({ context, setOptions, options: fetchOptions }) {
|
|
28
|
+
const shouldForward = options.forward.claims || options.forward.token;
|
|
29
|
+
if (shouldForward && context.jwt && shouldPassthroughIndicators.has(context.request)) {
|
|
30
|
+
const headers = {};
|
|
31
|
+
if (options.forward.token && context.jwt.token) {
|
|
32
|
+
const headerName = typeof options.forward.token === 'string' ? options.forward.token : 'Authorization';
|
|
33
|
+
const headerValue = [context.jwt.token.prefix, context.jwt.token.value]
|
|
34
|
+
.filter(r => !!r)
|
|
35
|
+
.join(' ');
|
|
36
|
+
headers[headerName] = headerValue;
|
|
37
|
+
}
|
|
38
|
+
if (options.forward.claims && context.jwt.payload) {
|
|
39
|
+
const headerName = typeof options.forward.claims === 'string'
|
|
40
|
+
? options.forward.claims
|
|
41
|
+
: 'X-JWT-Auth-Claims';
|
|
42
|
+
const headerValue = JSON.stringify(context.jwt.payload);
|
|
43
|
+
headers[headerName] = headerValue;
|
|
44
|
+
}
|
|
45
|
+
if (Object.keys(headers).length > 0) {
|
|
46
|
+
setOptions({
|
|
47
|
+
...fetchOptions,
|
|
48
|
+
headers: {
|
|
49
|
+
...fetchOptions.headers,
|
|
50
|
+
...headers,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export const useForwardedJWTClaims = () => {
|
|
59
|
+
return {};
|
|
60
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphql-mesh/plugin-jwt-auth",
|
|
3
|
+
"version": "0.0.2-alpha-20240717092838-06347d9cc3b6c01e65cedffa4f27922bda71cf0f",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"peerDependencies": {
|
|
6
|
+
"@graphql-mesh/types": "^0.99.0",
|
|
7
|
+
"@graphql-mesh/utils": "^0.99.0",
|
|
8
|
+
"graphql": "*",
|
|
9
|
+
"tslib": "^2.4.0"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@graphql-yoga/plugin-jwt": "3.0.0-alpha-20240716130747-2f341065"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "ardatan/graphql-mesh",
|
|
17
|
+
"directory": "packages/plugins/jwt-auth"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=16.0.0"
|
|
22
|
+
},
|
|
23
|
+
"main": "cjs/index.js",
|
|
24
|
+
"module": "esm/index.js",
|
|
25
|
+
"typings": "typings/index.d.ts",
|
|
26
|
+
"typescript": {
|
|
27
|
+
"definition": "typings/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./typings/index.d.cts",
|
|
34
|
+
"default": "./cjs/index.js"
|
|
35
|
+
},
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./esm/index.js"
|
|
39
|
+
},
|
|
40
|
+
"default": {
|
|
41
|
+
"types": "./typings/index.d.ts",
|
|
42
|
+
"default": "./esm/index.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Plugin as YogaPlugin } from 'graphql-yoga';
|
|
2
|
+
import type { MeshServePlugin } from '@graphql-mesh/serve-runtime';
|
|
3
|
+
import { type JWTExtendContextFields, type JwtPluginOptions } from '@graphql-yoga/plugin-jwt';
|
|
4
|
+
export type JWTAuthPluginOptions = JwtPluginOptions & {
|
|
5
|
+
forward: {
|
|
6
|
+
claims: boolean | string;
|
|
7
|
+
token: boolean | string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export default function useJWTAuth(options: JWTAuthPluginOptions): MeshServePlugin<{
|
|
11
|
+
jwt?: JWTExtendContextFields;
|
|
12
|
+
}>;
|
|
13
|
+
export declare const useForwardedJWTClaims: () => YogaPlugin;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Plugin as YogaPlugin } from 'graphql-yoga';
|
|
2
|
+
import type { MeshServePlugin } from '@graphql-mesh/serve-runtime';
|
|
3
|
+
import { type JWTExtendContextFields, type JwtPluginOptions } from '@graphql-yoga/plugin-jwt';
|
|
4
|
+
export type JWTAuthPluginOptions = JwtPluginOptions & {
|
|
5
|
+
forward: {
|
|
6
|
+
claims: boolean | string;
|
|
7
|
+
token: boolean | string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export default function useJWTAuth(options: JWTAuthPluginOptions): MeshServePlugin<{
|
|
11
|
+
jwt?: JWTExtendContextFields;
|
|
12
|
+
}>;
|
|
13
|
+
export declare const useForwardedJWTClaims: () => YogaPlugin;
|