Merge pull request #4004 from Budibase/fix/refactor-core-usage

Refactoring core library usage
This commit is contained in:
Michael Drury 2022-01-12 13:14:40 +00:00 committed by GitHub
commit 816c9e2701
27 changed files with 143 additions and 135 deletions

View File

@ -0,0 +1 @@
module.exports = require("./src/auth")

View File

@ -0,0 +1 @@
module.exports = require("./src/middleware")

View File

@ -0,0 +1,4 @@
module.exports = {
...require("./src/objectStore"),
...require("./src/objectStore/utils"),
}

View File

@ -0,0 +1,45 @@
const passport = require("koa-passport")
const LocalStrategy = require("passport-local").Strategy
const JwtStrategy = require("passport-jwt").Strategy
const { getGlobalDB } = require("./tenancy")
const {
jwt,
local,
authenticated,
google,
oidc,
auditLog,
tenancy,
appTenancy,
authError,
} = require("./middleware")
// Strategies
passport.use(new LocalStrategy(local.options, local.authenticate))
passport.use(new JwtStrategy(jwt.options, jwt.authenticate))
passport.serializeUser((user, done) => done(null, user))
passport.deserializeUser(async (user, done) => {
const db = getGlobalDB()
try {
const user = await db.get(user._id)
return done(null, user)
} catch (err) {
console.error("User not found", err)
return done(null, false, { message: "User not found" })
}
})
module.exports = {
buildAuthMiddleware: authenticated,
passport,
google,
oidc,
jwt: require("jsonwebtoken"),
buildTenancyMiddleware: tenancy,
buildAppTenancyMiddleware: appTenancy,
auditLog,
authError,
}

View File

@ -1,71 +1,17 @@
const passport = require("koa-passport")
const LocalStrategy = require("passport-local").Strategy
const JwtStrategy = require("passport-jwt").Strategy
const { StaticDatabases } = require("./db/utils")
const { getGlobalDB } = require("./tenancy")
const {
jwt,
local,
authenticated,
google,
oidc,
auditLog,
tenancy,
appTenancy,
authError,
} = require("./middleware")
const { setDB } = require("./db") const { setDB } = require("./db")
const userCache = require("./cache/user")
// Strategies
passport.use(new LocalStrategy(local.options, local.authenticate))
passport.use(new JwtStrategy(jwt.options, jwt.authenticate))
passport.serializeUser((user, done) => done(null, user))
passport.deserializeUser(async (user, done) => {
const db = getGlobalDB()
try {
const user = await db.get(user._id)
return done(null, user)
} catch (err) {
console.error("User not found", err)
return done(null, false, { message: "User not found" })
}
})
module.exports = { module.exports = {
init(pouch) { init(pouch) {
setDB(pouch) setDB(pouch)
}, },
db: require("./db/utils"), // some default exports from the library, however these ideally shouldn't
redis: { // be used, instead the syntax require("@budibase/backend-core/db") should be used
Client: require("./redis"), StaticDatabases: require("./db/utils").StaticDatabases,
utils: require("./redis/utils"), db: require("../db"),
}, redis: require("../redis"),
objectStore: { objectStore: require("../objectStore"),
...require("./objectStore"), utils: require("../utils"),
...require("./objectStore/utils"), cache: require("../cache"),
}, auth: require("../auth"),
utils: { constants: require("../constants"),
...require("./utils"),
...require("./hashing"),
},
auth: {
buildAuthMiddleware: authenticated,
passport,
google,
oidc,
jwt: require("jsonwebtoken"),
buildTenancyMiddleware: tenancy,
buildAppTenancyMiddleware: appTenancy,
auditLog,
authError,
},
cache: {
user: userCache,
},
StaticDatabases,
constants: require("./constants"),
} }

View File

@ -0,0 +1,4 @@
module.exports = {
...require("./src/utils"),
...require("./src/hashing"),
}

View File

