@budibase/worker 2.1.32-alpha.8 → 2.1.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/package.json +23 -13
  2. package/{src/tests/jestSetup.ts → scripts/jestSetup.js} +3 -12
  3. package/scripts/localdomain.js +7 -33
  4. package/src/api/controllers/global/auth.ts +31 -35
  5. package/src/api/controllers/global/configs.js +17 -17
  6. package/src/api/controllers/global/self.ts +2 -2
  7. package/src/api/controllers/global/users.ts +1 -3
  8. package/src/api/controllers/system/{environment.ts → environment.js} +2 -3
  9. package/src/api/controllers/system/{status.ts → status.js} +3 -4
  10. package/src/api/controllers/system/tenants.ts +51 -8
  11. package/src/api/index.ts +6 -29
  12. package/src/api/routes/global/auth.js +39 -22
  13. package/src/api/routes/global/configs.js +9 -9
  14. package/src/api/routes/global/templates.ts +2 -2
  15. package/src/api/routes/global/tests/auth.spec.ts +64 -134
  16. package/src/api/routes/global/tests/configs.spec.ts +35 -34
  17. package/src/api/routes/global/tests/email.spec.ts +3 -4
  18. package/src/api/routes/global/tests/realEmail.spec.ts +3 -2
  19. package/src/api/routes/global/tests/self.spec.ts +4 -3
  20. package/src/api/routes/global/tests/users.spec.ts +43 -51
  21. package/src/api/routes/system/environment.js +8 -0
  22. package/src/api/routes/system/status.js +8 -0
  23. package/src/api/routes/system/tenants.js +12 -0
  24. package/src/api/routes/system/tests/accounts.spec.ts +6 -5
  25. package/src/constants/{index.ts → index.js} +64 -57
  26. package/src/constants/templates/index.js +2 -2
  27. package/src/db/index.js +11 -0
  28. package/src/index.ts +1 -1
  29. package/src/middleware/cloudRestricted.js +2 -2
  30. package/src/tests/TestConfiguration.ts +49 -98
  31. package/src/tests/api/accounts.ts +8 -5
  32. package/src/tests/api/auth.ts +6 -3
  33. package/src/tests/api/configs.ts +8 -15
  34. package/src/tests/api/email.ts +6 -3
  35. package/src/tests/api/index.ts +0 -15
  36. package/src/tests/api/self.ts +6 -11
  37. package/src/tests/api/users.ts +6 -38
  38. package/src/tests/index.ts +1 -5
  39. package/src/tests/structures/accounts.ts +24 -0
  40. package/src/tests/structures/configs.js +6 -6
  41. package/src/tests/structures/index.ts +2 -4
  42. package/src/tests/structures/users.ts +0 -1
  43. package/src/utilities/appService.js +3 -3
  44. package/src/utilities/{email.ts → email.js} +45 -78
  45. package/src/utilities/{redis.ts → redis.js} +24 -29
  46. package/src/utilities/templates.js +49 -0
  47. package/tsconfig.build.json +2 -2
  48. package/tsconfig.json +1 -3
  49. package/__mocks__/aws-sdk.ts +0 -15
  50. package/__mocks__/oauth.ts +0 -57
  51. package/coverage/clover.xml +0 -2160
  52. package/coverage/coverage-final.json +0 -78
  53. package/coverage/lcov-report/base.css +0 -224
  54. package/coverage/lcov-report/block-navigation.js +0 -87
  55. package/coverage/lcov-report/favicon.png +0 -0
  56. package/coverage/lcov-report/index.html +0 -431
  57. package/coverage/lcov-report/prettify.css +0 -1
  58. package/coverage/lcov-report/prettify.js +0 -2
  59. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  60. package/coverage/lcov-report/sorter.js +0 -196
  61. package/coverage/lcov-report/src/api/controllers/global/auth.ts.html +0 -913
  62. package/coverage/lcov-report/src/api/controllers/global/configs.js.html +0 -1390
  63. package/coverage/lcov-report/src/api/controllers/global/email.js.html +0 -193
  64. package/coverage/lcov-report/src/api/controllers/global/index.html +0 -236
  65. package/coverage/lcov-report/src/api/controllers/global/license.ts.html +0 -187
  66. package/coverage/lcov-report/src/api/controllers/global/roles.js.html +0 -289
  67. package/coverage/lcov-report/src/api/controllers/global/self.ts.html +0 -577
  68. package/coverage/lcov-report/src/api/controllers/global/templates.ts.html +0 -316
  69. package/coverage/lcov-report/src/api/controllers/global/users.ts.html +0 -796
  70. package/coverage/lcov-report/src/api/controllers/global/workspaces.js.html +0 -253
  71. package/coverage/lcov-report/src/api/controllers/system/accounts.ts.html +0 -148
  72. package/coverage/lcov-report/src/api/controllers/system/environment.ts.html +0 -124
  73. package/coverage/lcov-report/src/api/controllers/system/index.html +0 -191
  74. package/coverage/lcov-report/src/api/controllers/system/migrations.ts.html +0 -124
  75. package/coverage/lcov-report/src/api/controllers/system/restore.ts.html +0 -124
  76. package/coverage/lcov-report/src/api/controllers/system/status.ts.html +0 -133
  77. package/coverage/lcov-report/src/api/controllers/system/tenants.ts.html +0 -154
  78. package/coverage/lcov-report/src/api/index.html +0 -116
  79. package/coverage/lcov-report/src/api/index.ts.html +0 -583
  80. package/coverage/lcov-report/src/api/routes/global/auth.js.html +0 -349
  81. package/coverage/lcov-report/src/api/routes/global/configs.js.html +0 -460
  82. package/coverage/lcov-report/src/api/routes/global/email.js.html +0 -196
  83. package/coverage/lcov-report/src/api/routes/global/index.html +0 -236
  84. package/coverage/lcov-report/src/api/routes/global/license.ts.html +0 -124
  85. package/coverage/lcov-report/src/api/routes/global/roles.js.html +0 -121
  86. package/coverage/lcov-report/src/api/routes/global/self.ts.html +0 -139
  87. package/coverage/lcov-report/src/api/routes/global/templates.ts.html +0 -196
  88. package/coverage/lcov-report/src/api/routes/global/users.js.html +0 -403
  89. package/coverage/lcov-report/src/api/routes/global/workspaces.js.html +0 -199
  90. package/coverage/lcov-report/src/api/routes/index.html +0 -116
  91. package/coverage/lcov-report/src/api/routes/index.ts.html +0 -193
  92. package/coverage/lcov-report/src/api/routes/system/accounts.ts.html +0 -142
  93. package/coverage/lcov-report/src/api/routes/system/environment.ts.html +0 -109
  94. package/coverage/lcov-report/src/api/routes/system/index.html +0 -191
  95. package/coverage/lcov-report/src/api/routes/system/migrations.ts.html +0 -142
  96. package/coverage/lcov-report/src/api/routes/system/restore.ts.html +0 -109
  97. package/coverage/lcov-report/src/api/routes/system/status.ts.html +0 -109
  98. package/coverage/lcov-report/src/api/routes/system/tenants.ts.html +0 -124
  99. package/coverage/lcov-report/src/api/routes/validation/index.html +0 -131
  100. package/coverage/lcov-report/src/api/routes/validation/index.ts.html +0 -88
  101. package/coverage/lcov-report/src/api/routes/validation/users.ts.html +0 -235
  102. package/coverage/lcov-report/src/constants/index.html +0 -116
  103. package/coverage/lcov-report/src/constants/index.ts.html +0 -613
  104. package/coverage/lcov-report/src/constants/templates/index.html +0 -116
  105. package/coverage/lcov-report/src/constants/templates/index.js.html +0 -304
  106. package/coverage/lcov-report/src/db/index.html +0 -116
  107. package/coverage/lcov-report/src/db/index.ts.html +0 -115
  108. package/coverage/lcov-report/src/environment.ts.html +0 -364
  109. package/coverage/lcov-report/src/index.html +0 -131
  110. package/coverage/lcov-report/src/index.ts.html +0 -397
  111. package/coverage/lcov-report/src/middleware/cloudRestricted.js.html +0 -136
  112. package/coverage/lcov-report/src/middleware/index.html +0 -116
  113. package/coverage/lcov-report/src/migrations/functions/globalInfoSyncUsers.ts.html +0 -145
  114. package/coverage/lcov-report/src/migrations/functions/index.html +0 -116
  115. package/coverage/lcov-report/src/migrations/index.html +0 -116
  116. package/coverage/lcov-report/src/migrations/index.ts.html +0 -271
  117. package/coverage/lcov-report/src/sdk/accounts/accounts.ts.html +0 -262
  118. package/coverage/lcov-report/src/sdk/accounts/index.html +0 -131
  119. package/coverage/lcov-report/src/sdk/accounts/index.ts.html +0 -88
  120. package/coverage/lcov-report/src/sdk/index.html +0 -116
  121. package/coverage/lcov-report/src/sdk/index.ts.html +0 -106
  122. package/coverage/lcov-report/src/sdk/users/events.ts.html +0 -577
  123. package/coverage/lcov-report/src/sdk/users/index.html +0 -146
  124. package/coverage/lcov-report/src/sdk/users/index.ts.html +0 -88
  125. package/coverage/lcov-report/src/sdk/users/users.ts.html +0 -1903
  126. package/coverage/lcov-report/src/tests/TestConfiguration.ts.html +0 -1051
  127. package/coverage/lcov-report/src/tests/api/accounts.ts.html +0 -160
  128. package/coverage/lcov-report/src/tests/api/auth.ts.html +0 -220
  129. package/coverage/lcov-report/src/tests/api/base.ts.html +0 -133
  130. package/coverage/lcov-report/src/tests/api/configs.ts.html +0 -226
  131. package/coverage/lcov-report/src/tests/api/email.ts.html +0 -148
  132. package/coverage/lcov-report/src/tests/api/environment.ts.html +0 -130
  133. package/coverage/lcov-report/src/tests/api/index.html +0 -296
  134. package/coverage/lcov-report/src/tests/api/index.ts.html +0 -205
  135. package/coverage/lcov-report/src/tests/api/migrations.ts.html +0 -151
  136. package/coverage/lcov-report/src/tests/api/restore.ts.html +0 -127
  137. package/coverage/lcov-report/src/tests/api/self.ts.html +0 -163
  138. package/coverage/lcov-report/src/tests/api/status.ts.html +0 -121
  139. package/coverage/lcov-report/src/tests/api/tenants.ts.html +0 -130
  140. package/coverage/lcov-report/src/tests/api/users.ts.html +0 -511
  141. package/coverage/lcov-report/src/tests/controllers.js.html +0 -106
  142. package/coverage/lcov-report/src/tests/index.html +0 -146
  143. package/coverage/lcov-report/src/tests/index.ts.html +0 -139
  144. package/coverage/lcov-report/src/tests/mocks/email.js.html +0 -115
  145. package/coverage/lcov-report/src/tests/mocks/index.html +0 -131
  146. package/coverage/lcov-report/src/tests/mocks/index.ts.html +0 -106
  147. package/coverage/lcov-report/src/tests/structures/configs.js.html +0 -313
  148. package/coverage/lcov-report/src/tests/structures/groups.ts.html +0 -118
  149. package/coverage/lcov-report/src/tests/structures/index.html +0 -161
  150. package/coverage/lcov-report/src/tests/structures/index.ts.html +0 -151
  151. package/coverage/lcov-report/src/tests/structures/users.ts.html +0 -196
  152. package/coverage/lcov-report/src/utilities/appService.js.html +0 -184
  153. package/coverage/lcov-report/src/utilities/email.ts.html +0 -850
  154. package/coverage/lcov-report/src/utilities/fileSystem.js.html +0 -100
  155. package/coverage/lcov-report/src/utilities/index.html +0 -206
  156. package/coverage/lcov-report/src/utilities/index.js.html +0 -112
  157. package/coverage/lcov-report/src/utilities/redis.ts.html +0 -424
  158. package/coverage/lcov-report/src/utilities/templates.ts.html +0 -232
  159. package/coverage/lcov-report/src/utilities/users.js.html +0 -136
  160. package/coverage/lcov.info +0 -3986
  161. package/jest.config.ts +0 -29
  162. package/src/api/routes/global/tests/license.spec.ts +0 -31
  163. package/src/api/routes/global/tests/roles.spec.ts +0 -27
  164. package/src/api/routes/global/tests/templates.spec.ts +0 -35
  165. package/src/api/routes/global/tests/workspaces.spec.ts +0 -29
  166. package/src/api/routes/system/environment.ts +0 -8
  167. package/src/api/routes/system/status.ts +0 -8
  168. package/src/api/routes/system/tenants.ts +0 -13
  169. package/src/api/routes/system/tests/environment.spec.ts +0 -29
  170. package/src/api/routes/system/tests/migrations.spec.ts +0 -63
  171. package/src/api/routes/system/tests/restore.spec.ts +0 -36
  172. package/src/api/routes/system/tests/status.spec.ts +0 -48
  173. package/src/api/routes/system/tests/tenants.spec.ts +0 -61
  174. package/src/db/index.ts +0 -10
  175. package/src/middleware/tests/tenancy.spec.ts +0 -73
  176. package/src/tests/api/base.ts +0 -16
  177. package/src/tests/api/environment.ts +0 -15
  178. package/src/tests/api/migrations.ts +0 -22
  179. package/src/tests/api/restore.ts +0 -14
  180. package/src/tests/api/status.ts +0 -12
  181. package/src/tests/api/tenants.ts +0 -15
  182. package/src/utilities/templates.ts +0 -49
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/worker",
3
3
  "email": "hi@budibase.com",
