sumba 2.35.0 → 2.37.0

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.
@@ -1,13 +1,9 @@
1
1
  <!DOCTYPE html><html lang="en" style="font-size:16px"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Source: index.js</title><!--[if lt IE 9]>
2
2
  <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
3
3
  <![endif]--><script src="scripts/third-party/hljs.js" defer="defer"></script><script src="scripts/third-party/hljs-line-num.js" defer="defer"></script><script src="scripts/third-party/popper.js" defer="defer"></script><script src="scripts/third-party/tippy.js" defer="defer"></script><script src="scripts/third-party/tocbot.min.js"></script><script>var baseURL="/",locationPathname="";baseURL=(locationPathname=document.location.pathname).substr(0,locationPathname.lastIndexOf("/")+1)</script><link rel="stylesheet" href="styles/clean-jsdoc-theme.min.css"><svg aria-hidden="true" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none"><defs><symbol id="copy-icon" viewbox="0 0 488.3 488.3"><g><path d="M314.25,85.4h-227c-21.3,0-38.6,17.3-38.6,38.6v325.7c0,21.3,17.3,38.6,38.6,38.6h227c21.3,0,38.6-17.3,38.6-38.6V124 C352.75,102.7,335.45,85.4,314.25,85.4z M325.75,449.6c0,6.4-5.2,11.6-11.6,11.6h-227c-6.4,0-11.6-5.2-11.6-11.6V124 c0-6.4,5.2-11.6,11.6-11.6h227c6.4,0,11.6,5.2,11.6,11.6V449.6z"/><path d="M401.05,0h-227c-21.3,0-38.6,17.3-38.6,38.6c0,7.5,6,13.5,13.5,13.5s13.5-6,13.5-13.5c0-6.4,5.2-11.6,11.6-11.6h227 c6.4,0,11.6,5.2,11.6,11.6v325.7c0,6.4-5.2,11.6-11.6,11.6c-7.5,0-13.5,6-13.5,13.5s6,13.5,13.5,13.5c21.3,0,38.6-17.3,38.6-38.6 V38.6C439.65,17.3,422.35,0,401.05,0z"/></g></symbol><symbol id="search-icon" viewBox="0 0 512 512"><g><g><path d="M225.474,0C101.151,0,0,101.151,0,225.474c0,124.33,101.151,225.474,225.474,225.474 c124.33,0,225.474-101.144,225.474-225.474C450.948,101.151,349.804,0,225.474,0z M225.474,409.323 c-101.373,0-183.848-82.475-183.848-183.848S124.101,41.626,225.474,41.626s183.848,82.475,183.848,183.848 S326.847,409.323,225.474,409.323z"/></g></g><g><g><path d="M505.902,476.472L386.574,357.144c-8.131-8.131-21.299-8.131-29.43,0c-8.131,8.124-8.131,21.306,0,29.43l119.328,119.328 c4.065,4.065,9.387,6.098,14.715,6.098c5.321,0,10.649-2.033,14.715-6.098C514.033,497.778,514.033,484.596,505.902,476.472z"/></g></g></symbol><symbol id="font-size-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11.246 15H4.754l-2 5H.6L7 4h2l6.4 16h-2.154l-2-5zm-.8-2L8 6.885 5.554 13h4.892zM21 12.535V12h2v8h-2v-.535a4 4 0 1 1 0-6.93zM19 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"/></symbol><symbol id="add-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 11V5h2v6h6v2h-6v6h-2v-6H5v-2z"/></symbol><symbol id="minus-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M5 11h14v2H5z"/></symbol><symbol id="dark-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/></symbol><symbol id="light-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></symbol><symbol id="reset-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M18.537 19.567A9.961 9.961 0 0 1 12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10c0 2.136-.67 4.116-1.81 5.74L17 12h3a8 8 0 1 0-2.46 5.772l.997 1.795z"/></symbol><symbol id="down-icon" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M12.7803 6.21967C13.0732 6.51256 13.0732 6.98744 12.7803 7.28033L8.53033 11.5303C8.23744 11.8232 7.76256 11.8232 7.46967 11.5303L3.21967 7.28033C2.92678 6.98744 2.92678 6.51256 3.21967 6.21967C3.51256 5.92678 3.98744 5.92678 4.28033 6.21967L8 9.93934L11.7197 6.21967C12.0126 5.92678 12.4874 5.92678 12.7803 6.21967Z"></path></symbol><symbol id="codepen-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M16.5 13.202L13 15.535v3.596L19.197 15 16.5 13.202zM14.697 12L12 10.202 9.303 12 12 13.798 14.697 12zM20 10.869L18.303 12 20 13.131V10.87zM19.197 9L13 4.869v3.596l3.5 2.333L19.197 9zM7.5 10.798L11 8.465V4.869L4.803 9 7.5 10.798zM4.803 15L11 19.131v-3.596l-3.5-2.333L4.803 15zM4 13.131L5.697 12 4 10.869v2.262zM2 9a1 1 0 0 1 .445-.832l9-6a1 1 0 0 1 1.11 0l9 6A1 1 0 0 1 22 9v6a1 1 0 0 1-.445.832l-9 6a1 1 0 0 1-1.11 0l-9-6A1 1 0 0 1 2 15V9z"/></symbol><symbol id="close-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z"/></symbol><symbol id="menu-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M3 4h18v2H3V4zm0 7h18v2H3v-2zm0 7h18v2H3v-2z"/></symbol></defs></svg></head><body data-theme="light"><div class="sidebar-container"><div class="sidebar" id="sidebar"><a href="/" class="sidebar-title sidebar-title-anchor">Sumba API</a><div class="sidebar-items-container"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="Sumba.html">Sumba</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#TConfig">TConfig</a></div><div class="sidebar-section-children"><a href="global.html#TTokenBody">TTokenBody</a></div><div class="sidebar-section-children"><a href="global.html#TTokenData">TTokenData</a></div><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div></div></div><div class="navbar-container" id="VuAckcnZhf"><nav class="navbar"><div class="navbar-left-items"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/sumba" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/sumba" target="">Github</a></div><div class="navbar-item"><a id="" href="https://sumba.bajo.app/" target="">Sumba</a></div><div class="navbar-item"><a id="" href="https://bajo.app/" target="">Bajo</a></div></div><div class="navbar-right-items"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div><nav></nav></nav></div><div class="toc-container"><div class="toc-content"><span class="bold">On this page</span><div id="eed4d2a0bfd64539bb9df78095dec881"></div></div></div><div class="body-wrapper"><div class="main-content"><div class="main-wrapper"><section id="source-page" class="source-page"><header><h1 id="title" class="has-anchor">index.js</h1></header><article><pre class="prettyprint source lang-js"><code>import path from 'path'
