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 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 = {
init(pouch) {
setDB(pouch)
},
db: require("./db/utils"),
redis: {
Client: require("./redis"),
utils: require("./redis/utils"),
},
objectStore: {
...require("./objectStore"),
...require("./objectStore/utils"),
},
utils: {
...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"),
// some default exports from the library, however these ideally shouldn't
// be used, instead the syntax require("@budibase/backend-core/db") should be used
StaticDatabases: require("./db/utils").StaticDatabases,
db: require("../db"),
redis: require("../redis"),
objectStore: require("../objectStore"),
utils: require("../utils"),
cache: require("../cache"),
auth: require("../auth"),
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 { request } = require("../../utilities/workerRequests")
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 { app: appCache } = require("@budibase/backend-core/cache")

View File

@ -1,7 +1,7 @@
const CouchDB = require("../../db")
const { getDeployedApps } = require("../../utilities/workerRequests")
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")
exports.fetchUrls = async ctx => {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ const actions = require("../automations/actions")
const automationUtils = require("../automations/automationUtils")
const AutomationEmitter = require("../events/AutomationEmitter")
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 { DocumentTypes, isDevAppID } = require("../db/utils")
const { doInTenant } = require("@budibase/backend-core/tenancy")

View File

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

View File

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

View File

@ -1,6 +1,6 @@
const env = require("../environment")
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 { generateMetadataID } = require("../db/utils")
const Readable = require("stream").Readable

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,10 +1,10 @@
const env = require("../../../../environment")
const controllers = require("./controllers")
const supertest = require("supertest")
const { jwt } = require("@budibase/backend-core").auth
const { Cookies } = require("@budibase/backend-core").constants
const { jwt } = require("@budibase/backend-core/auth")
const { Cookies } = require("@budibase/backend-core/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 { newid } = require("@budibase/backend-core/src/hashing")
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 =
"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 koaBody = require("koa-body")
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 http = require("http")
const api = require("./api")

View File

@ -1,5 +1,5 @@
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.

View File

@ -1,5 +1,5 @@
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) {
switch (db) {