1
0
Fork 0
mirror of synced 2024-06-28 11:00:55 +12:00
budibase/packages/server/src/api/controllers/auth.js

89 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const jwt = require("jsonwebtoken")
const CouchDB = require("../../db")
const bcrypt = require("../../utilities/bcrypt")
const env = require("../../environment")
const { getAPIKey } = require("../../utilities/usageQuota")
const { generateUserMetadataID } = require("../../db/utils")
const { setCookie } = require("../../utilities")
const { outputProcessing } = require("../../utilities/rowProcessor")
const { InternalTables } = require("../../db/utils")
const { UserStatus } = require("@budibase/auth")
const { getFullUser } = require("../../utilities/users")
2020-04-08 07:34:21 +12:00
const INVALID_ERR = "Invalid Credentials"
exports.authenticate = async ctx => {
const appId = ctx.appId
if (!appId) ctx.throw(400, "No appId")
2020-12-05 01:22:45 +13:00
const { email, password } = ctx.request.body
2020-12-05 01:22:45 +13:00
if (!email) ctx.throw(400, "Email Required.")
if (!password) ctx.throw(400, "Password Required.")
2020-12-05 01:22:45 +13:00
// Check the user exists in the instance DB by email
const db = new CouchDB(appId)
const app = await db.get(appId)
2020-05-28 04:23:01 +12:00
let dbUser
try {
dbUser = await db.get(generateUserMetadataID(email))
2020-05-28 04:23:01 +12:00
} catch (_) {
// do not want to throw a 404 - as this could be
2020-12-05 01:22:45 +13:00
// used to determine valid emails
ctx.throw(401, INVALID_ERR)
}
// check that the user is currently inactive, if this is the case throw invalid
if (dbUser.status === UserStatus.INACTIVE) {
ctx.throw(401, INVALID_ERR)
2020-05-28 04:23:01 +12:00
}
// authenticate
if (await bcrypt.compare(password, dbUser.password)) {
2020-05-07 21:53:34 +12:00
const payload = {
userId: dbUser._id,
roleId: dbUser.roleId,
version: app.version,
2020-05-07 21:53:34 +12:00
}
// if in prod add the user api key, unless self hosted
2021-03-11 01:20:07 +13:00
/* istanbul ignore next */
if (env.isProd() && !env.SELF_HOSTED) {
const { apiKey } = await getAPIKey(ctx.appId)
2020-10-10 09:42:20 +13:00
payload.apiKey = apiKey
}
2020-05-07 07:29:47 +12:00
const token = jwt.sign(payload, ctx.config.jwtSecret, {
2020-05-07 21:53:34 +12:00
expiresIn: "1 day",
})
2020-05-07 07:49:21 +12:00
setCookie(ctx, token, appId)
2020-05-07 07:29:47 +12:00
delete dbUser.password
ctx.body = {
token,
2020-05-07 21:53:34 +12:00
...dbUser,
appId,
2020-05-07 21:53:34 +12:00
}
} else {
ctx.throw(401, INVALID_ERR)
}
2020-05-07 21:53:34 +12:00
}
exports.fetchSelf = async ctx => {
const { userId, appId } = ctx.user
2021-03-11 01:20:07 +13:00
/* istanbul ignore next */
if (!userId || !appId) {
ctx.body = {}
return
}
const db = new CouchDB(appId)
const user = await getFullUser({ ctx, userId: userId })
const userTable = await db.get(InternalTables.USER_METADATA)
if (user) {
delete user.password
}
// specifically needs to make sure is enriched
ctx.body = await outputProcessing(appId, userTable, user)
}