@travetto/auth-web 6.0.3 → 7.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
|
@@ -107,10 +107,10 @@ export class AppConfig {
|
|
|
107
107
|
}
|
|
108
108
|
```
|
|
109
109
|
|
|
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#
|
|
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#L47) 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#
|
|
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#L8)'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
|
|
|
@@ -287,7 +287,7 @@ export class SampleAuth {
|
|
|
287
287
|
}
|
|
288
288
|
```
|
|
289
289
|
|
|
290
|
-
[@Authenticated](https://github.com/travetto/travetto/tree/main/module/auth-web/src/decorator.ts#L29) and [@Unauthenticated](https://github.com/travetto/travetto/tree/main/module/auth-web/src/decorator.ts#L45) will simply enforce whether or not a user is logged in and throw the appropriate error messages as needed. Additionally, the [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7) is accessible as a resource that can be exposed as a [@ContextParam](https://github.com/travetto/travetto/tree/main/module/web/src/decorator/param.ts#
|
|
290
|
+
[@Authenticated](https://github.com/travetto/travetto/tree/main/module/auth-web/src/decorator.ts#L29) and [@Unauthenticated](https://github.com/travetto/travetto/tree/main/module/auth-web/src/decorator.ts#L45) will simply enforce whether or not a user is logged in and throw the appropriate error messages as needed. Additionally, the [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7) is accessible as a resource that can be exposed as a [@ContextParam](https://github.com/travetto/travetto/tree/main/module/web/src/decorator/param.ts#L66) on an [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15) class.
|
|
291
291
|
|
|
292
292
|
## Multi-Step Login
|
|
293
293
|
When authenticating, with a multi-step process, it is useful to share information between steps. The `authenticatorState` of [AuthContext](https://github.com/travetto/travetto/tree/main/module/auth/src/context.ts#L14) field is intended to be a location in which that information is persisted. Currently only [passport](http://passportjs.org) support is included, when dealing with multi-step logins. This information can also be injected into a web endpoint method, using the [AuthenticatorState](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authenticator.ts#L9) type;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/auth-web",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-rc.1",
|
|
4
4
|
"description": "Web authentication integration support for the Travetto framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authentication",
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
"directory": "module/auth-web"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/auth": "^
|
|
30
|
-
"@travetto/config": "^
|
|
31
|
-
"@travetto/web": "^
|
|
29
|
+
"@travetto/auth": "^7.0.0-rc.0",
|
|
30
|
+
"@travetto/config": "^7.0.0-rc.0",
|
|
31
|
+
"@travetto/web": "^7.0.0-rc.1",
|
|
32
32
|
"njwt": "^2.0.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@travetto/test": "^
|
|
35
|
+
"@travetto/test": "^7.0.0-rc.0"
|
|
36
36
|
},
|
|
37
37
|
"peerDependenciesMeta": {
|
|
38
38
|
"@travetto/test": {
|
package/src/decorator.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ControllerRegistryIndex, EndpointDecorator } from '@travetto/web';
|
|
2
2
|
|
|
3
3
|
import { AuthVerifyInterceptor } from './interceptors/verify.ts';
|
|
4
4
|
import { AuthLoginInterceptor } from './interceptors/login.ts';
|
|
@@ -8,10 +8,10 @@ import { AuthLogoutInterceptor } from './interceptors/logout.ts';
|
|
|
8
8
|
* Authenticate an endpoint with a list of available identity sources
|
|
9
9
|
* @param source The symbol to target the specific authenticator
|
|
10
10
|
* @param sources Additional providers to support
|
|
11
|
-
* @
|
|
11
|
+
* @kind decorator
|
|
12
12
|
*/
|
|
13
13
|
export function Login(source: symbol, ...sources: symbol[]): EndpointDecorator {
|
|
14
|
-
return
|
|
14
|
+
return ControllerRegistryIndex.createInterceptorConfigDecorator(AuthLoginInterceptor, {
|
|
15
15
|
providers: [source, ...sources],
|
|
16
16
|
applies: true
|
|
17
17
|
}, {
|
|
@@ -24,10 +24,10 @@ export function Login(source: symbol, ...sources: symbol[]): EndpointDecorator {
|
|
|
24
24
|
/**
|
|
25
25
|
* Ensure the controller/endpoint is authenticated, give a set of permissions
|
|
26
26
|
* @param permissions Set of required/disallowed permissions
|
|
27
|
-
* @
|
|
27
|
+
* @kind decorator
|
|
28
28
|
*/
|
|
29
29
|
export function Authenticated(permissions: string[] = []): EndpointDecorator {
|
|
30
|
-
return
|
|
30
|
+
return ControllerRegistryIndex.createInterceptorConfigDecorator(AuthVerifyInterceptor, {
|
|
31
31
|
state: 'authenticated',
|
|
32
32
|
permissions,
|
|
33
33
|
applies: true,
|
|
@@ -40,10 +40,10 @@ export function Authenticated(permissions: string[] = []): EndpointDecorator {
|
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
42
|
* Require the controller/endpoint to be unauthenticated
|
|
43
|
-
* @
|
|
43
|
+
* @kind decorator
|
|
44
44
|
*/
|
|
45
45
|
export function Unauthenticated(): EndpointDecorator {
|
|
46
|
-
return
|
|
46
|
+
return ControllerRegistryIndex.createInterceptorConfigDecorator(AuthVerifyInterceptor, {
|
|
47
47
|
state: 'unauthenticated',
|
|
48
48
|
applies: true
|
|
49
49
|
});
|
|
@@ -51,10 +51,10 @@ export function Unauthenticated(): EndpointDecorator {
|
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Logs a user out of the auth state
|
|
54
|
-
* @
|
|
54
|
+
* @kind decorator
|
|
55
55
|
*/
|
|
56
56
|
export function Logout(): EndpointDecorator {
|
|
57
|
-
return
|
|
57
|
+
return ControllerRegistryIndex.createInterceptorConfigDecorator(AuthLogoutInterceptor, { applies: true }, {
|
|
58
58
|
responseContext: {
|
|
59
59
|
isPrivate: true
|
|
60
60
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { toConcrete } from '@travetto/runtime';
|
|
2
2
|
import { WebInterceptor, WebAsyncContext, WebInterceptorCategory, WebChainedContext, WebResponse } from '@travetto/web';
|
|
3
|
-
import { Injectable, Inject,
|
|
3
|
+
import { Injectable, Inject, DependencyRegistryIndex } from '@travetto/di';
|
|
4
4
|
import { AuthContext, AuthService, AuthToken, Principal } from '@travetto/auth';
|
|
5
|
+
import { Required } from '@travetto/schema';
|
|
5
6
|
|
|
6
7
|
import { CommonPrincipalCodecSymbol, PrincipalCodec } from '../types.ts';
|
|
7
8
|
import { WebAuthConfig } from '../config.ts';
|
|
@@ -20,7 +21,8 @@ export class AuthContextInterceptor implements WebInterceptor {
|
|
|
20
21
|
|
|
21
22
|
category: WebInterceptorCategory = 'application';
|
|
22
23
|
|
|
23
|
-
@Inject(
|
|
24
|
+
@Inject()
|
|
25
|
+
@Required(false)
|
|
24
26
|
codec: PrincipalCodec;
|
|
25
27
|
|
|
26
28
|
@Inject()
|
|
@@ -36,7 +38,7 @@ export class AuthContextInterceptor implements WebInterceptor {
|
|
|
36
38
|
webAsyncContext: WebAsyncContext;
|
|
37
39
|
|
|
38
40
|
async postConstruct(): Promise<void> {
|
|
39
|
-
this.codec ??= await
|
|
41
|
+
this.codec ??= await DependencyRegistryIndex.getInstance(toConcrete<PrincipalCodec>(), CommonPrincipalCodecSymbol);
|
|
40
42
|
this.webAsyncContext.registerSource(toConcrete<Principal>(), () => this.authContext.principal);
|
|
41
43
|
this.webAsyncContext.registerSource(toConcrete<AuthToken>(), () => this.authContext.authToken);
|
|
42
44
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Schema,
|
|
2
|
-
import { toConcrete, AnyMap
|
|
1
|
+
import { Schema, SchemaRegistryIndex } from '@travetto/schema';
|
|
2
|
+
import { toConcrete, AnyMap } from '@travetto/runtime';
|
|
3
3
|
import { Principal } from '@travetto/auth';
|
|
4
4
|
|
|
5
5
|
@Schema()
|
|
@@ -13,7 +13,6 @@ export class PrincipalSchema implements Principal {
|
|
|
13
13
|
permissions?: string[] | undefined;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
SchemaRegistry.getOrCreatePending(PrincipalSchema)
|
|
16
|
+
SchemaRegistryIndex.getForRegister(toConcrete<Principal>()).register(
|
|
17
|
+
SchemaRegistryIndex.getConfig(PrincipalSchema)
|
|
19
18
|
);
|
package/support/test/server.ts
CHANGED
|
@@ -3,7 +3,7 @@ import assert from 'node:assert';
|
|
|
3
3
|
|
|
4
4
|
import { Controller, Get, WebHeaders, WebResponse, Post, Cookie, CookieJar } from '@travetto/web';
|
|
5
5
|
import { Suite, Test } from '@travetto/test';
|
|
6
|
-
import {
|
|
6
|
+
import { DependencyRegistryIndex, Inject, InjectableFactory } from '@travetto/di';
|
|
7
7
|
import { AuthenticationError, Authenticator, AuthContext, AuthConfig } from '@travetto/auth';
|
|
8
8
|
|
|
9
9
|
import { InjectableSuite } from '@travetto/di/support/test/suite.ts';
|
|
@@ -235,7 +235,7 @@ export abstract class AuthWebServerSuite extends BaseWebSuite {
|
|
|
235
235
|
assert(lastStatus === 200);
|
|
236
236
|
assert(typeof body === 'string');
|
|
237
237
|
|
|
238
|
-
const codec = await
|
|
238
|
+
const codec = await DependencyRegistryIndex.getInstance(JWTPrincipalCodec, CommonPrincipalCodecSymbol);
|
|
239
239
|
await assert.doesNotReject(() => codec.verify(body));
|
|
240
240
|
}
|
|
241
241
|
|