4
- "version": "2.1.32-alpha.8",
4
+ "version": "2.1.32",
5
5
  "description": "Budibase background service",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "build:docker": "docker build . -t worker-service --label version=$BUDIBASE_RELEASE_VERSION",
23
23
  "dev:stack:init": "node ./scripts/dev/manage.js init",
24
24
  "dev:builder": "npm run dev:stack:init && nodemon",
25
- "test": "jest --coverage --runInBand",
25
+ "test": "jest --runInBand",
26
26
  "test:watch": "jest --watch",
27
27
  "env:multi:enable": "node scripts/multiTenancy.js enable",
28
28
  "env:multi:disable": "node scripts/multiTenancy.js disable",
@@ -36,10 +36,10 @@
36
36
  "author": "Budibase",
37
37
  "license": "GPL-3.0",
38
38
  "dependencies": {
39
- "@budibase/backend-core": "2.1.32-alpha.8",
40
- "@budibase/pro": "2.1.32-alpha.7",
41
- "@budibase/string-templates": "2.1.32-alpha.8",
42
- "@budibase/types": "2.1.32-alpha.8",
39
+ "@budibase/backend-core": "^2.1.32",
40
+ "@budibase/pro": "2.1.31",
41
+ "@budibase/string-templates": "^2.1.32",
42
+ "@budibase/types": "^2.1.32",
43
43
  "@koa/router": "8.0.8",
44
44
  "@sentry/node": "6.17.7",
45
45
  "@techpass/passport-openidconnect": "0.3.2",
@@ -71,28 +71,38 @@
71
71
  },
