forked from tsai/budibase
11 changed files with 95 additions and 136 deletions
@ -1,57 +1,21 @@ |
|||||
const CouchDB = require("../db") |
|
||||
const { Cookies } = require("../constants") |
const { Cookies } = require("../constants") |
||||
const { getAppId, setCookie, getCookie } = require("../utils") |
const { getCookie } = require("../utils") |
||||
const { StaticDatabases } = require("../db/utils") |
const { getEmailFromUserID } = require("../db/utils") |
||||
|
|
||||
async function setCurrentAppContext(ctx) { |
|
||||
let role = "PUBLIC" |
|
||||
|
|
||||
// Current app cookie
|
|
||||
let appId = getAppId(ctx) |
|
||||
if (!appId) { |
|
||||
ctx.user = { |
|
||||
role, |
|
||||
} |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
console.log("THE APP ID", appId) |
|
||||
|
|
||||
const currentAppCookie = getCookie(ctx, Cookies.CurrentApp, { decrypt: true }) |
|
||||
const appIdChanged = appId && currentAppCookie.appId !== appId |
|
||||
if (appIdChanged) { |
|
||||
try { |
|
||||
// get roles for user from global DB
|
|
||||
const db = new CouchDB(StaticDatabases.USER) |
|
||||
const user = await db.get(ctx.user) |
|
||||
role = user.roles[appId] |
|
||||
} catch (err) { |
|
||||
// no user exists
|
|
||||
} |
|
||||
} else if (currentAppCookie.appId) { |
|
||||
appId = currentAppCookie.appId |
|
||||
} |
|
||||
setCookie(ctx, { appId, role }, Cookies.CurrentApp, { encrypt: true }) |
|
||||
return appId |
|
||||
} |
|
||||
|
|
||||
module.exports = async (ctx, next) => { |
module.exports = async (ctx, next) => { |
||||
try { |
try { |
||||
// check the actual user is authenticated first
|
// check the actual user is authenticated first
|
||||
const authCookie = getCookie(ctx, Cookies.Auth, { decrypt: true }) |
const authCookie = getCookie(ctx, Cookies.Auth) |
||||
|
|
||||
if (authCookie) { |
if (authCookie) { |
||||
ctx.isAuthenticated = true |
ctx.isAuthenticated = true |
||||
ctx.user = authCookie._id |
ctx.user = authCookie |
||||
|
// make sure email is correct from ID
|
||||
|
ctx.user.email = getEmailFromUserID(authCookie._id) |
||||
} |
} |
||||
|
|
||||
ctx.appId = await setCurrentAppContext(ctx) |
|
||||
|
|
||||
console.log("CONTEXT", ctx) |
|
||||
|
|
||||
await next() |
await next() |
||||
} catch (err) { |
} catch (err) { |
||||
console.log(err) |
|
||||
ctx.throw(err.status || 403, err.text) |
ctx.throw(err.status || 403, err.text) |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,57 @@ |
|||||
|
const { getAppId, setCookie, getCookie, Cookies } = require("@budibase/auth") |
||||
|
const { getGlobalUsers } = require("../utilities/workerRequests") |
||||
|
const { BUILTIN_ROLE_IDS } = require("../utilities/security/roles") |
||||
|
|
||||
|
function CurrentAppCookie(appId, roleId) { |
||||
|
this.appId = appId |
||||
|
this.roleId = roleId |
||||
|
} |
||||
|
|
||||
|
function finish(ctx, next, { appId, roleId, cookie = false }) { |
||||
|
if (appId) { |
||||
|
ctx.appId = appId |
||||
|
} |
||||
|
if (roleId) { |
||||
|
ctx.roleId = roleId |
||||
|
} |
||||
|
if (cookie && appId) { |
||||
|
setCookie(ctx, new CurrentAppCookie(appId, roleId)) |
||||
|
} |
||||
|
return next() |
||||
|
} |
||||
|
|
||||
|
module.exports = async (ctx, next) => { |
||||
|
// try to get the appID from the request
|
||||
|
const requestAppId = getAppId(ctx) |
||||
|
// get app cookie if it exists
|
||||
|
const appCookie = getCookie(ctx, Cookies.CurrentApp) |
||||
|
if (!appCookie && !requestAppId) { |
||||
|
return next() |
||||
|
} |
||||
|
|
||||
|
let updateCookie = false, |
||||
|
appId, |
||||
|
roleId |
||||
|
if (!ctx.user) { |
||||
|
// not logged in, try to set a cookie for public apps
|
||||
|
updateCookie = true |
||||
|
appId = requestAppId |
||||
|
roleId = BUILTIN_ROLE_IDS.PUBLIC |
||||
|
} else if ( |
||||
|
requestAppId != null && |
||||
|
(appCookie == null || requestAppId === appCookie.appId) |
||||
|
) { |
||||
|
const globalUser = await getGlobalUsers(ctx, requestAppId, ctx.user.email) |
||||
|
updateCookie = true |
||||
|
appId = requestAppId |
||||
|
roleId = globalUser.roles[requestAppId] || BUILTIN_ROLE_IDS.PUBLIC |
||||
|
} else if (requestAppId == null && appCookie != null) { |
||||
|
appId = appCookie.appId |
||||
|
roleId = appCookie.roleId || BUILTIN_ROLE_IDS.PUBLIC |
||||
|
} |
||||
|
return finish(ctx, next, { |
||||
|
appId: appId, |
||||
|
roleId: roleId, |
||||
|
cookie: updateCookie, |
||||
|
}) |
||||
|
} |
||||
@ -1,19 +0,0 @@ |
|||||
const { apiKeyTable } = require("../../db/dynamoClient") |
|
||||
const env = require("../../environment") |
|
||||
|
|
||||
/** |
|
||||
* This file purely exists so that we can centralise all logic pertaining to API keys, as their usage differs |
|
||||
* in our Cloud environment versus self hosted. |
|
||||
*/ |
|
||||
|
|
||||
exports.isAPIKeyValid = async apiKeyId => { |
|
||||
if (!env.SELF_HOSTED) { |
|
||||
let apiKeyInfo = await apiKeyTable.get({ |
|
||||
primary: apiKeyId, |
|
||||
}) |
|
||||
return apiKeyInfo != null |
|
||||
} else { |
|
||||
// if the api key supplied is correct then return structure similar
|
|
||||
return apiKeyId === env.HOSTING_KEY |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue