@travetto/auth-model 8.0.0-alpha.21 → 8.0.0-alpha.23
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 +22 -21
- package/__index__.ts +1 -1
- package/package.json +4 -4
- package/src/model.ts +4 -10
- package/src/util.ts +3 -4
- package/support/test/model.ts +5 -5
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ yarn add @travetto/auth-model
|
|
|
15
15
|
|
|
16
16
|
This module supports the integration between the [Authentication](https://github.com/travetto/travetto/tree/main/module/auth#readme "Authentication support for the Travetto framework") module and the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.").
|
|
17
17
|
|
|
18
|
-
The asset module requires a [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#
|
|
18
|
+
The asset module requires a [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L10)-model to provide functionality for reading and storing user information. You can use any existing providers to serve as your [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L10), or you can roll your own.
|
|
19
19
|
|
|
20
20
|
**Install: provider**
|
|
21
21
|
```bash
|
|
@@ -25,7 +25,7 @@ npm install @travetto/model-{provider}
|
|
|
25
25
|
|
|
26
26
|
yarn add @travetto/model-{provider}
|
|
27
27
|
```
|
|
28
|
-
Currently, the following are packages that provide [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#
|
|
28
|
+
Currently, the following are packages that provide [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L10):
|
|
29
29
|
* [DynamoDB Model Support](https://github.com/travetto/travetto/tree/main/module/model-dynamodb#readme "DynamoDB backing for the travetto model module.") - @travetto/model-dynamodb
|
|
30
30
|
* [Elasticsearch Model Source](https://github.com/travetto/travetto/tree/main/module/model-elasticsearch#readme "Elasticsearch backing for the travetto model module, with real-time modeling support for Elasticsearch mappings.") - @travetto/model-elasticsearch
|
|
31
31
|
* [Firestore Model Support](https://github.com/travetto/travetto/tree/main/module/model-firestore#readme "Firestore backing for the travetto model module.") - @travetto/model-firestore
|
|
@@ -38,7 +38,7 @@ Currently, the following are packages that provide [CRUD](https://github.com/tra
|
|
|
38
38
|
* [Memory Model Support](https://github.com/travetto/travetto/tree/main/module/model-memory#readme "Memory backing for the travetto model module.") - @travetto/model-memory
|
|
39
39
|
* [File Model Support](https://github.com/travetto/travetto/tree/main/module/model-file#readme "File system backing for the travetto model module.") - @travetto/model-file
|
|
40
40
|
|
|
41
|
-
The module itself is fairly straightforward, and truly the only integration point for this module to work is defined at the model level.
|
|
41
|
+
The module itself is fairly straightforward, and truly the only integration point for this module to work is defined at the model level. The contract for authentication is established in code as providing translation to and from a [RegisteredPrincipal](https://github.com/travetto/travetto/tree/main/module/auth-model/src/model.ts#L11).
|
|
42
42
|
|
|
43
43
|
A registered principal extends the base concept of an principal, by adding in additional fields needed for local registration, specifically password management information.
|
|
44
44
|
|
|
@@ -70,8 +70,8 @@ export interface RegisteredPrincipal extends Principal {
|
|
|
70
70
|
|
|
71
71
|
**Code: A valid user model**
|
|
72
72
|
```typescript
|
|
73
|
-
import { Model } from '@travetto/model';
|
|
74
73
|
import type { RegisteredPrincipal } from '@travetto/auth-model';
|
|
74
|
+
import { Model } from '@travetto/model';
|
|
75
75
|
|
|
76
76
|
@Model()
|
|
77
77
|
export class User implements RegisteredPrincipal {
|
|
@@ -88,12 +88,12 @@ export class User implements RegisteredPrincipal {
|
|
|
88
88
|
```
|
|
89
89
|
|
|
90
90
|
## Configuration
|
|
91
|
-
Additionally, there exists a common practice of mapping various external security principals into a local contract. These external identities, as provided from countless authentication schemes, need to be homogenized for use.
|
|
91
|
+
Additionally, there exists a common practice of mapping various external security principals into a local contract. These external identities, as provided from countless authentication schemes, need to be homogenized for use. This has been handled in other frameworks by using external configuration, and creating a mapping between the two set of fields. Within this module, the mappings are defined as functions in which you can translate to the model from an identity or to an identity from a model.
|
|
92
92
|
|
|
93
93
|
**Code: Principal Source configuration**
|
|
94
94
|
```typescript
|
|
95
|
-
import { InjectableFactory } from '@travetto/di';
|
|
96
95
|
import { ModelAuthService } from '@travetto/auth-model';
|
|
96
|
+
import { InjectableFactory } from '@travetto/di';
|
|
97
97
|
import type { ModelCrudSupport } from '@travetto/model';
|
|
98
98
|
|
|
99
99
|
import { User } from './model.ts';
|
|
@@ -104,7 +104,8 @@ class AuthConfig {
|
|
|
104
104
|
return new ModelAuthService(
|
|
105
105
|
service,
|
|
106
106
|
User,
|
|
107
|
-
user => ({
|
|
107
|
+
user => ({
|
|
108
|
+
// This converts User to a RegisteredPrincipal
|
|
108
109
|
source: 'model',
|
|
109
110
|
provider: 'model',
|
|
110
111
|
id: user.id!,
|
|
@@ -114,17 +115,18 @@ class AuthConfig {
|
|
|
114
115
|
resetToken: user.resetToken,
|
|
115
116
|
resetExpires: user.resetExpires,
|
|
116
117
|
password: user.password,
|
|
117
|
-
details: user
|
|
118
|
+
details: user
|
|
118
119
|
}),
|
|
119
|
-
user =>
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
120
|
+
user =>
|
|
121
|
+
User.from({
|
|
122
|
+
// This converts a RegisteredPrincipal to a User
|
|
123
|
+
id: user.id,
|
|
124
|
+
permissions: [...(user.permissions || [])],
|
|
125
|
+
hash: user.hash,
|
|
126
|
+
salt: user.salt,
|
|
127
|
+
resetToken: user.resetToken,
|
|
128
|
+
resetExpires: user.resetExpires
|
|
129
|
+
})
|
|
128
130
|
);
|
|
129
131
|
}
|
|
130
132
|
}
|
|
@@ -132,15 +134,14 @@ class AuthConfig {
|
|
|
132
134
|
|
|
133
135
|
**Code: Sample usage**
|
|
134
136
|
```typescript
|
|
135
|
-
import { RuntimeError } from '@travetto/runtime';
|
|
136
|
-
import { Injectable, Inject } from '@travetto/di';
|
|
137
137
|
import type { ModelAuthService } from '@travetto/auth-model';
|
|
138
|
+
import { Inject, Injectable } from '@travetto/di';
|
|
139
|
+
import { RuntimeError } from '@travetto/runtime';
|
|
138
140
|
|
|
139
141
|
import type { User } from './model.ts';
|
|
140
142
|
|
|
141
143
|
@Injectable()
|
|
142
144
|
class UserService {
|
|
143
|
-
|
|
144
145
|
@Inject()
|
|
145
146
|
private auth: ModelAuthService<User>;
|
|
146
147
|
|
|
@@ -181,6 +182,6 @@ export class AuthModelUtil {
|
|
|
181
182
|
* @param salt Salt value, or if a number, length of salt
|
|
182
183
|
* @param validator Optional function to validate your password
|
|
183
184
|
*/
|
|
184
|
-
static async generatePassword(password: string, salt: number | string = 32): Promise<{ salt: string
|
|
185
|
+
static async generatePassword(password: string, salt: number | string = 32): Promise<{ salt: string; hash: string }>;
|
|
185
186
|
}
|
|
186
187
|
```
|
package/__index__.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './src/model.ts';
|
|
2
|
-
export * from './src/util.ts';
|
|
2
|
+
export * from './src/util.ts';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/auth-model",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.23",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Authentication model support for the Travetto framework",
|
|
6
6
|
"keywords": [
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"directory": "module/auth-model"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/auth": "^8.0.0-alpha.
|
|
30
|
-
"@travetto/model": "^8.0.0-alpha.
|
|
29
|
+
"@travetto/auth": "^8.0.0-alpha.20",
|
|
30
|
+
"@travetto/model": "^8.0.0-alpha.23"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/test": "^8.0.0-alpha.
|
|
33
|
+
"@travetto/test": "^8.0.0-alpha.21"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/test": {
|
package/src/model.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AuthenticationError, type Authenticator, type Authorizer, type Principal } from '@travetto/auth';
|
|
2
2
|
import { type ModelCrudSupport, type ModelType, NotFoundError, type OptionalId } from '@travetto/model';
|
|
3
|
-
import { type
|
|
3
|
+
import { type Class, castTo, TimeUtil, Util } from '@travetto/runtime';
|
|
4
4
|
|
|
5
5
|
import { AuthModelUtil } from './util.ts';
|
|
6
6
|
|
|
@@ -38,7 +38,6 @@ type FromPrincipal<T extends ModelType> = (item: Partial<RegisteredPrincipal>) =
|
|
|
38
38
|
* A model-based auth service
|
|
39
39
|
*/
|
|
40
40
|
export class ModelAuthService<T extends ModelType> implements Authenticator<T>, Authorizer {
|
|
41
|
-
|
|
42
41
|
#modelService: ModelCrudSupport;
|
|
43
42
|
#cls: Class<T>;
|
|
44
43
|
|
|
@@ -52,12 +51,7 @@ export class ModelAuthService<T extends ModelType> implements Authenticator<T>,
|
|
|
52
51
|
* @param toPrincipal Convert a model to an principal
|
|
53
52
|
* @param fromPrincipal Convert an identity to the model
|
|
54
53
|
*/
|
|
55
|
-
constructor(
|
|
56
|
-
modelService: ModelCrudSupport,
|
|
57
|
-
cls: Class<T>,
|
|
58
|
-
toPrincipal: ToPrincipal<T>,
|
|
59
|
-
fromPrincipal: FromPrincipal<T>,
|
|
60
|
-
) {
|
|
54
|
+
constructor(modelService: ModelCrudSupport, cls: Class<T>, toPrincipal: ToPrincipal<T>, fromPrincipal: FromPrincipal<T>) {
|
|
61
55
|
this.#modelService = modelService;
|
|
62
56
|
this.#cls = cls;
|
|
63
57
|
this.toPrincipal = toPrincipal;
|
|
@@ -181,4 +175,4 @@ export class ModelAuthService<T extends ModelType> implements Authenticator<T>,
|
|
|
181
175
|
const { id, password } = this.toPrincipal(payload);
|
|
182
176
|
return this.#authenticate(id, password!);
|
|
183
177
|
}
|
|
184
|
-
}
|
|
178
|
+
}
|
package/src/util.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { BinaryUtil, CodecUtil, RuntimeError, Util } from '@travetto/runtime';
|
|
|
4
4
|
* Standard auth utilities
|
|
5
5
|
*/
|
|
6
6
|
export class AuthModelUtil {
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* Generate a hash for a given value
|
|
10
9
|
*
|
|
@@ -28,7 +27,7 @@ export class AuthModelUtil {
|
|
|
28
27
|
name: 'PBKDF2',
|
|
29
28
|
hash: { name: digest },
|
|
30
29
|
salt: BinaryUtil.binaryArrayToBuffer(CodecUtil.fromUTF8String(salt)),
|
|
31
|
-
iterations
|
|
30
|
+
iterations
|
|
32
31
|
},
|
|
33
32
|
hashKey,
|
|
34
33
|
keylen * 8
|
|
@@ -44,7 +43,7 @@ export class AuthModelUtil {
|
|
|
44
43
|
* @param salt Salt value, or if a number, length of salt
|
|
45
44
|
* @param validator Optional function to validate your password
|
|
46
45
|
*/
|
|
47
|
-
static async generatePassword(password: string, salt: number | string = 32): Promise<{ salt: string
|
|
46
|
+
static async generatePassword(password: string, salt: number | string = 32): Promise<{ salt: string; hash: string }> {
|
|
48
47
|
if (!password) {
|
|
49
48
|
throw new RuntimeError('Password is required', { category: 'data' });
|
|
50
49
|
}
|
|
@@ -54,4 +53,4 @@ export class AuthModelUtil {
|
|
|
54
53
|
|
|
55
54
|
return { salt, hash };
|
|
56
55
|
}
|
|
57
|
-
}
|
|
56
|
+
}
|
package/support/test/model.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
|
|
3
|
-
import { RuntimeError, castTo, type Class } from '@travetto/runtime';
|
|
4
|
-
import { Suite, Test } from '@travetto/test';
|
|
5
3
|
import { Inject, InjectableFactory } from '@travetto/di';
|
|
6
|
-
import { type ModelCrudSupport,
|
|
4
|
+
import { Model, type ModelCrudSupport, Transient } from '@travetto/model';
|
|
5
|
+
import { type Class, castTo, RuntimeError } from '@travetto/runtime';
|
|
6
|
+
import { Suite, Test } from '@travetto/test';
|
|
7
|
+
|
|
7
8
|
import { InjectableSuite } from '@travetto/di/support/test/suite.ts';
|
|
8
9
|
import { ModelSuite } from '@travetto/model/support/test/suite.ts';
|
|
9
10
|
|
|
@@ -41,7 +42,6 @@ class TestConfig {
|
|
|
41
42
|
@ModelSuite()
|
|
42
43
|
@InjectableSuite()
|
|
43
44
|
export abstract class AuthModelServiceSuite<T> {
|
|
44
|
-
|
|
45
45
|
serviceClass: Class<T>;
|
|
46
46
|
configClass: Class;
|
|
47
47
|
|
|
@@ -88,4 +88,4 @@ export abstract class AuthModelServiceSuite<T> {
|
|
|
88
88
|
|
|
89
89
|
await assert.doesNotReject(() => this.authService.authenticate(pre));
|
|
90
90
|
}
|
|
91
|
-
}
|
|
91
|
+
}
|