@jskit-ai/auth-provider-local-core 0.2.36 → 0.2.38
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 +7 -0
- package/locales/en.json +9 -1
- package/package.json +8 -6
- package/src/server/lib/accounts.js +41 -11
- package/src/server/lib/config.js +52 -34
- package/src/server/lib/deliveries.js +4 -2
- package/src/server/lib/emailChange.js +183 -0
- package/src/server/lib/emailConfirmation.js +4 -2
- package/src/server/lib/fileBackend.js +40 -2
- package/src/server/lib/fileRecords.js +17 -7
- package/src/server/lib/firstAccountSetup.js +2 -2
- package/src/server/lib/index.js +2 -0
- package/src/server/lib/oauthFlows.js +18 -7
- package/src/server/lib/passSignIn.js +16 -6
- package/src/server/lib/passwordFlows.js +4 -2
- package/src/server/lib/registerHookDecorator.js +4 -4
- package/src/server/lib/service.js +28 -7
- package/src/server/lib/sessions.js +95 -17
- package/src/server/providers/AuthLocalProvider.js +28 -6
- package/src/server/types.js +63 -4
- package/test/accountEnumeration.test.js +12 -3
- package/test/cookieSameSite.test.js +25 -0
- package/test/emailChange.test.js +37 -0
- package/test/oauthSignIn.test.js +13 -1
- package/test/providerRuntime.test.js +133 -8
- package/test/sessionMetadata.test.js +38 -0
- package/test/signInEvents.test.js +206 -0
- package/test-support/emailChangeScenario.js +139 -0
- package/test-support/localService.js +9 -3
- package/test-support/passSignInScenario.js +10 -4
- package/test-support/sessionMetadataScenario.js +76 -0
|
@@ -155,8 +155,9 @@ function createLocalEmailConfirmation({
|
|
|
155
155
|
* code and the password it registered with, and signs the person in. A wrong
|
|
156
156
|
* password creates nothing and keeps the registration and its link.
|
|
157
157
|
* @param {{ token: string, password: string }} input
|
|
158
|
+
* @param {object | null} [request] The request that confirms it, where the new session is first seen from.
|
|
158
159
|
*/
|
|
159
|
-
async function confirm(input) {
|
|
160
|
+
async function confirm(input, request = null) {
|
|
160
161
|
const tokenHash = sha256Base64url(String(input.token || "").trim());
|
|
161
162
|
const waiting = await backend.withTransaction((tx) => tx.registrations.findByTokenHash(tokenHash));
|
|
162
163
|
if (!waiting || isExpiredIso(waiting.expiresAt)) {
|
|
@@ -188,7 +189,7 @@ function createLocalEmailConfirmation({
|
|
|
188
189
|
password: registration.password,
|
|
189
190
|
emailVerifiedAt: new Date().toISOString()
|
|
190
191
|
});
|
|
191
|
-
return { user, session: await sessions.createSessionForUser(tx, user) };
|
|
192
|
+
return { user, session: await sessions.createSessionForUser(tx, user, { request }) };
|
|
192
193
|
});
|
|
193
194
|
// Refused after the transaction, so a used-up registration stays deleted.
|
|
194
195
|
if (outcome.invalid) {
|
|
@@ -198,6 +199,7 @@ function createLocalEmailConfirmation({
|
|
|
198
199
|
throw emailTakenError();
|
|
199
200
|
}
|
|
200
201
|
firstAccountSetup.accountCreated();
|
|
202
|
+
sessions.recordSignInEvent("signedIn", { method: "confirmation", userId: outcome.user.id, email: outcome.user.email }, request);
|
|
201
203
|
return buildAuthPayload({ user: outcome.user, session: outcome.session, profileProjector, profileOptions });
|
|
202
204
|
}
|
|
203
205
|
|
|
@@ -224,7 +224,8 @@ function createTx(state, touched) {
|
|
|
224
224
|
...clone(input),
|
|
225
225
|
createdAt: input.createdAt || nowIso(),
|
|
226
226
|
updatedAt: input.updatedAt || nowIso(),
|
|
227
|
-
disabled: input.disabled === true
|
|
227
|
+
disabled: input.disabled === true,
|
|
228
|
+
pendingEmail: input.pendingEmail || ""
|
|
228
229
|
};
|
|
229
230
|
state.users.push(user);
|
|
230
231
|
mark("users");
|
|
@@ -260,6 +261,30 @@ function createTx(state, touched) {
|
|
|
260
261
|
user.updatedAt = nowIso();
|
|
261
262
|
mark("users");
|
|
262
263
|
return clone(user);
|
|
264
|
+
},
|
|
265
|
+
async updatePendingEmail(userId, pendingEmail) {
|
|
266
|
+
const user = state.users.find((entry) => entry.id === userId);
|
|
267
|
+
if (!user) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
user.pendingEmail = String(pendingEmail || "");
|
|
271
|
+
user.updatedAt = nowIso();
|
|
272
|
+
mark("users");
|
|
273
|
+
return clone(user);
|
|
274
|
+
},
|
|
275
|
+
async updateEmail(userId, email) {
|
|
276
|
+
const user = state.users.find((entry) => entry.id === userId);
|
|
277
|
+
if (!user) {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
if (state.users.some((entry) => entry.id !== userId && entry.email === email)) {
|
|
281
|
+
throw new ConflictError("An account with this id or email already exists.", { code: "auth.local.accounts.exists" });
|
|
282
|
+
}
|
|
283
|
+
user.email = email;
|
|
284
|
+
user.pendingEmail = "";
|
|
285
|
+
user.updatedAt = nowIso();
|
|
286
|
+
mark("users");
|
|
287
|
+
return clone(user);
|
|
263
288
|
}
|
|
264
289
|
}),
|
|
265
290
|
sessions: Object.freeze({
|
|
@@ -269,7 +294,10 @@ function createTx(state, touched) {
|
|
|
269
294
|
purpose: input.purpose || "normal",
|
|
270
295
|
createdAt: input.createdAt || nowIso(),
|
|
271
296
|
revokedAt: input.revokedAt || "",
|
|
272
|
-
secondFactorAt: input.secondFactorAt || ""
|
|
297
|
+
secondFactorAt: input.secondFactorAt || "",
|
|
298
|
+
ip: input.ip || "",
|
|
299
|
+
userAgent: input.userAgent || "",
|
|
300
|
+
lastSeenAt: input.lastSeenAt || ""
|
|
273
301
|
};
|
|
274
302
|
state.sessions.push(session);
|
|
275
303
|
mark("sessions");
|
|
@@ -325,6 +353,16 @@ function createTx(state, touched) {
|
|
|
325
353
|
mark("sessions");
|
|
326
354
|
}
|
|
327
355
|
return count;
|
|
356
|
+
},
|
|
357
|
+
async touch(sessionId, { lastSeenAt, ip, userAgent }) {
|
|
358
|
+
const session = state.sessions.find((entry) => entry.id === sessionId);
|
|
359
|
+
if (!session) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
session.lastSeenAt = lastSeenAt;
|
|
363
|
+
session.ip = ip || session.ip;
|
|
364
|
+
session.userAgent = userAgent || session.userAgent;
|
|
365
|
+
mark("sessions");
|
|
328
366
|
}
|
|
329
367
|
}),
|
|
330
368
|
recovery: Object.freeze({
|
|
@@ -21,7 +21,8 @@ function parseUsers(content) {
|
|
|
21
21
|
const users = [];
|
|
22
22
|
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
23
23
|
const parts = splitLine(line, "user");
|
|
24
|
-
|
|
24
|
+
// Twelve fields, or thirteen since an account keeps its waiting email change.
|
|
25
|
+
if (parts.length !== 12 && parts.length !== 13) {
|
|
25
26
|
throw new Error("Invalid user record field count.");
|
|
26
27
|
}
|
|
27
28
|
users.push({
|
|
@@ -36,7 +37,8 @@ function parseUsers(content) {
|
|
|
36
37
|
},
|
|
37
38
|
createdAt: decodeField(parts[9]),
|
|
38
39
|
updatedAt: decodeField(parts[10]),
|
|
39
|
-
disabled: parts[11] === "1"
|
|
40
|
+
disabled: parts[11] === "1",
|
|
41
|
+
pendingEmail: decodeField(parts[12])
|
|
40
42
|
});
|
|
41
43
|
}
|
|
42
44
|
return users;
|
|
@@ -58,7 +60,8 @@ function serializeUsers(users) {
|
|
|
58
60
|
user.password.hash,
|
|
59
61
|
encodeField(user.createdAt),
|
|
60
62
|
encodeField(user.updatedAt),
|
|
61
|
-
user.disabled ? "1" : "0"
|
|
63
|
+
user.disabled ? "1" : "0",
|
|
64
|
+
encodeField(user.pendingEmail)
|
|
62
65
|
].join(":")
|
|
63
66
|
)
|
|
64
67
|
.join("\n") + (users.length > 0 ? "\n" : "");
|
|
@@ -68,8 +71,9 @@ function parseSessions(content) {
|
|
|
68
71
|
const sessions = [];
|
|
69
72
|
for (const line of String(content || "").split("\n").filter(Boolean)) {
|
|
70
73
|
const parts = splitLine(line, "session");
|
|
71
|
-
// A session written before second factors has no tenth field: it has not proved one.
|
|
72
|
-
|
|
74
|
+
// A session written before second factors has no tenth field: it has not proved one. One written before sessions
|
|
75
|
+
// kept where they were seen has no eleventh to thirteenth: nobody knows where or when it was last seen.
|
|
76
|
+
if (parts.length !== 9 && parts.length !== 10 && parts.length !== 13) {
|
|
73
77
|
throw new Error("Invalid session record field count.");
|
|
74
78
|
}
|
|
75
79
|
sessions.push({
|
|
@@ -80,7 +84,10 @@ function parseSessions(content) {
|
|
|
80
84
|
createdAt: decodeField(parts[6]),
|
|
81
85
|
expiresAt: decodeField(parts[7]),
|
|
82
86
|
revokedAt: decodeField(parts[8] || ""),
|
|
83
|
-
secondFactorAt: decodeField(parts[9] || "")
|
|
87
|
+
secondFactorAt: decodeField(parts[9] || ""),
|
|
88
|
+
ip: decodeField(parts[10] || ""),
|
|
89
|
+
userAgent: decodeField(parts[11] || ""),
|
|
90
|
+
lastSeenAt: decodeField(parts[12] || "")
|
|
84
91
|
});
|
|
85
92
|
}
|
|
86
93
|
return sessions;
|
|
@@ -100,7 +107,10 @@ function serializeSessions(sessions) {
|
|
|
100
107
|
encodeField(session.createdAt),
|
|
101
108
|
encodeField(session.expiresAt),
|
|
102
109
|
encodeField(session.revokedAt || ""),
|
|
103
|
-
encodeField(session.secondFactorAt || "")
|
|
110
|
+
encodeField(session.secondFactorAt || ""),
|
|
111
|
+
encodeField(session.ip || ""),
|
|
112
|
+
encodeField(session.userAgent || ""),
|
|
113
|
+
encodeField(session.lastSeenAt || "")
|
|
104
114
|
].join(":")
|
|
105
115
|
)
|
|
106
116
|
.join("\n") + (sessions.length > 0 ? "\n" : "");
|
|
@@ -43,7 +43,7 @@ function setupCodeInvalid() {
|
|
|
43
43
|
/**
|
|
44
44
|
* The first account of a staging or production server. While local auth has no account,
|
|
45
45
|
* creating one needs a one-time setup code that only the server's operator can
|
|
46
|
-
* read: each start of an ownerless
|
|
46
|
+
* read: each start of an ownerless staging or production server makes a new code, writes
|
|
47
47
|
* it in the log and in `first-account-setup-code` in AUTH_LOCAL_STORE_DIR, and
|
|
48
48
|
* keeps only its hash. A hosting tool that supplies the code in
|
|
49
49
|
* AUTH_LOCAL_SETUP_CODE shows it to the operator itself: the server then uses
|
|
@@ -97,7 +97,7 @@ function createFirstAccountSetup({ backend, config, logger }) {
|
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
99
|
ownerless = true;
|
|
100
|
-
if (!config.
|
|
100
|
+
if (!config.isStagingOrProduction) {
|
|
101
101
|
return;
|
|
102
102
|
}
|
|
103
103
|
if (config.setupCode) {
|
package/src/server/lib/index.js
CHANGED
|
@@ -2,9 +2,11 @@ export { createLocalAuthService } from "./service.js";
|
|
|
2
2
|
export { createLocalDeliveries } from "./deliveries.js";
|
|
3
3
|
export { resolveLocalAuthConfig } from "./config.js";
|
|
4
4
|
export { createLoggedConfirmationSender } from "./emailConfirmation.js";
|
|
5
|
+
export { createLoggedEmailChangeSender } from "./emailChange.js";
|
|
5
6
|
export { createLoggedRecoverySender } from "./passwordFlows.js";
|
|
6
7
|
export { createLocalFileBackend } from "./fileBackend.js";
|
|
7
8
|
export { createLoggedSecondFactorEvents } from "./secondFactor.js";
|
|
9
|
+
export { createLoggedSignInEvents } from "./sessions.js";
|
|
8
10
|
export { SETUP_CODE_ENV, SETUP_CODE_FILE, createFirstAccountSetup } from "./firstAccountSetup.js";
|
|
9
11
|
export {
|
|
10
12
|
LOCAL_AUTH_USER_REGISTERED_EVENT,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
2
2
|
import { hostOnlyCookieName, normalizeReturnToPath } from "@jskit-ai/auth-core/server/utils";
|
|
3
3
|
import { AUTH_TEXTS, createAuthError } from "@jskit-ai/auth-core/server/authTexts";
|
|
4
|
-
import { NO_PASSWORD, hasPassword, normalizeDisplayName } from "./accounts.js";
|
|
4
|
+
import { NO_PASSWORD, accountDisabledError, hasPassword, normalizeDisplayName } from "./accounts.js";
|
|
5
5
|
import { buildAuthPayload } from "./authPayloads.js";
|
|
6
6
|
import { createOAuthClients } from "./oauth.js";
|
|
7
7
|
import { cookieOptions, safeRequestCookies } from "./sessions.js";
|
|
@@ -34,8 +34,8 @@ function createLocalOAuthFlows({
|
|
|
34
34
|
securityStatus,
|
|
35
35
|
sessions
|
|
36
36
|
}) {
|
|
37
|
-
const {
|
|
38
|
-
const OAUTH_ATTEMPT_COOKIE = hostOnlyCookieName(OAUTH_ATTEMPT_COOKIE_NAME,
|
|
37
|
+
const { isStagingOrProduction, oauthEnabled } = config;
|
|
38
|
+
const OAUTH_ATTEMPT_COOKIE = hostOnlyCookieName(OAUTH_ATTEMPT_COOKIE_NAME, isStagingOrProduction);
|
|
39
39
|
const oauthRedirectUri = oauthEnabled ? `${config.appPublicUrl.replace(/\/$/u, "")}${OAUTH_REDIRECT_PATH}` : "";
|
|
40
40
|
const appOrigin = oauthEnabled ? new URL(config.appPublicUrl).origin : "";
|
|
41
41
|
const oauthClients = createOAuthClients({ logger });
|
|
@@ -100,8 +100,9 @@ function createLocalOAuthFlows({
|
|
|
100
100
|
/**
|
|
101
101
|
* Subject first, then a verified email that matches an existing user, then a
|
|
102
102
|
* new user created like register; profile sync then provisions it as usual.
|
|
103
|
+
* The sign-in, and the refusal of a disabled account, are sign-in events.
|
|
103
104
|
*/
|
|
104
|
-
async function signInWithOAuthIdentity(identity) {
|
|
105
|
+
async function signInWithOAuthIdentity(identity, request) {
|
|
105
106
|
await firstAccountSetup.start();
|
|
106
107
|
const result = await backend.withTransaction(async (tx) => {
|
|
107
108
|
const linked = await tx.identities.findByProviderSubject(identity.provider, identity.providerSubject);
|
|
@@ -116,9 +117,12 @@ function createLocalOAuthFlows({
|
|
|
116
117
|
password: NO_PASSWORD
|
|
117
118
|
});
|
|
118
119
|
}
|
|
119
|
-
if (!user
|
|
120
|
+
if (!user) {
|
|
120
121
|
throw new AppError(401, "This account can't sign in.", { code: "auth.local.oauth.accountUnavailable" });
|
|
121
122
|
}
|
|
123
|
+
if (user.disabled) {
|
|
124
|
+
return { disabled: user };
|
|
125
|
+
}
|
|
122
126
|
if (!linked) {
|
|
123
127
|
await tx.identities.create({
|
|
124
128
|
userId: user.id,
|
|
@@ -127,11 +131,18 @@ function createLocalOAuthFlows({
|
|
|
127
131
|
email: identity.email
|
|
128
132
|
});
|
|
129
133
|
}
|
|
130
|
-
return { user, registered, session: await sessions.createSessionForUser(tx, user) };
|
|
134
|
+
return { user, registered, session: await sessions.createSessionForUser(tx, user, { request }) };
|
|
131
135
|
});
|
|
136
|
+
const method = { method: "provider", provider: identity.provider };
|
|
137
|
+
if (result.disabled) {
|
|
138
|
+
const { id: userId, email } = result.disabled;
|
|
139
|
+
sessions.recordSignInEvent("failed", { ...method, reason: "disabled", userId, email }, request);
|
|
140
|
+
throw accountDisabledError(result.disabled);
|
|
141
|
+
}
|
|
132
142
|
if (result.registered) {
|
|
133
143
|
firstAccountSetup.accountCreated();
|
|
134
144
|
}
|
|
145
|
+
sessions.recordSignInEvent("signedIn", { ...method, userId: result.user.id, email: result.user.email }, request);
|
|
135
146
|
return {
|
|
136
147
|
...(await buildAuthPayload({ user: result.user, session: result.session, profileProjector })),
|
|
137
148
|
registered: result.registered
|
|
@@ -197,7 +208,7 @@ function createLocalOAuthFlows({
|
|
|
197
208
|
const identity = await oauthClients.verifiedIdentity(provider, { callbackUrl, attempt });
|
|
198
209
|
const result = attempt.intent === "link"
|
|
199
210
|
? await linkOAuthIdentity(request, attempt, identity, provider)
|
|
200
|
-
: await signInWithOAuthIdentity(identity);
|
|
211
|
+
: await signInWithOAuthIdentity(identity, request);
|
|
201
212
|
return { ...result, provider: provider.id, returnTo: attempt.returnTo };
|
|
202
213
|
}
|
|
203
214
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PASS_TEXTS, createPassError, verifyPass } from "@jskit-ai/auth-pass/server";
|
|
2
|
+
import { accountDisabledError } from "./accounts.js";
|
|
2
3
|
import { buildAuthPayload } from "./authPayloads.js";
|
|
3
4
|
import { sha256Base64url } from "./tokens.js";
|
|
4
5
|
|
|
@@ -15,8 +16,9 @@ async function noPassIdentities() {
|
|
|
15
16
|
* Sign-in with a one-time signed pass (`@jskit-ai/auth-pass/server`), for a configuration whose passSignIn
|
|
16
17
|
* holds the issuer's key, audience and resource. A pass is checked, then recorded as used in the backend, so it signs
|
|
17
18
|
* in once however many servers share the backend's database, and then the application's pass identity names the
|
|
18
|
-
* account it signs in: one that is missing
|
|
19
|
-
* password sign-in makes.
|
|
19
|
+
* account it signs in: one that is missing is refused as unmapped, and a disabled one is told it is disabled, as its
|
|
20
|
+
* right password is. The session is an ordinary one, as a password sign-in makes. The sign-in, and the refusal of a
|
|
21
|
+
* disabled account, are sign-in events; a pass that names no account signs no account in, and is none.
|
|
20
22
|
* @param {object} dependencies
|
|
21
23
|
* @param {import("../types.js").AuthLocalBackend} dependencies.backend
|
|
22
24
|
* @param {import("../types.js").AuthLocalConfig} dependencies.config
|
|
@@ -28,8 +30,11 @@ async function noPassIdentities() {
|
|
|
28
30
|
function createLocalPassSignIn({ backend, config, passIdentity, profileProjector, sessions }) {
|
|
29
31
|
const settings = config.passSignIn;
|
|
30
32
|
|
|
31
|
-
/**
|
|
32
|
-
|
|
33
|
+
/**
|
|
34
|
+
* @param {{ pass: string }} input
|
|
35
|
+
* @param {object | null} [request] The request that posted the pass, where the new session is first seen from.
|
|
36
|
+
*/
|
|
37
|
+
async function signInWithPass(input, request = null) {
|
|
33
38
|
const pass = verifyPass(input.pass, settings);
|
|
34
39
|
// Kept by the hash of its id, which the issuer chose, until the pass could no longer be used anyway. The expired
|
|
35
40
|
// ones go in a transaction of their own: in the same one, the range their delete locks would make two concurrent
|
|
@@ -42,10 +47,15 @@ function createLocalPassSignIn({ backend, config, passIdentity, profileProjector
|
|
|
42
47
|
}
|
|
43
48
|
const userId = await passIdentity(pass);
|
|
44
49
|
const user = userId ? await backend.withTransaction((tx) => tx.users.findById(userId)) : null;
|
|
45
|
-
if (!user
|
|
50
|
+
if (!user) {
|
|
46
51
|
throw createPassError(PASS_TEXTS.passUnmapped);
|
|
47
52
|
}
|
|
48
|
-
|
|
53
|
+
if (user.disabled) {
|
|
54
|
+
sessions.recordSignInEvent("failed", { method: "pass", reason: "disabled", userId: user.id, email: user.email }, request);
|
|
55
|
+
throw accountDisabledError(user);
|
|
56
|
+
}
|
|
57
|
+
const session = await backend.withTransaction((tx) => sessions.createSessionForUser(tx, user, { request }));
|
|
58
|
+
sessions.recordSignInEvent("signedIn", { method: "pass", userId: user.id, email: user.email }, request);
|
|
49
59
|
// After the transaction, as a password sign-in does: the profile projector works in transactions of its own.
|
|
50
60
|
return buildAuthPayload({ user, session, profileProjector });
|
|
51
61
|
}
|
|
@@ -96,7 +96,7 @@ function createLocalPasswordFlows({ backend, config, deliveries, passwords, prof
|
|
|
96
96
|
return requested;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
async function completePasswordRecovery(input) {
|
|
99
|
+
async function completePasswordRecovery(input, request = null) {
|
|
100
100
|
if (!recoveryEnabled) {
|
|
101
101
|
throw new AppError(503, "Password recovery is not configured.", { code: "auth.local.recovery.notConfigured" });
|
|
102
102
|
}
|
|
@@ -123,10 +123,12 @@ function createLocalPasswordFlows({ backend, config, deliveries, passwords, prof
|
|
|
123
123
|
user,
|
|
124
124
|
session: await sessions.createSessionForUser(tx, user, {
|
|
125
125
|
purpose: "recovery",
|
|
126
|
-
ttlSeconds: RECOVERY_SESSION_TTL_SECONDS
|
|
126
|
+
ttlSeconds: RECOVERY_SESSION_TTL_SECONDS,
|
|
127
|
+
request
|
|
127
128
|
})
|
|
128
129
|
};
|
|
129
130
|
});
|
|
131
|
+
sessions.recordSignInEvent("signedIn", { method: "recovery", userId: user.id, email: user.email }, request);
|
|
130
132
|
// After the transaction, so the request holds one database connection at a time: the profile projector works in
|
|
131
133
|
// transactions of its own.
|
|
132
134
|
return buildAuthPayload({ user, session, profileProjector });
|
|
@@ -98,8 +98,8 @@ function createLocalAuthRegisterHookDecorator({ decoratorId = "auth.local.regist
|
|
|
98
98
|
Object.defineProperties(Object.create(authService), {
|
|
99
99
|
register: {
|
|
100
100
|
enumerable: true,
|
|
101
|
-
value: async function register(input) {
|
|
102
|
-
const result = await authService.register(input);
|
|
101
|
+
value: async function register(input, request = null) {
|
|
102
|
+
const result = await authService.register(input, request);
|
|
103
103
|
if (result.requiresEmailConfirmation !== true) {
|
|
104
104
|
await runRegisterHook({ hook: registerHook, result, logger });
|
|
105
105
|
}
|
|
@@ -108,8 +108,8 @@ function createLocalAuthRegisterHookDecorator({ decoratorId = "auth.local.regist
|
|
|
108
108
|
},
|
|
109
109
|
confirmRegistration: {
|
|
110
110
|
enumerable: true,
|
|
111
|
-
value: async function confirmRegistration(input) {
|
|
112
|
-
const result = await authService.confirmRegistration(input);
|
|
111
|
+
value: async function confirmRegistration(input, request = null) {
|
|
112
|
+
const result = await authService.confirmRegistration(input, request);
|
|
113
113
|
await runRegisterHook({ hook: registerHook, result, logger });
|
|
114
114
|
return result;
|
|
115
115
|
}
|
|
@@ -5,6 +5,7 @@ import { throwUnsupportedAuthOperation } from "@jskit-ai/auth-core/server/unsupp
|
|
|
5
5
|
import { createLocalAccounts } from "./accounts.js";
|
|
6
6
|
import { PROVIDER_ID } from "./authPayloads.js";
|
|
7
7
|
import { createLocalDeliveries } from "./deliveries.js";
|
|
8
|
+
import { createLocalEmailChange } from "./emailChange.js";
|
|
8
9
|
import { confirmEveryRegistrationByLink, createLocalEmailConfirmation } from "./emailConfirmation.js";
|
|
9
10
|
import { createFirstAccountSetup } from "./firstAccountSetup.js";
|
|
10
11
|
import { createLocalOAuthFlows } from "./oauthFlows.js";
|
|
@@ -42,9 +43,11 @@ const NO_PASS_SIGN_IN = Object.freeze({
|
|
|
42
43
|
* `auth.local.confirmation-mode`; by default every registration waits for its link.
|
|
43
44
|
* @param {import("../types.js").AuthLocalConfirmationSender} dependencies.confirmationSender The
|
|
44
45
|
* `auth.local.confirmation-sender`, or one made by createLoggedConfirmationSender.
|
|
45
|
-
* @param {import("../types.js").AuthLocalDeliveries} [dependencies.deliveries] Sends the password reset
|
|
46
|
-
* confirmation link
|
|
47
|
-
* for. The provider passes the one its shutdown waits for.
|
|
46
|
+
* @param {import("../types.js").AuthLocalDeliveries} [dependencies.deliveries] Sends the password reset, new
|
|
47
|
+
* confirmation link and email change emails, and hands over the sign-in events, after their requests have answered;
|
|
48
|
+
* by default, its own, whose settled() nothing waits for. The provider passes the one its shutdown waits for.
|
|
49
|
+
* @param {import("../types.js").AuthLocalEmailChangeSender} dependencies.emailChangeSender The
|
|
50
|
+
* `auth.local.email-change-sender`, or one made by createLoggedEmailChangeSender.
|
|
48
51
|
* @param {import("../types.js").AuthLocalFirstAccountSetup} [dependencies.firstAccountSetup] The first
|
|
49
52
|
* account's setup code, already started by the provider; by default, one that starts on first use.
|
|
50
53
|
* @param {import("@jskit-ai/auth-core/server/types").AuthInvitationContextResolver} dependencies.invitationContextResolver
|
|
@@ -59,6 +62,8 @@ const NO_PASS_SIGN_IN = Object.freeze({
|
|
|
59
62
|
* `auth.local.recovery-sender`, or one made by createLoggedRecoverySender.
|
|
60
63
|
* @param {import("../types.js").AuthLocalSecondFactorEvents} dependencies.secondFactorEvents The
|
|
61
64
|
* `auth.local.second-factor-events`, or one made by createLoggedSecondFactorEvents.
|
|
65
|
+
* @param {import("../types.js").AuthLocalSignInEvents} dependencies.signInEvents The `auth.local.sign-in-events`, or
|
|
66
|
+
* one made by createLoggedSignInEvents; it is given each event through `deliveries`.
|
|
62
67
|
*/
|
|
63
68
|
function createLocalAuthService({
|
|
64
69
|
backend,
|
|
@@ -66,6 +71,7 @@ function createLocalAuthService({
|
|
|
66
71
|
confirmationMode = confirmEveryRegistrationByLink,
|
|
67
72
|
confirmationSender,
|
|
68
73
|
deliveries: givenDeliveries = null,
|
|
74
|
+
emailChangeSender,
|
|
69
75
|
firstAccountSetup: givenFirstAccountSetup = null,
|
|
70
76
|
invitationContextResolver,
|
|
71
77
|
logger,
|
|
@@ -73,7 +79,8 @@ function createLocalAuthService({
|
|
|
73
79
|
passwordStrategy,
|
|
74
80
|
profileProjector,
|
|
75
81
|
recoverySender,
|
|
76
|
-
secondFactorEvents
|
|
82
|
+
secondFactorEvents,
|
|
83
|
+
signInEvents
|
|
77
84
|
}) {
|
|
78
85
|
const passwords = normalizePasswordStrategy(passwordStrategy);
|
|
79
86
|
const firstAccountSetup = givenFirstAccountSetup ?? createFirstAccountSetup({ backend, config, logger });
|
|
@@ -109,6 +116,8 @@ function createLocalAuthService({
|
|
|
109
116
|
},
|
|
110
117
|
emailConfirmation: config.emailConfirmation,
|
|
111
118
|
emailConfirmationCode: config.emailConfirmation,
|
|
119
|
+
// Off in staging and production without an auth.local.email-change-sender, which sends the links.
|
|
120
|
+
emailChange: config.emailChangeDelivery !== "off",
|
|
112
121
|
profileUpdate: true,
|
|
113
122
|
providerLinking: {
|
|
114
123
|
start: oauthEnabled,
|
|
@@ -131,7 +140,7 @@ function createLocalAuthService({
|
|
|
131
140
|
features: { ...capabilitiesForFirstAccount.features, firstAccountSetupCode: true }
|
|
132
141
|
});
|
|
133
142
|
|
|
134
|
-
const sessions = createLocalSessions({ appProfileProjection, backend, config, profileProjector });
|
|
143
|
+
const sessions = createLocalSessions({ appProfileProjection, backend, config, deliveries, profileProjector, signInEvents });
|
|
135
144
|
const emailConfirmation = createLocalEmailConfirmation({
|
|
136
145
|
backend,
|
|
137
146
|
config,
|
|
@@ -165,6 +174,15 @@ function createLocalAuthService({
|
|
|
165
174
|
recoverySender,
|
|
166
175
|
sessions
|
|
167
176
|
});
|
|
177
|
+
const emailChange = createLocalEmailChange({
|
|
178
|
+
backend,
|
|
179
|
+
config,
|
|
180
|
+
deliveries,
|
|
181
|
+
emailChangeSender,
|
|
182
|
+
passwords,
|
|
183
|
+
profileProjector,
|
|
184
|
+
sessions
|
|
185
|
+
});
|
|
168
186
|
const securityStatus = createLocalSecurityStatus({ backend, config, oauthProvidersById, sessions });
|
|
169
187
|
const oauthFlows = createLocalOAuthFlows({
|
|
170
188
|
backend,
|
|
@@ -199,11 +217,11 @@ function createLocalAuthService({
|
|
|
199
217
|
return emailConfirmation.resend(input);
|
|
200
218
|
}
|
|
201
219
|
|
|
202
|
-
function confirmRegistration(input) {
|
|
220
|
+
function confirmRegistration(input, request = null) {
|
|
203
221
|
if (!config.emailConfirmation) {
|
|
204
222
|
return throwUnsupportedAuthOperation("confirmRegistration");
|
|
205
223
|
}
|
|
206
|
-
return emailConfirmation.confirm(input);
|
|
224
|
+
return emailConfirmation.confirm(input, request);
|
|
207
225
|
}
|
|
208
226
|
|
|
209
227
|
function requestOtpLogin() {
|
|
@@ -243,6 +261,9 @@ function createLocalAuthService({
|
|
|
243
261
|
resetPassword: passwordFlows.resetPassword,
|
|
244
262
|
changePassword: passwordFlows.changePassword,
|
|
245
263
|
updateDisplayName: accounts.updateDisplayName,
|
|
264
|
+
requestEmailChange: emailChange.requestEmailChange,
|
|
265
|
+
requestEmailChangeForUser: emailChange.requestEmailChangeForUser,
|
|
266
|
+
confirmEmailChange: emailChange.confirmEmailChange,
|
|
246
267
|
getSecurityStatus: securityStatus.getSecurityStatus,
|
|
247
268
|
// readable-check: allow setter-seam - not a slot: setPasswordSignInEnabled is an auth.service operation
|
|
248
269
|
setPasswordSignInEnabled,
|