nodecommons-esm-security 0.0.15 → 2.0.0
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.
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { CommonsFixedDuration } from 'tscommons-esm-core';
|
|
1
2
|
import { ICommonsSession } from 'tscommons-esm-session';
|
|
2
3
|
export declare abstract class CommonsSessionService<T> {
|
|
3
4
|
private allowMultipleSessionsForSameUser;
|
|
5
|
+
private maximumLast?;
|
|
4
6
|
private sessions;
|
|
5
|
-
constructor(allowMultipleSessionsForSameUser?: boolean);
|
|
7
|
+
constructor(allowMultipleSessionsForSameUser?: boolean, maximumLast?: CommonsFixedDuration | undefined);
|
|
8
|
+
get activeSessions(): number;
|
|
6
9
|
protected abstract isEqual(a: T, b: T): boolean;
|
|
7
10
|
protected abstract isStillValid(data: T): Promise<boolean>;
|
|
11
|
+
private cleanup;
|
|
8
12
|
create(data: T): ICommonsSession<T>;
|
|
9
13
|
destroy(session: ICommonsSession<T>): boolean;
|
|
10
14
|
abort(data: T): boolean;
|
|
@@ -1,23 +1,41 @@
|
|
|
1
|
+
import { CommonsFixedDate } from 'tscommons-esm-core';
|
|
1
2
|
import { commonsHashRandomSha256 } from '../helpers/commons-hash.mjs';
|
|
2
3
|
export class CommonsSessionService {
|
|
3
4
|
allowMultipleSessionsForSameUser;
|
|
5
|
+
maximumLast;
|
|
4
6
|
sessions = [];
|
|
5
|
-
constructor(allowMultipleSessionsForSameUser = false) {
|
|
7
|
+
constructor(allowMultipleSessionsForSameUser = false, maximumLast) {
|
|
6
8
|
this.allowMultipleSessionsForSameUser = allowMultipleSessionsForSameUser;
|
|
9
|
+
this.maximumLast = maximumLast;
|
|
10
|
+
}
|
|
11
|
+
get activeSessions() {
|
|
12
|
+
this.cleanup();
|
|
13
|
+
return this.sessions.length;
|
|
14
|
+
}
|
|
15
|
+
cleanup() {
|
|
16
|
+
if (!this.maximumLast)
|
|
17
|
+
return;
|
|
18
|
+
const expirationThreshold = new CommonsFixedDate();
|
|
19
|
+
expirationThreshold.subtract(this.maximumLast);
|
|
20
|
+
for (const session of [...this.sessions]) {
|
|
21
|
+
if (session.last.isLessThan(expirationThreshold))
|
|
22
|
+
this.destroy(session);
|
|
23
|
+
}
|
|
7
24
|
}
|
|
8
25
|
create(data) {
|
|
9
26
|
if (!this.allowMultipleSessionsForSameUser)
|
|
10
27
|
this.abort(data); // clear out any existing sessions for the same user
|
|
11
28
|
const session = {
|
|
12
29
|
sid: commonsHashRandomSha256(),
|
|
13
|
-
start: new
|
|
14
|
-
last: new
|
|
30
|
+
start: new CommonsFixedDate(),
|
|
31
|
+
last: new CommonsFixedDate(),
|
|
15
32
|
data: data
|
|
16
33
|
};
|
|
17
34
|
this.sessions.push(session);
|
|
18
35
|
return session;
|
|
19
36
|
}
|
|
20
37
|
destroy(session) {
|
|
38
|
+
this.cleanup();
|
|
21
39
|
const match = this.sessions
|
|
22
40
|
.find((s) => s.sid === session.sid);
|
|
23
41
|
if (!match)
|
|
@@ -27,6 +45,7 @@ export class CommonsSessionService {
|
|
|
27
45
|
return true;
|
|
28
46
|
}
|
|
29
47
|
abort(data) {
|
|
48
|
+
this.cleanup();
|
|
30
49
|
const existing = this.sessions
|
|
31
50
|
.find((s) => this.isEqual(data, s.data));
|
|
32
51
|
if (!existing)
|
|
@@ -35,6 +54,7 @@ export class CommonsSessionService {
|
|
|
35
54
|
return true;
|
|
36
55
|
}
|
|
37
56
|
async validate(sid) {
|
|
57
|
+
this.cleanup();
|
|
38
58
|
const session = this.sessions
|
|
39
59
|
.find((s) => s.sid === sid);
|
|
40
60
|
if (!session)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commons-session.service.mjs","sourceRoot":"","sources":["../../src/services/commons-session.service.mts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"commons-session.service.mjs","sourceRoot":"","sources":["../../src/services/commons-session.service.mts"],"names":[],"mappings":"AAAA,OAAO,EA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AO,CAAC;IAChB,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nodecommons-esm-security",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"tsc": "./node_modules/typescript/bin/tsc",
|
|
@@ -15,18 +15,18 @@
|
|
|
15
15
|
"author": "Pete Morris",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@stylistic/eslint-plugin
|
|
18
|
+
"@stylistic/eslint-plugin": "^5.10.0",
|
|
19
19
|
"@types/node": "^22.10.0",
|
|
20
|
-
"eslint-plugin-import": "^2.
|
|
21
|
-
"eslint-plugin-prefer-arrow-functions": "^3.
|
|
22
|
-
"typescript": "^5.
|
|
23
|
-
"typescript-eslint": "^8.
|
|
20
|
+
"eslint-plugin-import": "^2.32.0",
|
|
21
|
+
"eslint-plugin-prefer-arrow-functions": "^3.9.1",
|
|
22
|
+
"typescript": "^5.8.0",
|
|
23
|
+
"typescript-eslint": "^8.53.0"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
26
|
"dist/**/*"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"tscommons-esm-core": "^0.
|
|
30
|
-
"tscommons-esm-session": "^
|
|
29
|
+
"tscommons-esm-core": "^1.0.2",
|
|
30
|
+
"tscommons-esm-session": "^2.0.1"
|
|
31
31
|
}
|
|
32
32
|
}
|