@travetto/auth-session 8.0.0-alpha.22 → 8.0.0-alpha.24
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 +5 -6
- package/__index__.ts +2 -2
- package/package.json +5 -5
- package/src/context.ts +5 -6
- package/src/model.ts +1 -1
- package/src/service.ts +17 -14
- package/src/session.ts +2 -3
- package/support/test/server.ts +8 -9
package/README.md
CHANGED
|
@@ -13,11 +13,11 @@ npm install @travetto/auth-session
|
|
|
13
13
|
yarn add @travetto/auth-session
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
This is a module that adds session support to the [Authentication](https://github.com/travetto/travetto/tree/main/module/auth#readme "Authentication support for the Travetto framework") framework, via [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") storage.
|
|
16
|
+
This is a module that adds session support to the [Authentication](https://github.com/travetto/travetto/tree/main/module/auth#readme "Authentication support for the Travetto framework") framework, via [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") storage. The concept here, is that the [Authentication](https://github.com/travetto/travetto/tree/main/module/auth#readme "Authentication support for the Travetto framework") module provides the solid foundation for ensuring authentication to the system, and transitively to the session data. The [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7) provides a session identifier, which refers to a unique authentication session. Each login will produce a novel session id. This id provides the contract between [Authentication](https://github.com/travetto/travetto/tree/main/module/auth#readme "Authentication support for the Travetto framework") and[Auth Session](https://github.com/travetto/travetto/tree/main/module/auth-session#readme "Session provider for the travetto auth module.").
|
|
17
17
|
|
|
18
|
-
This session identifier, is then used when retrieving data from [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") storage. This storage mechanism is not tied to a request/response model, but the [Web Auth Session](https://github.com/travetto/travetto/tree/main/module/auth-web-session#readme "Web authentication session integration support for the Travetto framework") does provide a natural integration with the [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications") module.
|
|
18
|
+
This session identifier, is then used when retrieving data from [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") storage. This storage mechanism is not tied to a request/response model, but the [Web Auth Session](https://github.com/travetto/travetto/tree/main/module/auth-web-session#readme "Web authentication session integration support for the Travetto framework") does provide a natural integration with the [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications") module.
|
|
19
19
|
|
|
20
|
-
Within the framework the sessions are stored against any [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") implementation that provides [ModelExpirySupport](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10), as the data needs to be able to be expired appropriately.
|
|
20
|
+
Within the framework the sessions are stored against any [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") implementation that provides [ModelExpirySupport](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10), as the data needs to be able to be expired appropriately. The list of supported model providers are:
|
|
21
21
|
* [Redis Model Support](https://github.com/travetto/travetto/tree/main/module/model-redis#readme "Redis backing for the travetto model module.")
|
|
22
22
|
* [MongoDB Model Support](https://github.com/travetto/travetto/tree/main/module/model-mongo#readme "Mongo backing for the travetto model module.")
|
|
23
23
|
* [S3 Model Support](https://github.com/travetto/travetto/tree/main/module/model-s3#readme "S3 backing for the travetto model module.")
|
|
@@ -26,12 +26,11 @@ Within the framework the sessions are stored against any [Data Modeling Support]
|
|
|
26
26
|
* [File Model Support](https://github.com/travetto/travetto/tree/main/module/model-file#readme "File system backing for the travetto model module.")
|
|
27
27
|
* [Memory Model Support](https://github.com/travetto/travetto/tree/main/module/model-memory#readme "Memory backing for the travetto model module.")
|
|
28
28
|
|
|
29
|
-
While the expiry is not necessarily a hard requirement, the implementation without it can be quite messy.
|
|
29
|
+
While the expiry is not necessarily a hard requirement, the implementation without it can be quite messy. To that end, the ability to add [ModelExpirySupport](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10) to the model provider would be the natural extension point if more expiry support is needed.
|
|
30
30
|
|
|
31
31
|
**Code: Sample usage of Session Service**
|
|
32
32
|
```typescript
|
|
33
33
|
export class AuthSessionInterceptor implements WebInterceptor {
|
|
34
|
-
|
|
35
34
|
category: WebInterceptorCategory = 'application';
|
|
36
35
|
dependsOn = [AuthContextInterceptor];
|
|
37
36
|
|
|
@@ -61,7 +60,7 @@ export class AuthSessionInterceptor implements WebInterceptor {
|
|
|
61
60
|
}
|
|
62
61
|
```
|
|
63
62
|
|
|
64
|
-
The [SessionService](https://github.com/travetto/travetto/tree/main/module/auth-session/src/service.ts#L14) provides the basic integration with the [AuthContext](https://github.com/travetto/travetto/tree/main/module/auth/src/context.ts#L14) to authenticate and isolate session data.
|
|
63
|
+
The [SessionService](https://github.com/travetto/travetto/tree/main/module/auth-session/src/service.ts#L14) provides the basic integration with the [AuthContext](https://github.com/travetto/travetto/tree/main/module/auth/src/context.ts#L14) to authenticate and isolate session data. The usage is fairly simple, but the import pattern to follow is:
|
|
65
64
|
* load
|
|
66
65
|
* read/modify
|
|
67
66
|
* persist
|
package/__index__.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/auth-session",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.24",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Session provider for the travetto auth module.",
|
|
6
6
|
"keywords": [
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"directory": "module/auth-session"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/auth": "^8.0.0-alpha.
|
|
30
|
-
"@travetto/config": "^8.0.0-alpha.
|
|
31
|
-
"@travetto/model": "^8.0.0-alpha.
|
|
29
|
+
"@travetto/auth": "^8.0.0-alpha.21",
|
|
30
|
+
"@travetto/config": "^8.0.0-alpha.23",
|
|
31
|
+
"@travetto/model": "^8.0.0-alpha.24"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@travetto/test": "^8.0.0-alpha.
|
|
34
|
+
"@travetto/test": "^8.0.0-alpha.22"
|
|
35
35
|
},
|
|
36
36
|
"peerDependenciesMeta": {
|
|
37
37
|
"@travetto/test": {
|
package/src/context.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Injectable, Inject } from '@travetto/di';
|
|
2
|
-
import { type AsyncContext, AsyncContextValue } from '@travetto/context';
|
|
3
1
|
import { type AuthContext, AuthenticationError } from '@travetto/auth';
|
|
2
|
+
import { type AsyncContext, AsyncContextValue } from '@travetto/context';
|
|
3
|
+
import { Inject, Injectable } from '@travetto/di';
|
|
4
4
|
|
|
5
5
|
import { Session } from './session.ts';
|
|
6
6
|
|
|
@@ -9,7 +9,6 @@ import { Session } from './session.ts';
|
|
|
9
9
|
*/
|
|
10
10
|
@Injectable()
|
|
11
11
|
export class SessionContext {
|
|
12
|
-
|
|
13
12
|
@Inject()
|
|
14
13
|
context: AsyncContext;
|
|
15
14
|
|
|
@@ -28,7 +27,7 @@ export class SessionContext {
|
|
|
28
27
|
expiresAt: principal.expiresAt,
|
|
29
28
|
issuedAt: principal.issuedAt,
|
|
30
29
|
action: 'create',
|
|
31
|
-
data: {}
|
|
30
|
+
data: {}
|
|
32
31
|
});
|
|
33
32
|
}
|
|
34
33
|
|
|
@@ -40,7 +39,7 @@ export class SessionContext {
|
|
|
40
39
|
get(createIfMissing?: boolean): Session | undefined {
|
|
41
40
|
let value = this.#value.get();
|
|
42
41
|
if (!value && createIfMissing) {
|
|
43
|
-
this.set(value = this.#create());
|
|
42
|
+
this.set((value = this.#create()));
|
|
44
43
|
}
|
|
45
44
|
return value;
|
|
46
45
|
}
|
|
@@ -59,4 +58,4 @@ export class SessionContext {
|
|
|
59
58
|
this.get()?.destroy();
|
|
60
59
|
this.set(undefined);
|
|
61
60
|
}
|
|
62
|
-
}
|
|
61
|
+
}
|
package/src/model.ts
CHANGED
package/src/service.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { Injectable, Inject } from '@travetto/di';
|
|
2
|
-
import { JSONUtil } from '@travetto/runtime';
|
|
3
|
-
import { type ModelExpirySupport, NotFoundError } from '@travetto/model';
|
|
4
1
|
import type { AuthContext, AuthService } from '@travetto/auth';
|
|
2
|
+
import { Inject, Injectable } from '@travetto/di';
|
|
3
|
+
import { type ModelExpirySupport, NotFoundError } from '@travetto/model';
|
|
4
|
+
import { JSONUtil } from '@travetto/runtime';
|
|
5
5
|
|
|
6
|
-
import { Session } from './session.ts';
|
|
7
|
-
import { SessionEntry, SessionModelSymbol } from './model.ts';
|
|
8
6
|
import type { SessionContext } from './context.ts';
|
|
7
|
+
import { SessionEntry, SessionModelSymbol } from './model.ts';
|
|
8
|
+
import { Session } from './session.ts';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Service for supporting the session and managing the session state
|
|
12
12
|
*/
|
|
13
13
|
@Injectable()
|
|
14
14
|
export class SessionService {
|
|
15
|
-
|
|
16
15
|
@Inject()
|
|
17
16
|
context: SessionContext;
|
|
18
17
|
|
|
@@ -43,7 +42,7 @@ export class SessionService {
|
|
|
43
42
|
|
|
44
43
|
// Validate session
|
|
45
44
|
if (session.isExpired()) {
|
|
46
|
-
await this.#modelService.delete(SessionEntry, session.id).catch(() => {
|
|
45
|
+
await this.#modelService.delete(SessionEntry, session.id).catch(() => {});
|
|
47
46
|
return new Session({ action: 'destroy' });
|
|
48
47
|
} else {
|
|
49
48
|
return session;
|
|
@@ -78,14 +77,18 @@ export class SessionService {
|
|
|
78
77
|
|
|
79
78
|
// If expiration time has changed, send new session information
|
|
80
79
|
if (session.action === 'create' || session.isChanged()) {
|
|
81
|
-
await this.#modelService.upsert(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
await this.#modelService.upsert(
|
|
81
|
+
SessionEntry,
|
|
82
|
+
SessionEntry.from({
|
|
83
|
+
...session,
|
|
84
|
+
data: JSONUtil.toBase64(session.data)
|
|
85
|
+
})
|
|
86
|
+
);
|
|
85
87
|
}
|
|
86
88
|
// If destroying
|
|
87
|
-
} else if (session.id) {
|
|
88
|
-
|
|
89
|
+
} else if (session.id) {
|
|
90
|
+
// If destroy and id
|
|
91
|
+
await this.#modelService.delete(SessionEntry, session.id).catch(() => {});
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
94
|
|
|
@@ -101,4 +104,4 @@ export class SessionService {
|
|
|
101
104
|
}
|
|
102
105
|
return this.context.get();
|
|
103
106
|
}
|
|
104
|
-
}
|
|
107
|
+
}
|
package/src/session.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { type AnyMap, castKey, castTo, JSONUtil } from '@travetto/runtime';
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
/**
|
|
5
4
|
* @concrete
|
|
6
5
|
*/
|
|
7
|
-
export interface SessionData extends AnyMap {
|
|
6
|
+
export interface SessionData extends AnyMap {}
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Full session object, with metadata
|
|
@@ -124,4 +123,4 @@ export class Session<T extends SessionData = SessionData> {
|
|
|
124
123
|
data: this.data
|
|
125
124
|
};
|
|
126
125
|
}
|
|
127
|
-
}
|
|
126
|
+
}
|
package/support/test/server.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
|
|
3
|
-
import { Suite, Test } from '@travetto/test';
|
|
4
|
-
import { Inject } from '@travetto/di';
|
|
5
|
-
import type { SessionContext, SessionService } from '@travetto/auth-session';
|
|
6
3
|
import { type AuthContext, AuthenticationError } from '@travetto/auth';
|
|
4
|
+
import type { SessionContext, SessionService } from '@travetto/auth-session';
|
|
7
5
|
import { type AsyncContext, WithAsyncContext } from '@travetto/context';
|
|
8
|
-
import {
|
|
6
|
+
import { Inject } from '@travetto/di';
|
|
7
|
+
import { type Class, Util } from '@travetto/runtime';
|
|
8
|
+
import { Suite, Test } from '@travetto/test';
|
|
9
9
|
|
|
10
10
|
import { InjectableSuite } from '@travetto/di/support/test/suite.ts';
|
|
11
11
|
import { ModelSuite } from '@travetto/model/support/test/suite.ts';
|
|
@@ -14,7 +14,6 @@ import { ModelSuite } from '@travetto/model/support/test/suite.ts';
|
|
|
14
14
|
@ModelSuite()
|
|
15
15
|
@InjectableSuite()
|
|
16
16
|
export abstract class AuthSessionServerSuite<T> {
|
|
17
|
-
|
|
18
17
|
timeScale = 1;
|
|
19
18
|
serviceClass: Class<T>;
|
|
20
19
|
configClass: Class;
|
|
@@ -41,7 +40,7 @@ export abstract class AuthSessionServerSuite<T> {
|
|
|
41
40
|
};
|
|
42
41
|
|
|
43
42
|
assert(this.sessionContext.get() === undefined);
|
|
44
|
-
assert(await this.session.load() === undefined);
|
|
43
|
+
assert((await this.session.load()) === undefined);
|
|
45
44
|
|
|
46
45
|
const session = this.sessionContext.get(true);
|
|
47
46
|
assert(session.id === this.auth.principal.sessionId);
|
|
@@ -50,7 +49,7 @@ export abstract class AuthSessionServerSuite<T> {
|
|
|
50
49
|
|
|
51
50
|
this.sessionContext.set(undefined); // Disconnect
|
|
52
51
|
|
|
53
|
-
assert(await this.session.load() !== undefined);
|
|
52
|
+
assert((await this.session.load()) !== undefined);
|
|
54
53
|
const session2 = this.sessionContext.get(true);
|
|
55
54
|
assert(session2.data?.name === 'bob');
|
|
56
55
|
|
|
@@ -62,7 +61,7 @@ export abstract class AuthSessionServerSuite<T> {
|
|
|
62
61
|
|
|
63
62
|
this.sessionContext.set(undefined); // Disconnect
|
|
64
63
|
|
|
65
|
-
assert(await this.session.load() === undefined);
|
|
64
|
+
assert((await this.session.load()) === undefined);
|
|
66
65
|
const session3 = this.sessionContext.get(true);
|
|
67
66
|
assert.deepStrictEqual(session3.data, {});
|
|
68
67
|
}
|
|
@@ -72,4 +71,4 @@ export abstract class AuthSessionServerSuite<T> {
|
|
|
72
71
|
async testUnauthenticatedSession() {
|
|
73
72
|
assert.throws(() => this.sessionContext.get(true), AuthenticationError);
|
|
74
73
|
}
|
|
75
|
-
}
|
|
74
|
+
}
|