Skip to content

Commit

Permalink
feat: circumvent permission checks when developing locally via serve-…
Browse files Browse the repository at this point in the history
…dev (credits go to bradleyDean)
  • Loading branch information
l4u532 committed Dec 19, 2023
1 parent cfada19 commit 2f3edee
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
],
"console": "integratedTerminal",
},
{
"type": "node",
"request": "launch",
"name": "Launch API Server (serve-dev)",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/src/main.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/build/**/*.js"],
"runtimeExecutable": "yarn",
"runtimeArgs": ["run", "serve-dev"],
"console": "integratedTerminal"
},
{
"name": "Debug Jest Tests",
"type": "node",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"build-release": "tsc -p tsconfig.release.json",
"clean": "tsc -b --clean && rm -rf build/*",
"serve": "yarn build && node --experimental-json-modules build/main.js",
"serve-dev": "echo \"🚨 LOCAL_DEV_BYPASS_AUTH enabled 🚨\" && LOCAL_DEV_BYPASS_AUTH=true yarn serve",
"refresh-db": "./refresh-db.sh",
"seed-usa": "yarn build && node build/db/import/usa/USADay0Seed.js",
"seed-db": "./seed-db.sh",
Expand Down
21 changes: 21 additions & 0 deletions src/auth/local-dev/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* This file is used when starting the server via `yarn serve-dev`
* It bypasses the authentication for local development and allows all queries and mutations.
*/
import muuid, { MUUID } from 'uuid-mongodb'
import { AuthUserType } from '../../types.js'
import { logger } from '../../logger.js'

export const localDevBypassAuthMiddleware = (() => {
const testUUID: MUUID = muuid.v4()

return async ({ req }): Promise<any> => {
const user: AuthUserType = {
roles: ['user_admin', 'org_admin', 'editor'],
uuid: testUUID,
isBuilder: false
}
logger.info(`The user.roles for this session is: ${user.roles.toString()}`)
return { user }
}
})()
19 changes: 19 additions & 0 deletions src/auth/local-dev/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This file is used when starting the server via `yarn serve-dev`
* It bypasses the authentication for local development and allows all queries and mutations.
*/
import { shield, allow } from 'graphql-shield'

const localDevBypassAuthPermissions = shield({
Query: {
'*': allow
},
Mutation: {
'*': allow
}
}, {
allowExternalErrors: true,
fallbackRule: allow
})

export default localDevBypassAuthPermissions
10 changes: 7 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import ChangeLogDataSource from './model/ChangeLogDataSource.js'
import MutableMediaDataSource from './model/MutableMediaDataSource.js'
import MutableClimbDataSource from './model/MutableClimbDataSource.js'
import TickDataSource from './model/TickDataSource.js'
import { createContext, permissions } from './auth/index.js'
import { createContext } from './auth/middleware.js'
import permissions from './auth/permissions.js'
import { localDevBypassAuthMiddleware } from './auth/local-dev/middleware.js'
import localDevBypassAuthPermissions from './auth/local-dev/permissions.js'
import XMediaDataSource from './model/XMediaDataSource.js'
import PostDataSource from './model/PostDataSource.js'
import MutableOrgDS from './model/MutableOrganizationDataSource.js'
Expand All @@ -19,7 +22,7 @@ import UserDataSource from './model/UserDataSource.js'
export async function createServer (): Promise<ApolloServer> {
const schema = applyMiddleware(
graphqlSchema,
permissions.generate(graphqlSchema)
(process.env.LOCAL_DEV_BYPASS_AUTH === 'true' ? localDevBypassAuthPermissions : permissions).generate(graphqlSchema)
)
const dataSources: () => DataSources<Context> = () => ({
climbs: MutableClimbDataSource.getInstance(),
Expand All @@ -36,10 +39,11 @@ export async function createServer (): Promise<ApolloServer> {
xmedia: new XMediaDataSource(mongoose.connection.db.collection('xmedia')),
post: new PostDataSource(mongoose.connection.db.collection('post'))
})

const server = new ApolloServer({
introspection: true,
schema,
context: createContext,
context: process.env.LOCAL_DEV_BYPASS_AUTH === 'true' ? localDevBypassAuthMiddleware : createContext,
dataSources,
cache: 'bounded'
})
Expand Down

0 comments on commit 2f3edee

Please sign in to comment.