budibase/packages/auth/src/index.js

72 lines
1.6 KiB
JavaScript
Raw Normal View History

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
const { StaticDatabases } = require("./db/utils")
const { getGlobalDB } = require("./tenancy")
const {
jwt,
local,
authenticated,
google,
oidc,
auditLog,
tenancy,
appTenancy,
authError,
} = require("./middleware")
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) => {
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" })
}
})
module.exports = {
init(pouch) {
setDB(pouch)
},
db: require("./db/utils"),
redis: {
Client: require("./redis"),
utils: require("./redis/utils"),
},
objectStore: {
...require("./objectStore"),
...require("./objectStore/utils"),
},
utils: {
...require("./utils"),
2021-04-21 17:46:51 +02:00
...require("./hashing"),
},
auth: {
buildAuthMiddleware: authenticated,
passport,
2021-04-22 16:27:09 +02:00
google,
2021-06-27 16:46:04 +02:00
oidc,
jwt: require("jsonwebtoken"),
buildTenancyMiddleware: tenancy,
buildAppTenancyMiddleware: appTenancy,
2021-05-28 11:09:32 +02:00
auditLog,
authError,
},
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"),
}