refactor: auth

This commit is contained in:
wancat committed 2024-02-19 18:05:01 +08:00
1 parent d6c95cea4a
commit 5489de2432
8 files changed
+120 -22

No files matched your search

+3
View File
@@ -50,6 +50,9 @@
"@types/express-session": "^1.17.10",
"@types/jsonwebtoken": "^9.0.5",
"@types/passport": "^1.0.16",
"@types/passport-github2": "^1.2.9",
"@types/passport-jwt": "^4.0.1",
"@types/passport-local": "^1.0.38",
"@types/passport-oauth2": "^1.4.15",
"@types/validator": "^13.11.9",
"concurrently": "^8.2.2",
@@ -1,9 +1,16 @@
import GitHubStrategy from 'passport-github2'
import { Strategy as GitHubStrategy } from 'passport-github2'
import type { Profile } from 'passport'
import type { VerifyCallback } from 'passport-oauth2'
import config from '../config.js'
import oauthVerify from './oauthVerify.js'
function verify(accessToken, refreshToken, profile, cb) {
function verify(
accessToken: string,
refreshToken: string,
profile: Profile,
cb: VerifyCallback
) {
oauthVerify('github', profile, { 'socialLogins.github.id': profile.id }, cb)
}
@@ -1,9 +1,9 @@
import GoogleStrategy from 'passport-google-oidc'
import { Strategy as GoogleStrategy } from 'passport-google-oidc'
import config from '../config.js'
import oauthVerify from './oauthVerify.js'
function verify(issuer, profile, cb) {
function verify(issuer: string, profile: any, cb: any) {
oauthVerify('google', profile, { 'socialLogins.google.id': profile.id }, cb)
}
@@ -15,7 +15,6 @@ export default enabled
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: `${config.baseUrl}/api/v1/auth/google/callback`,
scope: ['profile', 'email'],
},
verify
)
@@ -1,4 +1,5 @@
import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt'
import type { VerifyCallback } from 'passport-jwt'
import config from '../config.js'
const jwtOptions = {
@@ -6,7 +7,7 @@ const jwtOptions = {
secretOrKey: config.secret,
}
function verifyJwt(jwtPayload, done) {
function verifyJwt(jwtPayload: any, done: VerifyCallback) {
return done(null, jwtPayload)
}
@@ -1,11 +1,18 @@
import passport from 'passport'
import { UserProfile } from '@linux-odyssey/models'
import type { Express } from 'express'
import passwordStrategy from './passwordStrategy.js'
import jwtStrategy from './jwtStrategy.js'
import googleStrategy from './googleStrategy.js'
import githubStrategy from './githubStrategy.js'
import logger from '../utils/logger.js'
interface SerializedUser extends Express.User {
_id: string
username: string
email: string
}
passport.use(passwordStrategy)
passport.use(jwtStrategy)
@@ -17,32 +24,29 @@ const enabledMethods = {
logger.info('Enabled login methods', enabledMethods)
if (enabledMethods.google) {
if (googleStrategy !== null) {
passport.use(googleStrategy)
}
if (enabledMethods.github) {
if (githubStrategy !== null) {
passport.use(githubStrategy)
}
passport.serializeUser((user, done) => {
passport.serializeUser((user: Express.User, done: any) => {
process.nextTick(async () => {
try {
if (user._id && !(await UserProfile.exists({ user: user._id }))) {
await UserProfile.create({ user: user._id })
const serializedUser = user as SerializedUser
if (!(await UserProfile.exists({ user: serializedUser._id }))) {
await UserProfile.create({ user: serializedUser._id })
}
done(null, {
_id: user._id,
username: user.username,
email: user.email,
})
done(null, serializedUser)
} catch (err) {
done(err)
}
})
})
passport.deserializeUser((user, done) => {
passport.deserializeUser((user: SerializedUser, done) => {
process.nextTick(() => {
done(null, user)
})
@@ -1,8 +1,19 @@
/* eslint-disable no-unused-vars */
import bcrypt from 'bcrypt'
import LocalStrategy from 'passport-local'
import { Strategy as LocalStrategy } from 'passport-local'
import type { IVerifyOptions } from 'passport-local'
import type { Express } from 'express'
import { User } from '@linux-odyssey/models'
async function verifyPassword(username, password, done) {
async function verifyPassword(
username: string,
password: string,
done: (
error: any,
user?: Express.User | false,
options?: IVerifyOptions
) => void
) {
try {
const user = await User.findOne({
$or: [{ username }, { email: username }],
@@ -11,7 +22,10 @@ async function verifyPassword(username, password, done) {
return done(null, false, { message: 'Incorrect username or password.' })
}
const isPasswordValid = await bcrypt.compare(password, user.hashedPassword)
const isPasswordValid = await bcrypt.compare(
password,
user.hashedPassword || ''
)
if (!isPasswordValid) {
return done(null, false, { message: 'Incorrect username or password.' })
}
+25
View File
@@ -0,0 +1,25 @@
/* eslint-disable no-unused-vars */
declare module 'passport-google-oidc' {
// Add specific type declarations here as needed.
// For example, if you're only using it as a middleware in Express:
import { Strategy as PassportStrategy } from 'passport'
import { Request } from 'express'
interface StrategyOptions {
clientID: string
clientSecret: string
callbackURL: string
}
interface VerifyFunction {
(
issuer: string,
profile: any,
done: (error: any, user?: any, info?: any) => void
): void
}
export class Strategy extends PassportStrategy {
constructor(options: StrategyOptions, verify: VerifyFunction)
}
}