@proteinjs/user-server 1.26.1 → 1.28.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.
Files changed (68) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/generated/index.d.ts.map +1 -1
  3. package/dist/generated/index.js +3 -1
  4. package/dist/generated/index.js.map +1 -1
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +8 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/src/authentication/EmailVerificationToken.d.ts +23 -0
  10. package/dist/src/authentication/EmailVerificationToken.d.ts.map +1 -0
  11. package/dist/src/authentication/EmailVerificationToken.js +90 -0
  12. package/dist/src/authentication/EmailVerificationToken.js.map +1 -0
  13. package/dist/src/authentication/MailedToken.d.ts +91 -0
  14. package/dist/src/authentication/MailedToken.d.ts.map +1 -0
  15. package/dist/src/authentication/MailedToken.js +221 -0
  16. package/dist/src/authentication/MailedToken.js.map +1 -0
  17. package/dist/src/authentication/PasswordResetToken.d.ts +7 -61
  18. package/dist/src/authentication/PasswordResetToken.d.ts.map +1 -1
  19. package/dist/src/authentication/PasswordResetToken.js +25 -140
  20. package/dist/src/authentication/PasswordResetToken.js.map +1 -1
  21. package/dist/src/routes/signup.d.ts +8 -3
  22. package/dist/src/routes/signup.d.ts.map +1 -1
  23. package/dist/src/routes/signup.js +55 -15
  24. package/dist/src/routes/signup.js.map +1 -1
  25. package/dist/src/routes/verifyEmail.d.ts +21 -0
  26. package/dist/src/routes/verifyEmail.d.ts.map +1 -0
  27. package/dist/src/routes/verifyEmail.js +107 -0
  28. package/dist/src/routes/verifyEmail.js.map +1 -0
  29. package/dist/src/services/DisposableEmailDomains.d.ts +24 -0
  30. package/dist/src/services/DisposableEmailDomains.d.ts.map +1 -0
  31. package/dist/src/services/DisposableEmailDomains.js +129 -0
  32. package/dist/src/services/DisposableEmailDomains.js.map +1 -0
  33. package/dist/src/services/Signup.d.ts +27 -1
  34. package/dist/src/services/Signup.d.ts.map +1 -1
  35. package/dist/src/services/Signup.js +166 -30
  36. package/dist/src/services/Signup.js.map +1 -1
  37. package/dist/src/services/SignupGuard.d.ts +55 -0
  38. package/dist/src/services/SignupGuard.d.ts.map +1 -0
  39. package/dist/src/services/SignupGuard.js +13 -0
  40. package/dist/src/services/SignupGuard.js.map +1 -0
  41. package/dist/test/EmailVerification.test.d.ts +2 -0
  42. package/dist/test/EmailVerification.test.d.ts.map +1 -0
  43. package/dist/test/EmailVerification.test.js +451 -0
  44. package/dist/test/EmailVerification.test.js.map +1 -0
  45. package/dist/test/Signup.test.js +8 -0
  46. package/dist/test/Signup.test.js.map +1 -1
  47. package/dist/test/SignupGuardSeam.test.d.ts +2 -0
  48. package/dist/test/SignupGuardSeam.test.d.ts.map +1 -0
  49. package/dist/test/SignupGuardSeam.test.js +474 -0
  50. package/dist/test/SignupGuardSeam.test.js.map +1 -0
  51. package/dist/test/UserServerTestEnvironment.d.ts.map +1 -1
  52. package/dist/test/UserServerTestEnvironment.js +4 -0
  53. package/dist/test/UserServerTestEnvironment.js.map +1 -1
  54. package/generated/index.ts +3 -1
  55. package/index.ts +10 -0
  56. package/package.json +4 -4
  57. package/src/authentication/EmailVerificationToken.ts +32 -0
  58. package/src/authentication/MailedToken.ts +171 -0
  59. package/src/authentication/PasswordResetToken.ts +13 -131
  60. package/src/routes/signup.ts +58 -9
  61. package/src/routes/verifyEmail.ts +64 -0
  62. package/src/services/DisposableEmailDomains.ts +125 -0
  63. package/src/services/Signup.ts +111 -10
  64. package/src/services/SignupGuard.ts +58 -0
  65. package/test/EmailVerification.test.ts +297 -0
  66. package/test/Signup.test.ts +11 -1
  67. package/test/SignupGuardSeam.test.ts +299 -0
  68. package/test/UserServerTestEnvironment.ts +4 -0
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export { createAuthentication } from './src/authentication/authenticate';
2
2
  export * from './src/authentication/DbSessionStore';
