@proteinjs/user-server 1.27.0 → 1.28.1
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/CHANGELOG.md +23 -0
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +3 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/src/authentication/EmailVerificationToken.d.ts +23 -0
- package/dist/src/authentication/EmailVerificationToken.d.ts.map +1 -0
- package/dist/src/authentication/EmailVerificationToken.js +90 -0
- package/dist/src/authentication/EmailVerificationToken.js.map +1 -0
- package/dist/src/authentication/MailedToken.d.ts +91 -0
- package/dist/src/authentication/MailedToken.d.ts.map +1 -0
- package/dist/src/authentication/MailedToken.js +221 -0
- package/dist/src/authentication/MailedToken.js.map +1 -0
- package/dist/src/authentication/PasswordResetToken.d.ts +7 -61
- package/dist/src/authentication/PasswordResetToken.d.ts.map +1 -1
- package/dist/src/authentication/PasswordResetToken.js +25 -140
- package/dist/src/authentication/PasswordResetToken.js.map +1 -1
- package/dist/src/routes/verifyEmail.d.ts +21 -0
- package/dist/src/routes/verifyEmail.d.ts.map +1 -0
- package/dist/src/routes/verifyEmail.js +107 -0
- package/dist/src/routes/verifyEmail.js.map +1 -0
- package/dist/src/services/Signup.d.ts +27 -1
- package/dist/src/services/Signup.d.ts.map +1 -1
- package/dist/src/services/Signup.js +166 -30
- package/dist/src/services/Signup.js.map +1 -1
- package/dist/test/EmailVerification.test.d.ts +2 -0
- package/dist/test/EmailVerification.test.d.ts.map +1 -0
- package/dist/test/EmailVerification.test.js +451 -0
- package/dist/test/EmailVerification.test.js.map +1 -0
- package/dist/test/MachineAccounts.integration.test.js +98 -15
- package/dist/test/MachineAccounts.integration.test.js.map +1 -1
- package/dist/test/NoAddressesInLogs.integration.test.js +2 -1
- package/dist/test/NoAddressesInLogs.integration.test.js.map +1 -1
- package/dist/test/Signup.test.js +8 -0
- package/dist/test/Signup.test.js.map +1 -1
- package/dist/test/UpdateUserInfoAvatar.test.js +21 -15
- package/dist/test/UpdateUserInfoAvatar.test.js.map +1 -1
- package/generated/index.ts +3 -1
- package/package.json +15 -15
- package/src/authentication/EmailVerificationToken.ts +32 -0
- package/src/authentication/MailedToken.ts +171 -0
- package/src/authentication/PasswordResetToken.ts +13 -131
- package/src/routes/verifyEmail.ts +64 -0
- package/src/services/Signup.ts +111 -10
- package/test/EmailVerification.test.ts +297 -0
- package/test/MachineAccounts.integration.test.ts +52 -11
- package/test/NoAddressesInLogs.integration.test.ts +2 -1
- package/test/Signup.test.ts +11 -1
- package/test/UpdateUserInfoAvatar.test.ts +7 -2
|
@@ -1,4 +1,19 @@
|
|
|
1
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
|
+
})();
|
|
2
17
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
18
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
19
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -35,90 +50,22 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
35
50
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
51
|
}
|
|
37
52
|
};
|
|
38
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
-
};
|
|
41
53
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
54
|
exports.PasswordResetToken = void 0;
|
|
43
|
-
var
|
|
44
|
-
var moment_1 = __importDefault(require("moment"));
|
|
45
|
-
var db_1 = require("@proteinjs/db");
|
|
46
|
-
var user_1 = require("@proteinjs/user");
|
|
55
|
+
var MailedToken_1 = require("./MailedToken");
|
|
47
56
|
/**
|
|
48
|
-
* The password-reset token
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* A token is 32 bytes from the platform CSPRNG, hex-encoded (64 lowercase hex characters). The
|
|
52
|
-
* token itself exists only in the link mailed to the account: the user row stores its SHA-256
|
|
53
|
-
* digest (hex) beside the expiry, never the token, so a read of the table yields nothing that
|
|
54
|
-
* can be presented. A digest is not a token — presenting one is hashed again and matches no row.
|
|
55
|
-
* A fast unsalted hash is enough here because the input is 256 bits of CSPRNG output, not a
|
|
56
|
-
* human-chosen secret.
|
|
57
|
-
*
|
|
58
|
-
* Resolution refuses anything that is not a value of the token's shape BEFORE any lookup. A
|
|
59
|
-
* query filter built from a request value that is not a token does not compare the way a token
|
|
60
|
-
* does: `null` renders as `IS NULL` and would match every account with no pending reset, an
|
|
61
|
-
* empty string matches an emptied column, and other types reach the driver. The presented token
|
|
62
|
-
* is then hashed, the row is looked up by that digest, and the row the lookup returns is
|
|
63
|
-
* re-checked in code — its stored digest must be a string equal to the presented token's digest
|
|
64
|
-
* (compared in constant time) and its expiry a real timestamp still in the future — so the
|
|
65
|
-
* outcome never rests on how the storage compares.
|
|
57
|
+
* The password-reset token: a `MailedToken` on the row's `passwordResetToken` /
|
|
58
|
+
* `passwordResetTokenExpiration` cells, live for an hour, spent by the password write.
|
|
66
59
|
*
|
|
67
|
-
* No migration
|
|
60
|
+
* No migration accompanied the move from a stored token to a stored digest: a row that still
|
|
68
61
|
* holds a token minted before it never equals the digest of anything presented, so that token
|
|
69
62
|
* fails validation for what remains of its hour and the person asks for a new link.
|
|
70
63
|
*/
|
|
71
|
-
var PasswordResetToken = /** @class */ (function () {
|
|
64
|
+
var PasswordResetToken = /** @class */ (function (_super) {
|
|
65
|
+
__extends(PasswordResetToken, _super);
|
|
72
66
|
function PasswordResetToken() {
|
|
67
|
+
return _super.call(this, { token: 'passwordResetToken', expiration: 'passwordResetTokenExpiration' }, { amount: PasswordResetToken.LIFETIME_MINUTES, unit: 'minutes' }) || this;
|
|
73
68
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Issue a token to `user`: store its digest and its expiry on the row, replacing any token
|
|
76
|
-
* outstanding, and return the token — the only time it exists outside the mailed link.
|
|
77
|
-
*/
|
|
78
|
-
PasswordResetToken.prototype.mint = function (user) {
|
|
79
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
80
|
-
var token;
|
|
81
|
-
return __generator(this, function (_a) {
|
|
82
|
-
switch (_a.label) {
|
|
83
|
-
case 0:
|
|
84
|
-
token = (0, crypto_1.randomBytes)(32).toString('hex');
|
|
85
|
-
return [4 /*yield*/, (0, db_1.getDbAsSystem)().update(user_1.tables.User, {
|
|
86
|
-
id: user.id,
|
|
87
|
-
passwordResetToken: this.digest(token),
|
|
88
|
-
passwordResetTokenExpiration: (0, moment_1.default)().add(PasswordResetToken.LIFETIME_MINUTES, 'minutes'),
|
|
89
|
-
})];
|
|
90
|
-
case 1:
|
|
91
|
-
_a.sent();
|
|
92
|
-
return [2 /*return*/, token];
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
PasswordResetToken.prototype.resolve = function (presented) {
|
|
98
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
99
|
-
var token, digest, user;
|
|
100
|
-
return __generator(this, function (_a) {
|
|
101
|
-
switch (_a.label) {
|
|
102
|
-
case 0:
|
|
103
|
-
token = this.parse(presented);
|
|
104
|
-
if (token === undefined) {
|
|
105
|
-
return [2 /*return*/, { status: 'malformed' }];
|
|
106
|
-
}
|
|
107
|
-
digest = this.digest(token);
|
|
108
|
-
return [4 /*yield*/, (0, db_1.getDbAsSystem)().get(user_1.tables.User, { passwordResetToken: digest })];
|
|
109
|
-
case 1:
|
|
110
|
-
user = _a.sent();
|
|
111
|
-
if (!user || !this.matches(user.passwordResetToken, digest)) {
|
|
112
|
-
return [2 /*return*/, { status: 'unknown' }];
|
|
113
|
-
}
|
|
114
|
-
if (!this.isLive(user.passwordResetTokenExpiration)) {
|
|
115
|
-
return [2 /*return*/, { status: 'expired', user: user }];
|
|
116
|
-
}
|
|
117
|
-
return [2 /*return*/, { status: 'live', user: user, token: token }];
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
};
|
|
122
69
|
/**
|
|
123
70
|
* Write the new credential and clear the token in one conditional update: the row must still
|
|
124
71
|
* carry this token's digest at write time, so two presentations of the same token cannot both
|
|
@@ -126,78 +73,16 @@ var PasswordResetToken = /** @class */ (function () {
|
|
|
126
73
|
*/
|
|
127
74
|
PasswordResetToken.prototype.redeem = function (user, token, hashedPassword) {
|
|
128
75
|
return __awaiter(this, void 0, void 0, function () {
|
|
129
|
-
var updated;
|
|
130
76
|
return __generator(this, function (_a) {
|
|
131
77
|
switch (_a.label) {
|
|
132
|
-
case 0: return [4 /*yield*/, (
|
|
133
|
-
case 1:
|
|
134
|
-
updated = _a.sent();
|
|
135
|
-
return [2 /*return*/, updated === 1];
|
|
78
|
+
case 0: return [4 /*yield*/, this.spend(user, token, { password: hashedPassword })];
|
|
79
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
136
80
|
}
|
|
137
81
|
});
|
|
138
82
|
});
|
|
139
83
|
};
|
|
140
|
-
/**
|
|
141
|
-
* Withdraw a token that never reached its owner (the mail carrying it failed to send): the row
|
|
142
|
-
* is cleared only while it still carries this token's digest, so a newer token is left alone.
|
|
143
|
-
*/
|
|
144
|
-
PasswordResetToken.prototype.revoke = function (user, token) {
|
|
145
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
146
|
-
return __generator(this, function (_a) {
|
|
147
|
-
switch (_a.label) {
|
|
148
|
-
case 0: return [4 /*yield*/, (0, db_1.getDbAsSystem)().update(user_1.tables.User, { passwordResetToken: null, passwordResetTokenExpiration: null }, { id: user.id, passwordResetToken: this.digest(token) })];
|
|
149
|
-
case 1:
|
|
150
|
-
_a.sent();
|
|
151
|
-
return [2 /*return*/];
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
};
|
|
156
|
-
/** When the token outstanding on `user` was minted, or undefined when the row carries none. */
|
|
157
|
-
PasswordResetToken.prototype.mintedAt = function (user) {
|
|
158
|
-
if (!user.passwordResetToken || !user.passwordResetTokenExpiration) {
|
|
159
|
-
return undefined;
|
|
160
|
-
}
|
|
161
|
-
return (0, moment_1.default)(user.passwordResetTokenExpiration).subtract(PasswordResetToken.LIFETIME_MINUTES, 'minutes');
|
|
162
|
-
};
|
|
163
|
-
/**
|
|
164
|
-
* A log-safe reference to a presented token: a short prefix of a well-formed one's digest,
|
|
165
|
-
* never the value itself and never a part of it. The routes log it only for a token that
|
|
166
|
-
* resolved `unknown` — one no row carries — so it never lines up with a digest a row holds.
|
|
167
|
-
* What it is for is telling refusals apart in the log: one stale link presented again reads
|
|
168
|
-
* the same each time, a run of different values reads differently. 48 bits of the SHA-256 of
|
|
169
|
-
* 256 random bits identify nothing and can be presented nowhere.
|
|
170
|
-
*/
|
|
171
|
-
PasswordResetToken.prototype.fingerprint = function (presented) {
|
|
172
|
-
var token = this.parse(presented);
|
|
173
|
-
if (token === undefined) {
|
|
174
|
-
return undefined;
|
|
175
|
-
}
|
|
176
|
-
return this.digest(token).slice(0, 12);
|
|
177
|
-
};
|
|
178
|
-
PasswordResetToken.prototype.parse = function (presented) {
|
|
179
|
-
return typeof presented === 'string' && PasswordResetToken.SHAPE.test(presented) ? presented : undefined;
|
|
180
|
-
};
|
|
181
|
-
/** What the row stores in place of a token: its SHA-256, hex-encoded. */
|
|
182
|
-
PasswordResetToken.prototype.digest = function (token) {
|
|
183
|
-
return (0, crypto_1.createHash)('sha256').update(token).digest('hex');
|
|
184
|
-
};
|
|
185
|
-
PasswordResetToken.prototype.matches = function (storedDigest, presentedDigest) {
|
|
186
|
-
if (typeof storedDigest !== 'string' || storedDigest.length !== presentedDigest.length) {
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
return (0, crypto_1.timingSafeEqual)(Buffer.from(storedDigest), Buffer.from(presentedDigest));
|
|
190
|
-
};
|
|
191
|
-
PasswordResetToken.prototype.isLive = function (expiration) {
|
|
192
|
-
if (!expiration) {
|
|
193
|
-
return false;
|
|
194
|
-
}
|
|
195
|
-
var expiresAt = (0, moment_1.default)(expiration);
|
|
196
|
-
return expiresAt.isValid() && (0, moment_1.default)().isBefore(expiresAt);
|
|
197
|
-
};
|
|
198
|
-
PasswordResetToken.SHAPE = /^[0-9a-f]{64}$/;
|
|
199
84
|
PasswordResetToken.LIFETIME_MINUTES = 60;
|
|
200
85
|
return PasswordResetToken;
|
|
201
|
-
}());
|
|
86
|
+
}(MailedToken_1.MailedToken));
|
|
202
87
|
exports.PasswordResetToken = PasswordResetToken;
|
|
203
88
|
//# sourceMappingURL=PasswordResetToken.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PasswordResetToken.js","sourceRoot":"","sources":["../../../src/authentication/PasswordResetToken.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"PasswordResetToken.js","sourceRoot":"","sources":["../../../src/authentication/PasswordResetToken.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,6CAAmE;AAInE;;;;;;;GAOG;AACH;IAAwC,sCAAW;IAGjD;eACE,kBACE,EAAE,KAAK,EAAE,oBAAoB,EAAE,UAAU,EAAE,8BAA8B,EAAE,EAC3E,EAAE,MAAM,EAAE,kBAAkB,CAAC,gBAAgB,EAAE,IAAI,EAAE,SAAS,EAAE,CACjE;IACH,CAAC;IAED;;;;OAIG;IACG,mCAAM,GAAZ,UAAa,IAAU,EAAE,KAAa,EAAE,cAAsB;;;;4BACrD,qBAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,cAAc,EAAE,CAAC,EAAA;4BAAlE,sBAAO,SAA2D,EAAC;;;;KACpE;IAhBuB,mCAAgB,GAAG,EAAE,CAAC;IAiBhD,yBAAC;CAAA,AAlBD,CAAwC,yBAAW,GAkBlD;AAlBY,gDAAkB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Route } from '@proteinjs/server-api';
|
|
2
|
+
/** What the route answers, for the page that called it: `{ state }`. */
|
|
3
|
+
export type VerifyEmailState = 'confirmed' | 'expired' | 'invalid';
|
|
4
|
+
/**
|
|
5
|
+
* `GET /user/verify-email?token=` — confirms an account's email address from the token in its
|
|
6
|
+
* mailed link. The link lands on the app's verify page (`uiRoutes.account.verifyEmail`), which
|
|
7
|
+
* calls this route with the token and shows the state it answers, the way the password-reset
|
|
8
|
+
* page calls its token routes: JSON, never a redirect.
|
|
9
|
+
*
|
|
10
|
+
* Resolves the presented token through `EmailVerificationToken` — which refuses anything but a
|
|
11
|
+
* well-formed token before any lookup — checks its expiry, and spends it: `emailVerified` is
|
|
12
|
+
* written and the token cleared in one conditional update, so a link confirms once.
|
|
13
|
+
* - 'confirmed': the address is now verified and the token spent;
|
|
14
|
+
* - 'expired': the link lapsed — the flag is unchanged; the account page's re-send mints a new one;
|
|
15
|
+
* - 'invalid': malformed, unknown, or already spent — the same word for all three, so the
|
|
16
|
+
* answer says nothing about whether a token ever existed.
|
|
17
|
+
* A request with no token at all is refused (400, 'invalid'). Nothing in any answer names an
|
|
18
|
+
* address, and the token itself never reaches the log.
|
|
19
|
+
*/
|
|
20
|
+
export declare const verifyEmail: Route;
|
|
21
|
+
//# sourceMappingURL=verifyEmail.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verifyEmail.d.ts","sourceRoot":"","sources":["../../../src/routes/verifyEmail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAM9C,wEAAwE;AACxE,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,SAAS,GAAG,SAAS,CAAC;AAEnE;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,WAAW,EAAE,KAsCzB,CAAC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
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;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.verifyEmail = void 0;
|
|
40
|
+
var user_1 = require("@proteinjs/user");
|
|
41
|
+
var logger_1 = require("@proteinjs/logger");
|
|
42
|
+
var util_node_1 = require("@proteinjs/util-node");
|
|
43
|
+
var EmailVerificationToken_1 = require("../authentication/EmailVerificationToken");
|
|
44
|
+
/**
|
|
45
|
+
* `GET /user/verify-email?token=` — confirms an account's email address from the token in its
|
|
46
|
+
* mailed link. The link lands on the app's verify page (`uiRoutes.account.verifyEmail`), which
|
|
47
|
+
* calls this route with the token and shows the state it answers, the way the password-reset
|
|
48
|
+
* page calls its token routes: JSON, never a redirect.
|
|
49
|
+
*
|
|
50
|
+
* Resolves the presented token through `EmailVerificationToken` — which refuses anything but a
|
|
51
|
+
* well-formed token before any lookup — checks its expiry, and spends it: `emailVerified` is
|
|
52
|
+
* written and the token cleared in one conditional update, so a link confirms once.
|
|
53
|
+
* - 'confirmed': the address is now verified and the token spent;
|
|
54
|
+
* - 'expired': the link lapsed — the flag is unchanged; the account page's re-send mints a new one;
|
|
55
|
+
* - 'invalid': malformed, unknown, or already spent — the same word for all three, so the
|
|
56
|
+
* answer says nothing about whether a token ever existed.
|
|
57
|
+
* A request with no token at all is refused (400, 'invalid'). Nothing in any answer names an
|
|
58
|
+
* address, and the token itself never reaches the log.
|
|
59
|
+
*/
|
|
60
|
+
exports.verifyEmail = {
|
|
61
|
+
path: user_1.routes.verifyEmail.path,
|
|
62
|
+
method: user_1.routes.verifyEmail.method,
|
|
63
|
+
onRequest: function (request, response) { return __awaiter(void 0, void 0, void 0, function () {
|
|
64
|
+
var logger, token, verification, resolution, account;
|
|
65
|
+
var _a;
|
|
66
|
+
return __generator(this, function (_b) {
|
|
67
|
+
switch (_b.label) {
|
|
68
|
+
case 0:
|
|
69
|
+
logger = new logger_1.Logger({ name: 'verifyEmail' });
|
|
70
|
+
token = ((_a = request.query) !== null && _a !== void 0 ? _a : {}).token;
|
|
71
|
+
if (!token) {
|
|
72
|
+
response.status(400).send({ state: 'invalid' });
|
|
73
|
+
return [2 /*return*/];
|
|
74
|
+
}
|
|
75
|
+
verification = new EmailVerificationToken_1.EmailVerificationToken();
|
|
76
|
+
return [4 /*yield*/, verification.resolve(token)];
|
|
77
|
+
case 1:
|
|
78
|
+
resolution = _b.sent();
|
|
79
|
+
if (resolution.status === 'malformed' || resolution.status === 'unknown') {
|
|
80
|
+
logger.info({
|
|
81
|
+
message: "Invalid email verification token used",
|
|
82
|
+
obj: { reason: resolution.status, token: verification.fingerprint(token) },
|
|
83
|
+
});
|
|
84
|
+
response.status(200).send({ state: 'invalid' });
|
|
85
|
+
return [2 /*return*/];
|
|
86
|
+
}
|
|
87
|
+
account = new util_node_1.RequestDigests().account(resolution.user.email);
|
|
88
|
+
if (resolution.status === 'expired') {
|
|
89
|
+
logger.info({ message: "Expired email verification token used", obj: { account: account } });
|
|
90
|
+
response.status(200).send({ state: 'expired' });
|
|
91
|
+
return [2 /*return*/];
|
|
92
|
+
}
|
|
93
|
+
return [4 /*yield*/, verification.confirm(resolution.user, resolution.token)];
|
|
94
|
+
case 2:
|
|
95
|
+
if (!(_b.sent())) {
|
|
96
|
+
logger.info({ message: "Email verification token already spent", obj: { account: account } });
|
|
97
|
+
response.status(200).send({ state: 'invalid' });
|
|
98
|
+
return [2 /*return*/];
|
|
99
|
+
}
|
|
100
|
+
logger.info({ message: "Email address verified", obj: { account: account } });
|
|
101
|
+
response.status(200).send({ state: 'confirmed' });
|
|
102
|
+
return [2 /*return*/];
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}); },
|
|
106
|
+
};
|
|
107
|
+
//# sourceMappingURL=verifyEmail.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verifyEmail.js","sourceRoot":"","sources":["../../../src/routes/verifyEmail.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,wCAAyC;AACzC,4CAA2C;AAC3C,kDAAsD;AACtD,mFAAkF;AAKlF;;;;;;;;;;;;;;;GAeG;AACU,QAAA,WAAW,GAAU;IAChC,IAAI,EAAE,aAAM,CAAC,WAAW,CAAC,IAAI;IAC7B,MAAM,EAAE,aAAM,CAAC,WAAW,CAAC,MAAM;IACjC,SAAS,EAAE,UAAO,OAAO,EAAE,QAAQ;;;;;;oBAC3B,MAAM,GAAG,IAAI,eAAM,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC;oBAC3C,KAAK,GAAK,CAAA,MAAA,OAAO,CAAC,KAAK,mCAAI,EAAE,CAAA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CAAC,EAAE;wBACpE,MAAM,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,wCAAwC,EAAE,GAAG,EAAE,EAAE,OAAO,SAAA,EAAE,EAAE,CAAC,CAAC;wBACrF,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,SAAoC,EAAE,CAAC,CAAC;wBAC3E,sBAAO;qBACR;oBAED,MAAM,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,wBAAwB,EAAE,GAAG,EAAE,EAAE,OAAO,SAAA,EAAE,EAAE,CAAC,CAAC;oBACrE,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,WAAsC,EAAE,CAAC,CAAC;;;;SAC9E;CACF,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SendInviteResponse, SignupService, InitializeSignupResponse, Invite, User, UserSignup } from '@proteinjs/user';
|
|
1
|
+
import { SendInviteResponse, ResendEmailVerificationResponse, SignupService, InitializeSignupResponse, Invite, User, UserSignup } from '@proteinjs/user';
|
|
2
2
|
import { Loadable } from '@proteinjs/reflection';
|
|
3
3
|
/**
|
|
4
4
|
* How long an invite link stays usable. Deliberately generous: a legitimate invite that gets
|
|
@@ -6,6 +6,12 @@ import { Loadable } from '@proteinjs/reflection';
|
|
|
6
6
|
* Expiry stays non-infinite so a forgotten invite isn't a permanent credential sitting in a mailbox.
|
|
7
7
|
*/
|
|
8
8
|
export declare const INVITE_TOKEN_TTL_DAYS = 90;
|
|
9
|
+
/**
|
|
10
|
+
* The gap between two verification mails to one account — the password reset's five minutes:
|
|
11
|
+
* long enough that a double tap sends one mail, short enough that a mistyped-looking inbox wait
|
|
12
|
+
* ends soon.
|
|
13
|
+
*/
|
|
14
|
+
export declare const EMAIL_VERIFICATION_RESEND_GAP_MINUTES = 5;
|
|
9
15
|
export interface InviteConfig {
|
|
10
16
|
isInviteOnly: boolean;
|
|
11
17
|
}
|
|
@@ -49,6 +55,15 @@ export declare class Signup implements SignupService {
|
|
|
49
55
|
* never see.
|
|
50
56
|
*/
|
|
51
57
|
createUser(user: UserSignup, token?: string): Promise<CreateUserResult>;
|
|
58
|
+
/**
|
|
59
|
+
* The signed-in account's own re-send: a fresh token replaces the one outstanding (the earlier
|
|
60
|
+
* link stops working) and the welcome-shaped mail goes out again with the new link, through the
|
|
61
|
+
* same config as the sign-up's. Refused without a word — `{ sent: false }` — when the address is
|
|
62
|
+
* already verified, when the last mint is inside the gap, or when the mail fails to send (the
|
|
63
|
+
* token is withdrawn then, so the next tap is not held by the gap). The account is the
|
|
64
|
+
* session's, read fresh from its row: a verification completed on another device is seen here.
|
|
65
|
+
*/
|
|
66
|
+
resendEmailVerification(): Promise<ResendEmailVerificationResponse>;
|
|
52
67
|
sendInvite(email: string): Promise<SendInviteResponse>;
|
|
53
68
|
/**
|
|
54
69
|
* Re-sends a standing invite: the token in the earlier email stops working, a fresh token with
|
|
@@ -102,5 +117,16 @@ export declare class Signup implements SignupService {
|
|
|
102
117
|
private mintInviteToken;
|
|
103
118
|
/** The invite email itself — the signup link carrying `token`, rendered by the consumer's config. */
|
|
104
119
|
private emailInvite;
|
|
120
|
+
/** The sign-up mails' content factory: the consumer's registration, or the library's default. */
|
|
121
|
+
private signupConfirmationEmailConfig;
|
|
122
|
+
/**
|
|
123
|
+
* The welcome mail — the new-user content from the config, handed the confirm link when the
|
|
124
|
+
* address is unverified (an invited account's welcome carries no context).
|
|
125
|
+
*/
|
|
126
|
+
private emailWelcome;
|
|
127
|
+
/** A fresh verification token on the account's row, with the confirm link that carries it. */
|
|
128
|
+
private mintEmailVerification;
|
|
129
|
+
/** The partial path a confirm link carries — the app's verify page with the token (not a full URL). */
|
|
130
|
+
private verifyEmailLink;
|
|
105
131
|
}
|
|
106
132
|
//# sourceMappingURL=Signup.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Signup.d.ts","sourceRoot":"","sources":["../../../src/services/Signup.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,
|
|
1
|
+
{"version":3,"file":"Signup.d.ts","sourceRoot":"","sources":["../../../src/services/Signup.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAClB,+BAA+B,EAI/B,aAAa,EACb,wBAAwB,EAExB,MAAM,EACN,IAAI,EACJ,UAAU,EAGX,MAAM,iBAAiB,CAAC;AAazB,OAAO,EAAE,QAAQ,EAAoB,MAAM,uBAAuB,CAAC;AAInE;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,KAAK,CAAC;AAExC;;;;GAIG;AACH,eAAO,MAAM,qCAAqC,IAAI,CAAC;AAEvD,MAAM,WAAW,YAAY;IAC3B,YAAY,EAAE,OAAO,CAAC;CACvB;AAED;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,UAAU,CAAA;CAAE,CAAC;AAEhH,MAAM,WAAW,0BAA2B,SAAQ,QAAQ;IAC1D,SAAS,IAAI,YAAY,CAAC;CAC3B;AAED,eAAO,MAAM,6BAA6B,QAAO,0BAShD,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,SAAS,GAAG,QAAQ,CAAC;IAC9B,0FAA0F;IAC1F,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,qBAAa,MAAO,YAAW,aAAa;IACnC,eAAe;;oCAEM,MAAM,QAAQ,GAAG,EAAE;;MAkB7C;IAEF;;;;;OAKG;IACG,UAAU,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAkE7E;;;;;;;OAOG;IACG,uBAAuB,IAAI,OAAO,CAAC,+BAA+B,CAAC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