@dbsc-toolkit/server-express 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 +132 -0
- package/README.md +86 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +191 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity.
|
|
18
|
+
|
|
19
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
20
|
+
exercising permissions granted by this License.
|
|
21
|
+
|
|
22
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
23
|
+
including but not limited to software source code, documentation
|
|
24
|
+
source, and configuration files.
|
|
25
|
+
|
|
26
|
+
"Object" form shall mean any form resulting from mechanical
|
|
27
|
+
transformation or translation of a Source form, including but
|
|
28
|
+
not limited to compiled object code, generated documentation,
|
|
29
|
+
and conversions to other media types.
|
|
30
|
+
|
|
31
|
+
"Work" shall mean the work of authorship made available under
|
|
32
|
+
the License, as indicated by a copyright notice that is included in
|
|
33
|
+
or attached to the work.
|
|
34
|
+
|
|
35
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
36
|
+
form, that is based on (or derived from) the Work and for which the
|
|
37
|
+
editorial revisions, annotations, elaborations, or other transformations
|
|
38
|
+
represent, as a whole, an original work of authorship.
|
|
39
|
+
|
|
40
|
+
"Contribution" shall mean any work of authorship, including
|
|
41
|
+
the original version of the Work and any modifications or additions
|
|
42
|
+
to that Work or Derivative Works of the Work, that is intentionally
|
|
43
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
44
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
45
|
+
the copyright owner.
|
|
46
|
+
|
|
47
|
+
"Contributor" shall mean Licensor and any Legal Entity
|
|
48
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
49
|
+
subsequently incorporated within the Work.
|
|
50
|
+
|
|
51
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
52
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
53
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
54
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
55
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
56
|
+
Work and such Derivative Works in Source or Object form.
|
|
57
|
+
|
|
58
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
59
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
60
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
61
|
+
(except as stated in this section) patent license to make, have made,
|
|
62
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
63
|
+
where such license applies only to those patent claims licensable
|
|
64
|
+
by such Contributor that are necessarily infringed by their
|
|
65
|
+
Contribution(s) alone or by the combined work of their Contribution(s)
|
|
66
|
+
with the Work to which such Contribution(s) was submitted.
|
|
67
|
+
|
|
68
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
69
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
70
|
+
modifications, and in Source or Object form, provided that You
|
|
71
|
+
meet the following conditions:
|
|
72
|
+
|
|
73
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
74
|
+
a copy of this License; and
|
|
75
|
+
|
|
76
|
+
(b) You must cause any modified files to carry prominent notices
|
|
77
|
+
stating that You changed the files; and
|
|
78
|
+
|
|
79
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
80
|
+
that You distribute, all copyright, patent, trademark, and
|
|
81
|
+
attribution notices from the Source form of the Work; and
|
|
82
|
+
|
|
83
|
+
(d) If the Work includes a "NOTICE" text file, you must include a
|
|
84
|
+
readable copy of the attribution notices contained within such NOTICE
|
|
85
|
+
file, in at least one of the following places: within a NOTICE text
|
|
86
|
+
file distributed as part of the Derivative Works; or, within the
|
|
87
|
+
Source form or documentation, if provided along with the Derivative
|
|
88
|
+
Works; or, on a display generated by the Derivative Works.
|
|
89
|
+
|
|
90
|
+
The "NOTICE" file referenced above refers to a text file that may
|
|
91
|
+
accompany the source form of the Work.
|
|
92
|
+
|
|
93
|
+
You may add Your own attribution notices within Derivative Works
|
|
94
|
+
that You distribute, alongside or as an addendum to the NOTICE text
|
|
95
|
+
from the Work, provided that such additional attribution notices
|
|
96
|
+
cannot be construed as modifying the License.
|
|
97
|
+
|
|
98
|
+
You may add Your own license statement for Your modifications and
|
|
99
|
+
may provide additional grant of rights to use, reproduce, or
|
|
100
|
+
distribute Derivative Works, subject to the terms and conditions
|
|
101
|
+
for the Work itself.
|
|
102
|
+
|
|
103
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
104
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
105
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
106
|
+
this License, without any additional terms or conditions.
|
|
107
|
+
|
|
108
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
109
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
110
|
+
|
|
111
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
112
|
+
agreed to in writing, Licensor provides the Work (and each Contributor
|
|
113
|
+
provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
|
|
114
|
+
OR CONDITIONS OF ANY KIND, either express or implied, including,
|
|
115
|
+
without limitation, any warranties or conditions of TITLE,
|
|
116
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
|
|
117
|
+
|
|
118
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
119
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
120
|
+
unless required by applicable law (such as deliberate and grossly
|
|
121
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
122
|
+
liable to You for damages, including any direct, indirect, special,
|
|
123
|
+
incidental, or exemplary damages of any character arising as a
|
|
124
|
+
result of this License or out of the use or inability to use the
|
|
125
|
+
Work.
|
|
126
|
+
|
|
127
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
128
|
+
the Work or Derivative Works thereof, You may choose to offer, and
|
|
129
|
+
charge a fee for, acceptance of support, warranty, indemnity,
|
|
130
|
+
or other liability obligations and/or rights consistent with this License.
|
|
131
|
+
|
|
132
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# @dbsc-toolkit/server-express
|
|
2
|
+
|
|
3
|
+
Express middleware for the DBSC Toolkit. Mounts the registration and refresh routes, parses cookies, exposes session info on `res.locals.dbsc`.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @dbsc-toolkit/server-express @dbsc-toolkit/storage-memory cookie-parser
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import express from "express";
|
|
15
|
+
import cookieParser from "cookie-parser";
|
|
16
|
+
import { randomUUID } from "node:crypto";
|
|
17
|
+
import { dbsc } from "@dbsc-toolkit/server-express";
|
|
18
|
+
import { MemoryStorage } from "@dbsc-toolkit/storage-memory";
|
|
19
|
+
import { buildRegistrationHeader, issueChallenge } from "@dbsc-toolkit/core";
|
|
20
|
+
|
|
21
|
+
const app = express();
|
|
22
|
+
const storage = new MemoryStorage();
|
|
23
|
+
|
|
24
|
+
app.use(cookieParser());
|
|
25
|
+
app.use(express.json());
|
|
26
|
+
app.use(dbsc({ storage }));
|
|
27
|
+
|
|
28
|
+
app.post("/login", async (req, res) => {
|
|
29
|
+
const sessionId = randomUUID();
|
|
30
|
+
await storage.setSession({
|
|
31
|
+
id: sessionId,
|
|
32
|
+
userId: req.body.username,
|
|
33
|
+
tier: "none",
|
|
34
|
+
createdAt: Date.now(),
|
|
35
|
+
expiresAt: Date.now() + 24 * 60 * 60 * 1000,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const challenge = await issueChallenge(sessionId, storage);
|
|
39
|
+
const header = buildRegistrationHeader({
|
|
40
|
+
refreshPath: "/dbsc/registration",
|
|
41
|
+
challenge: challenge.jti,
|
|
42
|
+
});
|
|
43
|
+
res.setHeader("Secure-Session-Registration", header);
|
|
44
|
+
res.setHeader("Sec-Session-Registration", header);
|
|
45
|
+
|
|
46
|
+
res.cookie("__Host-dbsc-reg", sessionId, {
|
|
47
|
+
httpOnly: true, secure: true, sameSite: "lax", path: "/",
|
|
48
|
+
maxAge: 24 * 60 * 60 * 1000,
|
|
49
|
+
});
|
|
50
|
+
res.cookie("__Host-dbsc-challenge", challenge.jti, {
|
|
51
|
+
httpOnly: true, secure: true, sameSite: "lax", path: "/",
|
|
52
|
+
maxAge: 5 * 60 * 1000,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
res.json({ ok: true });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
app.get("/me", (req, res) => {
|
|
59
|
+
const { sessionId, tier } = res.locals.dbsc;
|
|
60
|
+
if (!sessionId) return res.status(401).end();
|
|
61
|
+
res.json({ sessionId, tier });
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The middleware automatically handles `POST /dbsc/registration` and `POST /dbsc/refresh`. Chrome drives both — your application code never sees those requests.
|
|
66
|
+
|
|
67
|
+
## Options
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
dbsc({
|
|
71
|
+
storage,
|
|
72
|
+
secure: true, // default. Disable for HTTP local dev (drops __Host- prefix)
|
|
73
|
+
registrationPath: "/dbsc/registration",
|
|
74
|
+
refreshPath: "/dbsc/refresh",
|
|
75
|
+
boundCookieTtl: 10 * 60 * 1000, // bound cookie lifetime in ms
|
|
76
|
+
registrationCookieTtl: 24 * 60 * 60 * 1000,
|
|
77
|
+
rateLimiter: customRateLimiter,
|
|
78
|
+
onEvent: (event) => { ... },
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
See the [main README](https://github.com/SulimanAbdulrazzaq/DBSC#readme) for the full picture.
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { RequestHandler } from "express";
|
|
2
|
+
import { type DbscOptions, type ProtectionTier } from "@dbsc-toolkit/core";
|
|
3
|
+
export interface DbscExpressOptions extends DbscOptions {
|
|
4
|
+
secure?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface DbscLocals {
|
|
7
|
+
sessionId: string | null;
|
|
8
|
+
tier: ProtectionTier;
|
|
9
|
+
revoke: () => Promise<void>;
|
|
10
|
+
requireBound: () => void;
|
|
11
|
+
}
|
|
12
|
+
declare global {
|
|
13
|
+
namespace Express {
|
|
14
|
+
interface Locals {
|
|
15
|
+
dbsc: DbscLocals;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export declare function dbsc(opts: DbscExpressOptions): RequestHandler;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/E,OAAO,EAgBL,KAAK,WAAW,EAEhB,KAAK,cAAc,EACpB,MAAM,oBAAoB,CAAC;AAW5B,MAAM,WAAW,kBAAmB,SAAQ,WAAW;IACrD,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,IAAI,EAAE,cAAc,CAAC;IACrB,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,YAAY,EAAE,MAAM,IAAI,CAAC;CAC1B;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,OAAO,CAAC;QAChB,UAAU,MAAM;YACd,IAAI,EAAE,UAAU,CAAC;SAClB;KACF;CACF;AAsBD,wBAAgB,IAAI,CAAC,IAAI,EAAE,kBAAkB,GAAG,cAAc,CAwM7D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { randomBytes as nodeRandomBytes } from "node:crypto";
|
|
2
|
+
import { handleRegistration, handleRefresh, issueChallenge, buildChallengeHeader, readSessionResponseHeader, CHALLENGE_HEADER, LEGACY_CHALLENGE_HEADER, NoopRateLimiter, emit, DbscProtocolError, DbscVerificationError, } from "@dbsc-toolkit/core";
|
|
3
|
+
const cookieNames = (secure) => ({
|
|
4
|
+
bound: secure ? "__Host-dbsc-session" : "dbsc-session",
|
|
5
|
+
reg: secure ? "__Host-dbsc-reg" : "dbsc-reg",
|
|
6
|
+
challenge: secure ? "__Host-dbsc-challenge" : "dbsc-challenge",
|
|
7
|
+
});
|
|
8
|
+
const DEFAULT_BOUND_TTL = 10 * 60 * 1000;
|
|
9
|
+
const DEFAULT_REG_TTL = 24 * 60 * 60 * 1000;
|
|
10
|
+
function cookieOpts(ttlMs, secure) {
|
|
11
|
+
return {
|
|
12
|
+
httpOnly: true,
|
|
13
|
+
secure,
|
|
14
|
+
sameSite: "lax",
|
|
15
|
+
maxAge: ttlMs / 1000,
|
|
16
|
+
path: "/",
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function serializeCookie(name, value, opts) {
|
|
20
|
+
const parts = [`${name}=${value}`];
|
|
21
|
+
parts.push("HttpOnly");
|
|
22
|
+
if (opts.secure)
|
|
23
|
+
parts.push("Secure");
|
|
24
|
+
parts.push(`SameSite=${opts.sameSite}`);
|
|
25
|
+
parts.push(`Max-Age=${opts.maxAge}`);
|
|
26
|
+
parts.push(`Path=${opts.path}`);
|
|
27
|
+
return parts.join("; ");
|
|
28
|
+
}
|
|
29
|
+
export function dbsc(opts) {
|
|
30
|
+
const { storage, fallback = "webauthn", registrationPath = "/dbsc/registration", refreshPath = "/dbsc/refresh", boundCookieTtl = DEFAULT_BOUND_TTL, registrationCookieTtl = DEFAULT_REG_TTL, rateLimiter = new NoopRateLimiter(), onEvent, secure = true, } = opts;
|
|
31
|
+
const hmacSecret = nodeRandomBytes(32);
|
|
32
|
+
const COOKIES = cookieNames(secure);
|
|
33
|
+
async function handleRegistrationRoute(req, res) {
|
|
34
|
+
const ip = req.ip ?? "unknown";
|
|
35
|
+
const allowed = await rateLimiter.checkRegistration(ip);
|
|
36
|
+
if (!allowed) {
|
|
37
|
+
res.status(429).json({ error: "rate limited" });
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const sessionId = req.cookies?.[COOKIES.reg];
|
|
41
|
+
const expectedJti = req.cookies?.[COOKIES.challenge];
|
|
42
|
+
if (!sessionId || !expectedJti) {
|
|
43
|
+
res.status(400).json({ error: "missing session or challenge cookie" });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
await handleRegistration({
|
|
48
|
+
sessionId,
|
|
49
|
+
secSessionResponseHeader: readSessionResponseHeader(req.headers),
|
|
50
|
+
expectedJti,
|
|
51
|
+
}, storage);
|
|
52
|
+
const session = await storage.getSession(sessionId);
|
|
53
|
+
if (session) {
|
|
54
|
+
await storage.setSession({ ...session, tier: "dbsc" });
|
|
55
|
+
}
|
|
56
|
+
emit(onEvent, {
|
|
57
|
+
type: "registration",
|
|
58
|
+
sessionId,
|
|
59
|
+
tier: "dbsc",
|
|
60
|
+
timestamp: Date.now(),
|
|
61
|
+
algorithm: "ES256",
|
|
62
|
+
ip,
|
|
63
|
+
});
|
|
64
|
+
res.setHeader("Set-Cookie", [
|
|
65
|
+
serializeCookie(COOKIES.bound, sessionId, cookieOpts(boundCookieTtl, secure)),
|
|
66
|
+
serializeCookie(COOKIES.challenge, "", { ...cookieOpts(0, secure), maxAge: 0 }),
|
|
67
|
+
]);
|
|
68
|
+
res.status(204).end();
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
await rateLimiter.recordFailure(ip, sessionId);
|
|
72
|
+
if (err instanceof DbscVerificationError || err instanceof DbscProtocolError) {
|
|
73
|
+
emit(onEvent, {
|
|
74
|
+
type: "verification_failure",
|
|
75
|
+
sessionId,
|
|
76
|
+
tier: "dbsc",
|
|
77
|
+
timestamp: Date.now(),
|
|
78
|
+
reason: err.code,
|
|
79
|
+
ip,
|
|
80
|
+
});
|
|
81
|
+
res.status(400).json({ error: err.message });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async function handleRefreshRoute(req, res) {
|
|
88
|
+
const ip = req.ip ?? "unknown";
|
|
89
|
+
const sessionId = req.cookies?.[COOKIES.bound];
|
|
90
|
+
if (!sessionId) {
|
|
91
|
+
res.status(401).json({ error: "no session" });
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const allowed = await rateLimiter.checkRefresh(ip, sessionId);
|
|
95
|
+
if (!allowed) {
|
|
96
|
+
res.status(429).json({ error: "rate limited" });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const responseHeader = readSessionResponseHeader(req.headers);
|
|
100
|
+
if (!responseHeader) {
|
|
101
|
+
const challenge = await issueChallenge(sessionId, storage);
|
|
102
|
+
res.setHeader(CHALLENGE_HEADER, buildChallengeHeader(challenge.jti));
|
|
103
|
+
res.setHeader(LEGACY_CHALLENGE_HEADER, buildChallengeHeader(challenge.jti));
|
|
104
|
+
res.setHeader("Set-Cookie", serializeCookie(COOKIES.challenge, challenge.jti, cookieOpts(5 * 60 * 1000, secure)));
|
|
105
|
+
res.status(403).end();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const expectedJti = req.cookies?.[COOKIES.challenge];
|
|
109
|
+
if (!expectedJti) {
|
|
110
|
+
res.status(400).json({ error: "missing challenge cookie" });
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
await handleRefresh({ sessionId, secSessionResponseHeader: responseHeader, expectedJti }, storage);
|
|
115
|
+
emit(onEvent, {
|
|
116
|
+
type: "refresh",
|
|
117
|
+
sessionId,
|
|
118
|
+
tier: "dbsc",
|
|
119
|
+
timestamp: Date.now(),
|
|
120
|
+
ip,
|
|
121
|
+
});
|
|
122
|
+
res.setHeader("Set-Cookie", [
|
|
123
|
+
serializeCookie(COOKIES.bound, sessionId, cookieOpts(boundCookieTtl, secure)),
|
|
124
|
+
serializeCookie(COOKIES.challenge, "", { ...cookieOpts(0, secure), maxAge: 0 }),
|
|
125
|
+
]);
|
|
126
|
+
res.status(204).end();
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
await rateLimiter.recordFailure(ip, sessionId);
|
|
130
|
+
const stolenCheck = await storage.getBoundKey(sessionId);
|
|
131
|
+
if (stolenCheck) {
|
|
132
|
+
emit(onEvent, {
|
|
133
|
+
type: "session_stolen",
|
|
134
|
+
sessionId,
|
|
135
|
+
tier: "dbsc",
|
|
136
|
+
timestamp: Date.now(),
|
|
137
|
+
ip,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (err instanceof DbscVerificationError || err instanceof DbscProtocolError) {
|
|
141
|
+
emit(onEvent, {
|
|
142
|
+
type: "verification_failure",
|
|
143
|
+
sessionId,
|
|
144
|
+
tier: "dbsc",
|
|
145
|
+
timestamp: Date.now(),
|
|
146
|
+
reason: err.code,
|
|
147
|
+
ip,
|
|
148
|
+
});
|
|
149
|
+
res.status(401).json({ error: err.message });
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return async (req, res, next) => {
|
|
156
|
+
if (req.method === "POST" && req.path === registrationPath) {
|
|
157
|
+
await handleRegistrationRoute(req, res);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (req.method === "POST" && req.path === refreshPath) {
|
|
161
|
+
await handleRefreshRoute(req, res);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const sessionId = req.cookies?.[COOKIES.bound];
|
|
165
|
+
res.locals.dbsc = {
|
|
166
|
+
sessionId: sessionId ?? null,
|
|
167
|
+
tier: "none",
|
|
168
|
+
revoke: async () => {
|
|
169
|
+
if (sessionId)
|
|
170
|
+
await storage.revokeSession(sessionId);
|
|
171
|
+
res.setHeader("Set-Cookie", [
|
|
172
|
+
serializeCookie(COOKIES.bound, "", { ...cookieOpts(0, secure), maxAge: 0 }),
|
|
173
|
+
]);
|
|
174
|
+
},
|
|
175
|
+
requireBound: () => {
|
|
176
|
+
if (!sessionId) {
|
|
177
|
+
res.status(401).json({ error: "authentication required" });
|
|
178
|
+
throw new Error("unauthenticated");
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
if (sessionId) {
|
|
183
|
+
const session = await storage.getSession(sessionId);
|
|
184
|
+
if (session) {
|
|
185
|
+
res.locals.dbsc.tier = session.tier;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
next();
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"Express middleware for DBSC Toolkit",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"cookie": "^0.6.0",
|
|
16
|
+
"@dbsc-toolkit/core": "0.1.0"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"express": ">=4.0.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/cookie": "^0.6.0",
|
|
23
|
+
"@types/express": "^4.17.0",
|
|
24
|
+
"@types/node": "^20.0.0",
|
|
25
|
+
"express": "^4.19.0",
|
|
26
|
+
"typescript": "^5.4.0",
|
|
27
|
+
"vitest": "^1.6.0"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"license": "Apache-2.0",
|
|
34
|
+
"homepage": "https://github.com/SulimanAbdulrazzaq/dbsc-toolkit#readme",
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/SulimanAbdulrazzaq/dbsc-toolkit/issues"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/SulimanAbdulrazzaq/dbsc-toolkit.git",
|
|
41
|
+
"directory": "packages/server-express"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"dbsc",
|
|
48
|
+
"express",
|
|
49
|
+
"middleware",
|
|
50
|
+
"session",
|
|
51
|
+
"security",
|
|
52
|
+
"auth"
|
|
53
|
+
],
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsc -p tsconfig.json",
|
|
56
|
+
"test": "vitest run --passWithNoTests",
|
|
57
|
+
"clean": "rm -rf dist"
|
|
58
|
+
}
|
|
59
|
+
}
|