2021-04-11 12:35:55 +02:00
|
|
|
const { Cookies } = require("../constants")
|
2021-04-22 12:45:22 +02:00
|
|
|
const database = require("../db")
|
2021-04-23 19:07:39 +02:00
|
|
|
const { getCookie, clearCookie } = require("../utils")
|
2021-04-22 12:45:22 +02:00
|
|
|
const { StaticDatabases } = require("../db/utils")
|
2021-05-11 13:02:29 +02:00
|
|
|
const env = require("../environment")
|
2021-04-11 12:35:55 +02:00
|
|
|
|
2021-04-28 19:13:21 +02:00
|
|
|
const PARAM_REGEX = /\/:(.*?)\//g
|
2021-04-28 15:28:25 +02:00
|
|
|
|
2021-04-28 19:13:21 +02:00
|
|
|
function buildNoAuthRegex(patterns) {
|
|
|
|
return patterns.map(pattern => {
|
|
|
|
const isObj = typeof pattern === "object" && pattern.route
|
|
|
|
const method = isObj ? pattern.method : "GET"
|
|
|
|
let route = isObj ? pattern.route : pattern
|
|
|
|
|
|
|
|
const matches = route.match(PARAM_REGEX)
|
|
|
|
if (matches) {
|
|
|
|
for (let match of matches) {
|
|
|
|
route = route.replace(match, "/.*/")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { regex: new RegExp(route), method }
|
|
|
|
})
|
2021-04-28 15:28:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 19:13:21 +02:00
|
|
|
module.exports = (noAuthPatterns = [], opts) => {
|
|
|
|
const noAuthOptions = noAuthPatterns ? buildNoAuthRegex(noAuthPatterns) : []
|
2021-04-21 17:42:44 +02:00
|
|
|
return async (ctx, next) => {
|
|
|
|
// the path is not authenticated
|
2021-04-28 19:13:21 +02:00
|
|
|
const found = noAuthOptions.find(({ regex, method }) => {
|
|
|
|
return (
|
|
|
|
regex.test(ctx.request.url) &&
|
|
|
|
ctx.request.method.toLowerCase() === method.toLowerCase()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
if (found != null) {
|
2021-04-21 17:42:44 +02:00
|
|
|
return next()
|
2021-04-11 12:35:55 +02:00
|
|
|
}
|
2021-04-21 17:42:44 +02:00
|
|
|
try {
|
2021-05-11 13:02:29 +02:00
|
|
|
const apiKey = ctx.request.headers["x-budibase-api-key"]
|
2021-04-21 17:42:44 +02:00
|
|
|
// check the actual user is authenticated first
|
|
|
|
const authCookie = getCookie(ctx, Cookies.Auth)
|
|
|
|
|
2021-05-11 13:02:29 +02:00
|
|
|
// this is an internal request, no user made it
|
2021-05-11 16:23:03 +02:00
|
|
|
if (apiKey && apiKey === env.INTERNAL_API_KEY) {
|
2021-05-11 13:02:29 +02:00
|
|
|
ctx.isAuthenticated = true
|
2021-05-21 17:43:01 +02:00
|
|
|
ctx.internal = true
|
2021-05-11 13:02:29 +02:00
|
|
|
} else if (authCookie) {
|
2021-04-23 19:07:39 +02:00
|
|
|
try {
|
|
|
|
const db = database.getDB(StaticDatabases.GLOBAL.name)
|
|
|
|
const user = await db.get(authCookie.userId)
|
|
|
|
delete user.password
|
|
|
|
ctx.isAuthenticated = true
|
|
|
|
ctx.user = user
|
|
|
|
} catch (err) {
|
|
|
|
// remove the cookie as the use does not exist anymore
|
|
|
|
clearCookie(ctx, Cookies.Auth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// be explicit
|
|
|
|
if (ctx.isAuthenticated !== true) {
|
|
|
|
ctx.isAuthenticated = false
|
2021-04-21 17:42:44 +02:00
|
|
|
}
|
|
|
|
return next()
|
|
|
|
} catch (err) {
|
2021-04-28 19:13:21 +02:00
|
|
|
// allow configuring for public access
|
|
|
|
if (opts && opts.publicAllowed) {
|
|
|
|
ctx.isAuthenticated = false
|
|
|
|
} else {
|
|
|
|
ctx.throw(err.status || 403, err)
|
|
|
|
}
|
2021-04-21 17:42:44 +02:00
|
|
|
}
|
2021-04-11 12:35:55 +02:00
|
|
|
}
|
|
|
|
}
|