3
3
  export { InviteConfig, DefaultInviteConfigFactory, Signup } from './src/services/Signup';
4
+ export { DefaultSignupGuard, SignupGuardFacts, SignupGuardVerdict, getDefaultSignupGuard, } from './src/services/SignupGuard';
5
+ export { DisposableEmailDomains } from './src/services/DisposableEmailDomains';
4
6
  export { AccessInvite } from './src/services/AccessInvite';
5
7
  export { AccountDeletion } from './src/services/AccountDeletion';
6
8
  export * from './src/emails/AccountDeletionEmailConfigs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,cAAc,qCAAqC,CAAC;AAIpD,OAAO,EAAE,YAAY,EAAE,0BAA0B,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,cAAc,0CAA0C,CAAC;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAG3E,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAI7D,OAAO,EAAE,mBAAmB,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AAC5G,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,cAAc,qCAAqC,CAAC;AAIpD,OAAO,EAAE,YAAY,EAAE,0BAA0B,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAIzF,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,cAAc,0CAA0C,CAAC;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAG3E,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAI7D,OAAO,EAAE,mBAAmB,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AAC5G,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC"}
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.RequestDigests = exports.ClientAddress = exports.SlidingWindow = exports.AccountDeletionEmails = exports.AccountDeletion = exports.AccessInvite = exports.Signup = exports.createAuthentication = void 0;
17
+ exports.RequestDigests = exports.ClientAddress = exports.SlidingWindow = exports.AccountDeletionEmails = exports.AccountDeletion = exports.AccessInvite = exports.DisposableEmailDomains = exports.getDefaultSignupGuard = exports.Signup = exports.createAuthentication = void 0;
18
18
  var authenticate_1 = require("./src/authentication/authenticate");
19
19
  Object.defineProperty(exports, "createAuthentication", { enumerable: true, get: function () { return authenticate_1.createAuthentication; } });
20
20
  __exportStar(require("./src/authentication/DbSessionStore"), exports);
@@ -23,6 +23,13 @@ __exportStar(require("./src/authentication/DbSessionStore"), exports);
23
23
  // single owner the RPC door dispatches to, never a parallel invite-write path.
24
24
  var Signup_1 = require("./src/services/Signup");
25
25
  Object.defineProperty(exports, "Signup", { enumerable: true, get: function () { return Signup_1.Signup; } });
26
+ // The seam a consumer's sign-up abuse posture plugs into (the signup route consults it on the
27
+ // no-invite road), with the facts it is handed and the verdict it answers, so the registration
28
+ // compiles against them; and the throwaway-mail list the route applies before it.
29
+ var SignupGuard_1 = require("./src/services/SignupGuard");
30
+ Object.defineProperty(exports, "getDefaultSignupGuard", { enumerable: true, get: function () { return SignupGuard_1.getDefaultSignupGuard; } });
31
+ var DisposableEmailDomains_1 = require("./src/services/DisposableEmailDomains");
32
+ Object.defineProperty(exports, "DisposableEmailDomains", { enumerable: true, get: function () { return DisposableEmailDomains_1.DisposableEmailDomains; } });
26
33
  var AccessInvite_1 = require("./src/services/AccessInvite");
27
34
  Object.defineProperty(exports, "AccessInvite", { enumerable: true, get: function () { return AccessInvite_1.AccessInvite; } });
