@travetto/auth 6.0.0-rc.0 → 6.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 CHANGED
@@ -82,9 +82,6 @@ export interface Authenticator<T = unknown, C = unknown, P extends Principal = P
82
82
 
83
83
  The [Authenticator Contract](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authenticator.ts#L14) only requires one method to be defined, and that is `authenticate`. This method receives a generic payload, and a supplemental context as an input. The interface is responsible for converting that to an authenticated principal.
84
84
 
85
- ### Example
86
- The [JWT](https://github.com/travetto/travetto/tree/main/module/jwt#readme "JSON Web Token implementation") module is a good example of an authenticator. This is a common use case for simple internal auth.
87
-
88
85
  ## Authorization Contract
89
86
 
90
87
  **Code: Authorizer Contract**
@@ -153,18 +150,8 @@ When working with framework's authentication, the authenticated information is e
153
150
 
154
151
  **Code: Auth Context Outline**
155
152
  ```typescript
156
- type AuthContextShape = {
157
- principal?: Principal;
158
- authToken?: AuthToken;
159
- authenticatorState?: AuthenticatorState;
160
- };
161
- @Injectable()
162
153
  export class AuthContext {
163
154
  @Inject()
164
- /**
165
- * Get the principal, if set
166
- */
167
- get principal(): Principal | undefined;
168
155
  /**
169
156
  * Set principal
170
157
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/auth",
3
- "version": "6.0.0-rc.0",
3
+ "version": "6.0.0-rc.1",
4
4
  "description": "Authentication scaffolding for the Travetto framework",
5
5
  "keywords": [
6
6
  "authentication",
@@ -23,7 +23,7 @@
23
23
  "directory": "module/auth"
24
24
  },
25
25
  "dependencies": {
26
- "@travetto/context": "^6.0.0-rc.0"
26
+ "@travetto/context": "^6.0.0-rc.1"
27
27
  },
28
28
  "travetto": {
29
29
  "displayName": "Authentication"
package/src/context.ts CHANGED
@@ -6,68 +6,62 @@ import { AuthToken } from './types/token';
6
6
  import { Principal } from './types/principal';
7
7
  import { AuthenticatorState } from './types/authenticator';
8
8
 
9
- type AuthContextShape = {
10
- principal?: Principal;
11
- authToken?: AuthToken;
12
- authenticatorState?: AuthenticatorState;
13
- };
14
-
9
+ /**
10
+ * Provides the primary context for the authenticated state
11
+ *
12
+ *
13
+ * Will silently fail on reads, but will error on writes if the context is not established.
14
+ */
15
15
  @Injectable()
16
16
  export class AuthContext {
17
17
 
18
- #value = new AsyncContextValue<AuthContextShape>(this);
18
+ #principal = new AsyncContextValue<Principal>(this, { failIfUnbound: { write: true } });
19
+ #authToken = new AsyncContextValue<AuthToken>(this, { failIfUnbound: { write: true } });
20
+ #authState = new AsyncContextValue<AuthenticatorState>(this, { failIfUnbound: { write: true } });
19
21
 
20
22
  @Inject()
21
23
  context: AsyncContext;
22
24
 
23
- /**
24
- * Initialize context
25
- * @private
26
- */
27
- init(): void {
28
- this.#value.set({});
29
- }
30
-
31
25
  /**
32
26
  * Get the principal, if set
33
27
  */
34
28
  get principal(): Principal | undefined {
35
- return this.#value.get()?.principal;
29
+ return this.#principal.get();
36
30
  }
37
31
 
38
32
  /**
39
33
  * Set principal
40
34
  */
41
35
  set principal(p: Principal | undefined) {
42
- this.#value.get()!.principal = p;
36
+ this.#principal.set(p);
43
37
  }
44
38
 
45
39
  /**
46
40
  * Get the authentication token, if it exists
47
41
  */
48
42
  get authToken(): AuthToken | undefined {
49
- return this.#value.get()?.authToken;
43
+ return this.#authToken.get();
50
44
  }
51
45
 
52
46
  /**
53
47
  * Set/overwrite the user's authentication token
54
48
  */
55
49
  set authToken(token: AuthToken | undefined) {
56
- this.#value.get()!.authToken = token;
50
+ this.#authToken.set(token);
57
51
  }
58
52
 
59
53
  /**
60
54
  * Get the authenticator state, if it exists
61
55
  */
62
56
  get authenticatorState(): AuthenticatorState | undefined {
63
- return this.#value.get()?.authenticatorState;
57
+ return this.#authState.get();
64
58
  }
65
59
 
66
60
  /**
67
61
  * Set/overwrite the authenticator state
68
62
  */
69
63
  set authenticatorState(state: AuthenticatorState | undefined) {
70
- this.#value.get()!.authenticatorState = state;
64
+ this.#authState.set(state);
71
65
  }
72
66
 
73
67
  /**
@@ -75,6 +69,8 @@ export class AuthContext {
75
69
  * @private
76
70
  */
77
71
  async clear(): Promise<void> {
78
- this.#value.set(undefined);
72
+ this.#principal.set(undefined);
73
+ this.#authToken.set(undefined);
74
+ this.#authState.set(undefined);
79
75
  }
80
76
  }
@@ -2,7 +2,7 @@ import { AnyMap } from '@travetto/runtime';
2
2
 
3
3
  /**
4
4
  * A user principal, including permissions and details
5
- * @augments `@travetto/rest:Context`
5
+ * @augments `@travetto/rest:ContextParam`
6
6
  * @concrete ../internal/types#PrincipalTarget
7
7
  */
8
8
  export interface Principal<D = AnyMap> {
@@ -1 +1,7 @@
1
- export type AuthToken = { value: string, type: string };
1
+ /**
2
+ * Represents the authenticated token used, allows for reuse on subsequent calls
3
+ */
4
+ export interface AuthToken {
5
+ value: string;
6
+ type: string;
7
+ };