@moshaver/cmb-auth 0.1.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.
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/package.json +48 -0
- package/src/index.d.ts +13 -0
- package/src/index.js +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mobin Karam
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# `@moshaver/cmb-auth`
|
|
2
|
+
|
|
3
|
+
Framework-neutral login normalization and secure session credential lifecycle. Account/student eligibility, password hashing, persistence, throttling, and HTTP/CSRF error mapping remain in the API adapter.
|
|
4
|
+
|
|
5
|
+
## Install and use
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @moshaver/cmb-auth
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const { SessionCredentialService, InMemorySessionStore } = require("@moshaver/cmb-auth");
|
|
13
|
+
// Use a durable host store instead of InMemorySessionStore in production.
|
|
14
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@moshaver/cmb-auth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Composable Modular Backend auth primitives.",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"require": "./src/index.js",
|
|
12
|
+
"default": "./src/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"cmb",
|
|
22
|
+
"modular-backend",
|
|
23
|
+
"backend",
|
|
24
|
+
"framework-agnostic",
|
|
25
|
+
"auth"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=22.13.0 <23"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/Mobin-Karam/moshaver.git",
|
|
33
|
+
"directory": "packages/cmb/auth"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/Mobin-Karam/moshaver/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/Mobin-Karam/moshaver/tree/main/packages/cmb",
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"sideEffects": false,
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@moshaver/cmb-identity": "^0.1.0",
|
|
46
|
+
"@moshaver/cmb-kernel": "^0.1.0"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const AUTH_MODULE: Readonly<{ id: "auth"; version: string; kind: "platform"; dependencies: readonly string[] }>;
|
|
2
|
+
export type SessionCredentials = { accessToken: string; refreshToken: string; csrfToken: string; expiresAt: Date; refreshExpiresAt: Date };
|
|
3
|
+
export class SessionCredentialService { constructor(options?: { accessTokenTtlMinutes?: number; refreshTokenTtlDays?: number; now?: () => number }); normalizeLogin(username: unknown): string; issue(): SessionCredentials; hash(value: string): string; safeEqual(expectedValue: string, receivedValue: string): boolean }
|
|
4
|
+
export type CmbSession<TAttributes extends object = Record<string, unknown>> = Readonly<{ subjectId: string; csrfToken: string; expiresAt: Date; revokedAt: Date | null; attributes: Readonly<TAttributes> }>;
|
|
5
|
+
export class InMemorySessionStore<TAttributes extends object = Record<string, unknown>> {
|
|
6
|
+
constructor(options?: { credentials?: SessionCredentialService; accessTokenTtlMinutes?: number; refreshTokenTtlDays?: number; now?: () => number });
|
|
7
|
+
create(subjectId: string, attributes?: TAttributes): SessionCredentials;
|
|
8
|
+
authenticate(accessToken: string): CmbSession<TAttributes> | null;
|
|
9
|
+
require(accessToken: string): CmbSession<TAttributes>;
|
|
10
|
+
verifyCsrf(accessToken: string, receivedToken: string): CmbSession<TAttributes>;
|
|
11
|
+
revoke(accessToken: string): boolean;
|
|
12
|
+
deactivateSubject(subjectId: string): number;
|
|
13
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const crypto = require("node:crypto");
|
|
3
|
+
const { defineModule } = require("@moshaver/cmb-kernel");
|
|
4
|
+
const { normalizeUsername } = require("@moshaver/cmb-identity");
|
|
5
|
+
const AUTH_MODULE = defineModule({ id: "auth", version: "0.1.0", kind: "platform", dependencies: ["identity", "kernel"], provides: ["security.sessions"], requires: ["identity.context"], configuration: { accessTokenTtlMinutes: 15, refreshTokenTtlDays: 30 } });
|
|
6
|
+
class SessionCredentialService {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.accessTokenTtlMinutes = Number(options.accessTokenTtlMinutes ?? 15);
|
|
9
|
+
this.refreshTokenTtlDays = Number(options.refreshTokenTtlDays ?? 30);
|
|
10
|
+
this.now = options.now ?? Date.now;
|
|
11
|
+
if (!(this.accessTokenTtlMinutes > 0) || !(this.refreshTokenTtlDays > 0)) throw new TypeError("Session credential TTL values must be positive.");
|
|
12
|
+
}
|
|
13
|
+
normalizeLogin(username) { return normalizeUsername(username); }
|
|
14
|
+
issue() { const now = this.now(); return { accessToken: crypto.randomBytes(32).toString("base64url"), refreshToken: crypto.randomBytes(48).toString("base64url"), csrfToken: crypto.randomBytes(24).toString("base64url"), expiresAt: new Date(now + this.accessTokenTtlMinutes * 60 * 1000), refreshExpiresAt: new Date(now + this.refreshTokenTtlDays * 24 * 60 * 60 * 1000) }; }
|
|
15
|
+
hash(value) { return crypto.createHash("sha256").update(value).digest("hex"); }
|
|
16
|
+
safeEqual(expectedValue, receivedValue) { const expected = Buffer.from(expectedValue); const received = Buffer.from(receivedValue); return expected.length === received.length && crypto.timingSafeEqual(expected, received); }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class InMemorySessionStore {
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
this.credentials = options.credentials ?? new SessionCredentialService(options);
|
|
22
|
+
this.now = options.now ?? Date.now;
|
|
23
|
+
this.sessions = new Map();
|
|
24
|
+
this.inactiveSubjects = new Set();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
create(subjectId, attributes = {}) {
|
|
28
|
+
const subject = String(subjectId ?? "").trim();
|
|
29
|
+
if (!subject) throw new TypeError("Session subjectId is required.");
|
|
30
|
+
if (this.inactiveSubjects.has(subject)) throw new Error("Session subject is inactive.");
|
|
31
|
+
const issued = this.credentials.issue();
|
|
32
|
+
this.sessions.set(this.credentials.hash(issued.accessToken), {
|
|
33
|
+
subjectId: subject,
|
|
34
|
+
csrfToken: issued.csrfToken,
|
|
35
|
+
expiresAt: issued.expiresAt,
|
|
36
|
+
revokedAt: null,
|
|
37
|
+
attributes: Object.freeze({ ...attributes }),
|
|
38
|
+
});
|
|
39
|
+
return issued;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
authenticate(accessToken) {
|
|
43
|
+
const token = String(accessToken ?? "");
|
|
44
|
+
if (!token) return null;
|
|
45
|
+
const session = this.sessions.get(this.credentials.hash(token));
|
|
46
|
+
if (!session || session.revokedAt || session.expiresAt.getTime() <= this.now()) return null;
|
|
47
|
+
if (this.inactiveSubjects.has(session.subjectId)) return null;
|
|
48
|
+
return Object.freeze({ ...session });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
require(accessToken) {
|
|
52
|
+
const session = this.authenticate(accessToken);
|
|
53
|
+
if (!session) throw new Error("Authentication required.");
|
|
54
|
+
return session;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
verifyCsrf(accessToken, receivedToken) {
|
|
58
|
+
const session = this.require(accessToken);
|
|
59
|
+
if (!this.credentials.safeEqual(session.csrfToken, String(receivedToken ?? ""))) throw new Error("Invalid CSRF token.");
|
|
60
|
+
return session;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
revoke(accessToken) {
|
|
64
|
+
const session = this.sessions.get(this.credentials.hash(String(accessToken ?? "")));
|
|
65
|
+
if (!session || session.revokedAt) return false;
|
|
66
|
+
session.revokedAt = new Date(this.now());
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
deactivateSubject(subjectId) {
|
|
71
|
+
const subject = String(subjectId ?? "").trim();
|
|
72
|
+
if (!subject) throw new TypeError("Session subjectId is required.");
|
|
73
|
+
this.inactiveSubjects.add(subject);
|
|
74
|
+
return [...this.sessions.values()].filter((session) => session.subjectId === subject).length;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports = { AUTH_MODULE, SessionCredentialService, InMemorySessionStore };
|