|
| 1 | +import { createSchema, createYoga, type Plugin } from 'graphql-yoga'; |
| 2 | +import jwt from 'jsonwebtoken'; |
| 3 | +import { createServeRuntime } from '@graphql-mesh/serve-runtime'; |
| 4 | +import { |
| 5 | + createInlineSigningKeyProvider, |
| 6 | + type JWTExtendContextFields, |
| 7 | +} from '@graphql-yoga/plugin-jwt'; |
| 8 | +import useJWTAuth, { useForwardedJWT } from './index'; |
| 9 | + |
| 10 | +describe('useExtractedJWT', () => { |
| 11 | + it('full flow with extraction on Yoga subgraph', async () => { |
| 12 | + const upstream = createYoga<{ jwt?: JWTExtendContextFields }>({ |
| 13 | + schema: createSchema({ |
| 14 | + typeDefs: /* GraphQL */ ` |
| 15 | + type Query { |
| 16 | + hello: String |
| 17 | + } |
| 18 | + `, |
| 19 | + resolvers: { |
| 20 | + Query: { |
| 21 | + hello: (_, args, context) => `Hi, user ${context.jwt?.payload.sub}`, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }), |
| 25 | + plugins: [useForwardedJWT({})], |
| 26 | + logging: false, |
| 27 | + }); |
| 28 | + |
| 29 | + const secret = 'topsecret'; |
| 30 | + const serveRuntime = createServeRuntime({ |
| 31 | + proxy: { |
| 32 | + endpoint: 'https://example.com/graphql', |
| 33 | + }, |
| 34 | + fetchAPI: { |
| 35 | + fetch: upstream.fetch as any, |
| 36 | + }, |
| 37 | + plugins: () => [ |
| 38 | + useJWTAuth({ |
| 39 | + forward: { |
| 40 | + payload: true, |
| 41 | + token: true, |
| 42 | + }, |
| 43 | + singingKeyProviders: [createInlineSigningKeyProvider(secret)], |
| 44 | + }), |
| 45 | + ], |
| 46 | + logging: false, |
| 47 | + }); |
| 48 | + |
| 49 | + const token = jwt.sign({ sub: '123' }, secret, {}); |
| 50 | + |
| 51 | + const response = await serveRuntime.fetch('http://localhost:4000/graphql', { |
| 52 | + method: 'POST', |
| 53 | + headers: { |
| 54 | + authorization: `Bearer ${token}`, |
| 55 | + 'content-type': 'application/json', |
| 56 | + }, |
| 57 | + body: JSON.stringify({ |
| 58 | + query: /* GraphQL */ ` |
| 59 | + query { |
| 60 | + hello |
| 61 | + } |
| 62 | + `, |
| 63 | + }), |
| 64 | + }); |
| 65 | + |
| 66 | + expect(response.status).toBe(200); |
| 67 | + const body = await response.json<any>(); |
| 68 | + expect(body.data?.hello).toBe('Hi, user 123'); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('useJWTAuth', () => { |
| 73 | + describe('forwarding of token and payload to the upstream, using extensions', () => { |
| 74 | + const requestTrackerPlugin = { |
| 75 | + onParams: jest.fn((() => {}) as Plugin['onParams']), |
| 76 | + }; |
| 77 | + const upstream = createYoga({ |
| 78 | + schema: createSchema({ |
| 79 | + typeDefs: /* GraphQL */ ` |
| 80 | + type Query { |
| 81 | + hello: String |
| 82 | + } |
| 83 | + `, |
| 84 | + resolvers: { |
| 85 | + Query: { |
| 86 | + hello: () => 'world', |
| 87 | + }, |
| 88 | + }, |
| 89 | + }), |
| 90 | + plugins: [requestTrackerPlugin], |
| 91 | + logging: false, |
| 92 | + }); |
| 93 | + beforeEach(() => { |
| 94 | + requestTrackerPlugin.onParams.mockClear(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should passthrough jwt token, jwt payload, and signature to the upstream api calls', async () => { |
| 98 | + const secret = 'topsecret'; |
| 99 | + const serveRuntime = createServeRuntime({ |
| 100 | + proxy: { |
| 101 | + endpoint: 'https://example.com/graphql', |
| 102 | + }, |
| 103 | + fetchAPI: { |
| 104 | + fetch: upstream.fetch as any, |
| 105 | + }, |
| 106 | + plugins: () => [ |
| 107 | + useJWTAuth({ |
| 108 | + forward: { |
| 109 | + payload: true, |
| 110 | + token: true, |
| 111 | + }, |
| 112 | + singingKeyProviders: [createInlineSigningKeyProvider(secret)], |
| 113 | + }), |
| 114 | + ], |
| 115 | + logging: false, |
| 116 | + }); |
| 117 | + |
| 118 | + const token = jwt.sign({ sub: '123' }, secret, {}); |
| 119 | + |
| 120 | + const response = await serveRuntime.fetch('http://localhost:4000/graphql', { |
| 121 | + method: 'POST', |
| 122 | + headers: { |
| 123 | + authorization: `Bearer ${token}`, |
| 124 | + 'content-type': 'application/json', |
| 125 | + }, |
| 126 | + body: JSON.stringify({ |
| 127 | + query: /* GraphQL */ ` |
| 128 | + query { |
| 129 | + hello |
| 130 | + } |
| 131 | + `, |
| 132 | + }), |
| 133 | + }); |
| 134 | + |
| 135 | + expect(response.status).toBe(200); |
| 136 | + expect(requestTrackerPlugin.onParams).toHaveBeenCalledTimes(2); |
| 137 | + const extensions = requestTrackerPlugin.onParams.mock.calls[1][0].params.extensions; |
| 138 | + expect(extensions.jwt.token.value).toBe(token); |
| 139 | + expect(extensions.jwt.token.prefix).toBe('Bearer'); |
| 140 | + expect(extensions.jwt.payload).toMatchObject({ |
| 141 | + sub: '123', |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments