@naturalcycles/backend-lib 5.4.2 → 5.6.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.
|
@@ -31,7 +31,7 @@ export declare class BaseAdminService {
|
|
|
31
31
|
* Otherwise returns Set of permissions.
|
|
32
32
|
* Empty array means it IS and Admin, but has no permissions (except being an Admin).
|
|
33
33
|
*/
|
|
34
|
-
getEmailPermissions(email?: string): Set<string> | undefined
|
|
34
|
+
getEmailPermissions(email?: string): Promise<Set<string> | undefined>;
|
|
35
35
|
/**
|
|
36
36
|
* To be extended.
|
|
37
37
|
*/
|
|
@@ -41,7 +41,7 @@ export declare class BaseAdminService {
|
|
|
41
41
|
* Current implementation is based on req=Request (from Express).
|
|
42
42
|
* Override if needed.
|
|
43
43
|
*/
|
|
44
|
-
getAdminToken(req: BackendRequest):
|
|
44
|
+
getAdminToken(req: BackendRequest): string | undefined;
|
|
45
45
|
isAdmin(req: BackendRequest | undefined): Promise<boolean>;
|
|
46
46
|
getAdminInfo(req: BackendRequest): Promise<AdminInfo | undefined>;
|
|
47
47
|
/**
|
|
@@ -32,7 +32,7 @@ class BaseAdminService {
|
|
|
32
32
|
* Otherwise returns Set of permissions.
|
|
33
33
|
* Empty array means it IS and Admin, but has no permissions (except being an Admin).
|
|
34
34
|
*/
|
|
35
|
-
getEmailPermissions(email) {
|
|
35
|
+
async getEmailPermissions(email) {
|
|
36
36
|
if (!email)
|
|
37
37
|
return;
|
|
38
38
|
console.log(`getEmailPermissions (${(0, nodejs_lib_1.dimGrey)(email)}) returning undefined (please override the implementation)`);
|
|
@@ -70,7 +70,7 @@ class BaseAdminService {
|
|
|
70
70
|
* Current implementation is based on req=Request (from Express).
|
|
71
71
|
* Override if needed.
|
|
72
72
|
*/
|
|
73
|
-
|
|
73
|
+
getAdminToken(req) {
|
|
74
74
|
return (req.cookies?.[this.cfg.adminTokenKey] ||
|
|
75
75
|
req.header(this.cfg.adminTokenKey) ||
|
|
76
76
|
req.header('x-admin-token'));
|
|
@@ -78,9 +78,9 @@ class BaseAdminService {
|
|
|
78
78
|
async isAdmin(req) {
|
|
79
79
|
if (!req)
|
|
80
80
|
return false;
|
|
81
|
-
const adminToken =
|
|
81
|
+
const adminToken = this.getAdminToken(req);
|
|
82
82
|
const email = await this.getEmailByToken(req, adminToken);
|
|
83
|
-
return !!this.getEmailPermissions(email);
|
|
83
|
+
return !!(await this.getEmailPermissions(email));
|
|
84
84
|
}
|
|
85
85
|
async getAdminInfo(req) {
|
|
86
86
|
return await this.hasPermissions(req);
|
|
@@ -96,9 +96,9 @@ class BaseAdminService {
|
|
|
96
96
|
async hasPermissions(req, reqPermissions = [], meta = {}) {
|
|
97
97
|
if (!this.cfg.authEnabled)
|
|
98
98
|
return adminInfoDisabled();
|
|
99
|
-
const adminToken =
|
|
99
|
+
const adminToken = this.getAdminToken(req);
|
|
100
100
|
const email = await this.getEmailByToken(req, adminToken);
|
|
101
|
-
const hasPermissions = this.getEmailPermissions(email);
|
|
101
|
+
const hasPermissions = await this.getEmailPermissions(email);
|
|
102
102
|
if (!hasPermissions)
|
|
103
103
|
return;
|
|
104
104
|
const granted = reqPermissions.every(p => hasPermissions.has(p));
|
|
@@ -113,7 +113,7 @@ class BaseAdminService {
|
|
|
113
113
|
async requirePermissions(req, reqPermissions = [], meta = {}, andComparison = true) {
|
|
114
114
|
if (!this.cfg.authEnabled)
|
|
115
115
|
return adminInfoDisabled();
|
|
116
|
-
const adminToken =
|
|
116
|
+
const adminToken = this.getAdminToken(req);
|
|
117
117
|
const email = await this.getEmailByToken(req, adminToken);
|
|
118
118
|
if (!email) {
|
|
119
119
|
throw new js_lib_1.AppError('adminToken required', {
|
|
@@ -122,7 +122,7 @@ class BaseAdminService {
|
|
|
122
122
|
userFriendly: true,
|
|
123
123
|
});
|
|
124
124
|
}
|
|
125
|
-
const hasPermissions = this.getEmailPermissions(email);
|
|
125
|
+
const hasPermissions = await this.getEmailPermissions(email);
|
|
126
126
|
const grantedPermissions = hasPermissions
|
|
127
127
|
? reqPermissions.filter(p => hasPermissions.has(p))
|
|
128
128
|
: [];
|
package/package.json
CHANGED
|
@@ -58,7 +58,7 @@ export class BaseAdminService {
|
|
|
58
58
|
* Otherwise returns Set of permissions.
|
|
59
59
|
* Empty array means it IS and Admin, but has no permissions (except being an Admin).
|
|
60
60
|
*/
|
|
61
|
-
getEmailPermissions(email?: string): Set<string> | undefined {
|
|
61
|
+
async getEmailPermissions(email?: string): Promise<Set<string> | undefined> {
|
|
62
62
|
if (!email) return
|
|
63
63
|
console.log(
|
|
64
64
|
`getEmailPermissions (${dimGrey(
|
|
@@ -115,7 +115,7 @@ export class BaseAdminService {
|
|
|
115
115
|
* Current implementation is based on req=Request (from Express).
|
|
116
116
|
* Override if needed.
|
|
117
117
|
*/
|
|
118
|
-
|
|
118
|
+
getAdminToken(req: BackendRequest): string | undefined {
|
|
119
119
|
return (
|
|
120
120
|
req.cookies?.[this.cfg.adminTokenKey] ||
|
|
121
121
|
req.header(this.cfg.adminTokenKey) ||
|
|
@@ -125,9 +125,9 @@ export class BaseAdminService {
|
|
|
125
125
|
|
|
126
126
|
async isAdmin(req: BackendRequest | undefined): Promise<boolean> {
|
|
127
127
|
if (!req) return false
|
|
128
|
-
const adminToken =
|
|
128
|
+
const adminToken = this.getAdminToken(req)
|
|
129
129
|
const email = await this.getEmailByToken(req, adminToken)
|
|
130
|
-
return !!this.getEmailPermissions(email)
|
|
130
|
+
return !!(await this.getEmailPermissions(email))
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
async getAdminInfo(req: BackendRequest): Promise<AdminInfo | undefined> {
|
|
@@ -150,9 +150,9 @@ export class BaseAdminService {
|
|
|
150
150
|
): Promise<AdminInfo | undefined> {
|
|
151
151
|
if (!this.cfg.authEnabled) return adminInfoDisabled()
|
|
152
152
|
|
|
153
|
-
const adminToken =
|
|
153
|
+
const adminToken = this.getAdminToken(req)
|
|
154
154
|
const email = await this.getEmailByToken(req, adminToken)
|
|
155
|
-
const hasPermissions = this.getEmailPermissions(email)
|
|
155
|
+
const hasPermissions = await this.getEmailPermissions(email)
|
|
156
156
|
if (!hasPermissions) return
|
|
157
157
|
|
|
158
158
|
const granted = reqPermissions.every(p => hasPermissions.has(p))
|
|
@@ -175,7 +175,7 @@ export class BaseAdminService {
|
|
|
175
175
|
): Promise<AdminInfo> {
|
|
176
176
|
if (!this.cfg.authEnabled) return adminInfoDisabled()
|
|
177
177
|
|
|
178
|
-
const adminToken =
|
|
178
|
+
const adminToken = this.getAdminToken(req)
|
|
179
179
|
const email = await this.getEmailByToken(req, adminToken)
|
|
180
180
|
|
|
181
181
|
if (!email) {
|
|
@@ -186,7 +186,7 @@ export class BaseAdminService {
|
|
|
186
186
|
})
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
const hasPermissions = this.getEmailPermissions(email)
|
|
189
|
+
const hasPermissions = await this.getEmailPermissions(email)
|
|
190
190
|
const grantedPermissions = hasPermissions
|
|
191
191
|
? reqPermissions.filter(p => hasPermissions.has(p))
|
|
192
192
|
: []
|