1
0
Fork 0
mirror of synced 2024-06-29 19:41:03 +12:00
budibase/packages/auth/src/middleware/authenticated.js

100 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-07-24 02:29:14 +12:00
const { Cookies, Headers } = require("../constants")
const { getCookie, clearCookie } = require("../utils")
2021-07-07 05:10:04 +12:00
const { getUser } = require("../cache/user")
const { getSession, updateSessionTTL } = require("../security/sessions")
const env = require("../environment")
2021-04-11 22:35:55 +12:00
2021-08-04 21:02:24 +12:00
const PARAM_REGEX = /\/:(.*?)\//g
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 }
})
}
function finalise(ctx, { authenticated, user, internal, version } = {}) {
ctx.isAuthenticated = authenticated || false
ctx.user = user
ctx.internal = internal || false
2021-07-24 02:29:14 +12:00
ctx.version = version
}
2021-08-04 21:02:24 +12:00
module.exports = (noAuthPatterns = [], opts) => {
const noAuthOptions = noAuthPatterns ? buildNoAuthRegex(noAuthPatterns) : []
return async (ctx, next) => {
2021-07-24 02:29:14 +12:00
const version = ctx.request.headers[Headers.API_VER]
// the path is not authenticated
2021-08-04 21:02:24 +12:00
const found = noAuthOptions.find(({ regex, method }) => {
return (
regex.test(ctx.request.url) &&
ctx.request.method.toLowerCase() === method.toLowerCase()
)
})
if (found != null) {
return next()
2021-04-11 22:35:55 +12:00
}
try {
// check the actual user is authenticated first
const authCookie = getCookie(ctx, Cookies.Auth)
let authenticated = false,
user = null,
internal = false
if (authCookie) {
2021-07-07 05:10:04 +12:00
let error = null
const sessionId = authCookie.sessionId,
userId = authCookie.userId
2021-07-07 05:10:04 +12:00
const session = await getSession(userId, sessionId)
if (!session) {
error = "No session found"
} else {
try {
2021-08-04 21:02:24 +12:00
user = await getUser(userId)
2021-07-07 05:10:04 +12:00
delete user.password
authenticated = true
} catch (err) {
error = err
}
}
if (error) {
console.error("Auth Error", error)
2021-07-07 05:10:04 +12:00
// remove the cookie as the user does not exist anymore
clearCookie(ctx, Cookies.Auth)
2021-07-07 05:10:04 +12:00
} else {
// make sure we denote that the session is still in use
2021-07-08 10:29:19 +12:00
await updateSessionTTL(session)
}
}
2021-07-24 02:29:14 +12:00
const apiKey = ctx.request.headers[Headers.API_KEY]
// this is an internal request, no user made it
if (!authenticated && apiKey && apiKey === env.INTERNAL_API_KEY) {
authenticated = true
internal = true
}
// be explicit
if (authenticated !== true) {
authenticated = false
}
// isAuthenticated is a function, so use a variable to be able to check authed state
2021-08-04 21:02:24 +12:00
finalise(ctx, { authenticated, user, internal, version })
return next()
} catch (err) {
// allow configuring for public access
2021-08-04 21:02:24 +12:00
if (opts && opts.publicAllowed) {
finalise(ctx, { authenticated: false, version })
} else {
ctx.throw(err.status || 403, err)
}
}
2021-04-11 22:35:55 +12:00
}
}