@travetto/auth-web 8.0.0-alpha.0 → 8.0.0-alpha.2
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 +4 -3
- package/package.json +5 -5
- package/src/codec.ts +3 -2
- package/src/config.ts +3 -1
- package/src/interceptors/context.ts +3 -2
package/README.md
CHANGED
|
@@ -110,7 +110,7 @@ export class AppConfig {
|
|
|
110
110
|
The symbol `FB_AUTH` is what will be used to reference providers at runtime. This was chosen, over `class` references due to the fact that most providers will not be defined via a new class, but via an [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L48) method.
|
|
111
111
|
|
|
112
112
|
## Maintaining Auth Context
|
|
113
|
-
The [AuthContextInterceptor](https://github.com/travetto/travetto/tree/main/module/auth-web/src/interceptors/context.ts#L20) acts as the bridge between the [Authentication](https://github.com/travetto/travetto/tree/main/module/auth#readme "Authentication scaffolding for the Travetto framework") and [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications") modules. It serves to take an authenticated principal (via the [WebRequest](https://github.com/travetto/travetto/tree/main/module/web/src/types/request.ts#L11)/[WebResponse](https://github.com/travetto/travetto/tree/main/module/web/src/types/response.ts#L3)) and integrate it into the [AuthContext](https://github.com/travetto/travetto/tree/main/module/auth/src/context.ts#L14). Leveraging [WebAuthConfig](https://github.com/travetto/travetto/tree/main/module/auth-web/src/config.ts#
|
|
113
|
+
The [AuthContextInterceptor](https://github.com/travetto/travetto/tree/main/module/auth-web/src/interceptors/context.ts#L20) acts as the bridge between the [Authentication](https://github.com/travetto/travetto/tree/main/module/auth#readme "Authentication scaffolding for the Travetto framework") and [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications") modules. It serves to take an authenticated principal (via the [WebRequest](https://github.com/travetto/travetto/tree/main/module/web/src/types/request.ts#L11)/[WebResponse](https://github.com/travetto/travetto/tree/main/module/web/src/types/response.ts#L3)) and integrate it into the [AuthContext](https://github.com/travetto/travetto/tree/main/module/auth/src/context.ts#L14). Leveraging [WebAuthConfig](https://github.com/travetto/travetto/tree/main/module/auth-web/src/config.ts#L9)'s configuration allows for basic control of how the principal is encoded and decoded, primarily with the choice between using a header or a cookie, and which header, or cookie value is specifically referenced. Additionally, the encoding process allows for auto-renewing of the token (on by default). The information is encoded into the [JWT](https://jwt.io/) appropriately, and when encoding using cookies, is also set as the expiry time for the cookie.
|
|
114
114
|
|
|
115
115
|
**Note for Cookie Use:** The automatic renewal, update, seamless receipt and transmission of the [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7) cookie act as a light-weight session. Generally the goal is to keep the token as small as possible, but for small amounts of data, this pattern proves to be fairly sufficient at maintaining a decentralized state.
|
|
116
116
|
|
|
@@ -121,7 +121,7 @@ The [PrincipalCodec](https://github.com/travetto/travetto/tree/main/module/auth-
|
|
|
121
121
|
import type { Jwt, Verifier, SupportedAlgorithms } from 'njwt';
|
|
122
122
|
|
|
123
123
|
import { type AuthContext, AuthenticationError, type AuthToken, type Principal } from '@travetto/auth';
|
|
124
|
-
import { Injectable, Inject } from '@travetto/di';
|
|
124
|
+
import { Injectable, Inject, PostConstruct } from '@travetto/di';
|
|
125
125
|
import { type WebResponse, type WebRequest, type WebAsyncContext, CookieJar } from '@travetto/web';
|
|
126
126
|
import { RuntimeError, castTo, TimeUtil } from '@travetto/runtime';
|
|
127
127
|
|
|
@@ -146,7 +146,8 @@ export class JWTPrincipalCodec implements PrincipalCodec {
|
|
|
146
146
|
#verifier: Verifier;
|
|
147
147
|
#algorithm: SupportedAlgorithms = 'HS256';
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
@PostConstruct()
|
|
150
|
+
async finalizeVerifier(): Promise<void> {
|
|
150
151
|
// Weird issue with their ES module support
|
|
151
152
|
const { default: { createVerifier } } = await import('njwt');
|
|
152
153
|
this.#verifier = createVerifier()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/auth-web",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Web authentication integration support for the Travetto framework",
|
|
6
6
|
"keywords": [
|
|
@@ -27,13 +27,13 @@
|
|
|
27
27
|
"directory": "module/auth-web"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/auth": "^8.0.0-alpha.
|
|
31
|
-
"@travetto/config": "^8.0.0-alpha.
|
|
32
|
-
"@travetto/web": "^8.0.0-alpha.
|
|
30
|
+
"@travetto/auth": "^8.0.0-alpha.2",
|
|
31
|
+
"@travetto/config": "^8.0.0-alpha.2",
|
|
32
|
+
"@travetto/web": "^8.0.0-alpha.2",
|
|
33
33
|
"njwt": "^2.0.1"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@travetto/test": "^8.0.0-alpha.
|
|
36
|
+
"@travetto/test": "^8.0.0-alpha.2"
|
|
37
37
|
},
|
|
38
38
|
"peerDependenciesMeta": {
|
|
39
39
|
"@travetto/test": {
|
package/src/codec.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Jwt, Verifier, SupportedAlgorithms } from 'njwt';
|
|
2
2
|
|
|
3
3
|
import { type AuthContext, AuthenticationError, type AuthToken, type Principal } from '@travetto/auth';
|
|
4
|
-
import { Injectable, Inject } from '@travetto/di';
|
|
4
|
+
import { Injectable, Inject, PostConstruct } from '@travetto/di';
|
|
5
5
|
import { type WebResponse, type WebRequest, type WebAsyncContext, CookieJar } from '@travetto/web';
|
|
6
6
|
import { RuntimeError, castTo, TimeUtil } from '@travetto/runtime';
|
|
7
7
|
|
|
@@ -26,7 +26,8 @@ export class JWTPrincipalCodec implements PrincipalCodec {
|
|
|
26
26
|
#verifier: Verifier;
|
|
27
27
|
#algorithm: SupportedAlgorithms = 'HS256';
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
@PostConstruct()
|
|
30
|
+
async finalizeVerifier(): Promise<void> {
|
|
30
31
|
// Weird issue with their ES module support
|
|
31
32
|
const { default: { createVerifier } } = await import('njwt');
|
|
32
33
|
this.#verifier = createVerifier()
|
package/src/config.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Config } from '@travetto/config';
|
|
2
|
+
import { PostConstruct } from '@travetto/di';
|
|
2
3
|
import { Runtime, RuntimeError, BinaryMetadataUtil } from '@travetto/runtime';
|
|
3
4
|
import { Ignore, Secret } from '@travetto/schema';
|
|
4
5
|
|
|
@@ -17,7 +18,8 @@ export class WebAuthConfig {
|
|
|
17
18
|
@Ignore()
|
|
18
19
|
keyMap: Record<string, KeyEntry> & { default?: KeyEntry } = {};
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
@PostConstruct()
|
|
22
|
+
finalize(): void {
|
|
21
23
|
if (!this.signingKey && Runtime.production) {
|
|
22
24
|
throw new RuntimeError('The default signing key is only valid for development use, please specify a config value at web.auth.signingKey');
|
|
23
25
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { toConcrete } from '@travetto/runtime';
|
|
2
2
|
import type { WebInterceptor, WebAsyncContext, WebInterceptorCategory, WebChainedContext, WebResponse } from '@travetto/web';
|
|
3
|
-
import { Injectable, Inject, DependencyRegistryIndex } from '@travetto/di';
|
|
3
|
+
import { Injectable, Inject, DependencyRegistryIndex, PostConstruct } from '@travetto/di';
|
|
4
4
|
import type { AuthContext, AuthService, AuthToken, Principal } from '@travetto/auth';
|
|
5
5
|
import { Required } from '@travetto/schema';
|
|
6
6
|
|
|
@@ -37,7 +37,8 @@ export class AuthContextInterceptor implements WebInterceptor {
|
|
|
37
37
|
@Inject()
|
|
38
38
|
webAsyncContext: WebAsyncContext;
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
@PostConstruct()
|
|
41
|
+
async registerContextHandlers(): Promise<void> {
|
|
41
42
|
this.codec ??= await DependencyRegistryIndex.getInstance(toConcrete<PrincipalCodec>(), CommonPrincipalCodecSymbol);
|
|
42
43
|
this.webAsyncContext.registerSource(toConcrete<Principal>(), () => this.authContext.principal);
|
|
43
44
|
this.webAsyncContext.registerSource(toConcrete<AuthToken>(), () => this.authContext.authToken);
|