72
72
  "devDependencies": {
73
73
  "@types/jest": "26.0.23",
74
- "@types/jsonwebtoken": "8.5.1",
75
74
  "@types/koa": "2.13.4",
76
75
  "@types/koa__router": "8.0.11",
77
76
  "@types/node": "14.18.20",
78
- "@types/node-fetch": "2.6.1",
79
77
  "@types/pouchdb": "6.4.0",
80
78
  "@types/uuid": "8.3.4",
81
79
  "@typescript-eslint/parser": "5.12.0",
82
80
  "copyfiles": "2.4.1",
83
81
  "eslint": "6.8.0",
84
- "jest": "28.1.1",
82
+ "jest": "27.4.7",
85
83
  "nodemon": "2.0.15",
86
84
  "pouchdb-adapter-memory": "7.2.2",
87
85
  "prettier": "2.3.1",
88
86
  "rimraf": "3.0.2",
89
87
  "supertest": "6.2.2",
90
88
  "timekeeper": "2.2.0",
91
- "ts-jest": "28.0.4",
92
- "ts-node": "10.8.1",
89
+ "ts-jest": "27.1.3",
90
+ "ts-node": "10.4.0",
93
91
  "tsconfig-paths": "4.0.0",
94
- "typescript": "4.7.3",
92
+ "typescript": "4.5.5",
95
93
  "update-dotenv": "1.1.1"
96
94
  },
