Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement stale PR #286: Bradley/dev mode credentials #371

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 a mod of src/auth/middleware.ts and is used when starting the server via `yarn serve-dev`
* It bypasses the authentication for local development
*/
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 a mod of src/auth/permissions.ts and is used when starting the server via `yarn serve-dev`
* It bypasses the authorization 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
Loading