28
35
  var AccountDeletion_1 = require("./src/services/AccountDeletion");
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,kEAAyE;AAAhE,oHAAA,oBAAoB,OAAA;AAC7B,sEAAoD;AACpD,8FAA8F;AAC9F,0FAA0F;AAC1F,+EAA+E;AAC/E,gDAAyF;AAAtC,gGAAA,MAAM,OAAA;AACzD,4DAA2D;AAAlD,4GAAA,YAAY,OAAA;AACrB,kEAAiE;AAAxD,kHAAA,eAAe,OAAA;AACxB,2EAAyD;AACzD,4EAA2E;AAAlE,8HAAA,qBAAqB,OAAA;AAC9B,+FAA+F;AAC/F,gGAAgG;AAChG,8DAA6D;AAApD,8GAAA,aAAa,OAAA;AAKtB,8DAA6D;AAApD,8GAAA,aAAa,OAAA;AACtB,gEAA+D;AAAtD,gHAAA,cAAc,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,kEAAyE;AAAhE,oHAAA,oBAAoB,OAAA;AAC7B,sEAAoD;AACpD,8FAA8F;AAC9F,0FAA0F;AAC1F,+EAA+E;AAC/E,gDAAyF;AAAtC,gGAAA,MAAM,OAAA;AACzD,8FAA8F;AAC9F,+FAA+F;AAC/F,kFAAkF;AAClF,0DAKoC;AADlC,oHAAA,qBAAqB,OAAA;AAEvB,gFAA+E;AAAtE,gIAAA,sBAAsB,OAAA;AAC/B,4DAA2D;AAAlD,4GAAA,YAAY,OAAA;AACrB,kEAAiE;AAAxD,kHAAA,eAAe,OAAA;AACxB,2EAAyD;AACzD,4EAA2E;AAAlE,8HAAA,qBAAqB,OAAA;AAC9B,+FAA+F;AAC/F,gGAAgG;AAChG,8DAA6D;AAApD,8GAAA,aAAa,OAAA;AAKtB,8DAA6D;AAApD,8GAAA,aAAa,OAAA;AACtB,gEAA+D;AAAtD,gHAAA,cAAc,OAAA"}
@@ -0,0 +1,23 @@
1
+ import { User } from '@proteinjs/user';
2
+ import { MailedToken } from './MailedToken';
3
+ /**
4
+ * The email-verification token: a `MailedToken` on the row's `emailVerificationToken` /
5
+ * `emailVerificationTokenExpiration` cells, live for a week, spent by the write that marks the
6
+ * address verified. Minted for an account that signs up without an invite (the welcome mail
7
+ * carries its link) and again by `SignupService.resendEmailVerification`; an account created
8
+ * from an invite proved its address by receiving the invite and never carries one.
9
+ */
10
+ export declare class EmailVerificationToken extends MailedToken {
11
+ /**
12
+ * A week: a welcome mail read late should still confirm; the account works meanwhile, and a
13
+ * lapsed link is answered with the way to a new one.
14
+ */
15
+ static readonly LIFETIME_DAYS = 7;
16
+ constructor();
17
+ /**
18
+ * Confirm the address: `emailVerified` set and the token cleared in one conditional update, so
19
+ * a link confirms once. Resolves false when the token was already spent.
20
+ */
21
+ confirm(user: User, token: string): Promise<boolean>;
22
+ }
23
+ //# sourceMappingURL=EmailVerificationToken.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EmailVerificationToken.d.ts","sourceRoot":"","sources":["../../../src/authentication/EmailVerificationToken.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C;;;;;;GAMG;AACH,qBAAa,sBAAuB,SAAQ,WAAW;IACrD;;;OAGG;IACH,MAAM,CAAC,QAAQ,CAAC,aAAa,KAAK;;IASlC;;;OAGG;IACG,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAG3D"}
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.EmailVerificationToken = void 0;
55
+ var MailedToken_1 = require("./MailedToken");
56
+ /**
57
+ * The email-verification token: a `MailedToken` on the row's `emailVerificationToken` /
58
+ * `emailVerificationTokenExpiration` cells, live for a week, spent by the write that marks the
59
+ * address verified. Minted for an account that signs up without an invite (the welcome mail
60
+ * carries its link) and again by `SignupService.resendEmailVerification`; an account created
61
+ * from an invite proved its address by receiving the invite and never carries one.
62
+ */
63
+ var EmailVerificationToken = /** @class */ (function (_super) {
64
+ __extends(EmailVerificationToken, _super);
65
+ function EmailVerificationToken() {
66
+ return _super.call(this, { token: 'emailVerificationToken', expiration: 'emailVerificationTokenExpiration' }, { amount: EmailVerificationToken.LIFETIME_DAYS, unit: 'days' }) || this;
67
+ }
68
+ /**
69
+ * Confirm the address: `emailVerified` set and the token cleared in one conditional update, so
70
+ * a link confirms once. Resolves false when the token was already spent.
71
+ */
72
+ EmailVerificationToken.prototype.confirm = function (user, token) {
73
+ return __awaiter(this, void 0, void 0, function () {
74
+ return __generator(this, function (_a) {
75
+ switch (_a.label) {
76
+ case 0: return [4 /*yield*/, this.spend(user, token, { emailVerified: true })];
77
+ case 1: return [2 /*return*/, _a.sent()];
78
+ }
79
+ });
80
+ });
81
+ };
82
+ /**
83
+ * A week: a welcome mail read late should still confirm; the account works meanwhile, and a
84
+ * lapsed link is answered with the way to a new one.
85
+ */
86
+ EmailVerificationToken.LIFETIME_DAYS = 7;
87
+ return EmailVerificationToken;
88
+ }(MailedToken_1.MailedToken));
89
+ exports.EmailVerificationToken = EmailVerificationToken;
90
+ //# sourceMappingURL=EmailVerificationToken.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EmailVerificationToken.js","sourceRoot":"","sources":["../../../src/authentication/EmailVerificationToken.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,6CAA4C;AAE5C;;;;;;GAMG;AACH;IAA4C,0CAAW;IAOrD;eACE,kBACE,EAAE,KAAK,EAAE,wBAAwB,EAAE,UAAU,EAAE,kCAAkC,EAAE,EACnF,EAAE,MAAM,EAAE,sBAAsB,CAAC,aAAa,EAAE,IAAI,EAAE,MAAM,EAAE,CAC/D;IACH,CAAC;IAED;;;OAGG;IACG,wCAAO,GAAb,UAAc,IAAU,EAAE,KAAa;;;;4BAC9B,qBAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,EAAA;4BAA7D,sBAAO,SAAsD,EAAC;;;;KAC/D;IAnBD;;;OAGG;IACa,oCAAa,GAAG,CAAC,CAAC;IAgBpC,6BAAC;CAAA,AArBD,CAA4C,yBAAW,GAqBtD;AArBY,wDAAsB"}
@@ -0,0 +1,91 @@
1
+ import { Moment } from 'moment';
2
+ import { User } from '@proteinjs/user';
3
+ export type MailedTokenResolution = {
4
+ status: 'malformed';
5
+ } | {
6
+ status: 'unknown';
7
+ } | {
8
+ status: 'expired';
9
+ user: User;
10
+ } | {
11
+ status: 'live';
12
+ user: User;
13
+ token: string;
14
+ };
15
+ /** The two cells a token kind keeps on the user row: the stored digest and when it lapses. */
16
+ export type MailedTokenCells = {
17
+ token: 'passwordResetToken' | 'emailVerificationToken';
18
+ expiration: 'passwordResetTokenExpiration' | 'emailVerificationTokenExpiration';
19
+ };
20
+ /** How long a token stays live after its mint. */
21
+ export type MailedTokenLifetime = {
22
+ amount: number;
23
+ unit: 'minutes' | 'days';
24
+ };
25
+ /**
26
+ * A single-use token mailed to an account as a link — the one owner of the mechanism every kind
27
+ * shares: its shape, its mint, the lookup that maps a presented value back to the user it was
28
+ * issued to, its withdrawal, and the conditional write that spends it once. A kind (the password
29
+ * reset, the email verification) names its two cells on the user row, its lifetime, and what
30
+ * spending it writes.
31
+ *
32
+ * A token is 32 bytes from the platform CSPRNG, hex-encoded (64 lowercase hex characters). The
33
+ * token itself exists only in the link mailed to the account: the user row stores its SHA-256
34
+ * digest (hex) beside the expiry, never the token, so a read of the table yields nothing that
35
+ * can be presented. A digest is not a token — presenting one is hashed again and matches no row.
36
+ * A fast unsalted hash is enough here because the input is 256 bits of CSPRNG output, not a
37
+ * human-chosen secret.
38
+ *
39
+ * Resolution refuses anything that is not a value of the token's shape BEFORE any lookup. A
40
+ * query filter built from a request value that is not a token does not compare the way a token
41
+ * does: `null` renders as `IS NULL` and would match every account with no pending token, an
42
+ * empty string matches an emptied column, and other types reach the driver. The presented token
43
+ * is then hashed, the row is looked up by that digest, and the row the lookup returns is
44
+ * re-checked in code — its stored digest must be a string equal to the presented token's digest
45
+ * (compared in constant time) and its expiry a real timestamp still in the future — so the
46
+ * outcome never rests on how the storage compares.
47
+ */
48
+ export declare abstract class MailedToken {
49
+ private readonly cells;
50
+ private readonly lifetime;
51
+ private static readonly SHAPE;
52
+ protected constructor(cells: MailedTokenCells, lifetime: MailedTokenLifetime);
53
+ /**
54
+ * Issue a token to `user`: store its digest and its expiry on the row, replacing any token
55
+ * outstanding, and return the token — the only time it exists outside the mailed link.
56
+ */
57
+ mint(user: User): Promise<string>;
58
+ resolve(presented: unknown): Promise<MailedTokenResolution>;
59
+ /**
60
+ * Withdraw a token that never reached its owner (the mail carrying it failed to send): the row
61
+ * is cleared only while it still carries this token's digest, so a newer token is left alone.
62
+ */
63
+ revoke(user: User, token: string): Promise<void>;
64
+ /** When the token outstanding on `user` was minted, or undefined when the row carries none. */
65
+ mintedAt(user: User): Moment | undefined;
66
+ /**
67
+ * A log-safe reference to a presented token: a short prefix of a well-formed one's digest,
68
+ * never the value itself and never a part of it. The routes log it only for a token that
69
+ * resolved `unknown` — one no row carries — so it never lines up with a digest a row holds.
70
+ * What it is for is telling refusals apart in the log: one stale link presented again reads
71
+ * the same each time, a run of different values reads differently. 48 bits of the SHA-256 of
72
+ * 256 random bits identify nothing and can be presented nowhere.
73
+ */
74
+ fingerprint(presented: unknown): string | undefined;
75
+ /**
76
+ * Spend the token: `effect` is written and the token cleared in one conditional update — the
77
+ * row must still carry this token's digest at write time, so two presentations of the same
78
+ * token cannot both succeed. Resolves false when the token was already spent.
79
+ */
80
+ protected spend(user: User, token: string, effect: Partial<User>): Promise<boolean>;
81
+ private parse;
82
+ /** What the row stores in place of a token: its SHA-256, hex-encoded. */
83
+ private digest;
84
+ private matches;
85
+ private isLive;
86
+ /** The kind's two cells as a write: a digest and its expiry, or nulls to clear them. */
87
+ private cellsWrite;
88
+ /** The filter that finds the row carrying `digest` in the kind's token cell. */
89
+ private cellsLookup;
90
+ }
91
+ //# sourceM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AAM,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOtD,+FAA+F;IAC/F,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,SAAS;IASxC;;;;;;;OAOG;IACH,WAAW,CAAC,SAAS,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IASnD;;;;OAIG;cACa,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;IASzF,OAAO,CAAC,KAAK;IAIb,yEAAyE;IACzE,OAAO,CAAC,MAAM;IAId,OAAO,CAAC,OAAO;IAQf,OAAO,CAAC,MAAM;IASd,wFAAwF;IACxF,OAAO,CAAC,UAAU;IAIlB,gFAAgF;IAChF,OAAO,CAAC,WAAW;CAGpB"}
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.MailedToken = void 0;
54
+ var crypto_1 = require("crypto");
55
+ var moment_1 = __importDefault(require("moment"));
56
+ var db_1 = require("@proteinjs/db");
57
+ var user_1 = require("@proteinjs/user");
58
+ /**
59
+ * A single-use token mailed to an account as a link — the one owner of the mechanism every kind
60
+ * shares: its shape, its mint, the lookup that maps a presented value back to the user it was
61
+ * issued to, its withdrawal, and the conditional write that spends it once. A kind (the password
62
+ * reset, the email verification) names its two cells on the user row, its lifetime, and what
63
+ * spending it writes.
64
+ *
65
+ * A token is 32 bytes from the platform CSPRNG, hex-encoded (64 lowercase hex characters). The
66
+ * token itself exists only in the link mailed to the account: the user row stores its SHA-256
67
+ * digest (hex) beside the expiry, never the token, so a read of the table yields nothing that
68
+ * can be presented. A digest is not a token — presenting one is hashed again and matches no row.
69
+ * A fast unsalted hash is enough here because the input is 256 bits of CSPRNG output, not a
70
+ * human-chosen secret.
71
+ *
72
+ * Resolution refuses anything that is not a value of the token's shape BEFORE any lookup. A
73
+ * query filter built from a request value that is not a token does not compare the way a token
74
+ * does: `null` renders as `IS NULL` and would match every account with no pending token, an
75
+ * empty string matches an emptied column, and other types reach the driver. The presented token
76
+ * is then hashed, the row is looked up by that digest, and the row the lookup returns is
77
+ * re-checked in code — its stored digest must be a string equal to the presented token's digest
78
+ * (compared in constant time) and its expiry a real timestamp still in the future — so the
79
+ * outcome never rests on how the storage compares.
80
+ */
81
+ var MailedToken = /** @class */ (function () {
82
+ function MailedToken(cells, lifetime) {
83
+ this.cells = cells;
84
+ this.lifetime = lifetime;
85
+ }
86
+ /**
87
+ * Issue a token to `user`: store its digest and its expiry on the row, replacing any token
88
+ * outstanding, and return the token — the only time it exists outside the mailed link.
89
+ */
90
+ MailedToken.prototype.mint = function (user) {
91
+ return __awaiter(this, void 0, void 0, function () {
92
+ var token;
93
+ return __generator(this, function (_a) {
94
+ switch (_a.label) {
95
+ case 0:
96
+ token = (0, crypto_1.randomBytes)(32).toString('hex');
97
+ return [4 /*yield*/, (0, db_1.getDbAsSystem)().update(user_1.tables.User, __assign({ id: user.id }, this.cellsWrite(this.digest(token), (0, moment_1.default)().add(this.lifetime.amount, this.lifetime.unit))))];
98
+ case 1:
99
+ _a.sent();
100
+ return [2 /*return*/, token];
101
+ }
102
+ });
103
+ });
104
+ };
105
+ MailedToken.prototype.resolve = function (presented) {
106
+ return __awaiter(this, void 0, void 0, function () {
107
+ var token, digest, user;
108
+ return __generator(this, function (_a) {
109
+ switch (_a.label) {
110
+ case 0:
111
+ token = this.parse(presented);
112
+ if (token === undefined) {
113
+ return [2 /*return*/, { status: 'malformed' }];
114
+ }
115
+ digest = this.digest(token);
116
+ return [4 /*yield*/, (0, db_1.getDbAsSystem)().get(user_1.tables.User, this.cellsLookup(digest))];
117
+ case 1:
118
+ user = _a.sent();
119
+ if (!user || !this.matches(user[this.cells.token], digest)) {
120
+ return [2 /*return*/, { status: 'unknown' }];
121
+ }
122
+ if (!this.isLive(user[this.cells.expiration])) {
123
+ return [2 /*return*/, { status: 'expired', user: user }];
124
+ }
125
+ return [2 /*return*/, { status: 'live', user: user, token: token }];
126
+ }
127
+ });
128
+ });
129
+ };
130
+ /**
131
+ * Withdraw a token that never reached its owner (the mail carrying it failed to send): the row
132
+ * is cleared only while it still carries this token's digest, so a newer token is left alone.
133
+ */
134
+ MailedToken.prototype.revoke = function (user, token) {
135
+ return __awaiter(this, void 0, void 0, function () {
136
+ return __generator(this, function (_a) {
137
+ switch (_a.label) {
138
+ case 0: return [4 /*yield*/, (0, db_1.getDbAsSystem)().update(user_1.tables.User, this.cellsWrite(null, null), __assign({ id: user.id }, this.cellsLookup(this.digest(token))))];
139
+ case 1:
140
+ _a.sent();
141
+ return [2 /*return*/];
142
+ }
143
+ });
144
+ });
145
+ };
146
+ /** When the token outstanding on `user` was minted, or undefined when the row carries none. */
147
+ MailedToken.prototype.mintedAt = function (user) {
148
+ var expiration = user[this.cells.expiration];
149
+ if (!user[this.cells.token] || !expiration) {
150
+ return undefined;
151
+ }
152
+ return (0, moment_1.default)(expiration).subtract(this.lifetime.amount, this.lifetime.unit);
153
+ };
154
+ /**
155
+ * A log-safe reference to a presented token: a short prefix of a well-formed one's digest,
156
+ * never the value itself and never a part of it. The routes log it only for a token that
157
+ * resolved `unknown` — one no row carries — so it never lines up with a digest a row holds.
158
+ * What it is for is telling refusals apart in the log: one stale link presented again reads
159
+ * the same each time, a run of different values reads differently. 48 bits of the SHA-256 of
160
+ * 256 random bits identify nothing and can be presented nowhere.
161
+ */
162
+ MailedToken.prototype.fingerprint = function (presented) {
163
+ var token = this.parse(presented);
164
+ if (token === undefined) {
165
+ return undefined;
166
+ }
167
+ return this.digest(token).slice(0, 12);
168
+ };
169
+ /**
170
+ * Spend the token: `effect` is written and the token cleared in one conditional update — the
171
+ * row must still carry this token's digest at write time, so two presentations of the same
172
+ * token cannot both succeed. Resolves false when the token was already spent.
173
+ */
174
+ MailedToken.prototype.spend = function (user, token, effect) {
175
+ return __awaiter(this, void 0, void 0, function () {
176
+ var updated;
177
+ return __generator(this, function (_a) {
178
+ switch (_a.label) {
179
+ case 0: return [4 /*yield*/, (0, db_1.getDbAsSystem)().update(user_1.tables.User, __assign(__assign({}, effect), this.cellsWrite(null, null)), __assign({ id: user.id }, this.cellsLookup(this.digest(token))))];
180
+ case 1:
181
+ updated = _a.sent();
182
+ return [2 /*return*/, updated === 1];
183
+ }
184
+ });
185
+ });
186
+ };
187
+ MailedToken.prototype.parse = function (presented) {
188
+ return typeof presented === 'string' && MailedToken.SHAPE.test(presented) ? presented : undefined;
189
+ };
190
+ /** What the row stores in place of a token: its SHA-256, hex-encoded. */
191
+ MailedToken.prototype.digest = function (token) {
192
+ return (0, crypto_1.createHash)('sha256').update(token).digest('hex');
193
+ };
194
+ MailedToken.prototype.matches = function (storedDigest, presentedDigest) {
195
+ if (typeof storedDigest !== 'string' || storedDigest.length !== presentedDigest.length) {
196
+ return false;
197
+ }
198
+ return (0, crypto_1.timingSafeEqual)(Buffer.from(storedDigest), Buffer.from(presentedDigest));
199
+ };
200
+ MailedToken.prototype.isLive = function (expiration) {
201
+ if (!expiration) {
202
+ return false;
203
+ }
204
+ var expiresAt = (0, moment_1.default)(expiration);
205
+ return expiresAt.isValid() && (0, moment_1.default)().isBefore(expiresAt);
206
+ };
207
+ /** The kind's two cells as a write: a digest and its expiry, or nulls to clear them. */
208
+ MailedToken.prototype.cellsWrite = function (digest, expiration) {
209
+ var _a;
210
+ return _a = {}, _a[this.cells.token] = digest, _a[this.cells.expiration] = expiration, _a;
211
+ };
212
+ /** The filter that finds the row carrying `digest` in the kind's token cell. */
213
+ MailedToken.prototype.cellsLookup = function (digest) {
214
+ var _a;
215
+ return _a = {}, _a[this.cells.token] = digest, _a;
216
+ };
217
+ MailedToken.SHAPE = /^[0-9a-f]{64}$/;
218
+ return MailedToken;
219
+ }());
220
+ exports.MailedToken = MailedToken;
221
+ //# sourceMappingURL=MailedToken.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MailedToken.js","sourceRoot":"","sources":["../../../src/authentication/MailedToken.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAkE;AAClE,kDAAwC;AACxC,oCAA8C;AAC9C,wCAA+C;AAiB/C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH;IAGE,qBACmB,KAAuB,EACvB,QAA6B;QAD7B,UAAK,GAAL,KAAK,CAAkB;QACvB,aAAQ,GAAR,QAAQ,CAAqB;IAC7C,CAAC;IAEJ;;;OAGG;IACG,0BAAI,GAAV,UAAW,IAAU;;;;;;wBACb,KAAK,GAAG,IAAA,oBAAW,EAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;wBAC9C,qBAAM,IAAA,kBAAa,GAAE,CAAC,MAAM,CAAC,aAAM,CAAC,IAAI,aACtC,EAAE,EAAE,IAAI,CAAC,EAAE,IACR,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,IAAA,gBAAM,GAAE,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI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AA,AA/HD,IA+HC;AA/HqB,kCAAW"}
@@ -1,76 +1,22 @@
1
- import { Moment } from 'moment';
2
1
  import { User } from '@proteinjs/user';
