@basaltkit/auth 1.1.0 → 1.2.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/dist/index.d.ts +3 -2
- package/dist/index.js +30 -27
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -435,8 +435,9 @@ declare class Auth {
|
|
|
435
435
|
verifyMfaCode(userId: string, code: string): Promise<boolean>;
|
|
436
436
|
private generateRecoveryCode;
|
|
437
437
|
private hashRecoveryCode;
|
|
438
|
-
/** SHA-256 of a one-time token — only this is
|
|
439
|
-
|
|
438
|
+
/** SHA-256 of a bearer secret (one-time token, refresh token) — only this is
|
|
439
|
+
* persisted, so a leak of the token/session table can't be replayed. */
|
|
440
|
+
private hashToken;
|
|
440
441
|
private issueOneTimeToken;
|
|
441
442
|
private consumeToken;
|
|
442
443
|
private updateUser;
|
package/dist/index.js
CHANGED
|
@@ -80,7 +80,8 @@ function verifyJwt(token, secret) {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
// src/stores.ts
|
|
83
|
-
import { randomBytes as randomBytes2, randomUUID } from "crypto";
|
|
83
|
+
import { createHash, randomBytes as randomBytes2, randomUUID } from "crypto";
|
|
84
|
+
var hashSessionId = (id) => createHash("sha256").update(id).digest("hex");
|
|
84
85
|
var MemoryUserSource = class {
|
|
85
86
|
users = /* @__PURE__ */ new Map();
|
|
86
87
|
async findByEmail(email) {
|
|
@@ -170,29 +171,28 @@ var MemoryMfaStore = class {
|
|
|
170
171
|
var MemorySessionStore = class {
|
|
171
172
|
sessions = /* @__PURE__ */ new Map();
|
|
172
173
|
async create(userId, ttlMs) {
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
};
|
|
178
|
-
this.sessions.set(record.id, record);
|
|
179
|
-
return record;
|
|
174
|
+
const rawId = randomBytes2(32).toString("base64url");
|
|
175
|
+
const hashed = hashSessionId(rawId);
|
|
176
|
+
const expiresAt = Date.now() + ttlMs;
|
|
177
|
+
this.sessions.set(hashed, { id: hashed, userId, expiresAt });
|
|
178
|
+
return { id: rawId, userId, expiresAt };
|
|
180
179
|
}
|
|
181
180
|
async find(id) {
|
|
182
|
-
const
|
|
181
|
+
const hashed = hashSessionId(id);
|
|
182
|
+
const record = this.sessions.get(hashed);
|
|
183
183
|
if (!record) return null;
|
|
184
184
|
if (Date.now() >= record.expiresAt) {
|
|
185
|
-
this.sessions.delete(
|
|
185
|
+
this.sessions.delete(hashed);
|
|
186
186
|
return null;
|
|
187
187
|
}
|
|
188
|
-
return record;
|
|
188
|
+
return { id, userId: record.userId, expiresAt: record.expiresAt };
|
|
189
189
|
}
|
|
190
190
|
async delete(id) {
|
|
191
|
-
return this.sessions.delete(id);
|
|
191
|
+
return this.sessions.delete(hashSessionId(id));
|
|
192
192
|
}
|
|
193
193
|
async deleteAllForUser(userId) {
|
|
194
|
-
for (const [
|
|
195
|
-
if (record.userId === userId) this.sessions.delete(
|
|
194
|
+
for (const [key, record] of this.sessions) {
|
|
195
|
+
if (record.userId === userId) this.sessions.delete(key);
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
};
|
|
@@ -221,7 +221,7 @@ var MemoryRefreshTokenStore = class {
|
|
|
221
221
|
};
|
|
222
222
|
|
|
223
223
|
// src/auth.ts
|
|
224
|
-
import { createHash, randomBytes as randomBytes4, randomUUID as randomUUID2 } from "crypto";
|
|
224
|
+
import { createHash as createHash2, randomBytes as randomBytes4, randomUUID as randomUUID2 } from "crypto";
|
|
225
225
|
import { BasaltError as BasaltError3, parseDuration as parseDuration2 } from "@basaltkit/core";
|
|
226
226
|
|
|
227
227
|
// src/throttle.ts
|
|
@@ -509,19 +509,20 @@ var Auth = class {
|
|
|
509
509
|
* (theft indicator), the whole family is revoked.
|
|
510
510
|
*/
|
|
511
511
|
async refresh(refreshToken) {
|
|
512
|
-
const
|
|
512
|
+
const hashed = this.hashToken(refreshToken);
|
|
513
|
+
const record = await this.refreshTokens.find(hashed);
|
|
513
514
|
if (!record) throw new RefreshInvalidError();
|
|
514
515
|
if (record.usedAt !== void 0) {
|
|
515
516
|
await this.refreshTokens.revokeFamily(record.familyId);
|
|
516
517
|
throw new RefreshReusedError();
|
|
517
518
|
}
|
|
518
519
|
if (Date.now() >= record.expiresAt) throw new RefreshInvalidError();
|
|
519
|
-
await this.refreshTokens.markUsed(
|
|
520
|
+
await this.refreshTokens.markUsed(hashed);
|
|
520
521
|
return this.issueTokens(record.userId, record.familyId);
|
|
521
522
|
}
|
|
522
523
|
/** Revokes a refresh family — logout for token-based clients. */
|
|
523
524
|
async revoke(refreshToken) {
|
|
524
|
-
const record = await this.refreshTokens.find(refreshToken);
|
|
525
|
+
const record = await this.refreshTokens.find(this.hashToken(refreshToken));
|
|
525
526
|
if (record) await this.refreshTokens.revokeFamily(record.familyId);
|
|
526
527
|
}
|
|
527
528
|
verifyAccess(accessToken) {
|
|
@@ -659,18 +660,19 @@ var Auth = class {
|
|
|
659
660
|
return `${raw.slice(0, 5)}-${raw.slice(5)}`;
|
|
660
661
|
}
|
|
661
662
|
hashRecoveryCode(code) {
|
|
662
|
-
return
|
|
663
|
+
return createHash2("sha256").update(code.replace(/-/g, "").toLowerCase()).digest("hex");
|
|
663
664
|
}
|
|
664
665
|
// --- token helpers -------------------------------------------------------
|
|
665
|
-
/** SHA-256 of a one-time token — only this is
|
|
666
|
-
|
|
667
|
-
|
|
666
|
+
/** SHA-256 of a bearer secret (one-time token, refresh token) — only this is
|
|
667
|
+
* persisted, so a leak of the token/session table can't be replayed. */
|
|
668
|
+
hashToken(token) {
|
|
669
|
+
return createHash2("sha256").update(token).digest("hex");
|
|
668
670
|
}
|
|
669
671
|
async issueOneTimeToken(userId, purpose, ttl) {
|
|
670
672
|
await this.tokens.deleteForUser(userId, purpose);
|
|
671
673
|
const token = randomBytes4(32).toString("base64url");
|
|
672
674
|
await this.tokens.create({
|
|
673
|
-
token: this.
|
|
675
|
+
token: this.hashToken(token),
|
|
674
676
|
userId,
|
|
675
677
|
purpose,
|
|
676
678
|
expiresAt: Date.now() + parseDuration2(ttl)
|
|
@@ -678,7 +680,7 @@ var Auth = class {
|
|
|
678
680
|
return token;
|
|
679
681
|
}
|
|
680
682
|
async consumeToken(token, purpose) {
|
|
681
|
-
const hashed = this.
|
|
683
|
+
const hashed = this.hashToken(token);
|
|
682
684
|
const record = await this.tokens.find(hashed);
|
|
683
685
|
if (!record || record.purpose !== purpose || record.usedAt !== void 0 || Date.now() >= record.expiresAt) {
|
|
684
686
|
throw new AuthTokenInvalidError();
|
|
@@ -695,7 +697,8 @@ var Auth = class {
|
|
|
695
697
|
async issueTokens(userId, familyId = randomUUID2()) {
|
|
696
698
|
const refreshToken = randomBytes4(32).toString("base64url");
|
|
697
699
|
await this.refreshTokens.create({
|
|
698
|
-
token
|
|
700
|
+
// Persist only the hash; the raw token is returned to the client below.
|
|
701
|
+
token: this.hashToken(refreshToken),
|
|
699
702
|
familyId,
|
|
700
703
|
userId,
|
|
701
704
|
expiresAt: Date.now() + parseDuration2(this.refreshTtl)
|
|
@@ -742,7 +745,7 @@ function authPlugin(options) {
|
|
|
742
745
|
}
|
|
743
746
|
|
|
744
747
|
// src/apikeys.ts
|
|
745
|
-
import { createHash as
|
|
748
|
+
import { createHash as createHash3, randomBytes as randomBytes5, randomUUID as randomUUID3 } from "crypto";
|
|
746
749
|
import { BasaltError as BasaltError4 } from "@basaltkit/core";
|
|
747
750
|
var ScopeRequiredError = class extends BasaltError4 {
|
|
748
751
|
status = 403;
|
|
@@ -751,7 +754,7 @@ var ScopeRequiredError = class extends BasaltError4 {
|
|
|
751
754
|
}
|
|
752
755
|
};
|
|
753
756
|
var KEY_PREFIX = "mk_live_";
|
|
754
|
-
var hashKey = (key) =>
|
|
757
|
+
var hashKey = (key) => createHash3("sha256").update(key).digest("hex");
|
|
755
758
|
var strip = (record) => {
|
|
756
759
|
const { hash: _hash, ...info } = record;
|
|
757
760
|
return info;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Authentication for Basalt: password hashing, JWT with refresh rotation and reuse detection, sessions, ready-made /auth routes and route guards.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|