@travetto/auth-web-session 6.0.0-rc.4

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 ADDED
@@ -0,0 +1,85 @@
1
+ <!-- This file was generated by @travetto/doc and should not be modified directly -->
2
+ <!-- Please modify https://github.com/travetto/travetto/tree/main/module/auth-web-session/DOC.tsx and execute "npx trv doc" to rebuild -->
3
+ # Web Auth Session
4
+
5
+ ## Web authentication session integration support for the Travetto framework
6
+
7
+ **Install: @travetto/auth-web-session**
8
+ ```bash
9
+ npm install @travetto/auth-web-session
10
+
11
+ # or
12
+
13
+ yarn add @travetto/auth-web-session
14
+ ```
15
+
16
+ One of [Web Auth](https://github.com/travetto/travetto/tree/main/module/auth-web#readme "Web authentication integration support for the Travetto framework")'s main responsibility is being able to send, validate and receive authentication/authorization information from the client.
17
+
18
+ This module's main responsibilities is to expose [Auth Session](https://github.com/travetto/travetto/tree/main/module/auth-session#readme "Session provider for the travetto auth module.")'s data within the scope of an authenticated request flow.
19
+
20
+ **Code: Anatomy of the Session Interceptor**
21
+ ```typescript
22
+ export class AuthSessionInterceptor implements WebInterceptor {
23
+
24
+ category: WebInterceptorCategory = 'application';
25
+ dependsOn = [AuthContextInterceptor];
26
+
27
+ @Inject()
28
+ service: SessionService;
29
+
30
+ @Inject()
31
+ context: SessionContext;
32
+
33
+ @Inject()
34
+ webAsyncContext: WebAsyncContext;
35
+
36
+ postConstruct(): void {
37
+ this.webAsyncContext.registerSource(toConcrete<Session>(), () => this.context.get(true));
38
+ this.webAsyncContext.registerSource(toConcrete<SessionData>(), () => this.context.get(true).data);
39
+ }
40
+
41
+ async filter({ next }: WebChainedContext): Promise<WebResponse> {
42
+ try {
43
+ await this.service.load();
44
+ return await next();
45
+ } finally {
46
+ await this.service.persist();
47
+ }
48
+ }
49
+ }
50
+ ```
51
+
52
+ Once operating within the [Session](https://github.com/travetto/travetto/tree/main/module/auth-session/src/session.ts#L6) boundaries, the session state can be injected via [@ContextParam](https://github.com/travetto/travetto/tree/main/module/web/src/decorator/param.ts#L61)s, injected as [SessionContext](https://github.com/travetto/travetto/tree/main/module/auth-session/src/context.ts#L11), or accessed via the [SessionService](https://github.com/travetto/travetto/tree/main/module/auth-session/src/service.ts#L14).
53
+
54
+ **Code: Sample Usage**
55
+ ```typescript
56
+ @Authenticated()
57
+ @Controller('/session')
58
+ export class SessionEndpoints {
59
+
60
+ @Inject()
61
+ session: SessionContext;
62
+
63
+ @ContextParam()
64
+ data: SessionData;
65
+
66
+ @Put('/info')
67
+ async storeInfo() {
68
+ if (this.data) {
69
+ this.data.age = 20;
70
+ this.data.name = 'Roger'; // Setting data
71
+ }
72
+ }
73
+
74
+ @Get('/logout')
75
+ async logout() {
76
+ await this.session.destroy();
77
+ }
78
+
79
+ @Get('/info/age')
80
+ async getInfo() {
81
+ const { data } = this.session.get(true);
82
+ return data?.age;
83
+ }
84
+ }
85
+ ```
package/__index__.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './src/interceptor.ts';
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@travetto/auth-web-session",
3
+ "version": "6.0.0-rc.4",
4
+ "description": "Web authentication session integration support for the Travetto framework",
5
+ "keywords": [
6
+ "authentication",
7
+ "session",
8
+ "web",
9
+ "travetto",
10
+ "typescript"
11
+ ],
12
+ "homepage": "https://travetto.io",
13
+ "license": "MIT",
14
+ "author": {
15
+ "email": "travetto.framework@gmail.com",
16
+ "name": "Travetto Framework"
17
+ },
18
+ "files": [
19
+ "__index__.ts",
20
+ "src"
21
+ ],
22
+ "main": "__index__.ts",
23
+ "repository": {
24
+ "url": "git+https://github.com/travetto/travetto.git",
25
+ "directory": "module/auth-web-session"
26
+ },
27
+ "dependencies": {
28
+ "@travetto/auth": "^6.0.0-rc.2",
29
+ "@travetto/auth-session": "^6.0.0-rc.2",
30
+ "@travetto/auth-web": "^6.0.0-rc.4",
31
+ "@travetto/di": "^6.0.0-rc.2",
32
+ "@travetto/web": "^6.0.0-rc.2"
33
+ },
34
+ "travetto": {
35
+ "displayName": "Web Auth Session"
36
+ },
37
+ "private": false,
38
+ "publishConfig": {
39
+ "access": "public"
40
+ }
41
+ }
@@ -0,0 +1,38 @@
1
+ import { toConcrete } from '@travetto/runtime';
2
+ import { Injectable, Inject } from '@travetto/di';
3
+ import { WebInterceptor, WebAsyncContext, WebInterceptorCategory, WebChainedContext, WebResponse } from '@travetto/web';
4
+ import { Session, SessionContext, SessionData, SessionService } from '@travetto/auth-session';
5
+ import { AuthContextInterceptor } from '@travetto/auth-web';
6
+
7
+ /**
8
+ * Loads session, and provides ability to create session as needed, persists when complete.
9
+ */
10
+ @Injectable()
11
+ export class AuthSessionInterceptor implements WebInterceptor {
12
+
13
+ category: WebInterceptorCategory = 'application';
14
+ dependsOn = [AuthContextInterceptor];
15
+
16
+ @Inject()
17
+ service: SessionService;
18
+
19
+ @Inject()
20
+ context: SessionContext;
21
+
22
+ @Inject()
23
+ webAsyncContext: WebAsyncContext;
24
+
25
+ postConstruct(): void {
26
+ this.webAsyncContext.registerSource(toConcrete<Session>(), () => this.context.get(true));
27
+ this.webAsyncContext.registerSource(toConcrete<SessionData>(), () => this.context.get(true).data);
28
+ }
29
+
30
+ async filter({ next }: WebChainedContext): Promise<WebResponse> {
31
+ try {
32
+ await this.service.load();
33
+ return await next();
34
+ } finally {
35
+ await this.service.persist();
36
+ }
37
+ }
38
+ }