3
- export type PasswordResetResolution = {
4
- status: 'malformed';
5
- } | {
6
- status: 'unknown';
7
- } | {
8
- status: 'expired';
9
- user: User;
10
- } | {
11
- status: 'live';
12
- user: User;
13
- token: string;
14
- };
2
+ import { MailedToken, MailedTokenResolution } from './MailedToken';
3
+ export type PasswordResetResolution = MailedTokenResolution;
15
4
  /**
16
- * The password-reset token's one owner: its shape, its mint, the lookup that maps a presented
17
- * value back to the user it was issued to, and its single-use redemption.
5
+ * The password-reset token: a `MailedToken` on the row's `passwordResetToken` /
6
+ * `passwordResetTokenExpiration` cells, live for an hour, spent by the password write.
18
7
  *
19
- * A token is 32 bytes from the platform CSPRNG, hex-encoded (64 lowercase hex characters). The
20
- * token itself exists only in the link mailed to the account: the user row stores its SHA-256
21
- * digest (hex) beside the expiry, never the token, so a read of the table yields nothing that
22
- * can be presented. A digest is not a token — presenting one is hashed again and matches no row.
23
- * A fast unsalted hash is enough here because the input is 256 bits of CSPRNG output, not a
24
- * human-chosen secret.
25
- *
26
- * Resolution refuses anything that is not a value of the token's shape BEFORE any lookup. A
27
- * query filter built from a request value that is not a token does not compare the way a token
28
- * does: `null` renders as `IS NULL` and would match every account with no pending reset, an
29
- * empty string matches an emptied column, and other types reach the driver. The presented token
30
- * is then hashed, the row is looked up by that digest, and the row the lookup returns is
31
- * re-checked in code — its stored digest must be a string equal to the presented token's digest
32
- * (compared in constant time) and its expiry a real timestamp still in the future — so the
33
- * outcome never rests on how the storage compares.
34
- *
35
- * No migration accompanies the move from a stored token to a stored digest: a row that still
8
+ * No migration accompanied the move from a stored token to a stored digest: a row that still
36
9
  * holds a token minted before it never equals the digest of anything presented, so that token
37
10
  * fails validation for what remains of its hour and the person asks for a new link.
38
11
  */
