117 fix oauth (#127)

* change session cookie from 'strict' to 'lax' to fix "forbidden" issue
* add scope settings to Google

some additional improvements:
* update ESLint settings to allow unused parameters before used ones

some related discussions:
* https://github.com/auth0/passport-auth0/issues/70
*
https://stackoverflow.com/questions/59221899/add-samesite-attribute-to-passport-js-session-cookie
This commit is contained in:
Lin, Hung-Shin authored and GitHub committed 2024-04-05 22:24:16 +08:00
commit d4c9d4577c
8 files changed
+16 -9

No files matched your search

+1 -1
View File
@@ -51,7 +51,7 @@ module.exports = {
'@typescript-eslint/no-unused-vars': [ '@typescript-eslint/no-unused-vars': [
'error', 'error',
{ {
args: 'all', args: 'after-used',
argsIgnorePattern: '^_', argsIgnorePattern: '^_',
caughtErrors: 'all', caughtErrors: 'all',
caughtErrorsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_',
+1 -1
View File
@@ -26,7 +26,7 @@ const api = axios.create({
async function readOrNone(file) { async function readOrNone(file) {
try { try {
return await fs.readFile(file, 'utf8') return await fs.readFile(file, 'utf8')
} catch (err) { } catch (_) {
return '' return ''
} }
} }
+2 -2
View File
@@ -25,7 +25,7 @@ async function collectFilesInfo(inputPath, level = 0, hiddenFiles = false) {
) )
) )
files.push(...result.flat()) files.push(...result.flat())
} catch (err) { } catch (_) {
// ignore // ignore
} }
} }
@@ -41,7 +41,7 @@ async function discoverFiles(argv) {
.map((p) => collectFilesInfo(p, 0, argv.a || argv.all)) .map((p) => collectFilesInfo(p, 0, argv.a || argv.all))
) )
return { discover: result.flat() } return { discover: result.flat() }
} catch (err) { } catch (_) {
return {} return {}
} }
} }
+4 -1
View File
@@ -46,7 +46,10 @@ router.get('/available-methods', (req, res) => {
}) })
if (enabledMethods.google) { if (enabledMethods.google) {
router.get('/google', passport.authenticate('google')) router.get(
'/google',
passport.authenticate('google', { scope: ['email', 'profile'] })
)
router.get( router.get(
'/google/callback', '/google/callback',
authenticateRateLimit, authenticateRateLimit,
+2
View File
@@ -1,6 +1,7 @@
import { User } from '@linux-odyssey/models' import { User } from '@linux-odyssey/models'
import type { Profile } from 'passport' import type { Profile } from 'passport'
import type { VerifyCallback } from 'passport-oauth2' import type { VerifyCallback } from 'passport-oauth2'
import logger from '../utils/logger.js'
export default async function oauthVerify( export default async function oauthVerify(
provider: string, provider: string,
@@ -37,6 +38,7 @@ export default async function oauthVerify(
email: user.email, email: user.email,
}) })
} catch (err) { } catch (err) {
logger.error(err)
return cb( return cb(
err instanceof Error ? err : new Error('An unknown error occurred') err instanceof Error ? err : new Error('An unknown error occurred')
) )
+4 -2
View File
@@ -1,6 +1,6 @@
import passport from 'passport' import passport from 'passport'
import { UserProfile } from '@linux-odyssey/models'
import type { Express } from 'express' import type { Express } from 'express'
import { UserProfile } from '@linux-odyssey/models'
import passwordStrategy from './passwordStrategy.js' import passwordStrategy from './passwordStrategy.js'
import jwtStrategy from './jwtStrategy.js' import jwtStrategy from './jwtStrategy.js'
import googleStrategy from './googleStrategy.js' import googleStrategy from './googleStrategy.js'
@@ -30,7 +30,8 @@ passport.serializeUser((user: Express.User, done) => {
process.nextTick(async () => { process.nextTick(async () => {
try { try {
if (!('id' in user)) { if (!('id' in user)) {
done(new Error('User has no _id')) // OAuth new user
done(null, user)
return return
} }
const { id } = user const { id } = user
@@ -39,6 +40,7 @@ passport.serializeUser((user: Express.User, done) => {
} }
done(null, user) done(null, user)
} catch (err) { } catch (err) {
logger.error(err)
done(err) done(err)
} }
}) })
+1 -1
View File
@@ -13,7 +13,7 @@ export default function errorHandler(
req: Request, req: Request,
res: Response, res: Response,
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
next: NextFunction _next: NextFunction
) { ) {
logger.error(err) // Log error stack trace to the console logger.error(err) // Log error stack trace to the console
res.status(500).send({ message: 'Something broke!' }) // Send error response res.status(500).send({ message: 'Something broke!' }) // Send error response
+1 -1
View File
@@ -12,7 +12,7 @@ export default session({
domain: config.domain, domain: config.domain,
httpOnly: true, httpOnly: true,
secure: config.protocol === 'https:', secure: config.protocol === 'https:',
sameSite: 'strict', sameSite: 'lax',
maxAge: config.sessionMaxAge, maxAge: config.sessionMaxAge,
}, },
store: MongoStore.create({ store: MongoStore.create({