added basic token auth draft for any graphql query

This commit is contained in:
Samuel Andert 2023-08-22 09:43:02 +02:00
parent 0a4a793d39
commit d91e0b7b6e
4 changed files with 55 additions and 0 deletions

View File

@ -1,5 +1,6 @@
serve:
endpoint: /api/graphql
additionalEnvelopPlugins: "./src/envelopePlugin.ts"
sources:
- name: Gitea
handler:

View File

@ -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",

16
pnpm-lock.yaml generated
View File

@ -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'}

37
src/envelopePlugin.ts Normal file
View File

@ -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