@data-fair/lib-common-types 0.1.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.
- package/account/.type/index.d.ts +29 -0
- package/account/.type/index.js +83 -0
- package/account/.type/validate.js +128 -0
- package/account/index.js +1 -0
- package/application/.type/index.d.ts +100 -0
- package/application/.type/index.js +161 -0
- package/application/index.js +1 -0
- package/package.json +14 -0
- package/session/.type/index.d.ts +66 -0
- package/session/.type/index.js +185 -0
- package/session/.type/validate.js +777 -0
- package/session/index.js +78 -0
package/session/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('./types.js').SessionStateAuthenticated} SessionStateAuthenticated
|
|
3
|
+
* @typedef {import('./.type/index.js').SessionState} SessionState
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { httpError } from '@data-fair/lib/http-errors.js'
|
|
7
|
+
|
|
8
|
+
export * from './.type/index.js'
|
|
9
|
+
|
|
10
|
+
/** @type {(sessionState: SessionState) => sessionState is SessionStateAuthenticated} */
|
|
11
|
+
export const isAuthenticated = (sessionState) => {
|
|
12
|
+
return !!sessionState.user
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** @type {(sessionState: SessionState) => asserts sessionState is SessionStateAuthenticated} */
|
|
16
|
+
export const assertAuthenticated = (sessionState) => {
|
|
17
|
+
if (!isAuthenticated(sessionState)) throw httpError(401)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** @type {(sessionState: SessionState) => asserts sessionState is SessionStateAuthenticated} */
|
|
21
|
+
export const assertAdminMode = (sessionState) => {
|
|
22
|
+
assertAuthenticated(sessionState)
|
|
23
|
+
// TODO: use sessionState.locale to internationalize error message
|
|
24
|
+
if (!sessionState.user.adminMode) throw httpError(403, 'super admin only')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {import('../account/index.js').AccountKeys} userAccount
|
|
29
|
+
* @param {import('../account/index.js').AccountKeys} resourceAccount
|
|
30
|
+
* @returns {boolean}
|
|
31
|
+
*/
|
|
32
|
+
const matchAccount = (userAccount, resourceAccount) => {
|
|
33
|
+
if (userAccount.type !== resourceAccount.type) return false
|
|
34
|
+
if (userAccount.id !== resourceAccount.id) return false
|
|
35
|
+
if (userAccount.department && userAccount.department !== resourceAccount.department) return false
|
|
36
|
+
return true
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {SessionState} sessionState
|
|
41
|
+
* @param {import('../account/index.js').AccountKeys} account
|
|
42
|
+
* @param {boolean} [onlyActiveAccount]
|
|
43
|
+
* @returns {string | null}
|
|
44
|
+
*/
|
|
45
|
+
export const getAccountRole = (sessionState, account, onlyActiveAccount = true) => {
|
|
46
|
+
if (!isAuthenticated(sessionState)) return null
|
|
47
|
+
if (sessionState.user.adminMode) return 'admin'
|
|
48
|
+
if (onlyActiveAccount) {
|
|
49
|
+
if (matchAccount(sessionState.account, account)) return sessionState.accountRole
|
|
50
|
+
} else {
|
|
51
|
+
if (account.type === 'user' && sessionState.user.id === account.id) return 'admin'
|
|
52
|
+
for (const org of sessionState.user.organizations) {
|
|
53
|
+
if (matchAccount({ type: 'organization', id: org.id, department: org.department }, account)) return org.role
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return null
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {SessionState} sessionState
|
|
61
|
+
* @param {import('../account/index.js').AccountKeys} account
|
|
62
|
+
* @param {string} role
|
|
63
|
+
* @param {boolean} [onlyActiveAccount]
|
|
64
|
+
*/
|
|
65
|
+
export const assertAccountRole = (sessionState, account, role, onlyActiveAccount = true) => {
|
|
66
|
+
const accountRole = getAccountRole(sessionState, account, onlyActiveAccount)
|
|
67
|
+
if (accountRole !== role) throw httpError(403, `requires ${role} role`)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @type {(type: string) => type is "user" | "organization"} */
|
|
71
|
+
export const isValidAccountType = (type) => {
|
|
72
|
+
return ['user', 'organization'].includes(type)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @type {(type: string) => asserts type is "user" | "organization"} */
|
|
76
|
+
export const assertValidAccountType = (type) => {
|
|
77
|
+
if (!isValidAccountType(type)) throw httpError(400, 'invalid account type')
|
|
78
|
+
}
|