Skip to content

Commit

Permalink
Add support for OAuth 2.0 State
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Kropp committed Aug 1, 2024
1 parent 7f4f70c commit 46deb55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"clean": "rm -rf dist",
"build": "tsc",
"fix": "npx prettier src --write",
"prepare": "yarn fix && yarn clean && yarn build",
"prepare": "npm run fix && npm run clean && npm run build",
"test": "vitest"
},
"exports": {
Expand Down
33 changes: 20 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ function oAuthPluginServer(
const sub = options.subField?.name || 'sub'
const oAuthStrategyCount = (incoming.custom?.oAuthStrategyCount || 0) + 1
const strategyName = `oauth2-${oAuthStrategyCount}`
const sessionMiddleware = session(
options.sessionOptions ?? {
resave: false,
saveUninitialized: false,
secret:
process.env.PAYLOAD_SECRET ||
log('Missing process.env.PAYLOAD_SECRET') ||
'unsafe',
store: options.databaseUri
? MongoStore.create({ mongoUrl: options.databaseUri })
: undefined,
}
)

if (options.clientID) {
// Validate paths, they must be unique
Expand Down Expand Up @@ -252,6 +265,12 @@ function oAuthPluginServer(
},
},
endpoints: (incoming.endpoints || []).concat([
{
path: authorizePath,
method: 'get',
root: true,
handler: sessionMiddleware,
},
{
path: authorizePath,
method: 'get',
Expand All @@ -262,19 +281,7 @@ function oAuthPluginServer(
path: callbackPath,
method: 'get',
root: true,
handler: session(
options.sessionOptions ?? {
resave: false,
saveUninitialized: false,
secret:
process.env.PAYLOAD_SECRET ||
log('Missing process.env.PAYLOAD_SECRET') ||
'unsafe',
store: options.databaseUri
? MongoStore.create({ mongoUrl: options.databaseUri })
: undefined,
}
),
handler: sessionMiddleware,
},
{
path: callbackPath,
Expand Down

0 comments on commit 46deb55

Please sign in to comment.