From d91e0b7b6e6708c4f6d422d9e7ef59e4d648303a Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Tue, 22 Aug 2023 09:43:02 +0200 Subject: [PATCH] added basic token auth draft for any graphql query --- .meshrc.yml | 1 + package.json | 1 + pnpm-lock.yaml | 16 ++++++++++++++++ src/envelopePlugin.ts | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 src/envelopePlugin.ts diff --git a/.meshrc.yml b/.meshrc.yml index 4008832..3961003 100644 --- a/.meshrc.yml +++ b/.meshrc.yml @@ -1,5 +1,6 @@ serve: endpoint: /api/graphql +additionalEnvelopPlugins: "./src/envelopePlugin.ts" sources: - name: Gitea handler: diff --git a/package.json b/package.json index 5f09ebd..7b8eb67 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "@directus/sdk": "^11.0.1", + "@envelop/generic-auth": "^6.0.0", "@graphql-mesh/cli": "^0.85.1", "@graphql-mesh/graphql": "^0.95.1", "@graphql-mesh/openapi": "^0.94.7", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e0cca36..3b65737 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,9 @@ dependencies: '@directus/sdk': specifier: ^11.0.1 version: 11.0.1 + '@envelop/generic-auth': + specifier: ^6.0.0 + version: 6.0.0(@envelop/core@4.0.0)(graphql@16.7.1) '@graphql-mesh/cli': specifier: ^0.85.1 version: 0.85.1(@babel/core@7.22.8)(@types/node@20.4.1)(graphql-tag@2.12.6)(graphql-yoga@4.0.3)(graphql@16.7.1)(react-native@0.72.3) @@ -1681,6 +1684,19 @@ packages: tslib: 2.6.0 dev: false + /@envelop/generic-auth@6.0.0(@envelop/core@4.0.0)(graphql@16.7.1): + resolution: {integrity: sha512-retM3YQPuUyLzhFR7x1E3lzGW8uh/Rp4m9DmCfdP35mHKL8jijJP8KbvLZb5WmSkVemt2L9ilP5m3opOy/2BiQ==} + engines: {node: '>=16.0.0'} + peerDependencies: + '@envelop/core': ^4.0.0 + graphql: ^14.0.0 || ^15.0.0 || ^16.0.0 + dependencies: + '@envelop/core': 4.0.0 + '@envelop/extended-validation': 3.0.0(@envelop/core@4.0.0)(graphql@16.7.1) + graphql: 16.7.1 + tslib: 2.6.0 + dev: false + /@envelop/types@4.0.0: resolution: {integrity: sha512-dmBK16VVfKCkqYYemvE+gt1cPBP0d9CbYO4yjNhSSYy9K+w6+Lw48wOLK238mSR339PNAvwj/JW/qzNy2llggA==} engines: {node: '>=16.0.0'} diff --git a/src/envelopePlugin.ts b/src/envelopePlugin.ts new file mode 100644 index 0000000..4580159 --- /dev/null +++ b/src/envelopePlugin.ts @@ -0,0 +1,37 @@ +import { useGenericAuth } from '@envelop/generic-auth' +import { MeshPlugin } from '@graphql-mesh/types' + +const path = require('path'); +require('dotenv').config({ path: path.resolve(__dirname, '../.env.local') }); + + +type UserType = { + id: string +} + +const resolveUserFn = async context => { + const authHeader = context.req.headers.authorization; + if (authHeader !== "token123") { + console.error('Failed to validate token') + return null + } + // If the token is valid, return the user + // Replace this with your actual user fetching logic + return { id: 'user1' }; +} + +const validateUser = params => { + if (!params.user) { + return new Error(`Unauthenticated!`) + } +} + +const plugins: MeshPlugin = [ + useGenericAuth({ + resolveUserFn, + validateUser, + mode: 'protect-all' + }) +] + +export default plugins \ No newline at end of file