-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nestjs-oidc): session setting & redirect statusCode chanage
* add connect-redis to store session * set express-session in dependencies and use memorystore as store * use 302 as redirect statusCode
- Loading branch information
hubert
committed
Nov 6, 2024
1 parent
6f2fdca
commit d23e5c8
Showing
12 changed files
with
173 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "nestjs-oidc", | ||
"description": "OpenID-Connect client module for NestJS.", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"author": "Hubert<[email protected]>", | ||
"main": "lib/index.js", | ||
"module": "esm/index.js", | ||
|
@@ -39,7 +39,7 @@ | |
], | ||
"scripts": { | ||
"serve": "run -T concurrently --raw \"tsc --project tsconfig.json -watch\"", | ||
"build": "run -T rimraf dist && yarn build:version && yarn build:cjs && yarn build:esm", | ||
"build": "run -T rimraf -rf lib esm dist && yarn build:version && yarn build:cjs && yarn build:esm", | ||
"build:version": "node -p \"'export const version = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts", | ||
"build:cjs": "run -T tsc --project tsconfig.build.json", | ||
"build:esm": "run -T tsc --project tsconfig.build.json --module es2015 --outDir esm", | ||
|
@@ -52,8 +52,10 @@ | |
"dependencies": { | ||
"@nestjs/passport": "^10.0.0", | ||
"cookie": "^0.5.0", | ||
"express-session": "^1.17.3", | ||
"flatted": "^3.2.6", | ||
"jose": "^4.15.9", | ||
"memorystore": "^1.6.7", | ||
"openid-client": "^5.6.5", | ||
"passport": "^0.6.0", | ||
"querystring": "^0.2.1", | ||
|
@@ -67,14 +69,14 @@ | |
"@types/passport": "^1.0.9", | ||
"@types/uuid": "^9.0.7", | ||
"connect-mongo": "^5.1.0", | ||
"express-session": "^1.17.3" | ||
"connect-redis": "^7.1.1" | ||
}, | ||
"peerDependencies": { | ||
"@nestjs/common": "^10.0.0", | ||
"@nestjs/core": "^10.0.0", | ||
"@nestjs/graphql": "^11.0.0", | ||
"connect-mongo": "4.6.0", | ||
"express-session": "^1.17.3", | ||
"connect-redis": "^7.1.1", | ||
"graphql": "^16.0.0", | ||
"graphql-parse-resolve-info": "^4.12.0" | ||
}, | ||
|
@@ -85,7 +87,7 @@ | |
"connect-mongo": { | ||
"optional": true | ||
}, | ||
"express-session": { | ||
"connect-redis": { | ||
"optional": true | ||
}, | ||
"graphql": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,29 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { SessionOptions } from 'express-session'; | ||
import { sessionInMemory } from './session/session-in-memory'; | ||
import { sessionInMongo } from './session/session-in-mongo'; | ||
import { sessionInRedis } from './session/session-in-redis'; | ||
|
||
export const setupSession = (app: INestApplication, name: string) => { | ||
return sessionInMemory(app, name); | ||
}; | ||
/** | ||
* setup session | ||
*/ | ||
export function setupSession( | ||
app: INestApplication, | ||
type: 'memory' | 'mongo' | 'redis' = 'memory', | ||
options?: Partial<SessionOptions> & { | ||
[key: string]: any; | ||
}, | ||
) { | ||
switch (type) { | ||
case 'memory': | ||
return sessionInMemory(app, options); | ||
case 'mongo': | ||
if (!options?.connectMongoOptions) throw new Error('connectMongoOptions is required for session type mongo'); | ||
return sessionInMongo(app, options as any); | ||
case 'redis': | ||
if (!options?.connectRedisOptions) throw new Error('connectRedisOptions is required for session type redis'); | ||
return sessionInRedis(app, options as any); | ||
default: | ||
throw new Error(`session type ${type} is not supported`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import session, { SessionOptions } from 'express-session'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { ChannelType } from '../interfaces/index'; | ||
|
||
// https://github.com/expressjs/session/issues/725#issuecomment-605922223 | ||
Object.defineProperty(session.Cookie.prototype, 'sameSite', { | ||
// sameSite cannot be set to `None` if cookie is not marked secure | ||
get() { | ||
return this._sameSite === 'none' && !this.secure ? 'lax' : this._sameSite; | ||
}, | ||
set(value) { | ||
this._sameSite = value; | ||
}, | ||
}); | ||
|
||
export const defaultOptions = function (): SessionOptions { | ||
return { | ||
secret: process.env.SESSION_SECRET || uuid(), // to sign session id | ||
resave: false, // will default to false in near future: https://github.com/expressjs/session#resave | ||
saveUninitialized: false, // will default to false in near future: https://github.com/expressjs/session#saveuninitialized | ||
rolling: true, // keep session alive | ||
proxy: true, // trust first proxy | ||
cookie: { | ||
maxAge: 60 * 60 * 1000, // session expires in 1hr, refreshed by `rolling: true` option. | ||
httpOnly: true, // so that cookie can't be accessed via client-side script | ||
secure: 'auto', // set to true if your communication is over HTTPS | ||
sameSite: 'none', // set to 'none' if your communication is over HTTPS | ||
}, | ||
}; | ||
}; | ||
|
||
declare module 'express-session' { | ||
interface SessionData { | ||
tenantId?: string; | ||
channelType?: ChannelType; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './session-in-memory'; | ||
export * from './session-mongo'; | ||
export * from './session-in-memory'; | ||
export * from './session-in-mongo'; | ||
export * from './session-in-redis'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import session, { SessionOptions } from 'express-session'; | ||
import createMemoryStore from 'memorystore'; | ||
import passport from 'passport'; | ||
import { createExpressSession } from './express-session'; | ||
import { defaultOptions } from './base-session'; | ||
|
||
const MomeryStore = createMemoryStore(session); | ||
|
||
/** | ||
* setup session with in-memory store | ||
*/ | ||
export function sessionInMemory( | ||
app: INestApplication, | ||
name: string, | ||
options?: { | ||
sessionStrategy?: (options: { name: string; [key: string]: any }) => any; | ||
// rest of sessionStrategy options | ||
[key: string]: any; | ||
options?: Partial<SessionOptions> & { | ||
memoryOptions?: ConstructorParameters<ReturnType<typeof createMemoryStore>>[0]; | ||
}, | ||
) { | ||
const { sessionStrategy, ...rest } = options ?? {}; | ||
app.use((sessionStrategy ?? createExpressSession)({ ...rest, name })); | ||
const { memoryOptions, ...rest } = options ?? {}; | ||
app.use( | ||
session({ | ||
...defaultOptions(), | ||
...rest, | ||
store: new MomeryStore({ | ||
checkPeriod: 86400000, // prune expired entries every 24h | ||
...memoryOptions, | ||
}), | ||
}), | ||
); | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
} |
20 changes: 10 additions & 10 deletions
20
.../nestjs-oidc/src/session/session-mongo.ts → ...stjs-oidc/src/session/session-in-mongo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { loadPackage } from '@nestjs/common/utils/load-package.util'; | ||
import session, { SessionOptions } from 'express-session'; | ||
import passport from 'passport'; | ||
import ConnectRedis from 'connect-redis'; | ||
import { defaultOptions } from './base-session'; | ||
|
||
/** | ||
* setup session with redis store | ||
*/ | ||
export const sessionInRedis = ( | ||
app: INestApplication, | ||
options: Partial<SessionOptions> & { | ||
connectRedisOptions: ConstructorParameters<typeof ConnectRedis>[0]; | ||
}, | ||
) => { | ||
const RedisStore = loadPackage('connect-redis', 'SessionModule', () => require('connect-redis')) | ||
.default as typeof ConnectRedis; | ||
|
||
const { connectRedisOptions, ...rest } = options; | ||
app.use( | ||
session({ | ||
...defaultOptions(), | ||
...rest, | ||
store: new RedisStore(connectRedisOptions), | ||
}), | ||
); | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export const version = "0.0.8"; | ||
export const version = "0.1.0"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters