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-04-21 22:08:04 +02:00
|
|
|
const constants = require("./constants")
|
2021-04-21 13:12:22 +02:00
|
|
|
const { StaticDatabases, DocumentTypes } = require("./db/utils")
|
|
|
|
const { jwt, local, google, authenticated } = require("./middleware")
|
2021-04-08 12:20:37 +02:00
|
|
|
const { Cookies, UserStatus } = require("./constants")
|
|
|
|
const { hash, compare } = require("./hashing")
|
2021-04-14 17:01:28 +02:00
|
|
|
const {
|
|
|
|
getAppId,
|
|
|
|
setCookie,
|
|
|
|
getCookie,
|
|
|
|
clearCookie,
|
|
|
|
isClient,
|
|
|
|
} = require("./utils")
|
2021-04-20 18:17:44 +02:00
|
|
|
const { setDB, getDB } = require("./db")
|
2021-04-11 12:35:55 +02:00
|
|
|
const {
|
|
|
|
generateUserID,
|
|
|
|
getUserParams,
|
2021-04-19 12:34:07 +02:00
|
|
|
generateGroupID,
|
|
|
|
getGroupParams,
|
2021-04-11 12:35:55 +02:00
|
|
|
getEmailFromUserID,
|
2021-04-20 19:14:36 +02:00
|
|
|
generateConfigID,
|
|
|
|
getConfigParams,
|
2021-04-22 14:46:54 +02:00
|
|
|
determineScopedConfig,
|
2021-04-11 12:35:55 +02:00
|
|
|
} = require("./db/utils")
|
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-04-20 18:17:44 +02:00
|
|
|
const db = getDB(StaticDatabases.GLOBAL.name)
|
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"),
|
|
|
|
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-21 22:08:04 +02:00
|
|
|
middlewares: {
|
|
|
|
google,
|
|
|
|
},
|
2021-04-15 17:45:21 +02:00
|
|
|
},
|
2021-04-21 22:08:04 +02:00
|
|
|
constants,
|
2021-04-08 12:20:37 +02:00
|
|
|
passport,
|
|
|
|
Cookies,
|
|
|
|
UserStatus,
|
2021-04-21 13:12:22 +02:00
|
|
|
DocumentTypes,
|
2021-04-08 12:26:08 +02:00
|
|
|
StaticDatabases,
|
|
|
|
generateUserID,
|
|
|
|
getUserParams,
|
2021-04-19 17:16:46 +02:00
|
|
|
generateGroupID,
|
|
|
|
getGroupParams,
|
2021-04-08 17:58:33 +02:00
|
|
|
getEmailFromUserID,
|
2021-04-20 19:14:36 +02:00
|
|
|
generateConfigID,
|
|
|
|
getConfigParams,
|
2021-04-22 14:46:54 +02:00
|
|
|
determineScopedConfig,
|
2021-04-08 12:20:37 +02:00
|
|
|
hash,
|
|
|
|
compare,
|
|
|
|
getAppId,
|
|
|
|
setCookie,
|
2021-04-12 19:31:58 +02:00
|
|
|
getCookie,
|
2021-04-13 14:56:28 +02:00
|
|
|
clearCookie,
|
2021-04-11 12:35:55 +02:00
|
|
|
authenticated,
|
2021-04-14 17:00:58 +02:00
|
|
|
isClient,
|
2021-04-08 12:20:37 +02:00
|
|
|
}
|