97
- "gitHead": "55efd024211da711baad385135fb2a802bbe0472"
95
+ "jest": {
96
+ "preset": "ts-jest",
97
+ "testEnvironment": "node",
98
+ "moduleNameMapper": {
99
+ "@budibase/backend-core/(.*)": "<rootDir>/../backend-core/$1",
100
+ "@budibase/backend-core": "<rootDir>/../backend-core/src",
101
+ "@budibase/types": "<rootDir>/../types/src"
102
+ },
103
+ "setupFiles": [
104
+ "./scripts/jestSetup.js"
105
+ ]
106
+ },
107
+ "gitHead": "e2edbced82903f7225429fe2a9d770de073356f3"
98
108
  }
@@ -1,21 +1,12 @@
1
- import env from "../environment"
1
+ const env = require("../src/environment")
2
2
 
3
- env._set("SELF_HOSTED", "0")
3
+ env._set("SELF_HOSTED", "1")
4
4
  env._set("NODE_ENV", "jest")
5
5
  env._set("JWT_SECRET", "test-jwtsecret")
6
6
  env._set("LOG_LEVEL", "silent")
7
7
  env._set("MULTI_TENANCY", true)
8
- env._set("MINIO_URL", "http://localhost")
9
- env._set("MINIO_ACCESS_KEY", "test")
10
- env._set("MINIO_SECRET_KEY", "test")
11
- env._set("PLATFORM_URL", "http://localhost:10000")
12
- env._set("INTERNAL_API_KEY", "test")
13
- env._set("DISABLE_ACCOUNT_PORTAL", false)
14
8
 
15
- import { mocks } from "@budibase/backend-core/tests"
16
-
17
- // must explicitly enable fetch mock
18
- mocks.fetch.enable()
9
+ const { mocks } = require("@budibase/backend-core/tests")
19
10
 
20
11
  // mock all dates to 2020-01-01T00:00:00.000Z
21
12
  // use tk.reset() to use real dates in individual tests
@@ -2,36 +2,6 @@
2
2
  const updateDotEnv = require("update-dotenv")
3
3
 
4
4
  const arg = process.argv.slice(2)[0]
5
- const isEnable = arg === "enable"
6
-
7
- let domain = process.argv.slice(2)[1]
8
- if (!domain) {
9
- domain = "local.com"
10
- }
11
-
12
- const getAccountPortalUrl = () => {
13
- if (isEnable) {
14
- return `http://account.${domain}:10001`
15
- } else {
16
- return `http://localhost:10001`
17
- }
18
- }
19
-
20
- const getBudibaseUrl = () => {
21
- if (isEnable) {
22
- return `http://${domain}:10000`
23
- } else {
24
- return `http://localhost:10000`
25
- }
26
- }
27
-
28
- const getCookieDomain = () => {
29
- if (isEnable) {
30
- return `.${domain}`
31
- } else {
32
- return ""
33
- }
34
- }
35
5
 
36
6
  /**
37
7
  * For testing multi tenancy sub domains locally.
@@ -46,7 +16,11 @@ const getCookieDomain = () => {
46
16
  * 127.0.0.1 t2.local.com
47
17
  */
48
18
  updateDotEnv({
49
- ACCOUNT_PORTAL_URL: getAccountPortalUrl(),
50
- COOKIE_DOMAIN: getCookieDomain(),
51
- PLATFORM_URL: getBudibaseUrl(),
19
+ ACCOUNT_PORTAL_URL:
20
+ arg === "enable"
21
+ ? "http://account.local.com:10001"
22
+ : "http://localhost:10001",
23
+ COOKIE_DOMAIN: arg === "enable" ? ".local.com" : "",
24
+ PLATFORM_URL:
25
+ arg === "enable" ? "http://local.com:10000" : "http://localhost:10000",
52
26
  }).then(() => console.log("Updated worker!"))
