From 030dc95e24fbab71b040d19d0321b40162fd9fa9 Mon Sep 17 00:00:00 2001 From: wancat Date: Mon, 11 Sep 2023 23:38:29 +0800 Subject: [PATCH] refactor: put passport to auth folder --- server/src/api/routes/auth.js | 36 ++++++----------------------- server/src/api/routes/index.js | 22 +++++------------- server/src/auth/jwtStrategy.js | 13 +++++++++++ server/src/auth/passport.js | 6 +++++ server/src/auth/passwordStrategy.js | 22 ++++++++++++++++++ server/src/index.js | 1 + server/src/middleware/auth.js | 11 --------- server/src/utils/auth.js | 23 +++--------------- 8 files changed, 58 insertions(+), 76 deletions(-) create mode 100644 server/src/auth/jwtStrategy.js create mode 100644 server/src/auth/passport.js create mode 100644 server/src/auth/passwordStrategy.js delete mode 100644 server/src/middleware/auth.js diff --git a/server/src/api/routes/auth.js b/server/src/api/routes/auth.js index 6277312..8d7afdd 100644 --- a/server/src/api/routes/auth.js +++ b/server/src/api/routes/auth.js @@ -1,40 +1,18 @@ import { Router } from 'express' import passport from 'passport' -import LocalStrategy from 'passport-local' -import bcrypt from 'bcrypt' -import User from '../../models/user.js' -import { genUserJWT } from '../../utils/auth.js' - -passport.use( - new LocalStrategy(async function verify(username, password, done) { - try { - const user = await User.findOne({ username }) - if (!user) { - return done(null, false, { message: 'Incorrect username.' }) - } - - const isPasswordValid = await bcrypt.compare( - password, - user.hashedPassword - ) - if (!isPasswordValid) { - return done(null, false, { message: 'Incorrect username or password.' }) - } - return done(null, user) - } catch (err) { - return done(err) - } - }) -) +import { genJwt } from '../../utils/auth.js' const router = Router() async function issueToken(req, res) { - const token = await genUserJWT(req.user) - res.json({ - token, + const { user } = req + const token = await genJwt({ + _id: user.id, + username: user.username, + email: user.email, }) + res.json({ token }) } router.post( diff --git a/server/src/api/routes/index.js b/server/src/api/routes/index.js index fdea3fc..20db430 100644 --- a/server/src/api/routes/index.js +++ b/server/src/api/routes/index.js @@ -1,23 +1,9 @@ import { Router } from 'express' import passport from 'passport' -import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt' import sessions from './sessions.js' import quests from './quests.js' import commands from './commands.js' import authRouter from './auth.js' -import config from '../../config.js' - -const jwtOptions = { - jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), - secretOrKey: config.jwtSecret, - issuer: 'linuxodyssey.xyz', -} - -passport.use( - new JwtStrategy(jwtOptions, function verifyJwt(jwtPayload, done) { - return done(null, jwtPayload) - }) -) const router = Router() @@ -26,12 +12,16 @@ router.get('/', (req, res) => { }) router.use(authRouter) +router.use('/quests', quests) router.use( '/sessions', passport.authenticate('jwt', { session: false }), sessions ) -router.use('/quests', quests) -router.use('/commands', commands) +router.use( + '/commands', + passport.authenticate('jwt', { session: false }), + commands +) export default router diff --git a/server/src/auth/jwtStrategy.js b/server/src/auth/jwtStrategy.js new file mode 100644 index 0000000..f1b2cd4 --- /dev/null +++ b/server/src/auth/jwtStrategy.js @@ -0,0 +1,13 @@ +import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt' +import config from '../config.js' + +const jwtOptions = { + jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), + secretOrKey: config.jwtSecret, +} + +function verifyJwt(jwtPayload, done) { + return done(null, jwtPayload) +} + +export default new JwtStrategy(jwtOptions, verifyJwt) diff --git a/server/src/auth/passport.js b/server/src/auth/passport.js new file mode 100644 index 0000000..fc1fe04 --- /dev/null +++ b/server/src/auth/passport.js @@ -0,0 +1,6 @@ +import passport from 'passport' +import PasswordStrategy from './passwordStrategy.js' +import JwtStrategy from './jwtStrategy.js' + +passport.use(PasswordStrategy) +passport.use(JwtStrategy) diff --git a/server/src/auth/passwordStrategy.js b/server/src/auth/passwordStrategy.js new file mode 100644 index 0000000..86b0b90 --- /dev/null +++ b/server/src/auth/passwordStrategy.js @@ -0,0 +1,22 @@ +import bcrypt from 'bcrypt' +import LocalStrategy from 'passport-local' +import User from '../models/user.js' + +async function verifyPassword(username, password, done) { + try { + const user = await User.findOne({ username }) + if (!user) { + return done(null, false, { message: 'Incorrect username.' }) + } + + const isPasswordValid = await bcrypt.compare(password, user.hashedPassword) + if (!isPasswordValid) { + return done(null, false, { message: 'Incorrect username or password.' }) + } + return done(null, user) + } catch (err) { + return done(err) + } +} + +export default new LocalStrategy(verifyPassword) diff --git a/server/src/index.js b/server/src/index.js index 2b38ff2..8a898ea 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -4,6 +4,7 @@ import http from 'http' import YAML from 'yaml' import swaggerUI from 'swagger-ui-express' +import './auth/passport.js' import socketServer from './api/socket.js' import connectDB from './db.js' import apiRouter from './api/routes/index.js' diff --git a/server/src/middleware/auth.js b/server/src/middleware/auth.js deleted file mode 100644 index 62bb190..0000000 --- a/server/src/middleware/auth.js +++ /dev/null @@ -1,11 +0,0 @@ -/* eslint-disable import/prefer-default-export */ -import { defaultUser } from '../utils/auth.js' - -export async function authMiddleware(req, res, next) { - try { - req.user = await defaultUser() - next() - } catch (err) { - res.status(500).json({ message: err.message }) - } -} diff --git a/server/src/utils/auth.js b/server/src/utils/auth.js index 9704e6c..6388cf6 100644 --- a/server/src/utils/auth.js +++ b/server/src/utils/auth.js @@ -27,27 +27,10 @@ export async function defaultUser() { return user } -export function genUserJWT(user) { +export function genJwt(payload) { return new Promise((resolve, reject) => { jwt.sign( - { _id: user.id, username: user.username, email: user.email }, - config.jwtSecret, - { expiresIn: config.expiry, issuer: 'linuxodyssey.xyz' }, - (err, token) => { - if (err) { - reject(err) - } else { - resolve(token) - } - } - ) - }) -} - -export function genSessionJWT(session) { - return new Promise((resolve, reject) => { - jwt.sign( - { session_id: session.id }, + payload, config.jwtSecret, { expiresIn: config.expiry }, (err, token) => { @@ -61,7 +44,7 @@ export function genSessionJWT(session) { }) } -export function verifySessionJWT(token) { +export function verifyJWT(token) { return new Promise((resolve, reject) => { jwt.verify(token, config.jwtSecret, (err, decoded) => { if (err) {