@travetto/auth-web 6.0.0-rc.5 → 6.0.0-rc.7

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
@@ -59,7 +59,7 @@ export interface Authenticator<T = unknown, C = unknown, P extends Principal = P
59
59
  ```
60
60
 
61
61
  The only required method to be defined is the `authenticate` method. This takes in a pre-principal payload and a filter context with a [WebRequest](https://github.com/travetto/travetto/tree/main/module/web/src/types/request.ts#L11), and is responsible for:
62
- * Returning an [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7) if authentication was successful
62
+ * Returning an [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L8) if authentication was successful
63
63
  * Throwing an error if it failed
64
64
  * Returning undefined if the authentication is multi-staged and has not completed yet
65
65
 
@@ -112,9 +112,9 @@ The symbol `FB_AUTH` is what will be used to reference providers at runtime. Th
112
112
  ## Maintaining Auth Context
113
113
  The [AuthContextInterceptor](https://github.com/travetto/travetto/tree/main/module/auth-web/src/interceptors/context.ts#L19) 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 api for Web Applications with support for the dependency injection.") 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
- **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.
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#L8) 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
 
117
- The [PrincipalCodec](https://github.com/travetto/travetto/tree/main/module/auth-web/src/types.ts#L10) contract is the primary interface for reading and writing [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7) data out of the [WebRequest](https://github.com/travetto/travetto/tree/main/module/web/src/types/request.ts#L11). This contract is flexible by design, allowing for all sorts of usage. [JWTPrincipalCodec](https://github.com/travetto/travetto/tree/main/module/auth-web/src/codec.ts#L15) is the default [PrincipalCodec](https://github.com/travetto/travetto/tree/main/module/auth-web/src/types.ts#L10), leveraging [JWT](https://jwt.io/)s for encoding/decoding the principal information.
117
+ The [PrincipalCodec](https://github.com/travetto/travetto/tree/main/module/auth-web/src/types.ts#L10) contract is the primary interface for reading and writing [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L8) data out of the [WebRequest](https://github.com/travetto/travetto/tree/main/module/web/src/types/request.ts#L11). This contract is flexible by design, allowing for all sorts of usage. [JWTPrincipalCodec](https://github.com/travetto/travetto/tree/main/module/auth-web/src/codec.ts#L15) is the default [PrincipalCodec](https://github.com/travetto/travetto/tree/main/module/auth-web/src/types.ts#L10), leveraging [JWT](https://jwt.io/)s for encoding/decoding the principal information.
118
118
 
119
119
  **Code: JWTPrincipalCodec**
120
120
  ```typescript
@@ -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#L25) and [@Unauthenticated](https://github.com/travetto/travetto/tree/main/module/auth-web/src/decorator.ts#L37) 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#L61) on an [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) class.
290
+ [@Authenticated](https://github.com/travetto/travetto/tree/main/module/auth-web/src/decorator.ts#L25) and [@Unauthenticated](https://github.com/travetto/travetto/tree/main/module/auth-web/src/decorator.ts#L37) 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#L8) 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#L61) on an [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) 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": "6.0.0-rc.5",
3
+ "version": "6.0.0-rc.7",
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": "^6.0.0-rc.2",
29
+ "@travetto/auth": "^6.0.0-rc.3",
30
30
  "@travetto/config": "^6.0.0-rc.2",
31
- "@travetto/web": "^6.0.0-rc.3",
31
+ "@travetto/web": "^6.0.0-rc.5",
32
32
  "njwt": "^2.0.1"
33
33
  },
34
34
  "peerDependencies": {
35
- "@travetto/test": "^6.0.0-rc.2"
35
+ "@travetto/test": "^6.0.0-rc.3"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "@travetto/test": {
@@ -1,7 +1,7 @@
1
1
  import timers from 'node:timers/promises';
2
2
  import assert from 'node:assert';
3
3
 
4
- import { Controller, Get, WebHeaders, WebResponse, Post } from '@travetto/web';
4
+ import { Controller, Get, WebHeaders, WebResponse, Post, Cookie, CookieJar } from '@travetto/web';
5
5
  import { Suite, Test } from '@travetto/test';
6
6
  import { DependencyRegistry, Inject, InjectableFactory } from '@travetto/di';
7
7
  import { AuthenticationError, Authenticator, AuthContext, AuthConfig } from '@travetto/auth';
@@ -90,16 +90,16 @@ export abstract class AuthWebServerSuite extends BaseWebSuite {
90
90
  @Inject()
91
91
  config: WebAuthConfig;
92
92
 
93
- getCookie(headers: WebHeaders): string | undefined {
94
- return headers.getSetCookie()[0];
93
+ getCookie(headers: WebHeaders): Cookie | undefined {
94
+ return new CookieJar().importSetCookieHeader(headers.getSetCookie()).getAll()[0];
95
95
  }
96
96
 
97
- getCookieValue(headers: WebHeaders): string | undefined {
98
- return this.getCookie(headers)?.split(';')[0];
97
+ getCookieHeader(headers: WebHeaders): string | undefined {
98
+ return new CookieJar().importSetCookieHeader(headers.getSetCookie()).exportCookieHeader();
99
99
  }
100
100
 
101
101
  getCookieExpires(headers: WebHeaders): Date | undefined {
102
- const v = this.getCookie(headers)?.match('expires=([^;]+)(;|$)')?.[1];
102
+ const v = this.getCookie(headers)?.expires;
103
103
  return v ? new Date(v) : undefined;
104
104
  }
105
105
 
@@ -152,7 +152,7 @@ export abstract class AuthWebServerSuite extends BaseWebSuite {
152
152
  }
153
153
  }, false);
154
154
  assert(statusCode === 201);
155
- const cookie = this.getCookieValue(headers);
155
+ const cookie = this.getCookieHeader(headers);
156
156
  assert(cookie);
157
157
 
158
158
  const { context: { httpStatusCode: lastStatus } } = await this.request({
@@ -225,7 +225,7 @@ export abstract class AuthWebServerSuite extends BaseWebSuite {
225
225
  }
226
226
  }, false);
227
227
  assert(statusCode === 201);
228
- const cookie = this.getCookieValue(headers);
228
+ const cookie = this.getCookieHeader(headers);
229
229
  assert(cookie);
230
230
 
231
231
  const { body, context: { httpStatusCode: lastStatus } } = await this.request({
@@ -253,7 +253,7 @@ export abstract class AuthWebServerSuite extends BaseWebSuite {
253
253
  assert(statusCode === 201);
254
254
 
255
255
  const start = Date.now();
256
- const cookie = this.getCookieValue(headers);
256
+ const cookie = this.getCookieHeader(headers);
257
257
  assert(cookie);
258
258
 
259
259
  const expires = this.getCookieExpires(headers);