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 291ba05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 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
39 changes: 23 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { str62 } from '@bothrs/util/random'
import MongoStore from 'connect-mongo'
import debug from 'debug'
import session from 'express-session'
import jwt from 'jsonwebtoken'
import passport from 'passport'
import OAuth2Strategy, { VerifyCallback } from 'passport-oauth2'
import debug from 'debug'
import payload from 'payload'
import { Config } from 'payload/config'
import { PaginatedDocs } from 'payload/dist/database/types'
import {
Field,
fieldAffectsData,
fieldHasSubFields,
} from 'payload/dist/fields/config/types'
import { PaginatedDocs } from 'payload/dist/database/types'
import getCookieExpiration from 'payload/dist/utilities/getCookieExpiration'
import { TextField } from 'payload/types'

import { createElement } from 'react'
import OAuthButton from './OAuthButton'
import type { oAuthPluginOptions } from './types'
import { createElement } from 'react'

export { OAuthButton, oAuthPluginOptions }

Expand Down 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 291ba05

Please sign in to comment.