2021-04-07 12:33:16 +02:00
|
|
|
const passport = require("koa-passport")
|
|
|
|
const LocalStrategy = require("passport-local").Strategy
|
|
|
|
const JwtStrategy = require("passport-jwt").Strategy
|
2021-08-02 19:34:43 +02:00
|
|
|
const { StaticDatabases } = require("./db/utils")
|
2021-08-05 10:59:08 +02:00
|
|
|
const { getGlobalDB } = require("./tenancy")
|
2021-07-05 18:16:45 +02:00
|
|
|
const {
|
|
|
|
jwt,
|
|
|
|
local,
|
|
|
|
authenticated,
|
|
|
|
google,
|
|
|
|
oidc,
|
|
|
|
auditLog,
|
2021-08-05 10:59:08 +02:00
|
|
|
tenancy,
|
2021-07-05 18:16:45 +02:00
|
|
|
} = require("./middleware")
|
2021-08-05 10:59:08 +02:00
|
|
|
const { setDB } = require("./db")
|
2021-07-06 19:10:04 +02:00
|
|
|
const userCache = require("./cache/user")
|
2021-04-07 12:33:16 +02:00
|
|
|
|
|
|
|
// Strategies
|
|
|
|
passport.use(new LocalStrategy(local.options, local.authenticate))
|
|
|
|
passport.use(new JwtStrategy(jwt.options, jwt.authenticate))
|
|
|
|
|
|
|
|
passport.serializeUser((user, done) => done(null, user))
|
|
|
|
|
|
|
|
passport.deserializeUser(async (user, done) => {
|
2021-08-05 10:59:08 +02:00
|
|
|
const db = getGlobalDB()
|
2021-04-07 12:33:16 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const user = await db.get(user._id)
|
|
|
|
return done(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
console.error("User not found", err)
|
|
|
|
return done(null, false, { message: "User not found" })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-08 12:20:37 +02:00
|
|
|
module.exports = {
|
2021-04-15 17:45:21 +02:00
|
|
|
init(pouch) {
|
2021-04-20 18:17:44 +02:00
|
|
|
setDB(pouch)
|
2021-04-15 17:45:21 +02:00
|
|
|
},
|
2021-04-21 17:42:44 +02:00
|
|
|
db: require("./db/utils"),
|
2021-05-04 19:13:44 +02:00
|
|
|
redis: {
|
2021-05-05 13:11:06 +02:00
|
|
|
Client: require("./redis"),
|
2021-05-04 19:13:44 +02:00
|
|
|
utils: require("./redis/utils"),
|
|
|
|
},
|
2021-05-06 20:27:24 +02:00
|
|
|
objectStore: {
|
|
|
|
...require("./objectStore"),
|
|
|
|
...require("./objectStore/utils"),
|
|
|
|
},
|
2021-04-21 17:42:44 +02:00
|
|
|
utils: {
|
|
|
|
...require("./utils"),
|
2021-04-21 17:46:51 +02:00
|
|
|
...require("./hashing"),
|
2021-04-21 17:42:44 +02:00
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
buildAuthMiddleware: authenticated,
|
|
|
|
passport,
|
2021-04-22 16:27:09 +02:00
|
|
|
google,
|
2021-06-27 16:46:04 +02:00
|
|
|
oidc,
|
2021-04-23 19:07:39 +02:00
|
|
|
jwt: require("jsonwebtoken"),
|
2021-08-05 10:59:08 +02:00
|
|
|
buildTenancyMiddleware: tenancy,
|
2021-05-28 11:09:32 +02:00
|
|
|
auditLog,
|
2021-04-15 17:45:21 +02:00
|
|
|
},
|
2021-07-06 19:10:04 +02:00
|
|
|
cache: {
|
|
|
|
user: userCache,
|
|
|
|
},
|
2021-04-08 12:26:08 +02:00
|
|
|
StaticDatabases,
|
2021-04-22 16:27:09 +02:00
|
|
|
constants: require("./constants"),
|
2021-04-08 12:20:37 +02:00
|
|
|
}
|