Merge pull request #4004 from Budibase/fix/refactor-core-usage
Refactoring core library usage
This commit is contained in:
commit
816c9e2701
|
@ -0,0 +1 @@
|
|||
module.exports = require("./src/auth")
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./src/middleware")
|
|
@ -0,0 +1,4 @@
|
|||
module.exports = {
|
||||
...require("./src/objectStore"),
|
||||
...require("./src/objectStore/utils"),
|
||||
}
|
|
@ -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,
|
||||
}
|
|
@ -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"),
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
module.exports = {
|
||||
...require("./src/utils"),
|
||||
...require("./src/hashing"),
|
||||
}
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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 => {
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -9,7 +9,7 @@ const {
|
|||
deleteFolder,
|
||||
uploadDirectory,
|
||||
downloadTarball,
|
||||
} = require("@budibase/backend-core").objectStore
|
||||
} = require("@budibase/backend-core/objectStore")
|
||||
|
||||
/***********************************
|
||||
* NOTE *
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue