@travetto/auth 3.0.0-rc.0 → 3.0.0-rc.1
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/README.md +19 -39
- package/package.json +2 -2
- package/src/types/authenticator.ts +2 -1
- package/src/util.ts +13 -66
package/README.md
CHANGED
|
@@ -63,7 +63,8 @@ As referenced above, a [Principal Structure](https://github.com/travetto/travett
|
|
|
63
63
|
```typescript
|
|
64
64
|
export interface Authenticator<T = unknown, P extends Principal = Principal, C = unknown> {
|
|
65
65
|
/**
|
|
66
|
-
* Verify the payload,
|
|
66
|
+
* Verify the payload, ensuring the payload is correctly identified.
|
|
67
|
+
*
|
|
67
68
|
* @returns Valid principal if authenticated
|
|
68
69
|
* @returns undefined if authentication is valid, but incomplete (multi-step)
|
|
69
70
|
* @throws AppError if authentication fails
|
|
@@ -105,7 +106,7 @@ Overall, the structure is simple, but drives home the primary use cases of the f
|
|
|
105
106
|
* To have access to the principal
|
|
106
107
|
|
|
107
108
|
## Common Utilities
|
|
108
|
-
The [AuthUtil](https://github.com/travetto/travetto/tree/main/module/auth/src/util.ts#
|
|
109
|
+
The [AuthUtil](https://github.com/travetto/travetto/tree/main/module/auth/src/util.ts#L11) provides the following functionality:
|
|
109
110
|
|
|
110
111
|
**Code: Auth util structure**
|
|
111
112
|
```typescript
|
|
@@ -113,42 +114,16 @@ import * as crypto from 'crypto';
|
|
|
113
114
|
import * as util from 'util';
|
|
114
115
|
import { AppError, Util } from '@travetto/base';
|
|
115
116
|
const pbkdf2 = util.promisify(crypto.pbkdf2);
|
|
116
|
-
type PermSet = Set<string> | ReadonlySet<string>;
|
|
117
|
-
type PermissionChecker = {
|
|
118
|
-
all: (perms: PermSet) => boolean;
|
|
119
|
-
any: (perms: PermSet) => boolean;
|
|
120
|
-
};
|
|
121
|
-
type PermissionCheckerSet = {
|
|
122
|
-
includes: (perms: PermSet) => boolean;
|
|
123
|
-
excludes: (perms: PermSet) => boolean;
|
|
124
|
-
check: (value: PermSet) => boolean;
|
|
125
|
-
};
|
|
126
117
|
/**
|
|
127
118
|
* Standard auth utilities
|
|
128
119
|
*/
|
|
129
120
|
export class AuthUtil {
|
|
130
121
|
/**
|
|
131
|
-
* Build
|
|
132
|
-
*
|
|
133
|
-
* @param perms Set of permissions to check
|
|
134
|
-
* @param defaultIfEmpty If no perms passed, default to empty
|
|
135
|
-
*/
|
|
136
|
-
/**
|
|
137
|
-
* Build a permission checker off of an include, and exclude set
|
|
138
|
-
*
|
|
139
|
-
* @param include Which permissions to include
|
|
140
|
-
* @param exclude Which permissions to exclude
|
|
141
|
-
* @param matchAll Whether not all permissions should be matched
|
|
142
|
-
*/
|
|
143
|
-
static permissionChecker(include: Iterable<string>, exclude: Iterable<string>, mode: 'all' | 'any' = 'any'): PermissionCheckerSet ;
|
|
144
|
-
/**
|
|
145
|
-
* Build a permission checker off of an include, and exclude set
|
|
122
|
+
* Build matcher for role permissions in allow/deny fashion
|
|
146
123
|
*
|
|
147
|
-
* @param
|
|
148
|
-
* @param exclude Which permissions to exclude
|
|
149
|
-
* @param matchAll Whether not all permissions should be matched
|
|
124
|
+
* @param roles Roles to build matcher for
|
|
150
125
|
*/
|
|
151
|
-
static
|
|
126
|
+
static roleMatcher(roles: string[]): (perms: Set<string>) => boolean ;
|
|
152
127
|
/**
|
|
153
128
|
* Generate a hash for a given value
|
|
154
129
|
*
|
|
@@ -170,13 +145,18 @@ export class AuthUtil {
|
|
|
170
145
|
}
|
|
171
146
|
```
|
|
172
147
|
|
|
173
|
-
`
|
|
148
|
+
`roleMatcher` is probably the only functionality that needs to be explained. The function extends the core allow/deny matcher functionality from [Base](https://github.com/travetto/travetto/tree/main/module/base#readme "Application phase management, environment config and common utilities for travetto applications.")'s Util class.
|
|
149
|
+
|
|
150
|
+
An example of role checks could be:
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
* Admin
|
|
154
|
+
* !Editor
|
|
155
|
+
* Owner+Author
|
|
174
156
|
|
|
157
|
+
The code would check the list in order, which would result in the following logic:
|
|
175
158
|
|
|
176
|
-
*
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
* Else
|
|
181
|
-
* The user does not have all permissions in the exclusion list
|
|
182
|
-
* The include list is empty, or the user has all permissions in the include list.
|
|
159
|
+
* If the user is an admin, always allow
|
|
160
|
+
* If the user has the editor role, deny
|
|
161
|
+
* If the user is both an owner and an author allow
|
|
162
|
+
* By default, deny due to the presence of positive checks
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/auth",
|
|
3
3
|
"displayName": "Authentication",
|
|
4
|
-
"version": "3.0.0-rc.
|
|
4
|
+
"version": "3.0.0-rc.1",
|
|
5
5
|
"description": "Authentication scaffolding for the travetto framework",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"authentication",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"directory": "module/auth"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@travetto/base": "^3.0.0-rc.
|
|
27
|
+
"@travetto/base": "^3.0.0-rc.1"
|
|
28
28
|
},
|
|
29
29
|
"private": false,
|
|
30
30
|
"publishConfig": {
|
|
@@ -7,7 +7,8 @@ import { Principal } from './principal';
|
|
|
7
7
|
*/
|
|
8
8
|
export interface Authenticator<T = unknown, P extends Principal = Principal, C = unknown> {
|
|
9
9
|
/**
|
|
10
|
-
* Verify the payload,
|
|
10
|
+
* Verify the payload, ensuring the payload is correctly identified.
|
|
11
|
+
*
|
|
11
12
|
* @returns Valid principal if authenticated
|
|
12
13
|
* @returns undefined if authentication is valid, but incomplete (multi-step)
|
|
13
14
|
* @throws AppError if authentication fails
|
package/src/util.ts
CHANGED
|
@@ -5,85 +5,32 @@ import { AppError, Util } from '@travetto/base';
|
|
|
5
5
|
|
|
6
6
|
const pbkdf2 = util.promisify(crypto.pbkdf2);
|
|
7
7
|
|
|
8
|
-
type PermSet = Set<string> | ReadonlySet<string>;
|
|
9
|
-
|
|
10
|
-
type PermissionChecker = {
|
|
11
|
-
all: (perms: PermSet) => boolean;
|
|
12
|
-
any: (perms: PermSet) => boolean;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
type PermissionCheckerSet = {
|
|
16
|
-
includes: (perms: PermSet) => boolean;
|
|
17
|
-
excludes: (perms: PermSet) => boolean;
|
|
18
|
-
check: (value: PermSet) => boolean;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
8
|
/**
|
|
22
9
|
* Standard auth utilities
|
|
23
10
|
*/
|
|
24
11
|
export class AuthUtil {
|
|
25
12
|
|
|
26
|
-
static #
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*
|
|
32
|
-
* @param perms Set of permissions to check
|
|
33
|
-
* @param defaultIfEmpty If no perms passed, default to empty
|
|
34
|
-
*/
|
|
35
|
-
static #buildChecker(perms: Iterable<string>, defaultIfEmpty: boolean): PermissionChecker {
|
|
36
|
-
const permArr = [...perms].map(x => x.toLowerCase());
|
|
37
|
-
let all = (_: PermSet): boolean => defaultIfEmpty;
|
|
38
|
-
let any = (_: PermSet): boolean => defaultIfEmpty;
|
|
39
|
-
if (permArr.length) {
|
|
40
|
-
all = (uPerms: PermSet): boolean => permArr.every(x => uPerms.has(x));
|
|
41
|
-
any = (uPerms: PermSet): boolean => permArr.some(x => uPerms.has(x));
|
|
13
|
+
static #matchPermissionSet(rule: string[], perms: Set<string>): boolean {
|
|
14
|
+
for (const el of rule) {
|
|
15
|
+
if (!perms.has(el)) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
42
18
|
}
|
|
43
|
-
return
|
|
19
|
+
return true;
|
|
44
20
|
}
|
|
45
21
|
|
|
46
22
|
/**
|
|
47
|
-
* Build
|
|
23
|
+
* Build matcher for role permissions in allow/deny fashion
|
|
48
24
|
*
|
|
49
|
-
* @param
|
|
50
|
-
* @param exclude Which permissions to exclude
|
|
51
|
-
* @param matchAll Whether not all permissions should be matched
|
|
25
|
+
* @param roles Roles to build matcher for
|
|
52
26
|
*/
|
|
53
|
-
static
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.#checkIncCache.set(incKey, this.#buildChecker(include, true));
|
|
59
|
-
}
|
|
60
|
-
if (!this.#checkExcCache.has(excKey)) {
|
|
61
|
-
this.#checkExcCache.set(excKey, this.#buildChecker(exclude, false));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const includes = this.#checkIncCache.get(incKey)![mode];
|
|
65
|
-
const excludes = this.#checkExcCache.get(excKey)![mode];
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
includes, excludes, check: (perms: PermSet) => includes(perms) && !excludes(perms)
|
|
69
|
-
};
|
|
27
|
+
static roleMatcher(roles: string[]): (perms: Set<string>) => boolean {
|
|
28
|
+
return Util.allowDenyMatcher<string[], [Set<string>]>(roles,
|
|
29
|
+
x => x.split('|'),
|
|
30
|
+
this.#matchPermissionSet.bind(this),
|
|
31
|
+
);
|
|
70
32
|
}
|
|
71
33
|
|
|
72
|
-
/**
|
|
73
|
-
* Build a permission checker off of an include, and exclude set
|
|
74
|
-
*
|
|
75
|
-
* @param include Which permissions to include
|
|
76
|
-
* @param exclude Which permissions to exclude
|
|
77
|
-
* @param matchAll Whether not all permissions should be matched
|
|
78
|
-
*/
|
|
79
|
-
static checkPermissions(permissions: Iterable<string>, include: Iterable<string>, exclude: Iterable<string>, mode: 'all' | 'any' = 'any'): void {
|
|
80
|
-
const { check } = this.permissionChecker(include, exclude, mode);
|
|
81
|
-
if (!check(!(permissions instanceof Set) ? new Set(permissions) : permissions)) {
|
|
82
|
-
throw new AppError('Insufficient permissions', 'permissions');
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
34
|
/**
|
|
88
35
|
* Generate a hash for a given value
|
|
89
36
|
*
|