@@ -1,26 +1,22 @@
1
- import core from "@budibase/backend-core"
2
- import {
3
- events,
4
- users as usersCore,
5
- context,
6
- tenancy,
7
- } from "@budibase/backend-core"
8
- import { Config, EmailTemplatePurpose } from "../../../constants"
9
- import { sendEmail, isEmailConfigured } from "../../../utilities/email"
10
- import { checkResetPasswordCode } from "../../../utilities/redis"
11
- import env from "../../../environment"
12
- import sdk from "../../../sdk"
13
- import { User } from "@budibase/types"
1
+ const core = require("@budibase/backend-core")
2
+ const { Configs, EmailTemplatePurpose } = require("../../../constants")
3
+ const { sendEmail, isEmailConfigured } = require("../../../utilities/email")
14
4
  const { setCookie, getCookie, clearCookie, hash, platformLogout } = core.utils
15
- const { Cookie, Header } = core.constants
5
+ const { Cookies, Headers } = core.constants
16
6
  const { passport, ssoCallbackUrl, google, oidc } = core.auth
7
+ const { checkResetPasswordCode } = require("../../../utilities/redis")
8
+ const { getGlobalDB } = require("@budibase/backend-core/tenancy")
9
+ const env = require("../../../environment")
10
+ import { events, users as usersCore, context } from "@budibase/backend-core"
11
+ import sdk from "../../../sdk"
12
+ import { User } from "@budibase/types"
17
13
 
18
14
  export const googleCallbackUrl = async (config: any) => {
19
- return ssoCallbackUrl(tenancy.getGlobalDB(), config, "google")
15
+ return ssoCallbackUrl(getGlobalDB(), config, "google")
20
16
  }
21
17
 
22
18
  export const oidcCallbackUrl = async (config: any) => {
23
- return ssoCallbackUrl(tenancy.getGlobalDB(), config, "oidc")
19
+ return ssoCallbackUrl(getGlobalDB(), config, "oidc")
24
20
  }
25
21
 
26
22
  async function authInternal(ctx: any, user: any, err = null, info = null) {
@@ -34,13 +30,13 @@ async function authInternal(ctx: any, user: any, err = null, info = null) {
34
30
  }
35
31
 
36
32
  // set a cookie for browser access
37
- setCookie(ctx, user.token, Cookie.Auth, { sign: false })
33
+ setCookie(ctx, user.token, Cookies.Auth, { sign: false })
38
34
  // set the token in a header as well for APIs
39
- ctx.set(Header.TOKEN, user.token)
35
+ ctx.set(Headers.TOKEN, user.token)
40
36
  // get rid of any app cookies on login
41
37
  // have to check test because this breaks cypress
42
38
  if (!env.isTest()) {
43
- clearCookie(ctx, Cookie.CurrentApp)
39
+ clearCookie(ctx, Cookies.CurrentApp)
44
40
  }
45
41
  }
46
42
 