39
- export declare class PasswordResetToken {
40
- private static readonly SHAPE;
12
+ export declare class PasswordResetToken extends MailedToken {
41
13
  private static readonly LIFETIME_MINUTES;
42
- /**
43
- * Issue a token to `user`: store its digest and its expiry on the row, replacing any token
44
- * outstanding, and return the token — the only time it exists outside the mailed link.
45
- */
46
- mint(user: User): Promise<string>;
47
- resolve(presented: unknown): Promise<PasswordResetResolution>;
14
+ constructor();
48
15
  /**
49
16
  * Write the new credential and clear the token in one conditional update: the row must still
50
17
  * carry this token's digest at write time, so two presentations of the same token cannot both
51
18
  * succeed. Resolves false when the token was already redeemed.
52
19
  */
53
20
  redeem(user: User, token: string, hashedPassword: string): Promise<boolean>;
54
- /**
55
- * Withdraw a token that never reached its owner (the mail carrying it failed to send): the row
56
- * is cleared only while it still carries this token's digest, so a newer token is left alone.
57
- */
58
- revoke(user: User, token: string): Promise<void>;
59
- /** When the token outstanding on `user` was minted, or undefined when the row carries none. */
60
- mintedAt(user: User): Moment | undefined;
61
- /**
62
- * A log-safe reference to a presented token: a short prefix of a well-formed one's digest,
63
- * never the value itself and never a part of it. The routes log it only for a token that
64
- * resolved `unknown` — one no row carries — so it never lines up with a digest a row holds.
65
- * What it is for is telling refusals apart in the log: one stale link presented again reads
66
- * the same each time, a run of different values reads differently. 48 bits of the SHA-256 of
67
- * 256 random bits identify nothing and can be presented nowhere.
68
- */
69
- fingerprint(presented: unknown): string | undefined;
70
- private parse;
71
- /** What the row stores in place of a token: its SHA-256, hex-encoded. */
72
- private digest;
73
- private matches;
74
- private isLive;
75
21
  }
76
22
  //# sourceMappingURL=PasswordResetToken.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"PasswordResetToken.d.ts","sourceRoot":"","sources":["../../../src/authentication/PasswordResetToken.ts"],"names":[],"mappings":"AACA,OAAe,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAExC,OAAO,EAAU,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE/C,MAAM,MAAM,uBAAuB,GAC/B;IAAE,MAAM,EAAE,WAAW,CAAA;CAAE,GACvB;IAAE,MAAM,EAAE,SAAS,CAAA;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