forked from tsai/budibase
5 changed files with 93 additions and 89 deletions
@ -1,73 +1,73 @@ |
|||||
const jwt = require("jsonwebtoken") |
// const jwt = require("jsonwebtoken")
|
||||
const STATUS_CODES = require("../utilities/statusCodes") |
// const STATUS_CODES = require("../utilities/statusCodes")
|
||||
const { getRole, getBuiltinRoles } = require("../utilities/security/roles") |
// const { getRole, getBuiltinRoles } = require("../utilities/security/roles")
|
||||
const { AuthTypes } = require("../constants") |
// const { AuthTypes } = require("../constants")
|
||||
const { |
// const {
|
||||
getAppId, |
// getAppId,
|
||||
getCookieName, |
// getCookieName,
|
||||
clearCookie, |
// clearCookie,
|
||||
setCookie, |
// setCookie,
|
||||
isClient, |
// isClient,
|
||||
} = require("../utilities") |
// } = require("../utilities")
|
||||
|
|
||||
module.exports = async (ctx, next) => { |
// module.exports = async (ctx, next) => {
|
||||
if (ctx.path === "/builder") { |
// if (ctx.path === "/builder") {
|
||||
await next() |
// await next()
|
||||
return |
// return
|
||||
} |
// }
|
||||
|
|
||||
// do everything we can to make sure the appId is held correctly
|
// // do everything we can to make sure the appId is held correctly
|
||||
// we hold it in state as a
|
// // we hold it in state as a
|
||||
let appId = getAppId(ctx) |
// let appId = getAppId(ctx)
|
||||
const cookieAppId = ctx.cookies.get(getCookieName("currentapp")) |
// const cookieAppId = ctx.cookies.get(getCookieName("currentapp"))
|
||||
const builtinRoles = getBuiltinRoles() |
// const builtinRoles = getBuiltinRoles()
|
||||
if (appId && cookieAppId !== appId) { |
// if (appId && cookieAppId !== appId) {
|
||||
setCookie(ctx, appId, "currentapp") |
// setCookie(ctx, appId, "currentapp")
|
||||
} else if (cookieAppId) { |
// } else if (cookieAppId) {
|
||||
appId = cookieAppId |
// appId = cookieAppId
|
||||
} |
// }
|
||||
let token, authType |
// let token, authType
|
||||
if (!isClient(ctx)) { |
// if (!isClient(ctx)) {
|
||||
token = ctx.cookies.get(getCookieName()) |
// token = ctx.cookies.get(getCookieName())
|
||||
authType = AuthTypes.BUILDER |
// authType = AuthTypes.BUILDER
|
||||
} |
// }
|
||||
|
|
||||
if (!token && appId) { |
// if (!token && appId) {
|
||||
token = ctx.cookies.get(getCookieName(appId)) |
// token = ctx.cookies.get(getCookieName(appId))
|
||||
authType = AuthTypes.APP |
// authType = AuthTypes.APP
|
||||
} |
// }
|
||||
|
|
||||
if (!token) { |
// if (!token) {
|
||||
ctx.auth.authenticated = false |
// ctx.auth.authenticated = false
|
||||
ctx.appId = appId |
// ctx.appId = appId
|
||||
ctx.user = { |
// ctx.user = {
|
||||
role: builtinRoles.PUBLIC, |
// role: builtinRoles.PUBLIC,
|
||||
} |
// }
|
||||
await next() |
// await next()
|
||||
return |
// return
|
||||
} |
// }
|
||||
|
|
||||
try { |
// try {
|
||||
ctx.auth.authenticated = authType |
// ctx.auth.authenticated = authType
|
||||
const jwtPayload = jwt.verify(token, ctx.config.jwtSecret) |
// const jwtPayload = jwt.verify(token, ctx.config.jwtSecret)
|
||||
ctx.appId = appId |
// ctx.appId = appId
|
||||
ctx.auth.apiKey = jwtPayload.apiKey |
// ctx.auth.apiKey = jwtPayload.apiKey
|
||||
ctx.user = { |
// ctx.user = {
|
||||
...jwtPayload, |
// ...jwtPayload,
|
||||
role: await getRole(appId, jwtPayload.roleId), |
// role: await getRole(appId, jwtPayload.roleId),
|
||||
} |
// }
|
||||
// appId no longer carried in user, make sure
|
// // appId no longer carried in user, make sure
|
||||
delete ctx.user.appId |
// delete ctx.user.appId
|
||||
} catch (err) { |
// } catch (err) {
|
||||
console.log(err) |
// console.log(err)
|
||||
if (authType === AuthTypes.BUILDER) { |
// if (authType === AuthTypes.BUILDER) {
|
||||
clearCookie(ctx) |
// clearCookie(ctx)
|
||||
ctx.status = 200 |
// ctx.status = 200
|
||||
return |
// return
|
||||
} else { |
// } else {
|
||||
ctx.throw(err.status || STATUS_CODES.FORBIDDEN, err.text) |
// ctx.throw(err.status || STATUS_CODES.FORBIDDEN, err.text)
|
||||
} |
// }
|
||||
} |
// }
|
||||
|
|
||||
await next() |
// await next()
|
||||
} |
// }
|
||||
|
|||||
Loading…
Reference in new issue