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

60 lines
1.7 KiB
JavaScript
Raw Normal View History

import cors from 'cors'
import http from 'passport-http'
import jwt from 'passport-jwt'
import { ApikeyModel } from './models/apikey.mjs'
import { UserModel } from './models/user.mjs'
/*
* In v2 we ended up with a bug where we did not properly track the last login
* So in v3 we switch to `lastSeen` and every authenticated API call we update
* this field. It's a bit of a perf hit to write to the database on ever API call
* but it's worth it to actually know which accounts are used and which are not.
*/
async function updateLastSeen(uid, tools) {
const User = new UserModel(tools)
await User.seen(uid)
}
function loadExpressMiddleware(app) {
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)
/*
* Update lastSeen field
*/
if (Apikey.verified) await updateLastSeen(Apikey.record.userId, tools)
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,
},
async (jwt_payload, done) => {
/*
* Update lastSeen field
*/
await updateLastSeen(jwt_payload._id, tools)
2022-11-12 17:33:55 +01:00
return done(null, {
...jwt_payload,
uid: jwt_payload._id,
level: tools.config.roles.levels[jwt_payload.role] || 0,
2022-11-12 17:33:55 +01:00
})
}
)
)
}
2022-10-29 22:25:00 +02:00
export { loadExpressMiddleware, loadPassportMiddleware }