1
0
Fork 0
freesewing/sites/backend/src/middleware.mjs

37 lines
1 KiB
JavaScript
Raw Normal View History

//import bodyParser from 'body-parser'
import cors from 'cors'
import http from 'passport-http'
import jwt from 'passport-jwt'
import { ApikeyModel } from './models/apikey.mjs'
function loadExpressMiddleware(app) {
// FIXME: Is this still needed in FreeSewing v3?
//app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors())
}
function loadPassportMiddleware(passport, tools) {
passport.use(
new http.BasicStrategy(async (key, secret, done) => {
const Apikey = new ApikeyModel(tools)
await Apikey.verify(key, secret)
2022-11-08 21:04:32 +01:00
return Apikey.verified
? done(null, { ...Apikey.record, apikey: true, uid: Apikey.record.userId })
: done(false)
})
)
passport.use(
new jwt.Strategy(
{
jwtFromRequest: jwt.ExtractJwt.fromAuthHeaderAsBearerToken(),
...tools.config.jwt,
},
(jwt_payload, done) => {
2022-11-08 21:04:32 +01:00
return done(null, { ...jwt_payload, uid: jwt_payload._id })
}
)
)
}
2022-10-29 22:25:00 +02:00
export { loadExpressMiddleware, loadPassportMiddleware }