mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
refactor: put passport to auth folder
This commit is contained in:
1 parent
41111f5036
commit
030dc95e24
8 files changed
+58
-76
No files matched your search
@@ -1,40 +1,18 @@
|
|||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
import passport from 'passport'
|
import passport from 'passport'
|
||||||
import LocalStrategy from 'passport-local'
|
|
||||||
import bcrypt from 'bcrypt'
|
|
||||||
|
|
||||||
import User from '../../models/user.js'
|
import { genJwt } from '../../utils/auth.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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
async function issueToken(req, res) {
|
async function issueToken(req, res) {
|
||||||
const token = await genUserJWT(req.user)
|
const { user } = req
|
||||||
res.json({
|
const token = await genJwt({
|
||||||
token,
|
_id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
})
|
})
|
||||||
|
res.json({ token })
|
||||||
}
|
}
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
|
|||||||
@@ -1,23 +1,9 @@
|
|||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
import passport from 'passport'
|
import passport from 'passport'
|
||||||
import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt'
|
|
||||||
import sessions from './sessions.js'
|
import sessions from './sessions.js'
|
||||||
import quests from './quests.js'
|
import quests from './quests.js'
|
||||||
import commands from './commands.js'
|
import commands from './commands.js'
|
||||||
import authRouter from './auth.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()
|
const router = Router()
|
||||||
|
|
||||||
@@ -26,12 +12,16 @@ router.get('/', (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
router.use(authRouter)
|
router.use(authRouter)
|
||||||
|
router.use('/quests', quests)
|
||||||
router.use(
|
router.use(
|
||||||
'/sessions',
|
'/sessions',
|
||||||
passport.authenticate('jwt', { session: false }),
|
passport.authenticate('jwt', { session: false }),
|
||||||
sessions
|
sessions
|
||||||
)
|
)
|
||||||
router.use('/quests', quests)
|
router.use(
|
||||||
router.use('/commands', commands)
|
'/commands',
|
||||||
|
passport.authenticate('jwt', { session: false }),
|
||||||
|
commands
|
||||||
|
)
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
@@ -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)
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import passport from 'passport'
|
||||||
|
import PasswordStrategy from './passwordStrategy.js'
|
||||||
|
import JwtStrategy from './jwtStrategy.js'
|
||||||
|
|
||||||
|
passport.use(PasswordStrategy)
|
||||||
|
passport.use(JwtStrategy)
|
||||||
@@ -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)
|
||||||
@@ -4,6 +4,7 @@ import http from 'http'
|
|||||||
import YAML from 'yaml'
|
import YAML from 'yaml'
|
||||||
import swaggerUI from 'swagger-ui-express'
|
import swaggerUI from 'swagger-ui-express'
|
||||||
|
|
||||||
|
import './auth/passport.js'
|
||||||
import socketServer from './api/socket.js'
|
import socketServer from './api/socket.js'
|
||||||
import connectDB from './db.js'
|
import connectDB from './db.js'
|
||||||
import apiRouter from './api/routes/index.js'
|
import apiRouter from './api/routes/index.js'
|
||||||
|
|||||||
@@ -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 })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -27,27 +27,10 @@ export async function defaultUser() {
|
|||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
export function genUserJWT(user) {
|
export function genJwt(payload) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
jwt.sign(
|
jwt.sign(
|
||||||
{ _id: user.id, username: user.username, email: user.email },
|
payload,
|
||||||
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 },
|
|
||||||
config.jwtSecret,
|
config.jwtSecret,
|
||||||
{ expiresIn: config.expiry },
|
{ expiresIn: config.expiry },
|
||||||
(err, token) => {
|
(err, token) => {
|
||||||
@@ -61,7 +44,7 @@ export function genSessionJWT(session) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function verifySessionJWT(token) {
|
export function verifyJWT(token) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
jwt.verify(token, config.jwtSecret, (err, decoded) => {
|
jwt.verify(token, config.jwtSecret, (err, decoded) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
Reference in new issue
Block a user