@@ -59,15 +55,15 @@ export const authenticate = async (ctx: any, next: any) => {
59
55
 
60
56
  export const setInitInfo = (ctx: any) => {
61
57
  const initInfo = ctx.request.body
62
- setCookie(ctx, initInfo, Cookie.Init)
58
+ setCookie(ctx, initInfo, Cookies.Init)
63
59
  ctx.status = 200
64
60
  }
65
61
 
66
62
  export const getInitInfo = (ctx: any) => {
67
63
  try {
68
- ctx.body = getCookie(ctx, Cookie.Init) || {}
64
+ ctx.body = getCookie(ctx, Cookies.Init) || {}
69
65
  } catch (err) {
70
- clearCookie(ctx, Cookie.Init)
66
+ clearCookie(ctx, Cookies.Init)
71
67
  ctx.body = {}
72
68
  }
73
69
  }
@@ -110,7 +106,7 @@ export const resetUpdate = async (ctx: any) => {
110
106
  const { resetCode, password } = ctx.request.body
111
107
  try {
112
108
  const { userId } = await checkResetPasswordCode(resetCode)
113
- const db = tenancy.getGlobalDB()
109
+ const db = getGlobalDB()
114
110
  const user = await db.get(userId)
115
111
  user.password = await hash(password)
116
112
  await db.put(user)
@@ -145,14 +141,14 @@ export const datasourcePreAuth = async (ctx: any, next: any) => {
145
141
  appId: ctx.query.appId,
146
142
  datasourceId: ctx.query.datasourceId,
147
143
  },
148
- Cookie.DatasourceAuth
144
+ Cookies.DatasourceAuth
149
145
  )
150
146
 
151
147
  return handler.preAuth(passport, ctx, next)
152
148
  }
153
149
 
154
150
  export const datasourceAuth = async (ctx: any, next: any) => {
155
- const authStateCookie = getCookie(ctx, Cookie.DatasourceAuth)
151
+ const authStateCookie = getCookie(ctx, Cookies.DatasourceAuth)
156
152
  const provider = authStateCookie.provider
157
153
  const middleware = require(`@budibase/backend-core/middleware`)
158
154
  const handler = middleware.datasource[provider]
@@ -164,10 +160,10 @@ export const datasourceAuth = async (ctx: any, next: any) => {
164
160
  * On a successful login, you will be redirected to the googleAuth callback route.
165
161
  */
166
162
  export const googlePreAuth = async (ctx: any, next: any) => {
167
- const db = tenancy.getGlobalDB()
163
+ const db = getGlobalDB()
168
164
 
169
165
  const config = await core.db.getScopedConfig(db, {
170
- type: Config.GOOGLE,
166
+ type: Configs.GOOGLE,
171
167
  workspace: ctx.query.workspace,
172
168
  })
173
169
  let callbackUrl = await exports.googleCallbackUrl(config)
@@ -185,10 +181,10 @@ export const googlePreAuth = async (ctx: any, next: any) => {
185
181
  }
186
182
 
187
183
  export const googleAuth = async (ctx: any, next: any) => {
188
- const db = tenancy.getGlobalDB()
184
+ const db = getGlobalDB()
189
185
 
190
186
  const config = await core.db.getScopedConfig(db, {
191
- type: Config.GOOGLE,
187
+ type: Configs.GOOGLE,
192
188
  workspace: ctx.query.workspace,
193
189
  })
194
190
  const callbackUrl = await exports.googleCallbackUrl(config)
@@ -212,9 +208,9 @@ export const googleAuth = async (ctx: any, next: any) => {
212
208
  }
213
209
 
214
210
  export const oidcStrategyFactory = async (ctx: any, configId: any) => {
215
- const db = tenancy.getGlobalDB()
211
+ const db = getGlobalDB()
216
212
  const config = await core.db.getScopedConfig(db, {
217
- type: Config.OIDC,
213
+ type: Configs.OIDC,
218
214
  group: ctx.query.group,
219
215
  })
220
216
 
@@ -237,11 +233,11 @@ export const oidcPreAuth = async (ctx: any, next: any) => {
237
233
  const { configId } = ctx.params
238
234
  const strategy = await oidcStrategyFactory(ctx, configId)
239
235
 
240
- setCookie(ctx, configId, Cookie.OIDC_CONFIG)
236
+ setCookie(ctx, configId, Cookies.OIDC_CONFIG)
241
237
 
242
- const db = tenancy.getGlobalDB()
238
+ const db = getGlobalDB()
243
239
  const config = await core.db.getScopedConfig(db, {
244
- type: Config.OIDC,
240
+ type: Configs.OIDC,
245
241
  group: ctx.query.group,
246
242
  })
247
243
 
@@ -259,7 +255,7 @@ export const oidcPreAuth = async (ctx: any, next: any) => {
259
255
  }
260
256
 
261
257
  export const oidcAuth = async (ctx: any, next: any) => {
262
- const configId = getCookie(ctx, Cookie.OIDC_CONFIG)
258
+ const configId = getCookie(ctx, Cookies.OIDC_CONFIG)
263
259
  const strategy = await oidcStrategyFactory(ctx, configId)
264
260
 
265
261
  return passport.authenticate(
@@ -4,7 +4,7 @@ const {
4
4
  getScopedFullConfig,
5
5
  getAllApps,
6
6
  } = require("@budibase/backend-core/db")
7
- const { Config } = require("../../../constants")
7
+ const { Configs } = require("../../../constants")
8
8
  const email = require("../../../utilities/email")
9
9
  const {
10
10
  upload,
@@ -33,25 +33,25 @@ const getEventFns = async (db, config) => {
33
33
 
34
34
  if (!existing) {
35
35
  switch (config.type) {
36
- case Config.SMTP: {
36
+ case Configs.SMTP: {
37
37
  fns.push(events.email.SMTPCreated)
38
38
  break
39
39
  }
40
- case Config.GOOGLE: {
40
+ case Configs.GOOGLE: {
41
41
  fns.push(() => events.auth.SSOCreated(type))
42
42
  if (config.config.activated) {
43
43
  fns.push(() => events.auth.SSOActivated(type))
44
44
  }
45
45
  break
46
46
  }
47
- case Config.OIDC: {
47
+ case Configs.OIDC: {
48
48
  fns.push(() => events.auth.SSOCreated(type))
49
49
  if (config.config.configs[0].activated) {
50
50
  fns.push(() => events.auth.SSOActivated(type))
51
51
  }
52
52
  break
53
53
  }
54
- case Config.SETTINGS: {
54
+ case Configs.SETTINGS: {
55
55
  // company
56
56
  const company = config.config.company
57
57
  if (company && company !== "Budibase") {
@@ -78,11 +78,11 @@ const getEventFns = async (db, config) => {
78
78
  }
79
79
  } else {
80
80
  switch (config.type) {
81
- case Config.SMTP: {
81
+ case Configs.SMTP: {
82
82
  fns.push(events.email.SMTPUpdated)
83
83
  break
84
84
  }
85
- case Config.GOOGLE: {
85
+ case Configs.GOOGLE: {
86
86
  fns.push(() => events.auth.SSOUpdated(type))
87
87
  if (!existing.config.activated && config.config.activated) {
88
88
  fns.push(() => events.auth.SSOActivated(type))
@@ -91,7 +91,7 @@ const getEventFns = async (db, config) => {
91
91
  }
92
92
  break
93
93
  }
94
- case Config.OIDC: {
94
+ case Configs.OIDC: {
95
95
  fns.push(() => events.auth.SSOUpdated(type))
96
96
  if (
97
97
  !existing.config.configs[0].activated &&
@@ -106,7 +106,7 @@ const getEventFns = async (db, config) => {
106
106
  }
107
107
  break
108
108
  }
109
- case Config.SETTINGS: {
109
+ case Configs.SETTINGS: {
110
110
  // company
111
111
  const existingCompany = existing.config.company
112
112
  const company = config.config.company
@@ -155,7 +155,7 @@ exports.save = async function (ctx) {
155
155
  try {
156
156
  // verify the configuration
157
157
  switch (type) {
158
- case Config.SMTP:
158
+ case Configs.SMTP:
159
159
  await email.verifyConfig(config)
160
160
  break
161
161
  }
@@ -237,7 +237,7 @@ exports.publicOidc = async function (ctx) {
237
237
  try {
238
238
  // Find the config with the most granular scope based on context
239
239
  const oidcConfig = await getScopedFullConfig(db, {
240
- type: Config.OIDC,
240
+ type: Configs.OIDC,
241
241
  })
242
242
 
243
243
  if (!oidcConfig) {
@@ -260,15 +260,15 @@ exports.publicSettings = async function (ctx) {
260
260
  try {
261
261
  // Find the config with the most granular scope based on context
262
262
  const publicConfig = await getScopedFullConfig(db, {
263
- type: Config.SETTINGS,
263
+ type: Configs.SETTINGS,
264
264
  })
265
265
 
266
266
  const googleConfig = await getScopedFullConfig(db, {
267
- type: Config.GOOGLE,
267
+ type: Configs.GOOGLE,
268
268
  })
269
269
 
270
270
  const oidcConfig = await getScopedFullConfig(db, {
271
- type: Config.OIDC,
271
+ type: Configs.OIDC,
272
272
  })
273
273
 
274
274
  let config
@@ -390,17 +390,17 @@ exports.configChecklist = async function (ctx) {
390
390
 
391
391
  // They have set up SMTP
392
392
  const smtpConfig = await getScopedFullConfig(db, {
393
- type: Config.SMTP,
393
+ type: Configs.SMTP,
394
394
  })
395
395
 
396
396
  // They have set up Google Auth
397
397
  const googleConfig = await getScopedFullConfig(db, {
398
- type: Config.GOOGLE,
398
+ type: Configs.GOOGLE,
399
399
  })
400
400
 
401
401
  // They have set up OIDC
402
402
  const oidcConfig = await getScopedFullConfig(db, {
403
- type: Config.OIDC,
403
+ type: Configs.OIDC,
404
404
  })
405
405
 
406
406
  // They have set up an global user
@@ -73,12 +73,12 @@ export async function fetchAPIKey(ctx: any) {
73
73
  }
74
74
 
75
75
  const checkCurrentApp = (ctx: any) => {
76
- const appCookie = getCookie(ctx, constants.Cookie.CurrentApp)
76
+ const appCookie = getCookie(ctx, constants.Cookies.CurrentApp)
77
77
  if (appCookie && !tenancy.isUserInAppTenant(appCookie.appId)) {
78
78
  // there is a currentapp cookie from another tenant
79
79
  // remove the cookie as this is incompatible with the builder
80
80
  // due to builder and admin permissions being removed
81
- clearCookie(ctx, constants.Cookie.CurrentApp)
81
+ clearCookie(ctx, constants.Cookies.CurrentApp)
82
82
  }
83
83
  }
84
84
 
@@ -5,7 +5,6 @@ import {
5
5
  BulkUserRequest,
6
6
  BulkUserResponse,
7
7
  CloudAccount,
8
- CreateAdminUserRequest,
9
8
  InviteUserRequest,
10
9
  InviteUsersRequest,
11
10
  SearchUsersRequest,
@@ -68,8 +67,7 @@ const parseBooleanParam = (param: any) => {
68
67
  }
69
68
 
70
69
  export const adminUser = async (ctx: any) => {
71
- const { email, password, tenantId } = ctx.request
72
- .body as CreateAdminUserRequest
70
+ const { email, password, tenantId } = ctx.request.body
73
71
  await tenancy.doInTenant(tenantId, async () => {
74
72
  // account portal sends a pre-hashed password - honour param to prevent double hashing
75
73
  const hashPassword = parseBooleanParam(ctx.request.query.hashPassword)
@@ -1,7 +1,6 @@
1
- import { BBContext } from "@budibase/types"
2
- import env from "../../../environment"
1
+ const env = require("../../../environment")
3
2
 
4
- export const fetch = async (ctx: BBContext) => {
3
+ exports.fetch = async ctx => {
5
4
  ctx.body = {
6
5
  multiTenancy: !!env.MULTI_TENANCY,
7
6
  cloud: !env.SELF_HOSTED,
@@ -1,8 +1,7 @@
1
- import { accounts } from "@budibase/backend-core"
2
- import env from "../../../environment"
3
- import { BBContext } from "@budibase/types"
1
+ const accounts = require("@budibase/backend-core/accounts")
2
+ const env = require("../../../environment")
4
3
 
5
- export const fetch = async (ctx: BBContext) => {
4
+ exports.fetch = async ctx => {
6
5
  if (!env.SELF_HOSTED && !env.DISABLE_ACCOUNT_PORTAL) {
7
6
  const status = await accounts.getStatus()
8
7
  ctx.body = status
@@ -1,18 +1,61 @@
1
- import { BBContext } from "@budibase/types"
2
- import { deprovisioning } from "@budibase/backend-core"
1
+ const { StaticDatabases, doWithDB } = require("@budibase/backend-core/db")
2
+ const { getTenantId } = require("@budibase/backend-core/tenancy")
3
+ const { deleteTenant } = require("@budibase/backend-core/deprovision")
3
4
  import { quotas } from "@budibase/pro"
4
5
 
5
- const _delete = async (ctx: BBContext) => {
6
- const user = ctx.user!
7
- const tenantId = ctx.params.tenantId
6
+ export const exists = async (ctx: any) => {
7
+ const tenantId = ctx.request.params
8
+ ctx.body = {
9
+ exists: await doWithDB(
10
+ StaticDatabases.PLATFORM_INFO.name,
11
+ async (db: any) => {
12
+ let exists = false
13
+ try {
14
+ const tenantsDoc = await db.get(
15
+ StaticDatabases.PLATFORM_INFO.docs.tenants
16
+ )
17
+ if (tenantsDoc) {
18
+ exists = tenantsDoc.tenantIds.indexOf(tenantId) !== -1
19
+ }
20
+ } catch (err) {
21
+ // if error it doesn't exist
22
+ }
23
+ return exists
24
+ }
25
+ ),
26
+ }
27
+ }
28
+
29
+ export const fetch = async (ctx: any) => {
30
+ ctx.body = await doWithDB(
31
+ StaticDatabases.PLATFORM_INFO.name,
32
+ async (db: any) => {
33
+ let tenants = []
34
+ try {
35
+ const tenantsDoc = await db.get(
36
+ StaticDatabases.PLATFORM_INFO.docs.tenants
37
+ )
38
+ if (tenantsDoc) {
39
+ tenants = tenantsDoc.tenantIds
40
+ }
41
+ } catch (err) {
42
+ // if error it doesn't exist
43
+ }
44
+ return tenants
45
+ }
46
+ )
47
+ }
48
+
49
+ const _delete = async (ctx: any) => {
50
+ const tenantId = getTenantId()
8
51
 
9
- if (tenantId !== user.tenantId) {
10
- ctx.throw(403, "Tenant ID does not match current user")
52
+ if (ctx.params.tenantId !== tenantId) {
53
+ ctx.throw(403, "Unauthorized")
11
54
  }
12
55
 
13
56
  try {
57
+ await deleteTenant(tenantId)
14
58
  await quotas.bustCache()
15
- await deprovisioning.deleteTenant(tenantId)
16
59
  ctx.status = 204
17
60
  } catch (err) {
18
61
  ctx.log.error(err)
package/src/api/index.ts CHANGED
@@ -7,12 +7,11 @@ import { errors, auth, middleware } from "@budibase/backend-core"
7
7
  import { APIError } from "@budibase/types"
8
8
 
9
9
  const PUBLIC_ENDPOINTS = [
10
- // deprecated single tenant sso callback
10
+ // old deprecated endpoints kept for backwards compat
11
11
  {
12
12
  route: "/api/admin/auth/google/callback",
13
13
  method: "GET",
14
14
  },
15
- // deprecated single tenant sso callback
16
15
  {
17
16
  route: "/api/admin/auth/oidc/callback",
18
17
  method: "GET",
@@ -45,19 +44,17 @@ const PUBLIC_ENDPOINTS = [
45
44
  method: "POST",
46
45
  },
47
46
  {
48
- route: "/api/system/environment",
47
+ route: "api/system/environment",
49
48
  method: "GET",
50
49
  },
51
50
  {
52
- route: "/api/system/status",
51
+ route: "api/system/status",
53
52
  method: "GET",
54
53
  },
55
- // TODO: This should be an internal api
56
54
  {
57
55
  route: "/api/global/users/tenant/:id",
58
56
  method: "GET",
59
57
  },
60
- // TODO: This should be an internal api
61
58
  {
62
59
  route: "/api/system/restored",
63
60
  method: "POST",
@@ -65,37 +62,17 @@ const PUBLIC_ENDPOINTS = [
65
62
  ]
66
63
 
67
64
  const NO_TENANCY_ENDPOINTS = [
68
- // system endpoints are not specific to any tenant
65
+ ...PUBLIC_ENDPOINTS,
69
66
  {
70
67
  route: "/api/system",
71
68
  method: "ALL",
72
69
  },
73
- // tenant is determined in request body
74
- // used for creating the tenant
75
70
  {
76
- route: "/api/global/users/init",
77
- method: "POST",
78
- },
79
- // deprecated single tenant sso callback
80
- {
81
- route: "/api/admin/auth/google/callback",
82
- method: "GET",
83
- },
84
- // deprecated single tenant sso callback
85
- {
86
- route: "/api/admin/auth/oidc/callback",
71
+ route: "/api/global/users/self",
87
72
  method: "GET",
88
73
  },
89
- // tenant is determined from code in redis
90
74
  {
91
- route: "/api/global/users/invite/accept",
92
- method: "POST",
93
- },
94
- // global user search - no tenancy
95
- // :id is user id
96
- // TODO: this should really be `/api/system/users/:id`
97
- {
98
- route: "/api/global/users/tenant/:id",
75
+ route: "/api/global/self",
99
76
  method: "GET",
100
77
  },
101
78
  ]