@jskit-ai/auth-provider-local-core 0.2.37 → 0.2.39
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 +4 -4
- package/src/server/lib/config.js +37 -37
- package/src/server/lib/firstAccountSetup.js +2 -2
- package/src/server/lib/oauthFlows.js +2 -2
- package/src/server/lib/sessions.js +9 -9
- package/test/cookieSameSite.test.js +25 -0
- package/test/oauthSignIn.test.js +1 -1
- package/test/providerRuntime.test.js +45 -11
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- **Breaking:** the local file auth backend in staging and production needs `AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK=true`, in place of `AUTH_LOCAL_FILE_PRODUCTION_ACK`, which it always needed in staging too; a server with the old variable set refuses to start, naming the new one. The resolved configuration (`resolveLocalAuthConfig`) names whether it runs in staging or production `isStagingOrProduction`, in place of `isDeployed`.
|
|
5
6
|
- Email change: the signed-in person asks for a new email with their password (`requestEmailChange(request, { email, password })`), or the application asks for an account it chose, such as an operator's change for a person who lost their old address (`requestEmailChangeForUser({ userId, email })`, who may ask being the application's decision). The account keeps the address as its waiting change (`pendingEmail`) and the new address is sent a link to `/auth/email-change?token=`, signed with the session secret and working for a day; a newer request replaces the waiting change, so an earlier link stops working. Using it (`confirmEmailChange({ token })`) changes the email, which the link proves, ends the password resets sent to the old address and sends the old address a notice; the account's sessions stay signed in. Refusals: `auth.local.emailChange.passwordInvalid` (400), `sameEmail` (400), `emailTaken` (409, also when an account took the address since the link was sent), `linkInvalid` (400, a used, replaced, expired or altered link) and `accountNotFound` (404), each with its message key under `jskit.auth.local.emailChange`. The links and notices go through the new optional `auth.local.email-change-sender` capability (`AuthLocalEmailChangeSender`: `{ type: "confirmation", email, currentEmail, confirmationUrl }` or `{ type: "notice", email, newEmail }`) after the request has answered, through the local deliveries, and send as `APP_ENV` says, like the recovery and confirmation senders (production to each address, staging only to `STAGING_EMAIL_TO` with `originalEmail`, development and test to the log). Without one, development and test log them (`createLoggedEmailChangeSender`, new), and a staging or production server has no email change (`features.emailChange` false, the operations `AUTH_OPERATION_UNSUPPORTED`). A sender needs `APP_PUBLIC_URL`, which its links name.
|
|
6
7
|
- Upgrading: `createLocalAuthService()` takes `emailChangeSender`, and `resolveLocalAuthConfig()` takes `emailChangeSender: true` when one is installed (`config.emailChangeDelivery` is `email`, `dev-log` or `off`); a custom `auth.local.backend` keeps a user's `pendingEmail` and implements `users.updatePendingEmail(userId, pendingEmail)` and `users.updateEmail(userId, email)`, which refuses an email another account has with `auth.local.accounts.exists` and clears the waiting change. The file backend adds `pendingEmail` to each line of `users.passwd` and reads the lines it wrote before as having none.
|
|
7
8
|
- Each sign-in and each refused one is an event for the new optional `auth.local.sign-in-events` capability (`AuthLocalSignInEvent`, `AuthLocalSignInEvents`, also `createLocalAuthService({ signInEvents })`), for an application's list of an account's sign-ins or its audit log: `signedIn` or `failed`, the `method` (`password`, `registration`, `confirmation`, `recovery`, `pass`, or `provider` with the provider's id), a refusal's `reason` (`invalidCredentials`, `locked`, `unconfirmed`, `disabled`), the account's `userId` ("" when the email names none), the `email`, the request's `ip` and `userAgent`, and the time `at`; never a password, secret or token. It is given each event after the request has answered, through the deliveries the emails use, so a refused sign-in takes the same time whatever the recorder does; a recorder that fails is logged (`Sending the sign-in event failed.`), and a stopping server waits for the events still being handed over, as for the emails. Without one, local auth logs each event (`createLoggedSignInEvents`, new). A pass that names no account, and a provider callback refused before it names one, are not events.
|
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.39",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"./locales/en.json": "./locales/en.json"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@jskit-ai/auth-pass": "0.1.
|
|
15
|
+
"@jskit-ai/auth-pass": "0.1.3",
|
|
16
16
|
"openid-client": "6.8.8"
|
|
17
17
|
},
|
|
18
18
|
"description": "Local auth provider with a file backend default, no database requirement, and optional Google, Microsoft and GitHub sign-in.",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
}
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
|
-
"@jskit-ai/auth-core": "0.2.
|
|
77
|
-
"@jskit-ai/kernel": "0.2.
|
|
76
|
+
"@jskit-ai/auth-core": "0.2.33",
|
|
77
|
+
"@jskit-ai/kernel": "0.2.29"
|
|
78
78
|
}
|
|
79
79
|
}
|
package/src/server/lib/config.js
CHANGED
|
@@ -27,12 +27,12 @@ function resolveStoreDir(env, appRoot) {
|
|
|
27
27
|
* AUTH_LOCAL_SESSION_SECRET, or in development and test a secret kept in the
|
|
28
28
|
* store directory, generated on first use.
|
|
29
29
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
30
|
-
* @param {{ storeDir: string,
|
|
30
|
+
* @param {{ storeDir: string, isStagingOrProduction: boolean }} options
|
|
31
31
|
*/
|
|
32
|
-
function resolveSessionSecret(env, { storeDir,
|
|
32
|
+
function resolveSessionSecret(env, { storeDir, isStagingOrProduction }) {
|
|
33
33
|
const explicit = String(env.AUTH_LOCAL_SESSION_SECRET || "").trim();
|
|
34
34
|
if (explicit) return explicit;
|
|
35
|
-
if (
|
|
35
|
+
if (isStagingOrProduction) throw new Error("AUTH_LOCAL_SESSION_SECRET is required for local auth in staging and production.");
|
|
36
36
|
|
|
37
37
|
const secretPath = path.join(storeDir, "session.secret");
|
|
38
38
|
try {
|
|
@@ -57,9 +57,9 @@ const SECOND_FACTOR_KEY_MIN_CHARACTERS = 32;
|
|
|
57
57
|
* production server without it has no second factor (""); one shorter than 32
|
|
58
58
|
* characters stops startup.
|
|
59
59
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
60
|
-
* @param {{
|
|
60
|
+
* @param {{ isStagingOrProduction: boolean, sessionSecret: string }} options
|
|
61
61
|
*/
|
|
62
|
-
function resolveSecondFactorKey(env, {
|
|
62
|
+
function resolveSecondFactorKey(env, { isStagingOrProduction, sessionSecret }) {
|
|
63
63
|
const key = String(env.AUTH_LOCAL_SECOND_FACTOR_KEY || "").trim();
|
|
64
64
|
if (key) {
|
|
65
65
|
if (key.length < SECOND_FACTOR_KEY_MIN_CHARACTERS) {
|
|
@@ -67,7 +67,7 @@ function resolveSecondFactorKey(env, { isDeployed, sessionSecret }) {
|
|
|
67
67
|
}
|
|
68
68
|
return key;
|
|
69
69
|
}
|
|
70
|
-
return
|
|
70
|
+
return isStagingOrProduction ? "" : sessionSecret;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
/**
|
|
@@ -75,11 +75,11 @@ function resolveSecondFactorKey(env, { isDeployed, sessionSecret }) {
|
|
|
75
75
|
* supplies it, so that every start and every server asks for the same code.
|
|
76
76
|
* Only staging and production ask for a setup code; elsewhere the variable is ignored.
|
|
77
77
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
78
|
-
* @param {{
|
|
78
|
+
* @param {{ isStagingOrProduction: boolean }} options
|
|
79
79
|
*/
|
|
80
|
-
function resolveSetupCode(env, {
|
|
80
|
+
function resolveSetupCode(env, { isStagingOrProduction }) {
|
|
81
81
|
const code = String(env[SETUP_CODE_ENV] || "").trim();
|
|
82
|
-
if (!
|
|
82
|
+
if (!isStagingOrProduction || !code) return "";
|
|
83
83
|
if (normalizedSetupCode(code).length < SETUP_CODE_MIN_CHARACTERS) {
|
|
84
84
|
throw new Error(`${SETUP_CODE_ENV} must have at least ${SETUP_CODE_MIN_CHARACTERS} letters and digits.`);
|
|
85
85
|
}
|
|
@@ -89,12 +89,12 @@ function resolveSetupCode(env, { isDeployed }) {
|
|
|
89
89
|
/**
|
|
90
90
|
* APP_PUBLIC_URL, from which recovery, confirmation and email change links and
|
|
91
91
|
* OAuth redirect addresses are built, and whose host authenticator apps show
|
|
92
|
-
* beside a second factor's codes. Emailed links, OAuth sign-in and a
|
|
93
|
-
* server's second factor require it.
|
|
92
|
+
* beside a second factor's codes. Emailed links, OAuth sign-in and a staging or
|
|
93
|
+
* production server's second factor require it.
|
|
94
94
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
95
|
-
* @param {{ emailRecovery: boolean, emailedConfirmation: boolean, emailedChange: boolean, oauthEnabled: boolean,
|
|
95
|
+
* @param {{ emailRecovery: boolean, emailedConfirmation: boolean, emailedChange: boolean, oauthEnabled: boolean, stagingOrProductionSecondFactor: boolean }} options
|
|
96
96
|
*/
|
|
97
|
-
function resolveAppPublicUrl(env, { emailRecovery, emailedConfirmation, emailedChange, oauthEnabled,
|
|
97
|
+
function resolveAppPublicUrl(env, { emailRecovery, emailedConfirmation, emailedChange, oauthEnabled, stagingOrProductionSecondFactor }) {
|
|
98
98
|
const configured = String(env.APP_PUBLIC_URL || "").trim();
|
|
99
99
|
if (!configured) {
|
|
100
100
|
if (emailRecovery) throw new Error("APP_PUBLIC_URL is required when local auth email recovery is configured.");
|
|
@@ -103,7 +103,7 @@ function resolveAppPublicUrl(env, { emailRecovery, emailedConfirmation, emailedC
|
|
|
103
103
|
throw new Error("APP_PUBLIC_URL is required when local auth emails confirmation links.");
|
|
104
104
|
}
|
|
105
105
|
if (oauthEnabled) throw new Error("APP_PUBLIC_URL is required when local auth OAuth sign-in is configured.");
|
|
106
|
-
if (
|
|
106
|
+
if (stagingOrProductionSecondFactor) {
|
|
107
107
|
throw new Error("APP_PUBLIC_URL is required when AUTH_LOCAL_SECOND_FACTOR_KEY is set: authenticator apps show its host.");
|
|
108
108
|
}
|
|
109
109
|
return "http://localhost:5173";
|
|
@@ -127,12 +127,12 @@ function resolveAppPublicUrl(env, { emailRecovery, emailedConfirmation, emailedC
|
|
|
127
127
|
* production without a sender, recovery is disabled, and
|
|
128
128
|
* AUTH_LOCAL_RECOVERY_DEV_OUTPUT stops startup (resolveLocalAuthConfig).
|
|
129
129
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
130
|
-
* @param {{ emailRecovery: boolean,
|
|
130
|
+
* @param {{ emailRecovery: boolean, isStagingOrProduction: boolean }} options
|
|
131
131
|
* @returns {"email" | "disabled" | "dev-response" | "dev-log"}
|
|
132
132
|
*/
|
|
133
|
-
function resolveRecoveryDelivery(env, { emailRecovery,
|
|
133
|
+
function resolveRecoveryDelivery(env, { emailRecovery, isStagingOrProduction }) {
|
|
134
134
|
if (emailRecovery) return "email";
|
|
135
|
-
if (
|
|
135
|
+
if (isStagingOrProduction) return "disabled";
|
|
136
136
|
const devOutput = String(env.AUTH_LOCAL_RECOVERY_DEV_OUTPUT || "").trim().toLowerCase();
|
|
137
137
|
return devOutput === "response" ? "dev-response" : "dev-log";
|
|
138
138
|
}
|
|
@@ -148,13 +148,13 @@ function resolveRecoveryDelivery(env, { emailRecovery, isDeployed }) {
|
|
|
148
148
|
* (invited accounts and the first one, admitted by its setup code) nothing is
|
|
149
149
|
* confirmed.
|
|
150
150
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
151
|
-
* @param {{ emailConfirmationSender: boolean,
|
|
151
|
+
* @param {{ emailConfirmationSender: boolean, isStagingOrProduction: boolean, selfRegistration: boolean }} options
|
|
152
152
|
* @returns {"email" | "dev-response" | "off"}
|
|
153
153
|
*/
|
|
154
|
-
function resolveConfirmationDelivery(env, { emailConfirmationSender,
|
|
154
|
+
function resolveConfirmationDelivery(env, { emailConfirmationSender, isStagingOrProduction, selfRegistration }) {
|
|
155
155
|
if (!parseBooleanFlag(env.AUTH_LOCAL_EMAIL_CONFIRMATION, true)) return "off";
|
|
156
156
|
if (emailConfirmationSender) return "email";
|
|
157
|
-
if (
|
|
157
|
+
if (isStagingOrProduction) {
|
|
158
158
|
if (!selfRegistration) return "off";
|
|
159
159
|
throw new Error(
|
|
160
160
|
"Local auth confirms each new account's email address in staging and production, and no auth.local.confirmation-sender " +
|
|
@@ -169,14 +169,14 @@ function resolveConfirmationDelivery(env, { emailConfirmationSender, isDeployed,
|
|
|
169
169
|
/**
|
|
170
170
|
* How an email change's link and the notice to the old address reach the
|
|
171
171
|
* person: by email through the installed email-change sender; otherwise, in
|
|
172
|
-
* development and test, in the log. A
|
|
173
|
-
*
|
|
174
|
-
* @param {{ emailChangeSender: boolean,
|
|
172
|
+
* development and test, in the log. A staging or production server without a
|
|
173
|
+
* sender has no email change.
|
|
174
|
+
* @param {{ emailChangeSender: boolean, isStagingOrProduction: boolean }} options
|
|
175
175
|
* @returns {"email" | "dev-log" | "off"}
|
|
176
176
|
*/
|
|
177
|
-
function resolveEmailChangeDelivery({ emailChangeSender,
|
|
177
|
+
function resolveEmailChangeDelivery({ emailChangeSender, isStagingOrProduction }) {
|
|
178
178
|
if (emailChangeSender) return "email";
|
|
179
|
-
return
|
|
179
|
+
return isStagingOrProduction ? "off" : "dev-log";
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
/**
|
|
@@ -196,7 +196,7 @@ function resolvePasswordMinLength(env) {
|
|
|
196
196
|
|
|
197
197
|
/**
|
|
198
198
|
* The local auth configuration, resolved and checked once from the environment.
|
|
199
|
-
* Whether it
|
|
199
|
+
* Whether it runs in staging or production (APP_ENV) is worked out once, by
|
|
200
200
|
* auth-core's dev auth policy, so local auth and its dev sign-in agree on it.
|
|
201
201
|
* @param {object} input
|
|
202
202
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeAppRoot} input.appRoot
|
|
@@ -209,33 +209,33 @@ function resolvePasswordMinLength(env) {
|
|
|
209
209
|
*/
|
|
210
210
|
function resolveLocalAuthConfig({ appRoot, env, fileBackend, emailRecovery, emailConfirmationSender = false, emailChangeSender = false }) {
|
|
211
211
|
const devAuth = resolveDevAuthPolicyFromEnv(env);
|
|
212
|
-
const {
|
|
212
|
+
const { isStagingOrProduction } = devAuth;
|
|
213
213
|
const storeDir = resolveStoreDir(env, appRoot);
|
|
214
|
-
if (fileBackend &&
|
|
215
|
-
throw new Error("
|
|
214
|
+
if (fileBackend && isStagingOrProduction && !parseBooleanFlag(env.AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK, false)) {
|
|
215
|
+
throw new Error("AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK is required to use the local file auth backend in staging and production.");
|
|
216
216
|
}
|
|
217
217
|
assertDevAuthPolicy(devAuth);
|
|
218
218
|
assertDevelopmentOnlySettings(env, ["AUTH_LOCAL_RECOVERY_DEV_OUTPUT"]);
|
|
219
219
|
const oauthProviders = resolveOAuthProviders(env);
|
|
220
220
|
const oauthEnabled = oauthProviders.length > 0;
|
|
221
|
-
const recoveryDelivery = resolveRecoveryDelivery(env, { emailRecovery,
|
|
221
|
+
const recoveryDelivery = resolveRecoveryDelivery(env, { emailRecovery, isStagingOrProduction });
|
|
222
222
|
// AUTH_LOCAL_SELF_REGISTRATION, on unless set to false: off, an account registers only by an invitation a feature
|
|
223
223
|
// resolves, or as the first account.
|
|
224
224
|
const selfRegistration = parseBooleanFlag(env.AUTH_LOCAL_SELF_REGISTRATION, true);
|
|
225
|
-
const confirmationDelivery = resolveConfirmationDelivery(env, { emailConfirmationSender,
|
|
226
|
-
const sessionSecret = resolveSessionSecret(env, { storeDir,
|
|
227
|
-
const secondFactorKey = resolveSecondFactorKey(env, {
|
|
228
|
-
const emailChangeDelivery = resolveEmailChangeDelivery({ emailChangeSender,
|
|
225
|
+
const confirmationDelivery = resolveConfirmationDelivery(env, { emailConfirmationSender, isStagingOrProduction, selfRegistration });
|
|
226
|
+
const sessionSecret = resolveSessionSecret(env, { storeDir, isStagingOrProduction });
|
|
227
|
+
const secondFactorKey = resolveSecondFactorKey(env, { isStagingOrProduction, sessionSecret });
|
|
228
|
+
const emailChangeDelivery = resolveEmailChangeDelivery({ emailChangeSender, isStagingOrProduction });
|
|
229
229
|
const appPublicUrl = resolveAppPublicUrl(env, {
|
|
230
230
|
emailRecovery,
|
|
231
231
|
emailedConfirmation: confirmationDelivery === "email",
|
|
232
232
|
emailedChange: emailChangeDelivery === "email",
|
|
233
233
|
oauthEnabled,
|
|
234
|
-
|
|
234
|
+
stagingOrProductionSecondFactor: isStagingOrProduction && Boolean(secondFactorKey)
|
|
235
235
|
});
|
|
236
236
|
return Object.freeze({
|
|
237
237
|
storeDir,
|
|
238
|
-
|
|
238
|
+
isStagingOrProduction,
|
|
239
239
|
// The SameSite of every local auth cookie: AUTH_COOKIE_SAME_SITE, "lax" unless set to "strict".
|
|
240
240
|
cookieSameSite: resolveAuthCookieSameSite(env),
|
|
241
241
|
appPublicUrl,
|
|
@@ -244,7 +244,7 @@ function resolveLocalAuthConfig({ appRoot, env, fileBackend, emailRecovery, emai
|
|
|
244
244
|
sessionSecret,
|
|
245
245
|
// "" when the server has no second factor.
|
|
246
246
|
secondFactorKey,
|
|
247
|
-
setupCode: resolveSetupCode(env, {
|
|
247
|
+
setupCode: resolveSetupCode(env, { isStagingOrProduction }),
|
|
248
248
|
recoveryDelivery,
|
|
249
249
|
recoveryEnabled: recoveryDelivery !== "disabled",
|
|
250
250
|
selfRegistration,
|
|
@@ -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) {
|
|
@@ -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 });
|
|
@@ -56,20 +56,20 @@ function unauthenticatedAuthResult(clearSession = false) {
|
|
|
56
56
|
/**
|
|
57
57
|
* A local auth cookie: httpOnly at Path=/, Secure in staging and production, with the application's SameSite
|
|
58
58
|
* (AUTH_COOKIE_SAME_SITE).
|
|
59
|
-
* @param {Pick<import("../types.js").AuthLocalConfig, "
|
|
59
|
+
* @param {Pick<import("../types.js").AuthLocalConfig, "isStagingOrProduction" | "cookieSameSite">} config
|
|
60
60
|
* @param {number} maxAge
|
|
61
61
|
*/
|
|
62
|
-
function cookieOptions({
|
|
62
|
+
function cookieOptions({ isStagingOrProduction, cookieSameSite }, maxAge) {
|
|
63
63
|
return {
|
|
64
64
|
path: "/",
|
|
65
65
|
httpOnly: true,
|
|
66
66
|
sameSite: cookieSameSite,
|
|
67
|
-
secure:
|
|
67
|
+
secure: isStagingOrProduction,
|
|
68
68
|
maxAge
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
/** @param {Pick<import("../types.js").AuthLocalConfig, "
|
|
72
|
+
/** @param {Pick<import("../types.js").AuthLocalConfig, "isStagingOrProduction" | "cookieSameSite">} config */
|
|
73
73
|
function clearCookieOptions(config) {
|
|
74
74
|
return [cookieOptions(config, 0), { ...cookieOptions(config, 0), path: "/api" }];
|
|
75
75
|
}
|
|
@@ -127,11 +127,11 @@ function createLocalSessions({ appProfileProjection, backend, config, deliveries
|
|
|
127
127
|
if (!signInEvents) {
|
|
128
128
|
throw new TypeError("createLocalSessions() takes signInEvents, the auth.local.sign-in-events function each sign-in event is given.");
|
|
129
129
|
}
|
|
130
|
-
const { devAuth,
|
|
130
|
+
const { devAuth, isStagingOrProduction } = config;
|
|
131
131
|
// Staging and production cookies are Secure, so they are also host-only __Host- cookies.
|
|
132
|
-
const ACCESS_TOKEN_COOKIE = hostOnlyCookieName(ACCESS_TOKEN_COOKIE_NAME,
|
|
133
|
-
const REFRESH_TOKEN_COOKIE = hostOnlyCookieName(REFRESH_TOKEN_COOKIE_NAME,
|
|
134
|
-
const RECOVERY_TOKEN_COOKIE = hostOnlyCookieName(RECOVERY_TOKEN_COOKIE_NAME,
|
|
132
|
+
const ACCESS_TOKEN_COOKIE = hostOnlyCookieName(ACCESS_TOKEN_COOKIE_NAME, isStagingOrProduction);
|
|
133
|
+
const REFRESH_TOKEN_COOKIE = hostOnlyCookieName(REFRESH_TOKEN_COOKIE_NAME, isStagingOrProduction);
|
|
134
|
+
const RECOVERY_TOKEN_COOKIE = hostOnlyCookieName(RECOVERY_TOKEN_COOKIE_NAME, isStagingOrProduction);
|
|
135
135
|
|
|
136
136
|
/**
|
|
137
137
|
* A new session of the user, seen now from where the request that signs it in comes from.
|
|
@@ -373,7 +373,7 @@ function createLocalSessions({ appProfileProjection, backend, config, deliveries
|
|
|
373
373
|
}
|
|
374
374
|
for (const options of clearCookieOptions(config)) {
|
|
375
375
|
// A __Host- cookie only exists at Path=/.
|
|
376
|
-
if (options.path === "/" || !
|
|
376
|
+
if (options.path === "/" || !isStagingOrProduction) {
|
|
377
377
|
reply.clearCookie(ACCESS_TOKEN_COOKIE, options);
|
|
378
378
|
reply.clearCookie(REFRESH_TOKEN_COOKIE, options);
|
|
379
379
|
reply.clearCookie(RECOVERY_TOKEN_COOKIE, options);
|
|
@@ -92,3 +92,28 @@ test("an AUTH_COOKIE_SAME_SITE other than lax or strict stops local auth's start
|
|
|
92
92
|
});
|
|
93
93
|
}
|
|
94
94
|
});
|
|
95
|
+
|
|
96
|
+
test("in staging and production the OAuth attempt's cookie is the host-only __Host- one, Secure, set and cleared at Path=/",
|
|
97
|
+
async (t) => {
|
|
98
|
+
for (const APP_ENV of ["staging", "production"]) {
|
|
99
|
+
const storeDir = await fs.mkdtemp(path.join(os.tmpdir(), "jskit-auth-local-oauth-attempt-"));
|
|
100
|
+
t.after(() => fs.rm(storeDir, { recursive: true, force: true }));
|
|
101
|
+
const service = createService({
|
|
102
|
+
backend: createLocalFileBackend({ storeDir }),
|
|
103
|
+
oauthProviders: [],
|
|
104
|
+
profileProjector: noAppProfiles,
|
|
105
|
+
env: { APP_ENV, NODE_ENV: "production", AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "" }
|
|
106
|
+
});
|
|
107
|
+
const written = [];
|
|
108
|
+
const reply = {
|
|
109
|
+
setCookie: (name, _value, options) => written.push([name, "set", options.path, options.secure]),
|
|
110
|
+
clearCookie: (name, options) => written.push([name, "cleared", options.path, options.secure])
|
|
111
|
+
};
|
|
112
|
+
service.writeOAuthStartCookies(reply, { attempt: "browser-key" });
|
|
113
|
+
service.clearOAuthStartCookies(reply);
|
|
114
|
+
assert.deepEqual(written, [
|
|
115
|
+
["__Host-jskit_local_oauth_attempt", "set", "/", true],
|
|
116
|
+
["__Host-jskit_local_oauth_attempt", "cleared", "/", true]
|
|
117
|
+
], APP_ENV);
|
|
118
|
+
}
|
|
119
|
+
});
|
package/test/oauthSignIn.test.js
CHANGED
|
@@ -154,7 +154,7 @@ test("while a production server waits for its first account, a Google sign-in ca
|
|
|
154
154
|
const codes = [];
|
|
155
155
|
const firstAccountSetup = createFirstAccountSetup({
|
|
156
156
|
backend,
|
|
157
|
-
config: {
|
|
157
|
+
config: { isStagingOrProduction: true, storeDir: backend.storeDir },
|
|
158
158
|
logger: { warn: (fields) => codes.push(fields.firstAccountSetupCode) }
|
|
159
159
|
});
|
|
160
160
|
await firstAccountSetup.start();
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
createLocalFileBackend,
|
|
19
19
|
hashPassword,
|
|
20
20
|
normalizePasswordStrategy,
|
|
21
|
+
resolveLocalAuthConfig,
|
|
21
22
|
verifyPassword
|
|
22
23
|
} from "../src/server/lib/index.js";
|
|
23
24
|
|
|
@@ -28,7 +29,7 @@ const PRODUCTION = Object.freeze({
|
|
|
28
29
|
APP_ENV: "production",
|
|
29
30
|
NODE_ENV: "production",
|
|
30
31
|
AUTH_LOCAL_SESSION_SECRET: "fixture-session-secret",
|
|
31
|
-
|
|
32
|
+
AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: "true",
|
|
32
33
|
AUTH_LOCAL_RECOVERY_DEV_OUTPUT: ""
|
|
33
34
|
});
|
|
34
35
|
const STAGING = Object.freeze({ ...PRODUCTION, APP_ENV: "staging" });
|
|
@@ -447,7 +448,7 @@ test("local auth provider rejects dev login-as enablement in staging and product
|
|
|
447
448
|
() => createStartedApp({
|
|
448
449
|
env: {
|
|
449
450
|
...devAuthEnv(),
|
|
450
|
-
|
|
451
|
+
AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: "true",
|
|
451
452
|
AUTH_LOCAL_SESSION_SECRET: "production-session-secret",
|
|
452
453
|
AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "",
|
|
453
454
|
APP_ENV, NODE_ENV: "production"
|
|
@@ -465,7 +466,7 @@ test("a production server with AUTH_LOCAL_RECOVERY_DEV_OUTPUT set refuses to sta
|
|
|
465
466
|
await assert.rejects(
|
|
466
467
|
() => createStartedApp({
|
|
467
468
|
env: {
|
|
468
|
-
|
|
469
|
+
AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: "true",
|
|
469
470
|
AUTH_LOCAL_SESSION_SECRET: "production-session-secret",
|
|
470
471
|
AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "log",
|
|
471
472
|
APP_ENV: "production", NODE_ENV: "production"
|
|
@@ -477,6 +478,39 @@ test("a production server with AUTH_LOCAL_RECOVERY_DEV_OUTPUT set refuses to sta
|
|
|
477
478
|
);
|
|
478
479
|
});
|
|
479
480
|
|
|
481
|
+
test("staging and production need AUTH_LOCAL_SESSION_SECRET, and AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK on the file " +
|
|
482
|
+
"backend, and have no password recovery without a sender", async () => {
|
|
483
|
+
const appRoot = await temporaryStoreDir("jskit-auth-local-config-");
|
|
484
|
+
const resolve = (env, { fileBackend = false, emailRecovery = false } = {}) => resolveLocalAuthConfig({
|
|
485
|
+
appRoot,
|
|
486
|
+
env: { AUTH_LOCAL_EMAIL_CONFIRMATION: "false", APP_PUBLIC_URL: "https://app.example.test", ...env },
|
|
487
|
+
fileBackend,
|
|
488
|
+
emailRecovery
|
|
489
|
+
});
|
|
490
|
+
for (const env of [STAGING, PRODUCTION]) {
|
|
491
|
+
const { AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: _acknowledged, ...unacknowledged } = env;
|
|
492
|
+
assert.throws(() => resolve(unacknowledged, { fileBackend: true }), {
|
|
493
|
+
message: "AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK is required to use the local file auth backend in staging and production."
|
|
494
|
+
}, env.APP_ENV);
|
|
495
|
+
assert.throws(() => resolve({ ...env, AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: "false" }, { fileBackend: true }),
|
|
496
|
+
{ message: /^AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK is required/u }, env.APP_ENV);
|
|
497
|
+
assert.equal(resolve(env, { fileBackend: true }).isStagingOrProduction, true, env.APP_ENV);
|
|
498
|
+
assert.equal(resolve(unacknowledged).isStagingOrProduction, true, `${env.APP_ENV}: an installed backend needs no acknowledgement`);
|
|
499
|
+
assert.throws(() => resolve({ ...env, AUTH_LOCAL_SESSION_SECRET: "" }), {
|
|
500
|
+
message: "AUTH_LOCAL_SESSION_SECRET is required for local auth in staging and production."
|
|
501
|
+
}, env.APP_ENV);
|
|
502
|
+
assert.equal(resolve(env).recoveryEnabled, false, `${env.APP_ENV} without a recovery sender`);
|
|
503
|
+
assert.equal(resolve(env, { emailRecovery: true }).recoveryEnabled, true, `${env.APP_ENV} with one`);
|
|
504
|
+
}
|
|
505
|
+
// Development and test need neither: they keep a generated session secret and recover through the log.
|
|
506
|
+
for (const APP_ENV of ["development", "test"]) {
|
|
507
|
+
const config = resolve({ APP_ENV }, { fileBackend: true });
|
|
508
|
+
assert.equal(config.isStagingOrProduction, false, APP_ENV);
|
|
509
|
+
assert.match(config.sessionSecret, /^[\w-]{43}$/u, APP_ENV);
|
|
510
|
+
assert.equal(config.recoveryEnabled, true, APP_ENV);
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
|
|
480
514
|
test("local auth provider applies auth service decorators for blocking and non-blocking hooks", async () => {
|
|
481
515
|
const calls = [];
|
|
482
516
|
const errors = [];
|
|
@@ -1406,18 +1440,18 @@ test("an installed auth.local.sign-in-events is given each sign-in event, and wi
|
|
|
1406
1440
|
[["Local auth sign-in: signedIn.", "registration"]]);
|
|
1407
1441
|
});
|
|
1408
1442
|
|
|
1409
|
-
test("an installed auth.local.email-change-sender sends as APP_ENV says; without one, development and test log the links and
|
|
1443
|
+
test("an installed auth.local.email-change-sender sends as APP_ENV says; without one, development and test log the links and staging and production have none",
|
|
1410
1444
|
async (t) => {
|
|
1411
1445
|
const PASSWORD = "correct horse battery staple";
|
|
1412
1446
|
const account = async (app, id, email) => {
|
|
1413
1447
|
await app.backend.withTransaction(async (tx) => tx.users.create({ id, email, displayName: id, password: await hashPassword(PASSWORD) }));
|
|
1414
1448
|
return id;
|
|
1415
1449
|
};
|
|
1416
|
-
const
|
|
1450
|
+
const stagingOrProduction = { AUTH_LOCAL_EMAIL_CONFIRMATION: "", AUTH_LOCAL_SELF_REGISTRATION: "false" };
|
|
1417
1451
|
|
|
1418
1452
|
// Production sends each link to its address.
|
|
1419
1453
|
const sent = [];
|
|
1420
|
-
const production = await createStartedApp({ env: { ...PRODUCTION, ...
|
|
1454
|
+
const production = await createStartedApp({ env: { ...PRODUCTION, ...stagingOrProduction }, emailChangeSender: async (delivery) => { sent.push(delivery); } });
|
|
1421
1455
|
t.after(() => fs.rm(production.storeDir, { recursive: true, force: true }));
|
|
1422
1456
|
assert.equal(production.authService.getCapabilities().features.emailChange, true);
|
|
1423
1457
|
await production.authService.requestEmailChangeForUser({ userId: await account(production, "usr_ada", "ada@example.com"), email: "ada.new@example.com" });
|
|
@@ -1450,7 +1484,7 @@ test("an installed auth.local.email-change-sender sends as APP_ENV says; without
|
|
|
1450
1484
|
|
|
1451
1485
|
// Staging and production without one offer no email change, and a sender needs the public URL its links name.
|
|
1452
1486
|
for (const env of [STAGING, PRODUCTION]) {
|
|
1453
|
-
const without = await createStartedApp({ env: { ...env, ...
|
|
1487
|
+
const without = await createStartedApp({ env: { ...env, ...stagingOrProduction } });
|
|
1454
1488
|
t.after(() => fs.rm(without.storeDir, { recursive: true, force: true }));
|
|
1455
1489
|
assert.equal(without.authService.getCapabilities().features.emailChange, false, env.APP_ENV);
|
|
1456
1490
|
for (const asked of [
|
|
@@ -1640,7 +1674,7 @@ test("in production, email confirmation needs an auth.local.confirmation-sender,
|
|
|
1640
1674
|
const production = {
|
|
1641
1675
|
APP_ENV: "production", NODE_ENV: "production",
|
|
1642
1676
|
AUTH_LOCAL_SESSION_SECRET: "fixture-session-secret",
|
|
1643
|
-
|
|
1677
|
+
AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: "true",
|
|
1644
1678
|
AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "",
|
|
1645
1679
|
AUTH_LOCAL_EMAIL_CONFIRMATION: ""
|
|
1646
1680
|
};
|
|
@@ -1664,7 +1698,7 @@ test("with AUTH_LOCAL_SELF_REGISTRATION=false, production starts without a confi
|
|
|
1664
1698
|
env: {
|
|
1665
1699
|
APP_ENV: "production", NODE_ENV: "production",
|
|
1666
1700
|
AUTH_LOCAL_SESSION_SECRET: "fixture-session-secret",
|
|
1667
|
-
|
|
1701
|
+
AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: "true",
|
|
1668
1702
|
AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "",
|
|
1669
1703
|
AUTH_LOCAL_EMAIL_CONFIRMATION: "",
|
|
1670
1704
|
AUTH_LOCAL_SELF_REGISTRATION: "false"
|
|
@@ -1730,7 +1764,7 @@ test("the confirmation sender emails the link, which is never returned or logged
|
|
|
1730
1764
|
env: {
|
|
1731
1765
|
APP_ENV: "production", NODE_ENV: "production",
|
|
1732
1766
|
AUTH_LOCAL_SESSION_SECRET: "fixture-session-secret",
|
|
1733
|
-
|
|
1767
|
+
AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: "true",
|
|
1734
1768
|
AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "",
|
|
1735
1769
|
AUTH_LOCAL_EMAIL_CONFIRMATION: "",
|
|
1736
1770
|
APP_PUBLIC_URL: "https://app.example.test"
|
|
@@ -1944,7 +1978,7 @@ test("only the exact answer \"without-link\" confirms without a link, and a conf
|
|
|
1944
1978
|
env: {
|
|
1945
1979
|
APP_ENV: "production", NODE_ENV: "production",
|
|
1946
1980
|
AUTH_LOCAL_SESSION_SECRET: "fixture-session-secret",
|
|
1947
|
-
|
|
1981
|
+
AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK: "true",
|
|
1948
1982
|
AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "",
|
|
1949
1983
|
AUTH_LOCAL_EMAIL_CONFIRMATION: ""
|
|
1950
1984
|
},
|