2020-05-07 21:53:34 +12:00
|
|
|
const jwt = require("jsonwebtoken")
|
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const bcrypt = require("../../utilities/bcrypt")
|
2020-10-29 09:35:06 +13:00
|
|
|
const env = require("../../environment")
|
2020-10-08 05:56:47 +13:00
|
|
|
const { getAPIKey } = require("../../utilities/usageQuota")
|
2020-10-03 00:52:15 +13:00
|
|
|
const { generateUserID } = require("../../db/utils")
|
2020-04-08 07:34:21 +12:00
|
|
|
|
2020-04-24 01:37:08 +12:00
|
|
|
exports.authenticate = async ctx => {
|
2020-10-29 23:28:27 +13:00
|
|
|
const appId = ctx.user.appId
|
|
|
|
if (!appId) ctx.throw(400, "No appId")
|
2020-06-13 07:42:55 +12:00
|
|
|
|
2020-05-07 21:53:34 +12:00
|
|
|
const { username, password } = ctx.request.body
|
2020-04-21 03:17:11 +12:00
|
|
|
|
2020-05-07 21:53:34 +12:00
|
|
|
if (!username) ctx.throw(400, "Username Required.")
|
2020-10-29 09:35:06 +13:00
|
|
|
if (!password) ctx.throw(400, "Password Required.")
|
2020-05-06 21:33:30 +12:00
|
|
|
|
|
|
|
// Check the user exists in the instance DB by username
|
2020-10-29 23:28:27 +13:00
|
|
|
const db = new CouchDB(appId)
|
|
|
|
const app = await db.get(appId)
|
2020-05-06 21:33:30 +12:00
|
|
|
|
2020-05-28 04:23:01 +12:00
|
|
|
let dbUser
|
|
|
|
try {
|
2020-10-29 09:35:06 +13:00
|
|
|
dbUser = await db.get(generateUserID(username))
|
2020-05-28 04:23:01 +12:00
|
|
|
} catch (_) {
|
|
|
|
// do not want to throw a 404 - as this could be
|
2020-10-14 09:33:56 +13:00
|
|
|
// used to determine valid usernames
|
2020-05-28 04:23:01 +12:00
|
|
|
ctx.throw(401, "Invalid Credentials")
|
|
|
|
}
|
2020-05-06 21:33:30 +12:00
|
|
|
|
|
|
|
// authenticate
|
2020-04-24 01:37:08 +12:00
|
|
|
if (await bcrypt.compare(password, dbUser.password)) {
|
2020-05-07 21:53:34 +12:00
|
|
|
const payload = {
|
|
|
|
userId: dbUser._id,
|
2020-05-28 04:23:01 +12:00
|
|
|
accessLevelId: dbUser.accessLevelId,
|
2020-10-22 03:28:30 +13:00
|
|
|
version: app.version,
|
2020-10-29 23:28:27 +13:00
|
|
|
appId,
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-10-07 07:13:41 +13:00
|
|
|
// if in cloud add the user api key
|
2020-10-29 09:35:06 +13:00
|
|
|
if (env.CLOUD) {
|
2020-10-10 09:42:20 +13:00
|
|
|
const { apiKey } = await getAPIKey(ctx.user.appId)
|
|
|
|
payload.apiKey = apiKey
|
2020-10-07 07:13:41 +13:00
|
|
|
}
|
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
|
|
|
|
2020-06-20 03:59:46 +12:00
|
|
|
const expires = new Date()
|
|
|
|
expires.setDate(expires.getDate() + 1)
|
2020-05-07 21:53:34 +12:00
|
|
|
|
2020-06-20 03:59:46 +12:00
|
|
|
ctx.cookies.set("budibase:token", token, {
|
|
|
|
expires,
|
|
|
|
path: "/",
|
|
|
|
httpOnly: false,
|
2020-10-16 01:52:56 +13:00
|
|
|
overwrite: true,
|
2020-06-20 03:59:46 +12:00
|
|
|
})
|
2020-05-07 07:29:47 +12:00
|
|
|
|
2020-05-06 21:33:30 +12:00
|
|
|
ctx.body = {
|
|
|
|
token,
|
2020-05-07 21:53:34 +12:00
|
|
|
...dbUser,
|
|
|
|
}
|
2020-04-24 01:37:08 +12:00
|
|
|
} else {
|
2020-05-07 21:53:34 +12:00
|
|
|
ctx.throw(401, "Invalid credentials.")
|
2020-04-24 01:37:08 +12:00
|
|
|
}
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|