4
+ import configFactory, { defMultiSite } from './lib/config.js'
4
5
  import { joiPasswordExtendCore } from 'joi-password'
5
6
 
6
- const defMultiSite = {
7
- enabled: false,
8
- catchAll: 'default'
9
- }
10
-
11
7
  /**
12
8
  * Plugin factory.
13
9
  *
@@ -20,6 +16,7 @@ async function factory (pkgName) {
20
16
  const me = this
21
17
  const { getModel } = this.app.dobo
22
18
  const { cloneDeep, isEmpty } = this.app.lib._
19
+ const config = await configFactory.call(this)
23
20
 
24
21
  /**
25
22
  * Sumba class definition.
@@ -32,141 +29,7 @@ async function factory (pkgName) {
32
29
  */
33
30
  constructor () {
34
31
  super(pkgName, me.app)
35
- /**
36
- * @type {TConfig}
37
- */
38
- this.config = {
39
- multiSite: cloneDeep(defMultiSite),
40
- xSiteAdmins: [], // format: "&lt;siteAlias>:&lt;username>"
41
- waibu: {
42
- title: 'site',
43
- prefix: 'site'
44
- },
45
- dobo: {
46
- model: {}
47
- },
48
- waibuMpa: {
49
- icon: 'globe',
50
- redirect: {
51
- '/': 'sumba:/your-stuff/profile',
52
- '/your-stuff': 'sumba:/your-stuff/profile',
53
- '/info': 'sumba:/info/about-us',
54
- '/user': 'sumba:/your-stuff/profile',
55
- '/db/export': 'sumba:/db/export/list',
56
- '/help': 'sumba:/help/contact-form',
57
- '/help/trouble-tickets': 'sumba:/help/trouble-tickets/list'
58
- },
59
- redirectSubRoute: {
60
- waibuAdmin: {
61
- '/': 'waibuAdmin:/site/site',
62
- '/x': 'waibuAdmin:/site/x/site/list',
63
- '/x/*': 'waibuAdmin:/site/x/{2}/list',
64
- '/*': 'waibuAdmin:/site/{1}/list'
65
- }
66
- },
67
- menuHandler: [{
68
- title: 'account',
69
- icon: 'person',
70
- level: 9998,
71
- children: [
72
- // anonymous only
73
- { title: 'signin', href: 'sumba:/signin', visible: 'anon' },
74
- { title: 'forgotPassword', href: 'sumba:/user/forgot-password', visible: 'anon' },
75
- { title: 'newUserSignup', href: 'sumba:/user/signup', visible: 'anon' },
76
- { title: '-', visible: 'anon' },
77
- { title: 'activation', href: 'sumba:/user/activation', visible: 'anon' },
78
- // authenticated only
79
- { title: 'yourProfile', href: 'sumba:/your-stuff/profile', visible: 'auth' },
80
- { title: 'changePassword', href: 'sumba:/your-stuff/change-password', visible: 'auth' },
81
- { title: 'downloadList', href: 'sumba:/your-stuff/download/list', visible: 'auth' },
82
- { title: '-', visible: 'auth' },
83
- { title: 'signout', href: 'sumba:/signout', visible: 'auth' }
84
- ]
85
- }, {
86
- title: 'help',
87
- icon: 'signInfo',
88
- level: 9999,
89
- children: [
90
- { title: 'contactForm', href: 'sumba:/help/contact-form' },
91
- { title: 'troubleTickets', href: 'sumba:/help/trouble-tickets', visible: 'auth' },
92
- { title: '-' },
93
- { title: 'cookiePolicy', href: 'sumba:/info/cookie-policy' },
94
- { title: 'privacy', href: 'sumba:/info/privacy' },
95
- { title: 'termsConditions', href: 'sumba:/info/terms-conditions' }
96
- ]
97
- }]
98
- },
99
- waibuAdmin: {
100
- menuHandler: 'sumba:adminMenu',
101
- menuCollapsible: true,
102
- modelDisabled: 'all'
103
- },
104
- auth: {
105
- common: {
106
- apiKey: {
107
- type: 'Bearer',
108
- qsKey: 'apiKey',
109
- headerKey: 'X-Auth-ApiKey',
110
- algo: 'sha256' // changing this require each and every user to reset their apiKey
111
- },
112
- basic: {
113
- },
114
- jwt: {
115
- type: 'Bearer',
116
- qsKey: 'token',
117
- headerKey: 'X-Auth-Jwt',
118
- secret: '668de9cf57316c7dbf52f7ff7611c299',
119
- expiresInDur: '7d'
120
- }
121
- },
122
- waibuRestApi: {
123
- methods: ['basic', 'apiKey', 'jwt'],
124
- silentOnError: false
125
- },
126
- waibuMpa: {
127
- methods: ['session'],
128
- silentOnError: false
129
- },
130
- waibuStatic: {
131
- methods: ['basic', 'apiKey', 'jwt'],
132
- basic: {
133
- useUtf8: true,
134
- realm: true,
135
- warningMessage: 'pleaseAuthenticate'
136
- },
137
- silentOnError: false
138
- }
139
- },
140
- redirect: {
141
- signin: 'sumba:/signin',
142
- afterSignin: '/',
143
- signout: 'sumba:/signout',
144
- afterSignout: '/'
145
- },
146
- siteSetting: {
147
- forgotPasswordExpDur: '5m',
148
- timeZone: 'UTC',
149
- userPassword: {
150
- minUpperCase: 1,
151
- minLowerCase: 1,
152
- minSpecialChar: 1,
153
- minNumeric: 1,
154
- noWhitespace: false,
155
- latinOnlyChars: false,
156
- pattern: {
157
- lowerCase: 'abcdefghijklmnopqrstuvwxyz',
158
- upperCase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
159
- specialChar: '!@#$%*',
160
- numeric: '0123456789'
161
- }
162
- }
163
- },
164
- cacheTtl: {
165
- getSiteDur: '1m',
166
- getUserByIdDur: '1m',
167
- getUserByTokenDur: '1m'
168
- }
169
- }
32
+ this.config = config
170
33
  this.secureGuards = []
171
34
  this.actionGuards = []
172
35
  this.queryGuards = []
@@ -299,18 +162,20 @@ async function factory (pkgName) {
299
162
  /**
300
163
  * Get authentication setting based on type and source
301
164
  *
302
- * @async
303
165
  * @method
304
166
  * @private
305
- * @param {string} type - The type of authentication setting to retrieve.
306
- * @param {string} source - The source of the authentication setting.
167
+ * @param {string} type - The type of authentication setting to retrieve. If not provided, it will use parent setting.
168
+ * @param {string} [source] - The source of the authentication setting. If not provided, it will use the default setting.
307
169
  * @returns {object} - The authentication setting object.
308
170
  */
309
- _getAuthSetting = async (type, source) => {
171
+ _getAuthSetting = (type, source) => {
310
172
  const { defaultsDeep } = this.app.lib.aneka
311
173
  const { get } = this.app.lib._
312
174
 
313
- const setting = defaultsDeep(get(this.config, `auth.${source}.${type}`, {}), get(this.config, `auth.common.${type}`, {}))
175
+ let setting
176
+ const withType = type ? `.${type}` : ''
177
+ if (source) setting = defaultsDeep(get(this.config, `${source}.auth${withType}`, {}), get(this.config, `auth${withType}`, {}))
178
+ else setting = get(this.config, `auth${withType}`, {})
314
179
  if (type === 'basic') setting.type = 'Basic'
315
180
  return setting
316
181
  }
@@ -318,18 +183,17 @@ async function factory (pkgName) {
318
183
  /**
319
184
  * Get authentication token based on type, request, and source
320
185
  *
321
- * @async
322
186
  * @method
323
187
  * @private
324
188
  * @param {string} type - The type of authentication setting.
325
189
  * @param {object} req - The request object.
326
- * @param {string} source - The source of the authentication setting.
190
+ * @param {string} [source] - The source of the authentication setting. If not provided, it will use the default setting.
327
191
  * @returns {string|boolean} - The token if found, otherwise false.
328
192
  */
329
- _getAuthToken = async (type, req, source) => {
193
+ _getAuthToken = (type, req, source) => {
330
194
  const { isEmpty } = this.app.lib._
331
195
 
332
- const setting = await this._getAuthSetting(type, source)
196
+ const setting = this._getAuthSetting(type, source)
333
197
  let token = req.headers[setting.headerKey.toLowerCase()]
334
198
  if (!['basic'].includes(type) &amp;&amp; isEmpty(token)) token = req.query[setting.qsKey]
335
199
  if (isEmpty(token)) {
@@ -341,7 +205,11 @@ async function factory (pkgName) {
341
205
  }
342
206
 
343
207
  /**
344
- * Build admin menu based on locals and request
208
+ * Build admin menu based on locals and request.
209
+ *
210
+ * - If multi-site is enabled, it will include the cross-site menu with options to manage all sites and site settings.
211
+ * - If caching is enabled, it will include the cache storage menu in the appropriate section.
212
+ * - Session management menu is also included in the appropriate section.
345
213
  *
346
214
  * @async
347
215
  * @method
@@ -359,29 +227,25 @@ async function factory (pkgName) {
359
227
  title: 'manageSite',
360
228
  children: [
361
229
  { title: 'siteProfile', href: `waibuAdmin:/${prefix}/site` },
362
- { title: 'siteSetting', href: `waibuAdmin:/${prefix}/site-setting/:action`, params }
230
+ { title: 'manageSiteSetting', href: `waibuAdmin:/${prefix}/site-setting/:action`, params }
363
231
  ]
364
232
  }, {
365
- title: 'manageUser',
233
+ title: 'manageUserTeam',
366
234
  children: [
367
- { title: 'userProfile', href: `waibuAdmin:/${prefix}/user/:action`, params },
368
- { title: 'userSetting', href: `waibuAdmin:/${prefix}/user-setting/:action`, params },
235
+ { title: 'manageUser', href: `waibuAdmin:/${prefix}/user/:action`, params },
236
+ { title: 'manageUserSetting', href: `waibuAdmin:/${prefix}/user-setting/:action`, params },
237
+ { title: 'manageTeam', href: `waibuAdmin:/${prefix}/team/:action`, params },
238
+ { title: 'manageTeamUser', href: `waibuAdmin:/${prefix}/team-user/:action`, params },
239
+ { title: 'manageTeamSetting', href: `waibuAdmin:/${prefix}/team-setting/:action`, params },
369
240
  { title: 'resetUserPassword', href: `waibuAdmin:/${prefix}/reset-user-password` }
370
241
  ]
371
242
  }, {
372
- title: 'manageTeam',
373
- children: [
374
- { title: 'teamProfile', href: `waibuAdmin:/${prefix}/team/:action`, params },
375
- { title: 'teamUser', href: `waibuAdmin:/${prefix}/team-user/:action`, params },
376
- { title: 'teamSetting', href: `waibuAdmin:/${prefix}/team-setting/:action`, params }
377
- ]
378
- }, {
379
- title: 'permission',
243
+ title: 'permissionGuard',
380
244
  children: [
381
245
  { title: 'secureGuard', href: `waibuAdmin:/${prefix}/secure-guard/:action`, params },
382
246
  { title: 'anonymousGuard', href: `waibuAdmin:/${prefix}/anonymous-guard/:action`, params },
383
- { title: 'queryGuard', href: `waibuAdmin:/${prefix}/query-guard/:action`, params },
384
247
  { title: 'actionGuard', href: `waibuAdmin:/${prefix}/action-guard/:action`, params },
248
+ { title: 'queryGuard', href: `waibuAdmin:/${prefix}/query-guard/:action`, params },
385
249
  { title: 'attribGuard', href: `waibuAdmin:/${prefix}/attrib-guard/:action`, params }
386
250
  ]
387
251
  }, {
@@ -404,8 +268,8 @@ async function factory (pkgName) {
404
268
  items.unshift({
405
269
  title: 'xSite',
406
270
  children: [
407
- { title: 'allSites', href: `waibuAdmin:/${prefix}/x/site/:action`, params },
408
- { title: 'siteSetting', href: `waibuAdmin:/${prefix}/x/site-setting/:action`, params },
271
+ { title: 'manageAllSites', href: `waibuAdmin:/${prefix}/x/site/:action`, params },
272
+ { title: 'manageAllSiteSettings', href: `waibuAdmin:/${prefix}/x/site-setting/:action`, params },
409
273
  sessionMenu
410
274
  ]
411
275
  })
@@ -426,7 +290,7 @@ async function factory (pkgName) {
426
290
  * @async
427
291
  * @method
428
292
  * @param {object} rec - The user record object.
429
- * @returns {object} - The JWT token and its expiration date.
293
+ * @returns {Promise&lt;object>} - The JWT token and its expiration date.
430
294
  */
431
295
  createJwtFromUserRecord = async (rec) => {
432
296
  const { importPkg } = this.app.bajo
@@ -436,8 +300,9 @@ async function factory (pkgName) {
436
300
  const fastJwt = await importPkg('bajoExtra:fast-jwt')
437
301
  const { createSigner } = fastJwt
438
302
 
439
- const opts = pick(this.config.auth.common.jwt, ['expiresInDur'])
440
- opts.key = get(this.config, 'auth.common.jwt.secret')
303
+ const setting = this._getAuthSetting('jwt')
304
+ const opts = pick(setting, ['expiresInDur'])
305
+ opts.key = get(setting, 'secret')
441
306
  const sign = createSigner(opts)
442
307
  const apiKey = await this.hash(rec.token)
443
308
  const payload = { uid: rec.id, apiKey }
@@ -484,7 +349,8 @@ async function factory (pkgName) {
484
349
  */
485
350
  verifyApiKey = async (req, reply, source, payload) => {
486
351
  const { merge, camelCase } = this.app.lib._
487
- const checker = this.app.bajoExtra[camelCase(`is ${this.config.auth.common.apiKey.algo}`)]
352
+ const setting = this._getAuthSetting(null, source)
353
+ const checker = this.app.bajoExtra[camelCase(`is ${setting.apiKey.algo}`)]
488
354
 
489
355
  let token = await this._getAuthToken('apiKey', req, source)
490
356
  if (!checker(token)) return false
@@ -520,7 +386,7 @@ async function factory (pkgName) {
520
386
  reply.code(401)
521
387
  }
522
388
 
523
- const setting = await this._getAuthSetting('basic', source)
389
+ const setting = this._getAuthSetting('basic', source)
524
390
  let authInfo
525
391
  const parts = (req.headers.authorization ?? '').split(' ')
526
392
  if (parts[0] === setting.type) authInfo = parts[1]
@@ -561,8 +427,8 @@ async function factory (pkgName) {
561
427
 
562
428
  const fastJwt = await importPkg('bajoExtra:fast-jwt')
563
429
  const { createVerifier } = fastJwt
564
- const setting = await this._getAuthSetting('jwt', source)
565
- const token = await this._getAuthToken('jwt', req, source)
430
+ const setting = this._getAuthSetting('jwt', source)
431
+ const token = this._getAuthToken('jwt', req, source)
566
432
  if (isEmpty(token)) return false
567
433
  const verifier = createVerifier({
568
434
  key: setting.secret,
@@ -802,7 +668,8 @@ async function factory (pkgName) {
802
668
  */
803
669
  hash = async (item) => {
804
670
  const { hash } = this.app.bajoExtra
805
- return await hash(item, this.config.auth.common.apiKey.algo)
671
+ const setting = this._getAuthSetting('apiKey')
672
+ return await hash(item, setting.algo)
806
673
  }
807
674
 
808
675
  /**
@@ -811,7 +678,7 @@ async function factory (pkgName) {
811
678
  * @async
812
679
  * @method
813
680
  * @private
814
- * @param {string} type - The type of guards to fetch.
681
+ * @param {string} type - The type of guards to fetch. Available types are 'Action', 'Attrib', 'Query', 'Secure', and 'Anonymous'.
815
682
  * @returns {Promise&lt;Array>} - A promise that resolves to an array of guards.
816
683
  */
817
684
  _fetchGuards = async (type) => {
@@ -1082,12 +949,11 @@ async function factory (pkgName) {
1082
949
  await setUser()
1083
950
  return secure
1084
951
  }
1085
- const silentOnError = this.config.auth[webApp].silentOnError ?? this.config.auth.common.silentOnError
952
+ const { silentOnError, methods } = this._getAuthSetting(null, webApp)
1086
953
  const payload = silentOnError ? { noContent: true } : undefined
1087
- const authMethods = this.config.auth[webApp].methods ?? []
1088
- if (isEmpty(authMethods)) throw this.error('noAuthMethod', merge({ statusCode: 500 }, payload))
954
+ if (isEmpty(methods)) throw this.error('noAuthMethod', merge({ statusCode: 500 }, payload))
1089
955
  let success
1090
- for (const m of authMethods) {
956
+ for (const m of methods) {
1091
957
  const handler = this[camelCase(`verify ${m}`)]
1092
958
  if (!handler) throw this.error('invalidAuthMethod%s', m, merge({ statusCode: 500 }, payload))
1093
959
  const check = await handler(req, reply, source, payload)
@@ -1652,7 +1518,6 @@ async function factory (pkgName) {
1652
1518
  for (const field of fields) {
1653
1519
  if (!model.getNonVirtualProperties(true).includes(field)) continue // or, should it throws exception instead?
1654
1520
  const inSetting = this.filterModelFromSetting({ model, field, options })
1655
- if (inSetting.results.length > 0) inSetting.results.push(...inSetting.results)
1656
1521
  const prop = model.getProperty(field)
1657
1522
  const items = rules.filter(item => item.field === field)
1658
1523
  for (const item of items) {
@@ -1831,34 +1696,51 @@ async function factory (pkgName) {
1831
1696
  */
1832
1697
  applyActionHook = async (opts = {}) => {
1833
1698
  const { model, options = {} } = opts
1834
- const { includes } = this.app.lib.aneka
1835
1699
  const { req } = options
1836
- const { teamIds, aliases } = this.getTeamIdsAndAliases(req)
1837
- if (aliases.includes('administrator')) return
1838
-
1839
- const actions = {
1840
- READ: ['findOneRecord', 'findRecord', 'countRecord', 'getRecord'],
1841
- EXPORT: ['findAllRecord'],
1842
- CREATE: ['createRecord', 'bulkCreateRecord'],
1843
- UPDATE: ['updateRecord', 'upsertRecord'],
1844
- DELETE: ['removeRecord']
1845
- }
1700
+ if (!req) return
1701
+ if (!req.site) return
1702
+
1703
+ let action
1704
+ if (['findOneRecord', 'findRecord', 'countRecord', 'getRecord'].includes(options.action)) action = 'READ'
1705
+ else if (['createRecord', 'bulkCreateRecord'].includes(options.action)) action = 'CREATE'
1706
+ else if (['updateRecord', 'upsertRecord'].includes(options.action)) action = 'UPDATE'
1707
+ else if (['removeRecord'].includes(options.action)) action = 'REMOVE'
1708
+ else if (['findAllRecord'].includes(options.action)) action = 'EXPORT'
1709
+ if (!action) return
1846
1710
 
1711
+ const allowed = await this.isActionAllowed(action, model, req)
1712
+ if (!allowed) throw this.error('accessDenied', { statusCode: 403 })
1713
+ }
1714
+
1715
+ /**
1716
+ * Check if the specified action is allowed for the given model and request. It retrieves the action guards, filters them based on the site ID, model name, and team IDs, and checks if the action is included in the allowed values. If any guard denies the action, it returns false; otherwise, it returns true.
1717
+ *
1718
+ * @async
1719
+ * @method
1720
+ * @param {string} action - The action to check. Available actions are: 'READ', 'CREATE', 'UPDATE', 'REMOVE' and 'EXPORT'.
1721
+ * @param {object} model - The model object.
1722
+ * @param {object} req - The request object.
1723
+ * @returns {Promise&lt;boolean>} - Returns true if the action is allowed, otherwise false.
1724
+ */
1725
+ isActionAllowed = async (action, model, req) => {
1726
+ const { includes } = this.app.lib.aneka
1727
+ if (!req) return true
1728
+ if (!req.site) return true
1847
1729
  const guards = await this.getActionGuards()
1730
+ const { teamIds, aliases } = this.getTeamIdsAndAliases(req)
1731
+ if (aliases.includes('administrator')) return true
1848
1732
  const filterFn = item => {
1849
1733
  const bySiteId = item.siteId === req.site.id + ''
1850
1734
  const byModel = item.models.includes(model.name)
1851
1735
  const byTeamId = item.allTeams ? true : includes(item.teamIds, teamIds)
1852
1736
  return bySiteId &amp;&amp; byModel &amp;&amp; byTeamId
1853
1737
  }
1854
- let abort = false
1738
+ let denied = false
1855
1739
  for (const item of guards.filter(filterFn)) {
1856
1740
  if (item.value.includes('*')) continue
1857
- for (const action in actions) {
1858
- if (actions[action].includes(options.action) &amp;&amp; !item.value.includes(action)) abort = true
1859
- }
1741
+ if (!item.value.includes(action)) denied = true
1860
1742
  }
1861
- if (abort) throw this.error('accessDenied', { statusCode: 403 })
1743
+ return !denied
1862
1744
  }
1863
1745
  }
1864
1746