@ -4,7 +4,7 @@ const env = require("../../environment")
const { checkSlashesInUrl } = require("../../utilities") const { checkSlashesInUrl } = require("../../utilities")
const { request } = require("../../utilities/workerRequests") const { request } = require("../../utilities/workerRequests")
const { clearLock } = require("../../utilities/redis") const { clearLock } = require("../../utilities/redis")
const { Replication } = require("@budibase/backend-core").db const { Replication } = require("@budibase/backend-core/db")
const { DocumentTypes } = require("../../db/utils") const { DocumentTypes } = require("../../db/utils")
const { app: appCache } = require("@budibase/backend-core/cache") const { app: appCache } = require("@budibase/backend-core/cache")

View File

@ -1,7 +1,7 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const { getDeployedApps } = require("../../utilities/workerRequests") const { getDeployedApps } = require("../../utilities/workerRequests")
const { getScopedConfig } = require("@budibase/backend-core/db") const { getScopedConfig } = require("@budibase/backend-core/db")
const { Configs } = require("@budibase/backend-core").constants const { Configs } = require("@budibase/backend-core/constants")
const { checkSlashesInUrl } = require("../../utilities") const { checkSlashesInUrl } = require("../../utilities")
exports.fetchUrls = async ctx => { exports.fetchUrls = async ctx => {

View File

@ -4,7 +4,7 @@ const {
auditLog, auditLog,
buildTenancyMiddleware, buildTenancyMiddleware,
buildAppTenancyMiddleware, buildAppTenancyMiddleware,
} = require("@budibase/backend-core").auth } = require("@budibase/backend-core/auth")
const currentApp = require("../middleware/currentapp") const currentApp = require("../middleware/currentapp")
const compress = require("koa-compress") const compress = require("koa-compress")
const zlib = require("zlib") const zlib = require("zlib")

View File

@ -1,6 +1,6 @@
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
const { UserStatus } = require("@budibase/backend-core").constants const { UserStatus } = require("@budibase/backend-core/constants")
const { ObjectStoreBuckets } = require("@budibase/backend-core").objectStore const { ObjectStoreBuckets } = require("@budibase/backend-core/objectStore")
exports.JobQueues = { exports.JobQueues = {
AUTOMATIONS: "automationQueue", AUTOMATIONS: "automationQueue",

View File

@ -1,6 +1,10 @@
const { getAppId, setCookie, getCookie, clearCookie } = const {
require("@budibase/backend-core").utils getAppId,
const { Cookies } = require("@budibase/backend-core").constants setCookie,
getCookie,
clearCookie,
} = require("@budibase/backend-core/utils")
const { Cookies } = require("@budibase/backend-core/constants")
const { getRole } = require("@budibase/backend-core/roles") const { getRole } = require("@budibase/backend-core/roles")
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
const { generateUserMetadataID, isDevAppID } = require("../db/utils") const { generateUserMetadataID, isDevAppID } = require("../db/utils")

View File

@ -32,34 +32,30 @@ function mockAuthWithNoCookie() {
}, },
}, },
})) }))
jest.mock("@budibase/backend-core", () => ({ jest.mock("@budibase/backend-core/utils", () => ({
utils: { getAppId: jest.fn(),
getAppId: jest.fn(), setCookie: jest.fn(),
setCookie: jest.fn(), getCookie: jest.fn(),
getCookie: jest.fn(), }))
}, jest.mock("@budibase/backend-core/constants", () => ({
constants: { Cookies: {},
Cookies: {},
},
})) }))
} }
function mockAuthWithCookie() { function mockAuthWithCookie() {
jest.resetModules() jest.resetModules()
mockWorker() mockWorker()
jest.mock("@budibase/backend-core", () => ({ jest.mock("@budibase/backend-core/utils", () => ({
utils: { getAppId: () => {
getAppId: () => { return "app_test"
return "app_test"
},
setCookie: jest.fn(),
getCookie: () => ({appId: "app_different", roleId: "PUBLIC"}),
}, },
constants: { setCookie: jest.fn(),
Cookies: { getCookie: () => ({appId: "app_different", roleId: "PUBLIC"}),
Auth: "auth", }))
CurrentApp: "currentapp", jest.mock("@budibase/backend-core/constants", () => ({
}, Cookies: {
Auth: "auth",
CurrentApp: "currentapp",
}, },
})) }))
} }
@ -121,7 +117,7 @@ describe("Current app middleware", () => {
async function checkExpected(setCookie) { async function checkExpected(setCookie) {
config.setUser() config.setUser()
await config.executeMiddleware() await config.executeMiddleware()
const cookieFn = require("@budibase/backend-core").utils.setCookie let { setCookie: cookieFn } = require("@budibase/backend-core/utils")
if (setCookie) { if (setCookie) {
expect(cookieFn).toHaveBeenCalled() expect(cookieFn).toHaveBeenCalled()
} else { } else {
@ -140,32 +136,30 @@ describe("Current app middleware", () => {
it("should perform correct when no cookie exists", async () => { it("should perform correct when no cookie exists", async () => {
mockReset() mockReset()
jest.mock("@budibase/backend-core", () => ({ jest.mock("@budibase/backend-core/utils", () => ({
utils: { getAppId: () => {
getAppId: () => { return "app_test"
return "app_test"
},
setCookie: jest.fn(),
getCookie: jest.fn(),
},
constants: {
Cookies: {},
}, },
setCookie: jest.fn(),
getCookie: jest.fn(),
}))
jest.mock("@budibase/backend-core/constants", () => ({
Cookies: {},
})) }))
await checkExpected(true) await checkExpected(true)
}) })
it("lastly check what occurs when cookie doesn't need updated", async () => { it("lastly check what occurs when cookie doesn't need updated", async () => {
mockReset() mockReset()
jest.mock("@budibase/backend-core", () => ({ jest.mock("@budibase/backend-core/utils", () => ({
utils: { getAppId: () => {
getAppId: () => { return "app_test"
return "app_test"
},
setCookie: jest.fn(),
getCookie: () => ({appId: "app_test", roleId: "PUBLIC"}),
}, },
constants: { Cookies: {} }, setCookie: jest.fn(),
getCookie: () => ({appId: "app_test", roleId: "PUBLIC"}),
}))
jest.mock("@budibase/backend-core/constants", () => ({
Cookies: {},
})) }))
await checkExpected(false) await checkExpected(false)
}) })

View File

@ -15,8 +15,8 @@ const {
const controllers = require("./controllers") const controllers = require("./controllers")
const supertest = require("supertest") const supertest = require("supertest")
const { cleanup } = require("../../utilities/fileSystem") const { cleanup } = require("../../utilities/fileSystem")
const { Cookies, Headers } = require("@budibase/backend-core").constants const { Cookies, Headers } = require("@budibase/backend-core/constants")
const { jwt } = require("@budibase/backend-core").auth const { jwt } = require("@budibase/backend-core/auth")
const core = require("@budibase/backend-core") const core = require("@budibase/backend-core")
const { getGlobalDB } = require("@budibase/backend-core/tenancy") const { getGlobalDB } = require("@budibase/backend-core/tenancy")
const { createASession } = require("@budibase/backend-core/sessions") const { createASession } = require("@budibase/backend-core/sessions")

View File

@ -4,7 +4,7 @@ const actions = require("../automations/actions")
const automationUtils = require("../automations/automationUtils") const automationUtils = require("../automations/automationUtils")
const AutomationEmitter = require("../events/AutomationEmitter") const AutomationEmitter = require("../events/AutomationEmitter")
const { processObject } = require("@budibase/string-templates") const { processObject } = require("@budibase/string-templates")
const { DEFAULT_TENANT_ID } = require("@budibase/backend-core").constants const { DEFAULT_TENANT_ID } = require("@budibase/backend-core/constants")
const CouchDB = require("../db") const CouchDB = require("../db")
const { DocumentTypes, isDevAppID } = require("../db/utils") const { DocumentTypes, isDevAppID } = require("../db/utils")
const { doInTenant } = require("@budibase/backend-core/tenancy") const { doInTenant } = require("@budibase/backend-core/tenancy")

View File

@ -1,7 +1,7 @@
const { join } = require("./centralPath") const { join } = require("./centralPath")
const { homedir } = require("os") const { homedir } = require("os")
const env = require("../environment") const env = require("../environment")
const { budibaseTempDir } = require("@budibase/backend-core").objectStore const { budibaseTempDir } = require("@budibase/backend-core/objectStore")
module.exports.budibaseAppsDir = function () { module.exports.budibaseAppsDir = function () {
return env.BUDIBASE_DIR || join(homedir(), ".budibase") return env.BUDIBASE_DIR || join(homedir(), ".budibase")

View File

@ -9,7 +9,7 @@ const {
deleteFolder, deleteFolder,
uploadDirectory, uploadDirectory,
downloadTarball, downloadTarball,
} = require("@budibase/backend-core").objectStore } = require("@budibase/backend-core/objectStore")
/*********************************** /***********************************
* NOTE * * NOTE *

View File

@ -1,6 +1,6 @@
const env = require("../environment") const env = require("../environment")
const { OBJ_STORE_DIRECTORY } = require("../constants") const { OBJ_STORE_DIRECTORY } = require("../constants")
const { sanitizeKey } = require("@budibase/backend-core/src/objectStore") const { sanitizeKey } = require("@budibase/backend-core/objectStore")
const CouchDB = require("../db") const CouchDB = require("../db")
const { generateMetadataID } = require("../db/utils") const { generateMetadataID } = require("../db/utils")
const Readable = require("stream").Readable const Readable = require("stream").Readable

View File

@ -1,7 +1,7 @@
const core = require("@budibase/backend-core") const core = require("@budibase/backend-core")
const { getScopedConfig } = require("@budibase/backend-core/db") const { getScopedConfig } = require("@budibase/backend-core/db")
const { google } = require("@budibase/backend-core/src/middleware") const { google } = require("@budibase/backend-core/middleware")
const { oidc } = require("@budibase/backend-core/src/middleware") const { oidc } = require("@budibase/backend-core/middleware")
const { Configs, EmailTemplatePurpose } = require("../../../constants") const { Configs, EmailTemplatePurpose } = require("../../../constants")
const { sendEmail, isEmailConfigured } = require("../../../utilities/email") const { sendEmail, isEmailConfigured } = require("../../../utilities/email")
const { const {

View File

@ -7,8 +7,10 @@ const {
} = require("@budibase/backend-core/db") } = require("@budibase/backend-core/db")
const { Configs } = require("../../../constants") const { Configs } = require("../../../constants")
const email = require("../../../utilities/email") const email = require("../../../utilities/email")
const { upload, ObjectStoreBuckets } = const {
require("@budibase/backend-core").objectStore upload,
ObjectStoreBuckets,
} = require("@budibase/backend-core/objectStore")
const CouchDB = require("../../../db") const CouchDB = require("../../../db")
const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy") const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
const env = require("../../../environment") const env = require("../../../environment")

View File

@ -3,8 +3,12 @@ const {
StaticDatabases, StaticDatabases,
generateNewUsageQuotaDoc, generateNewUsageQuotaDoc,
} = require("@budibase/backend-core/db") } = require("@budibase/backend-core/db")
const { hash, getGlobalUserByEmail, saveUser, platformLogout } = const {
require("@budibase/backend-core").utils hash,
getGlobalUserByEmail,
saveUser,
platformLogout,
} = require("@budibase/backend-core/utils")
const { EmailTemplatePurpose } = require("../../../constants") const { EmailTemplatePurpose } = require("../../../constants")
const { checkInviteCode } = require("../../../utilities/redis") const { checkInviteCode } = require("../../../utilities/redis")
const { sendEmail } = require("../../../utilities/email") const { sendEmail } = require("../../../utilities/email")

View File

@ -2,8 +2,11 @@ const Router = require("@koa/router")
const compress = require("koa-compress") const compress = require("koa-compress")
const zlib = require("zlib") const zlib = require("zlib")
const { routes } = require("./routes") const { routes } = require("./routes")
const { buildAuthMiddleware, auditLog, buildTenancyMiddleware } = const {
require("@budibase/backend-core").auth buildAuthMiddleware,
auditLog,
buildTenancyMiddleware,
} = require("@budibase/backend-core/auth")
const PUBLIC_ENDPOINTS = [ const PUBLIC_ENDPOINTS = [
// old deprecated endpoints kept for backwards compat // old deprecated endpoints kept for backwards compat

View File

@ -54,7 +54,7 @@ describe("/api/global/auth", () => {
}) })
describe("oidc", () => { describe("oidc", () => {
const auth = require("@budibase/backend-core").auth const auth = require("@budibase/backend-core/auth")
// mock the oidc strategy implementation and return value // mock the oidc strategy implementation and return value
strategyFactory = jest.fn() strategyFactory = jest.fn()
@ -104,4 +104,4 @@ describe("/api/global/auth", () => {
}) })
}) })
}) })

View File

@ -1,10 +1,10 @@
const env = require("../../../../environment") const env = require("../../../../environment")
const controllers = require("./controllers") const controllers = require("./controllers")
const supertest = require("supertest") const supertest = require("supertest")
const { jwt } = require("@budibase/backend-core").auth const { jwt } = require("@budibase/backend-core/auth")
const { Cookies } = require("@budibase/backend-core").constants const { Cookies } = require("@budibase/backend-core/constants")
const { Configs, LOGO_URL } = require("../../../../constants") const { Configs, LOGO_URL } = require("../../../../constants")
const { getGlobalUserByEmail } = require("@budibase/backend-core").utils const { getGlobalUserByEmail } = require("@budibase/backend-core/utils")
const { createASession } = require("@budibase/backend-core/sessions") const { createASession } = require("@budibase/backend-core/sessions")
const { newid } = require("@budibase/backend-core/src/hashing") const { newid } = require("@budibase/backend-core/src/hashing")
const { TENANT_ID } = require("./structures") const { TENANT_ID } = require("./structures")

View File

@ -1,4 +1,4 @@
const { Configs } = require("@budibase/backend-core").constants const { Configs } = require("@budibase/backend-core/constants")
exports.LOGO_URL = exports.LOGO_URL =
"https://d33wubrfki0l68.cloudfront.net/aac32159d7207b5085e74a7ef67afbb7027786c5/2b1fd/img/logo/bb-emblem.svg" "https://d33wubrfki0l68.cloudfront.net/aac32159d7207b5085e74a7ef67afbb7027786c5/2b1fd/img/logo/bb-emblem.svg"

View File

@ -6,7 +6,7 @@ const Koa = require("koa")
const destroyable = require("server-destroy") const destroyable = require("server-destroy")
const koaBody = require("koa-body") const koaBody = require("koa-body")
const koaSession = require("koa-session") const koaSession = require("koa-session")
const { passport } = require("@budibase/backend-core").auth const { passport } = require("@budibase/backend-core/auth")
const logger = require("koa-pino-logger") const logger = require("koa-pino-logger")
const http = require("http") const http = require("http")
const api = require("./api") const api = require("./api")

View File

@ -1,5 +1,5 @@
const env = require("../environment") const env = require("../environment")
const { Headers } = require("@budibase/backend-core").constants const { Headers } = require("@budibase/backend-core/constants")
/** /**
* This is a restricted endpoint in the cloud. * This is a restricted endpoint in the cloud.

View File

@ -1,5 +1,5 @@
const { Client, utils } = require("@budibase/backend-core/redis") const { Client, utils } = require("@budibase/backend-core/redis")
const { newid } = require("@budibase/backend-core").utils const { newid } = require("@budibase/backend-core/utils")
function getExpirySecondsForDB(db) { function getExpirySecondsForDB(db) {
switch (db) { switch (db) {