@jskit-ai/auth-provider-local-core 0.2.23 → 0.2.25
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 +1 -0
- package/package.json +6 -5
- package/src/server/lib/accounts.js +5 -3
- package/src/server/lib/emailConfirmation.js +37 -1
- package/src/server/lib/service.js +6 -1
- package/src/server/providers/AuthLocalProvider.js +23 -3
- package/src/server/types.js +11 -0
- package/test/providerRuntime.test.js +151 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- An application can confirm a new account at once, without a link, when it chooses to, for example while its email isn't set up: it installs the optional `auth.local.confirmation-mode` capability, a function that receives `{ email }` for each registration that would wait for its link and answers `"link"` (send it, as before) or `"without-link"` (create the account and sign the person in at once, with an address nobody proved, and run the register hook). Only that exact answer skips the link; any other answer means a link and logs a warning. The mode sends nothing, so a test copy (`SIDE_EFFECTS` is not `live`) keeps it and registers exactly as the live copy does, while an installed sender is still replaced by the simulated one there, whatever it does: a test copy never sends real email. A production server still needs a sender (or `AUTH_LOCAL_EMAIL_CONFIRMATION=false`). Before, the only way to confirm at once was code that confirmed with the link the application's own sender was given; in a test copy that sender never runs, so the code got no link and registering failed with 500 (`Invalid URL`). Such code moves to `auth.local.confirmation-mode`. Applications without one are unchanged. For direct library composition, `createLocalAuthService()` takes it as `confirmationMode`.
|
|
5
6
|
- A test copy (`SIDE_EFFECTS` is not `live`) sends no email: with an installed sender, confirmation and password-reset emails are logged as simulated (with their links) instead of sent. A logged (development) sender is unchanged. Tests that prove an installed sender set `SIDE_EFFECTS: "live"`.
|
|
6
7
|
- With email confirmation on, two registrations whose email is already proven sign in at once instead of waiting for a link: one through an invitation a feature's invitation context resolver resolved, still pending, that names the registering email (letter case does not matter) and has not expired (`expiresAt`), whose link reached that address; and the app's first account, admitted by the production setup code only the server's operator can read (`firstAccountSetup.admitRegistration` answers true for it). An invitation passed through as given (no resolver installed), or one whose context names no email, another email or an expiry that has passed, proves nothing: that registration still waits, as does every other sign-up, and development's first account, which needs no code. Signing in with a waiting registration's password is refused as unconfirmed (403 `auth.login.emailUnconfirmed`) whatever `AUTH_LOCAL_EMAIL_CONFIRMATION` says now, where with it off such a sign-in was an ordinary failed one; registering again with it off creates the account.
|
|
7
8
|
- Completing a password reset consumes the user's other password resets (`rec_…` tokens) only, leaving an app's own kinds of recovery token, such as sign-in links, open: the backend contract's `recovery.consumeForUser(userId, { idPrefix })` takes the kind it consumes, and the file backend requires it as the database backend does.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/auth-provider-local-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.25",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
},
|
|
53
53
|
{
|
|
54
54
|
"subpath": "./server/types",
|
|
55
|
-
"summary": "Types of the auth.local.backend contract (AuthLocalBackend) that every local auth backend implements, of the auth.local.password-strategy, auth.local.recovery-sender and auth.local.confirmation-
|
|
55
|
+
"summary": "Types of the auth.local.backend contract (AuthLocalBackend) that every local auth backend implements, of the auth.local.password-strategy, auth.local.recovery-sender, auth.local.confirmation-sender and auth.local.confirmation-mode capabilities, and of the configuration, passwords and sessions local auth's modules share."
|
|
56
56
|
}
|
|
57
57
|
],
|
|
58
58
|
"extensionCapabilities": [
|
|
@@ -61,13 +61,14 @@
|
|
|
61
61
|
"auth.local.password-strategy",
|
|
62
62
|
"auth.profile.projector",
|
|
63
63
|
"auth.local.recovery-sender",
|
|
64
|
-
"auth.local.confirmation-sender"
|
|
64
|
+
"auth.local.confirmation-sender",
|
|
65
|
+
"auth.local.confirmation-mode"
|
|
65
66
|
]
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
},
|
|
69
70
|
"peerDependencies": {
|
|
70
|
-
"@jskit-ai/auth-core": "0.2.
|
|
71
|
-
"@jskit-ai/kernel": "0.2.
|
|
71
|
+
"@jskit-ai/auth-core": "0.2.21",
|
|
72
|
+
"@jskit-ai/kernel": "0.2.21"
|
|
72
73
|
}
|
|
73
74
|
}
|
|
@@ -122,7 +122,8 @@ async function resolveRegistrationProfileOptions(invitationContextResolver, invi
|
|
|
122
122
|
/**
|
|
123
123
|
* The accounts of local auth: registering one (which waits for its email to
|
|
124
124
|
* be confirmed while config.emailConfirmation is on, unless an invitation sent
|
|
125
|
-
* to that email or the first account's setup code admits it
|
|
125
|
+
* to that email or the first account's setup code admits it, or the
|
|
126
|
+
* application's confirmation mode confirms it without a link), signing in with its
|
|
126
127
|
* password (failures slow the account down, as config.signInThrottle says) or
|
|
127
128
|
* as a dev user, and renaming it.
|
|
128
129
|
* @param {object} dependencies
|
|
@@ -175,9 +176,10 @@ function createLocalAccounts({
|
|
|
175
176
|
const profileOptions = await resolveRegistrationProfileOptions(invitationContextResolver, input.invitation, email);
|
|
176
177
|
if (!config.selfRegistration) await admitClosedRegistration(profileOptions);
|
|
177
178
|
const password = await passwords.hashPassword(input.password);
|
|
178
|
-
// The first account, admitted by the setup code only the operator can read, and an invited person need no link
|
|
179
|
+
// The first account, admitted by the setup code only the operator can read, and an invited person need no link,
|
|
180
|
+
// nor does a registration the application's confirmation mode chose to confirm without one.
|
|
179
181
|
const emailProven = firstAccount || invitationProvesEmail(profileOptions.invitation, email);
|
|
180
|
-
if (config.emailConfirmation && !emailProven) {
|
|
182
|
+
if (config.emailConfirmation && !emailProven && !(await emailConfirmation.confirmsWithoutLink(email))) {
|
|
181
183
|
return emailConfirmation.begin({ email, displayName, password, invitation: normalizeInvitationInput(input.invitation) });
|
|
182
184
|
}
|
|
183
185
|
const result = await backend.withTransaction(async (tx) => {
|
|
@@ -5,6 +5,17 @@ import { buildAuthPayload } from "./authPayloads.js";
|
|
|
5
5
|
import { createId, isExpiredIso, isoFromNow, randomToken, sha256Base64url } from "./tokens.js";
|
|
6
6
|
|
|
7
7
|
const CONFIRMATION_TOKEN_TTL_SECONDS = 24 * 60 * 60;
|
|
8
|
+
const CONFIRM_BY_LINK = "link";
|
|
9
|
+
const CONFIRM_WITHOUT_LINK = "without-link";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The confirmation mode while no `auth.local.confirmation-mode` is installed:
|
|
13
|
+
* every registration waits for its link.
|
|
14
|
+
* @type {import("../types.js").AuthLocalConfirmationMode}
|
|
15
|
+
*/
|
|
16
|
+
async function confirmEveryRegistrationByLink() {
|
|
17
|
+
return CONFIRM_BY_LINK;
|
|
18
|
+
}
|
|
8
19
|
|
|
9
20
|
/**
|
|
10
21
|
* The confirmation sender while no `auth.local.confirmation-sender` is
|
|
@@ -64,15 +75,19 @@ function confirmationPasswordInvalidError() {
|
|
|
64
75
|
* after a day; asking for a new link replaces it, and registering the email again replaces the waiting registration
|
|
65
76
|
* whole, so a waiting registration never keeps the email from its owner. Confirming needs the link and the waiting
|
|
66
77
|
* registration's password: the link proves the email and the password proves the registration, so whoever registered
|
|
67
|
-
* an email, in whatever order, the account its link creates has a password the person confirming chose.
|
|
78
|
+
* an email, in whatever order, the account its link creates has a password the person confirming chose. The
|
|
79
|
+
* application's confirmation mode may instead confirm a registration without a link (confirmsWithoutLink).
|
|
68
80
|
* @param {object} dependencies
|
|
69
81
|
* @param {import("../types.js").AuthLocalBackend} dependencies.backend
|
|
70
82
|
* @param {import("../types.js").AuthLocalConfig} dependencies.config
|
|
83
|
+
* @param {import("../types.js").AuthLocalConfirmationMode} dependencies.confirmationMode The
|
|
84
|
+
* `auth.local.confirmation-mode`, or confirmEveryRegistrationByLink.
|
|
71
85
|
* @param {import("@jskit-ai/auth-core/server/types").AuthInvitationContextResolver} dependencies.invitationContextResolver
|
|
72
86
|
* @param {import("../types.js").AuthLocalFirstAccountSetup} dependencies.firstAccountSetup
|
|
73
87
|
* @param {import("../types.js").AuthLocalConfirmationSender} dependencies.confirmationSender The
|
|
74
88
|
* `auth.local.confirmation-sender`, or one made by createLoggedConfirmationSender;
|
|
75
89
|
* in development without a sender, links also go in the response.
|
|
90
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeLogger} dependencies.logger
|
|
76
91
|
* @param {import("../types.js").AuthLocalPasswords} dependencies.passwords
|
|
77
92
|
* @param {import("@jskit-ai/auth-core/server/types").AuthProfileProjector} dependencies.profileProjector
|
|
78
93
|
* @param {import("../types.js").AuthLocalSessions} dependencies.sessions
|
|
@@ -80,9 +95,11 @@ function confirmationPasswordInvalidError() {
|
|
|
80
95
|
function createLocalEmailConfirmation({
|
|
81
96
|
backend,
|
|
82
97
|
config,
|
|
98
|
+
confirmationMode,
|
|
83
99
|
confirmationSender,
|
|
84
100
|
firstAccountSetup,
|
|
85
101
|
invitationContextResolver,
|
|
102
|
+
logger,
|
|
86
103
|
passwords,
|
|
87
104
|
profileProjector,
|
|
88
105
|
sessions
|
|
@@ -106,6 +123,23 @@ function createLocalEmailConfirmation({
|
|
|
106
123
|
return confirmationDelivery === "dev-response" ? { confirmationUrl } : {};
|
|
107
124
|
}
|
|
108
125
|
|
|
126
|
+
/**
|
|
127
|
+
* True when the application's confirmation mode answers exactly "without-link" for this registration: it chose to
|
|
128
|
+
* create the account without proof of the address. Any other answer is a link; one that is neither answer is
|
|
129
|
+
* logged, since the application meant something else.
|
|
130
|
+
* @param {string} email
|
|
131
|
+
*/
|
|
132
|
+
async function confirmsWithoutLink(email) {
|
|
133
|
+
const mode = await confirmationMode({ email });
|
|
134
|
+
if (mode !== CONFIRM_BY_LINK && mode !== CONFIRM_WITHOUT_LINK) {
|
|
135
|
+
logger.warn(
|
|
136
|
+
{ mode: typeof mode === "string" ? mode : typeof mode },
|
|
137
|
+
'auth.local.confirmation-mode answered neither "link" nor "without-link", so the registration waits for its link.'
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return mode === CONFIRM_WITHOUT_LINK;
|
|
141
|
+
}
|
|
142
|
+
|
|
109
143
|
/**
|
|
110
144
|
* Keeps a registration, already validated and with its password hashed,
|
|
111
145
|
* until its email is confirmed. It replaces a registration already waiting
|
|
@@ -221,6 +255,7 @@ function createLocalEmailConfirmation({
|
|
|
221
255
|
}
|
|
222
256
|
|
|
223
257
|
return Object.freeze({
|
|
258
|
+
confirmsWithoutLink,
|
|
224
259
|
begin,
|
|
225
260
|
confirm,
|
|
226
261
|
resend,
|
|
@@ -230,6 +265,7 @@ function createLocalEmailConfirmation({
|
|
|
230
265
|
|
|
231
266
|
export {
|
|
232
267
|
CONFIRMATION_TOKEN_TTL_SECONDS,
|
|
268
|
+
confirmEveryRegistrationByLink,
|
|
233
269
|
createLocalEmailConfirmation,
|
|
234
270
|
createLoggedConfirmationSender,
|
|
235
271
|
createSimulatedConfirmationSender
|
|
@@ -4,7 +4,7 @@ import { noAppProfiles } from "@jskit-ai/auth-core/server/noAppProfiles";
|
|
|
4
4
|
import { throwUnsupportedAuthOperation } from "@jskit-ai/auth-core/server/unsupportedOperation";
|
|
5
5
|
import { createLocalAccounts } from "./accounts.js";
|
|
6
6
|
import { PROVIDER_ID } from "./authPayloads.js";
|
|
7
|
-
import { createLocalEmailConfirmation } from "./emailConfirmation.js";
|
|
7
|
+
import { confirmEveryRegistrationByLink, createLocalEmailConfirmation } from "./emailConfirmation.js";
|
|
8
8
|
import { createFirstAccountSetup } from "./firstAccountSetup.js";
|
|
9
9
|
import { createLocalOAuthFlows } from "./oauthFlows.js";
|
|
10
10
|
import { createLocalPasswordFlows } from "./passwordFlows.js";
|
|
@@ -18,6 +18,8 @@ import { createLocalSessions } from "./sessions.js";
|
|
|
18
18
|
* @param {object} dependencies
|
|
19
19
|
* @param {import("../types.js").AuthLocalBackend} dependencies.backend
|
|
20
20
|
* @param {import("../types.js").AuthLocalConfig} dependencies.config
|
|
21
|
+
* @param {import("../types.js").AuthLocalConfirmationMode} [dependencies.confirmationMode] The
|
|
22
|
+
* `auth.local.confirmation-mode`; by default every registration waits for its link.
|
|
21
23
|
* @param {import("../types.js").AuthLocalConfirmationSender} dependencies.confirmationSender The
|
|
22
24
|
* `auth.local.confirmation-sender`, or one made by createLoggedConfirmationSender.
|
|
23
25
|
* @param {import("../types.js").AuthLocalFirstAccountSetup} [dependencies.firstAccountSetup] The first
|
|
@@ -34,6 +36,7 @@ import { createLocalSessions } from "./sessions.js";
|
|
|
34
36
|
function createLocalAuthService({
|
|
35
37
|
backend,
|
|
36
38
|
config,
|
|
39
|
+
confirmationMode = confirmEveryRegistrationByLink,
|
|
37
40
|
confirmationSender,
|
|
38
41
|
firstAccountSetup: givenFirstAccountSetup = null,
|
|
39
42
|
invitationContextResolver,
|
|
@@ -100,9 +103,11 @@ function createLocalAuthService({
|
|
|
100
103
|
const emailConfirmation = createLocalEmailConfirmation({
|
|
101
104
|
backend,
|
|
102
105
|
config,
|
|
106
|
+
confirmationMode,
|
|
103
107
|
confirmationSender,
|
|
104
108
|
firstAccountSetup,
|
|
105
109
|
invitationContextResolver,
|
|
110
|
+
logger,
|
|
106
111
|
passwords,
|
|
107
112
|
profileProjector,
|
|
108
113
|
sessions
|
|
@@ -6,7 +6,11 @@ import { noAppProfiles } from "@jskit-ai/auth-core/server/noAppProfiles";
|
|
|
6
6
|
import { defineProvider } from "@jskit-ai/kernel/shared/capabilities";
|
|
7
7
|
import { sideEffectsAreLive } from "@jskit-ai/kernel/shared/support/sideEffects";
|
|
8
8
|
import { resolveLocalAuthConfig } from "../lib/config.js";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
confirmEveryRegistrationByLink,
|
|
11
|
+
createLoggedConfirmationSender,
|
|
12
|
+
createSimulatedConfirmationSender
|
|
13
|
+
} from "../lib/emailConfirmation.js";
|
|
10
14
|
import { createLocalFileBackend } from "../lib/fileBackend.js";
|
|
11
15
|
import { createFirstAccountSetup } from "../lib/firstAccountSetup.js";
|
|
12
16
|
import { createLoggedRecoverySender, createSimulatedRecoverySender } from "../lib/passwordFlows.js";
|
|
@@ -23,6 +27,7 @@ const AuthLocalProvider = defineProvider({
|
|
|
23
27
|
},
|
|
24
28
|
optional: {
|
|
25
29
|
backend: "auth.local.backend",
|
|
30
|
+
confirmationMode: "auth.local.confirmation-mode",
|
|
26
31
|
confirmationSender: "auth.local.confirmation-sender",
|
|
27
32
|
passwordStrategy: "auth.local.password-strategy",
|
|
28
33
|
profileProjector: "auth.profile.projector",
|
|
@@ -38,6 +43,8 @@ const AuthLocalProvider = defineProvider({
|
|
|
38
43
|
* @param {object} dependencies
|
|
39
44
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeAppRoot} dependencies.appRoot
|
|
40
45
|
* @param {import("../types.js").AuthLocalBackend | null} dependencies.backend The `auth.local.backend`, when one is installed.
|
|
46
|
+
* @param {import("../types.js").AuthLocalConfirmationMode | null} dependencies.confirmationMode The
|
|
47
|
+
* `auth.local.confirmation-mode`, when one is installed.
|
|
41
48
|
* @param {import("../types.js").AuthLocalConfirmationSender | null} dependencies.confirmationSender The
|
|
42
49
|
* `auth.local.confirmation-sender`, when one is installed.
|
|
43
50
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} dependencies.env
|
|
@@ -48,7 +55,18 @@ const AuthLocalProvider = defineProvider({
|
|
|
48
55
|
* @param {import("../types.js").AuthLocalRecoverySender | null} dependencies.recoverySender The `auth.local.recovery-sender`, when one is installed.
|
|
49
56
|
*/
|
|
50
57
|
async boot(
|
|
51
|
-
{
|
|
58
|
+
{
|
|
59
|
+
appRoot,
|
|
60
|
+
backend,
|
|
61
|
+
confirmationMode,
|
|
62
|
+
confirmationSender,
|
|
63
|
+
env,
|
|
64
|
+
extensions,
|
|
65
|
+
logger,
|
|
66
|
+
passwordStrategy,
|
|
67
|
+
profileProjector,
|
|
68
|
+
recoverySender
|
|
69
|
+
},
|
|
52
70
|
{ outputs }
|
|
53
71
|
) {
|
|
54
72
|
const fileBackend = backend === null;
|
|
@@ -59,11 +77,13 @@ const AuthLocalProvider = defineProvider({
|
|
|
59
77
|
// A production server without accounts logs its setup code as it starts.
|
|
60
78
|
const firstAccountSetup = createFirstAccountSetup({ backend: localBackend, config, logger });
|
|
61
79
|
await firstAccountSetup.start();
|
|
62
|
-
// A test copy (SIDE_EFFECTS is not live) logs the emails an installed sender would send, and sends none
|
|
80
|
+
// A test copy (SIDE_EFFECTS is not live) logs the emails an installed sender would send, and sends none, whatever
|
|
81
|
+
// the sender. The confirmation mode sends nothing, so a test copy keeps it.
|
|
63
82
|
const live = sideEffectsAreLive(env);
|
|
64
83
|
const baseService = createLocalAuthService({
|
|
65
84
|
backend: localBackend,
|
|
66
85
|
config,
|
|
86
|
+
confirmationMode: confirmationMode ?? confirmEveryRegistrationByLink,
|
|
67
87
|
confirmationSender: emailConfirmationSender
|
|
68
88
|
? (live ? confirmationSender : createSimulatedConfirmationSender(logger))
|
|
69
89
|
: createLoggedConfirmationSender(logger),
|
package/src/server/types.js
CHANGED
|
@@ -113,6 +113,17 @@
|
|
|
113
113
|
* @typedef {(delivery: { email: string, confirmationUrl: string }) => Promise<void>} AuthLocalConfirmationSender
|
|
114
114
|
*/
|
|
115
115
|
|
|
116
|
+
/**
|
|
117
|
+
* The `auth.local.confirmation-mode` capability: the application's choice, for
|
|
118
|
+
* one registration that would wait for its email to be confirmed, of how it is
|
|
119
|
+
* confirmed. "link" sends the confirmation link, as without one; "without-link"
|
|
120
|
+
* creates the account and signs the person in at once, with an address nobody
|
|
121
|
+
* proved, for example while the application's email isn't set up. Any other
|
|
122
|
+
* answer means "link". It sends nothing, so a test copy keeps it, where it
|
|
123
|
+
* replaces the confirmation sender.
|
|
124
|
+
* @typedef {(registration: { email: string }) => Promise<"link" | "without-link">} AuthLocalConfirmationMode
|
|
125
|
+
*/
|
|
126
|
+
|
|
116
127
|
/** @typedef {ReturnType<typeof import("./lib/config.js").resolveLocalAuthConfig>} AuthLocalConfig */
|
|
117
128
|
/** @typedef {AuthLocalConfig["oauthProviders"][number]} AuthLocalOAuthProvider */
|
|
118
129
|
/** @typedef {ReturnType<typeof import("./lib/firstAccountSetup.js").createFirstAccountSetup>} AuthLocalFirstAccountSetup */
|
|
@@ -96,6 +96,7 @@ async function createStartedApp({
|
|
|
96
96
|
passwordStrategy = null,
|
|
97
97
|
recoverySender = null,
|
|
98
98
|
confirmationSender = null,
|
|
99
|
+
confirmationMode = null,
|
|
99
100
|
invitationContextResolver = null,
|
|
100
101
|
logger = null,
|
|
101
102
|
configureExtensions = null
|
|
@@ -150,6 +151,7 @@ async function createStartedApp({
|
|
|
150
151
|
...(passwordStrategy ? { "auth.local.password-strategy": passwordStrategy } : {}),
|
|
151
152
|
...(recoverySender !== null ? { "auth.local.recovery-sender": recoverySender } : {}),
|
|
152
153
|
...(confirmationSender !== null ? { "auth.local.confirmation-sender": confirmationSender } : {}),
|
|
154
|
+
...(confirmationMode !== null ? { "auth.local.confirmation-mode": confirmationMode } : {}),
|
|
153
155
|
};
|
|
154
156
|
const runtime = createCapabilityRuntime({
|
|
155
157
|
inputs,
|
|
@@ -1599,3 +1601,152 @@ test("the confirmation sender emails the link, which is never returned or logged
|
|
|
1599
1601
|
assert.ok(confirmed.session.access_token);
|
|
1600
1602
|
assert.deepEqual(hooked, ["owner@example.com", "mail@example.com"]);
|
|
1601
1603
|
});
|
|
1604
|
+
|
|
1605
|
+
const CONFIRMATION_PASSWORD = "correct horse battery staple";
|
|
1606
|
+
const SIDE_EFFECTS_SETTINGS = Object.freeze([
|
|
1607
|
+
Object.freeze({ name: "live", SIDE_EFFECTS: "live" }),
|
|
1608
|
+
Object.freeze({ name: "a test copy", SIDE_EFFECTS: undefined })
|
|
1609
|
+
]);
|
|
1610
|
+
|
|
1611
|
+
// A logger that keeps what local auth logs and warns.
|
|
1612
|
+
function recordingLogger() {
|
|
1613
|
+
const infos = [];
|
|
1614
|
+
const warnings = [];
|
|
1615
|
+
return {
|
|
1616
|
+
infos,
|
|
1617
|
+
warnings,
|
|
1618
|
+
logger: {
|
|
1619
|
+
info: (details, message) => infos.push({ details, message }),
|
|
1620
|
+
warn: (details, message) => warnings.push({ details, message }),
|
|
1621
|
+
error() {},
|
|
1622
|
+
debug() {}
|
|
1623
|
+
}
|
|
1624
|
+
};
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
// The emails a test copy logged as simulated, by recipient.
|
|
1628
|
+
function simulatedEmails(infos) {
|
|
1629
|
+
return infos
|
|
1630
|
+
.filter(({ details }) => details?.sideEffects === "simulate" && details.kind === "email")
|
|
1631
|
+
.map(({ details }) => details.email);
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
// An app that confirms registrations at once while its email isn't set up says so, for each registration, through
|
|
1635
|
+
// auth.local.confirmation-mode. The mode sends nothing, so a test copy keeps it and answers as live does.
|
|
1636
|
+
test("a registration the app's confirmation mode confirms without a link signs in at once, live and in a test copy", async (t) => {
|
|
1637
|
+
for (const { name, SIDE_EFFECTS } of SIDE_EFFECTS_SETTINGS) {
|
|
1638
|
+
await t.test(name, async () => {
|
|
1639
|
+
const sent = [];
|
|
1640
|
+
const asked = [];
|
|
1641
|
+
const hooked = [];
|
|
1642
|
+
const { infos, logger } = recordingLogger();
|
|
1643
|
+
const app = await createStartedApp({
|
|
1644
|
+
env: { SIDE_EFFECTS, AUTH_LOCAL_EMAIL_CONFIRMATION: "" },
|
|
1645
|
+
confirmationSender: async (message) => { sent.push(message.email); },
|
|
1646
|
+
confirmationMode: async ({ email }) => {
|
|
1647
|
+
asked.push(email);
|
|
1648
|
+
return "without-link";
|
|
1649
|
+
},
|
|
1650
|
+
logger,
|
|
1651
|
+
configureExtensions(extensions, runtimeLogger) {
|
|
1652
|
+
extensions.registerServiceDecorator(createLocalAuthRegisterHookDecorator({
|
|
1653
|
+
logger: runtimeLogger,
|
|
1654
|
+
hook: { hookId: "welcome", blocking: true, async handle({ actor }) { hooked.push(actor.email); } }
|
|
1655
|
+
}));
|
|
1656
|
+
}
|
|
1657
|
+
});
|
|
1658
|
+
const registered = await app.authService.register({ email: "Ada@example.com", password: CONFIRMATION_PASSWORD });
|
|
1659
|
+
assert.equal(registered.requiresEmailConfirmation, false);
|
|
1660
|
+
assert.equal(registered.actor.email, "ada@example.com");
|
|
1661
|
+
assert.ok(registered.session.access_token);
|
|
1662
|
+
assert.deepEqual(asked, ["ada@example.com"]);
|
|
1663
|
+
assert.deepEqual(hooked, ["ada@example.com"]);
|
|
1664
|
+
assert.deepEqual(sent, [], "nothing sent");
|
|
1665
|
+
assert.deepEqual(simulatedEmails(infos), [], "nothing simulated");
|
|
1666
|
+
const signedIn = await app.authService.login({ email: "ada@example.com", password: CONFIRMATION_PASSWORD });
|
|
1667
|
+
assert.equal(signedIn.actor.email, "ada@example.com");
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
});
|
|
1671
|
+
|
|
1672
|
+
test("without a confirmation mode a registration waits for its link, which live sends and a test copy logs", async (t) => {
|
|
1673
|
+
for (const { name, SIDE_EFFECTS } of SIDE_EFFECTS_SETTINGS) {
|
|
1674
|
+
await t.test(name, async () => {
|
|
1675
|
+
const sent = [];
|
|
1676
|
+
const { infos, logger } = recordingLogger();
|
|
1677
|
+
const app = await createStartedApp({
|
|
1678
|
+
env: { SIDE_EFFECTS, AUTH_LOCAL_EMAIL_CONFIRMATION: "" },
|
|
1679
|
+
confirmationSender: async (message) => { sent.push(message.email); },
|
|
1680
|
+
logger
|
|
1681
|
+
});
|
|
1682
|
+
const registered = await app.authService.register({ email: "bea@example.com", password: CONFIRMATION_PASSWORD });
|
|
1683
|
+
assert.equal(registered.requiresEmailConfirmation, true);
|
|
1684
|
+
assert.equal(registered.session, null);
|
|
1685
|
+
assert.equal(Object.hasOwn(registered, "confirmationUrl"), false);
|
|
1686
|
+
await assert.rejects(
|
|
1687
|
+
app.authService.login({ email: "bea@example.com", password: CONFIRMATION_PASSWORD }),
|
|
1688
|
+
{ status: 403, code: "auth.login.emailUnconfirmed" }
|
|
1689
|
+
);
|
|
1690
|
+
const live = SIDE_EFFECTS === "live";
|
|
1691
|
+
assert.deepEqual(sent, live ? ["bea@example.com"] : []);
|
|
1692
|
+
assert.deepEqual(simulatedEmails(infos), live ? [] : ["bea@example.com"]);
|
|
1693
|
+
});
|
|
1694
|
+
}
|
|
1695
|
+
});
|
|
1696
|
+
|
|
1697
|
+
test("a test copy never sends through the app's sender, whatever its confirmation mode answers", async () => {
|
|
1698
|
+
const sent = [];
|
|
1699
|
+
let emailSetUp = true;
|
|
1700
|
+
const { infos, logger } = recordingLogger();
|
|
1701
|
+
const app = await createStartedApp({
|
|
1702
|
+
env: { SIDE_EFFECTS: undefined, AUTH_LOCAL_EMAIL_CONFIRMATION: "" },
|
|
1703
|
+
// A sender that sends real email: a test copy never calls it.
|
|
1704
|
+
confirmationSender: async (message) => { sent.push(message.email); },
|
|
1705
|
+
confirmationMode: async () => (emailSetUp ? "link" : "without-link"),
|
|
1706
|
+
logger
|
|
1707
|
+
});
|
|
1708
|
+
const linked = await app.authService.register({ email: "cy@example.com", password: CONFIRMATION_PASSWORD });
|
|
1709
|
+
assert.equal(linked.requiresEmailConfirmation, true);
|
|
1710
|
+
emailSetUp = false;
|
|
1711
|
+
const atOnce = await app.authService.register({ email: "dee@example.com", password: CONFIRMATION_PASSWORD });
|
|
1712
|
+
assert.equal(atOnce.requiresEmailConfirmation, false);
|
|
1713
|
+
assert.deepEqual(sent, [], "the app's sender never ran");
|
|
1714
|
+
assert.deepEqual(simulatedEmails(infos), ["cy@example.com"]);
|
|
1715
|
+
});
|
|
1716
|
+
|
|
1717
|
+
test("only the exact answer \"without-link\" confirms without a link, and a confirmation mode never stands in for a sender", async () => {
|
|
1718
|
+
const notWithoutLink = [true, "WITHOUT-LINK", "without link", "", undefined, null, 1, { mode: "without-link" }, ["without-link"]];
|
|
1719
|
+
let answer = "link";
|
|
1720
|
+
const { warnings, logger } = recordingLogger();
|
|
1721
|
+
const app = await createStartedApp({
|
|
1722
|
+
env: { AUTH_LOCAL_EMAIL_CONFIRMATION: "" },
|
|
1723
|
+
confirmationSender: async () => {},
|
|
1724
|
+
confirmationMode: async () => answer,
|
|
1725
|
+
logger
|
|
1726
|
+
});
|
|
1727
|
+
const linked = await app.authService.register({ email: "link@example.com", password: CONFIRMATION_PASSWORD });
|
|
1728
|
+
assert.equal(linked.requiresEmailConfirmation, true);
|
|
1729
|
+
assert.deepEqual(warnings, []);
|
|
1730
|
+
for (const [index, each] of notWithoutLink.entries()) {
|
|
1731
|
+
answer = each;
|
|
1732
|
+
const registered = await app.authService.register({ email: `user${index}@example.com`, password: CONFIRMATION_PASSWORD });
|
|
1733
|
+
assert.equal(registered.requiresEmailConfirmation, true, `answer ${index}`);
|
|
1734
|
+
}
|
|
1735
|
+
assert.equal(warnings.length, notWithoutLink.length);
|
|
1736
|
+
assert.ok(warnings.every(({ message }) => message.includes("auth.local.confirmation-mode")));
|
|
1737
|
+
|
|
1738
|
+
// In production a confirmation mode leaves the sender required, as without one.
|
|
1739
|
+
await assert.rejects(
|
|
1740
|
+
() => createStartedApp({
|
|
1741
|
+
env: {
|
|
1742
|
+
NODE_ENV: "production",
|
|
1743
|
+
AUTH_LOCAL_SESSION_SECRET: "fixture-session-secret",
|
|
1744
|
+
AUTH_LOCAL_FILE_PRODUCTION_ACK: "true",
|
|
1745
|
+
AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "",
|
|
1746
|
+
AUTH_LOCAL_EMAIL_CONFIRMATION: ""
|
|
1747
|
+
},
|
|
1748
|
+
confirmationMode: async () => "without-link"
|
|
1749
|
+
}),
|
|
1750
|
+
(error) => String(error.details?.cause?.message || error.message || "").includes("no auth.local.confirmation-sender")
|
|
1751
|
+
);
|
|
1752
|
+
});
|