@softure-ai/auth 0.1.9 → 0.1.11
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 +14 -0
- package/README.md +12 -7
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/proxy/index.d.ts +5 -4
- package/dist/proxy/index.d.ts.map +1 -1
- package/dist/proxy/index.js +13 -26
- package/dist/proxy/index.js.map +1 -1
- package/dist/server/password.d.ts +7 -1
- package/dist/server/password.d.ts.map +1 -1
- package/dist/server/password.js +20 -2
- package/dist/server/password.js.map +1 -1
- package/module.json +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -1
- package/src/proxy/index.ts +8 -31
- package/src/server/password.ts +25 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,20 @@ Newest first. Each version lists what changed for an app that uses `@softure-ai/
|
|
|
4
4
|
production, the version gets a line `verified in: <app>@<commit>` ([docs/05](../../docs/05-adoption-playbook.md),
|
|
5
5
|
"Definition of done"). Versions before the first one below are described in their GitHub Releases (`auth@x.y.z`).
|
|
6
6
|
|
|
7
|
+
## 0.1.11
|
|
8
|
+
|
|
9
|
+
- The guard's login redirect uses core's `resolveAppOrigin` (#311): an origin listed in the config's
|
|
10
|
+
`origins.trustedOrigins` is trusted like one in the guard's `trustedOrigins` option, and `origins.trustRequestHost`
|
|
11
|
+
is honoured. A non-http(s) `X-Forwarded-Proto` value now falls back to the URL's scheme instead of making the
|
|
12
|
+
origin unreadable. Needs `@softure-ai/core` 0.1.8.
|
|
13
|
+
|
|
14
|
+
## 0.1.10
|
|
15
|
+
|
|
16
|
+
- `isPasswordHash(value)` is exact: the salt and key must be unpadded base64url of exactly 16 and 64 bytes (22 and 86
|
|
17
|
+
characters) and the cost a power of two above 1. A hash cut by a character or carrying a stray one no longer
|
|
18
|
+
passes, so `set-temporary-password --password-hash` refuses it as a usage error instead of storing a hash that
|
|
19
|
+
locks the account. Reading stored hashes at login is unchanged.
|
|
20
|
+
|
|
7
21
|
## 0.1.9
|
|
8
22
|
|
|
9
23
|
- `set-temporary-password` takes `--password-hash=<hash>` (or `--password-hash-file=-` from stdin): the script stores a
|
package/README.md
CHANGED
|
@@ -226,16 +226,19 @@ A protected prefix matches whole path segments (`/account` covers `/account/pass
|
|
|
226
226
|
because behind a reverse proxy the request URL carries the server's internal host.
|
|
227
227
|
|
|
228
228
|
An app served on several hosts from one build (the product on `app.example.com`, pages on
|
|
229
|
-
`example.com`) lists the others in `trustedOrigins
|
|
230
|
-
host they used
|
|
229
|
+
`example.com`) lists the others in the config's `origins.trustedOrigins` (read by every module), so a
|
|
230
|
+
visitor is sent to the login page on the host they used. The guard's own `trustedOrigins` option adds
|
|
231
|
+
origins for the guard alone:
|
|
231
232
|
|
|
232
233
|
```ts
|
|
233
|
-
|
|
234
|
+
defineSoftureConfig({ appOrigin: "https://app.example.com", origins: { trustedOrigins: ["https://example.com"] }, ... });
|
|
235
|
+
const guard = createAuthGuard(softureConfig, { protect: ["/account"] });
|
|
234
236
|
```
|
|
235
237
|
|
|
236
|
-
The guard
|
|
237
|
-
|
|
238
|
-
|
|
238
|
+
The guard builds the redirect on core's `resolveAppOrigin` (core README, "Request origins"): the
|
|
239
|
+
request's public origin from `X-Forwarded-Proto` and `X-Forwarded-Host` (first values), else from
|
|
240
|
+
`Host` and the URL's scheme, used only when it equals a listed origin (scheme, host and port); any
|
|
241
|
+
other request goes to `appOrigin` (or, with `origins.trustRequestHost`, to the `Host` origin). Headers alone can therefore never
|
|
239
242
|
send a visitor to a host the app did not list. An entry that is not an `http(s)` origin throws when
|
|
240
243
|
the guard is created. A session cookie shared by the hosts needs `cookie.domain`.
|
|
241
244
|
|
|
@@ -347,7 +350,9 @@ node temporary-password-hash.mjs | ssh app-host docker exec -i app node set-temp
|
|
|
347
350
|
shell history). The script refuses a value that is not a `hashPassword` hash without printing it,
|
|
348
351
|
stores the hash, ends the sessions and the reset link as before, and reports `passwordFrom: "hash"`
|
|
349
352
|
instead of a password. A hash made with other scrypt parameters than the config's is rehashed at
|
|
350
|
-
the first login. `isPasswordHash(value)` checks the format locally
|
|
353
|
+
the first login. `isPasswordHash(value)` checks the format locally, exactly: a hash cut or padded in transport,
|
|
354
|
+
a character outside base64url or a cost scrypt refuses is not a hash `hashPassword` writes, so the script refuses it
|
|
355
|
+
instead of storing a hash no password matches.
|
|
351
356
|
|
|
352
357
|
**Password reset.** Pass a sender, and the login form links to the request page. With
|
|
353
358
|
`@softure-ai/mailing` enabled, `mailingResetSender()` is that sender:
|
package/dist/index.js
CHANGED
|
@@ -36,7 +36,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
|
|
|
36
36
|
export const auth = defineModule({
|
|
37
37
|
manifest: {
|
|
38
38
|
id: MODULE_ID,
|
|
39
|
-
version: "0.1.
|
|
39
|
+
version: "0.1.11",
|
|
40
40
|
dependsOn: { security: "^0.1.0", mailing: "^0.1.0?", ops: "^0.1.0?" },
|
|
41
41
|
dbSchema: "auth",
|
|
42
42
|
tables: ["users", "sessions", "user_roles", "password_resets"],
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,qIAAqI;AACrI,4EAA4E;AAC5E,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAE7D,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC;AAEhC,2DAA2D;AAC3D,MAAM,CAAC,MAAM,0BAA0B,GAAG,0BAA0B,CAAC;AAErE;;;GAGG;AACH,MAA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CAAC;AAC7H,OAAO,EAAE,gBAAgB,EAAsB,MAAM,qBAAqB,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/proxy/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type SoftureConfig } from "@softure-ai/core";
|
|
2
2
|
export interface AuthGuardOptions {
|
|
3
3
|
/** Path prefixes that need a session; each matches whole segments (`/account` → `/account/…`). */
|
|
4
4
|
readonly protect: readonly string[];
|
|
@@ -8,9 +8,10 @@ export interface AuthGuardOptions {
|
|
|
8
8
|
*/
|
|
9
9
|
readonly exclude?: readonly string[];
|
|
10
10
|
/**
|
|
11
|
-
* Origins besides `appOrigin` that the login redirect may stay on
|
|
12
|
-
*
|
|
13
|
-
* else `Host`
|
|
11
|
+
* Origins besides `appOrigin` and the config's `origins.trustedOrigins` that the login redirect may stay on
|
|
12
|
+
* (`https://example.com`). Prefer the config's block, which every module reads. The request's public origin
|
|
13
|
+
* (core `resolveAppOrigin`: `X-Forwarded-Host`, else `Host`) is used only when it is listed; any other goes to
|
|
14
|
+
* `appOrigin`.
|
|
14
15
|
*/
|
|
15
16
|
readonly trustedOrigins?: readonly string[];
|
|
16
17
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAiC,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAIrF,MAAM,WAAW,gBAAgB;IAC/B,kGAAkG;IAClG,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC;;;;;OAKG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC7C;AAKD,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,QAAQ,GAAG,IAAI,CAAC;AAE9D,6GAA6G;AAC7G,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,gBAAgB,GAAG,SAAS,CAgC3F"}
|
package/dist/proxy/index.js
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
// The route guard for the app's `proxy.ts` (roadmap ID-3, unknown 1). It uses only Web `Request`
|
|
2
|
+
// and `Response`, imports nothing from Next and no React, so it chains with other proxy pieces:
|
|
3
|
+
// `return channels.carry(request, guard(request)) ?? channels.tag(request) ?? NextResponse.next()`
|
|
4
|
+
// (the channel piece of @softure-ai/analytics/proxy).
|
|
5
|
+
//
|
|
6
|
+
// It checks that the session cookie is present, nothing more: an optimistic check that keeps
|
|
7
|
+
// anonymous visitors off private pages without a database round trip in the proxy. Pages and
|
|
8
|
+
// actions still call `requireUser`, which reads the session row. An app that is private by default
|
|
9
|
+
// protects "/" and lists its public paths in `exclude`; auth's own public pages are never guarded.
|
|
10
|
+
import { parseOrigin, resolveAppOrigin } from "@softure-ai/core";
|
|
1
11
|
import { getAuthOptions, getAuthRoutes } from "../server/options.js";
|
|
2
12
|
import { getSessionCookie, readSessionToken } from "../session-cookie.js";
|
|
3
13
|
/** Mounted at a fixed path by the module (manifest `mount`); it answers `{ user: null }` without a session. */
|
|
@@ -11,7 +21,7 @@ export function createAuthGuard(config, options) {
|
|
|
11
21
|
// Auth's own public pages: guarding them would send a visitor from the login page to itself.
|
|
12
22
|
const publicPaths = [routes.login, routes.register, routes.forgotPassword, routes.resetPassword, SESSION_ROUTE].map((path) => normalizePrefix(path, "excluded"));
|
|
13
23
|
const excluded = (options.exclude ?? []).map((prefix) => normalizePrefix(prefix, "excluded"));
|
|
14
|
-
const trustedOrigins =
|
|
24
|
+
const trustedOrigins = (options.trustedOrigins ?? []).map(normalizeOrigin);
|
|
15
25
|
const isGuarded = (path) => {
|
|
16
26
|
if (isUnder(path, changePassword))
|
|
17
27
|
return true;
|
|
@@ -31,8 +41,7 @@ export function createAuthGuard(config, options) {
|
|
|
31
41
|
return null;
|
|
32
42
|
// Built on appOrigin, or on the request's public origin when the app trusts it: behind a proxy
|
|
33
43
|
// the request URL carries an internal host, and request headers alone are never trusted.
|
|
34
|
-
const
|
|
35
|
-
const login = new URL(routes.login, origin !== null && trustedOrigins.has(origin) ? origin : config.appOrigin);
|
|
44
|
+
const login = new URL(routes.login, resolveAppOrigin(config, request, { trustedOrigins }));
|
|
36
45
|
login.searchParams.set("next", `${url.pathname}${url.search}`);
|
|
37
46
|
return Response.redirect(login, 307);
|
|
38
47
|
};
|
|
@@ -46,34 +55,12 @@ function normalizePrefix(prefix, kind) {
|
|
|
46
55
|
}
|
|
47
56
|
/** `entry` as an origin (`https://example.com`, a trailing `/` dropped); anything else is a config error. */
|
|
48
57
|
function normalizeOrigin(entry) {
|
|
49
|
-
const parsed = parseOrigin(entry
|
|
58
|
+
const parsed = parseOrigin(entry);
|
|
50
59
|
if (parsed === null) {
|
|
51
60
|
throw new Error(`createAuthGuard: trusted origin "${entry}" must be an http(s) origin such as https://example.com`);
|
|
52
61
|
}
|
|
53
62
|
return parsed;
|
|
54
63
|
}
|
|
55
|
-
/** The origin the browser used: the front proxy's forwarded scheme and host, else `Host` and the URL's scheme. */
|
|
56
|
-
function findPublicOrigin(request, url) {
|
|
57
|
-
const host = firstValue(request.headers.get("x-forwarded-host")) ?? firstValue(request.headers.get("host")) ?? url.host;
|
|
58
|
-
const scheme = firstValue(request.headers.get("x-forwarded-proto")) ?? url.protocol.slice(0, -1);
|
|
59
|
-
return parseOrigin(`${scheme}://${host}`);
|
|
60
|
-
}
|
|
61
|
-
function firstValue(header) {
|
|
62
|
-
const value = header?.split(",")[0]?.trim();
|
|
63
|
-
return value === undefined || value === "" ? null : value;
|
|
64
|
-
}
|
|
65
|
-
/** The normalized origin of `candidate` when it is exactly `http(s)://host[:port]`, else null. */
|
|
66
|
-
function parseOrigin(candidate) {
|
|
67
|
-
// Checked on the raw text, so a path, query, fragment or credentials make it no origin.
|
|
68
|
-
if (!/^https?:\/\/[^/\\?#@\s]+$/i.test(candidate))
|
|
69
|
-
return null;
|
|
70
|
-
try {
|
|
71
|
-
return new URL(candidate).origin;
|
|
72
|
-
}
|
|
73
|
-
catch {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
64
|
function decodePath(pathname) {
|
|
78
65
|
try {
|
|
79
66
|
return decodeURIComponent(pathname).toLowerCase();
|
package/dist/proxy/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,gGAAgG;AAChG,mGAAmG;AACnG,sDAAsD;AACtD,EAAE;AACF,6FAA6F;AAC7F,6FAA6F;AAC7F,mGAAmG;AACnG,mGAAmG;AACnG,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAsB,MAAM,kBAAkB,CAAC;AACrF,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAmB1E,+GAA+G;AAC/G,MAAM,aAAa,GAAG,mBAAmB,CAAC;AAI1C,6GAA6G;AAC7G,MAAM,UAAU,eAAe,CAAC,MAAqB,EAAE,OAAyB;IAC9E,MAAM,MAAM,GAA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oBAAoB,IAAI,UAAU,MAAM,qBAAqB,CAAC,CAAC;IACjF,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IACzF,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED,6GAA6G;AAC7G,SAAS,eAAe,CAAC,KAAa;IACpC,MAAM,MAAM,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,oCAAoC,KAAK,yDAAyD,CAAC,CAAC;IACtH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,UAAU,CAAC,QAAgB;IAClC,IAAI,CAAC;QACH,OAAO,kBAAkB,CAAC,QAAQ,CAAC,CAAC,WAAW,EAAE,CAAC;IACpD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,6FAA6F;AAC7F,SAAS,YAAY,CAAC,QAAgB,EAAE,MAAc;IACpD,OAAO,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,QAAQ,KAAK,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AACvE,CAAC;AAED,SAAS,OAAO,CAAC,QAAgB,EAAE,MAAc;IAC/C,OAAO,MAAM,KAAK,GAAG,IAAI,QAAQ,KAAK,MAAM,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC;AACpF,CAAC"}
|
|
@@ -22,6 +22,12 @@ export declare function needsRehash(storedHash: string, params: ScryptParams): b
|
|
|
22
22
|
* The dummy hash is made once per parameter set.
|
|
23
23
|
*/
|
|
24
24
|
export declare function verifyDummyPassword(password: string, params: ScryptParams): Promise<void>;
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Whether `value` is a hash `hashPassword` could have written: `scrypt$N$r$p$salt$key` with a cost scrypt accepts
|
|
27
|
+
* (a power of two above 1) and an unpadded base64url salt and key of exactly `SALT_BYTES` and `KEY_BYTES`. A hash
|
|
28
|
+
* cut or padded in transport fails here instead of being stored and failing every login. It checks the format, not
|
|
29
|
+
* integrity: a character swapped for another base64url character still passes, and so does a last character that
|
|
30
|
+
* differs only in unused bits (it decodes to the same bytes, so the hash still verifies).
|
|
31
|
+
*/
|
|
26
32
|
export declare function isPasswordHash(value: string): boolean;
|
|
27
33
|
//# sourceMappingURL=password.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"password.d.ts","sourceRoot":"","sources":["../../src/server/password.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD,8FAA8F;AAC9F,eAAO,MAAM,mBAAmB,OAAO,CAAC;AAQxC,uCAAuC;AACvC,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAI1F;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAMhG;AAED,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,QAAQ,GAAG,UAAU,CAAC;AAE5D,gFAAgF;AAChF,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAE3F;AAMD,yEAAyE;AACzE,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAG7E;AAID;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAS/F;
|
|
1
|
+
{"version":3,"file":"password.d.ts","sourceRoot":"","sources":["../../src/server/password.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD,8FAA8F;AAC9F,eAAO,MAAM,mBAAmB,OAAO,CAAC;AAQxC,uCAAuC;AACvC,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAI1F;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAMhG;AAED,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,QAAQ,GAAG,UAAU,CAAC;AAE5D,gFAAgF;AAChF,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAE3F;AAMD,yEAAyE;AACzE,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAG7E;AAID;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAS/F;AAqBD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAQrD"}
|
package/dist/server/password.js
CHANGED
|
@@ -74,9 +74,27 @@ function deriveKey(password, salt, params) {
|
|
|
74
74
|
});
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
|
-
|
|
77
|
+
const BASE64URL = /^[A-Za-z0-9_-]+$/;
|
|
78
|
+
/**
|
|
79
|
+
* Whether `value` is a hash `hashPassword` could have written: `scrypt$N$r$p$salt$key` with a cost scrypt accepts
|
|
80
|
+
* (a power of two above 1) and an unpadded base64url salt and key of exactly `SALT_BYTES` and `KEY_BYTES`. A hash
|
|
81
|
+
* cut or padded in transport fails here instead of being stored and failing every login. It checks the format, not
|
|
82
|
+
* integrity: a character swapped for another base64url character still passes, and so does a last character that
|
|
83
|
+
* differs only in unused bits (it decodes to the same bytes, so the hash still verifies).
|
|
84
|
+
*/
|
|
78
85
|
export function isPasswordHash(value) {
|
|
79
|
-
|
|
86
|
+
const parts = readHashParts(value);
|
|
87
|
+
return (parts !== null &&
|
|
88
|
+
isScryptCost(parts.params.cost) &&
|
|
89
|
+
isBase64urlOf(parts.salt, SALT_BYTES) &&
|
|
90
|
+
isBase64urlOf(parts.key, KEY_BYTES));
|
|
91
|
+
}
|
|
92
|
+
function isScryptCost(cost) {
|
|
93
|
+
// Not a bitwise test: a safe integer can exceed the 32 bits `&` works on.
|
|
94
|
+
return cost > 1 && Number.isInteger(Math.log2(cost));
|
|
95
|
+
}
|
|
96
|
+
function isBase64urlOf(value, bytes) {
|
|
97
|
+
return value.length === Math.ceil((bytes * 4) / 3) && BASE64URL.test(value);
|
|
80
98
|
}
|
|
81
99
|
function readHashParts(value) {
|
|
82
100
|
const [scheme, cost, blockSize, parallelization, salt, key, ...rest] = value.split("$");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"password.js","sourceRoot":"","sources":["../../src/server/password.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,mGAAmG;AACnG,iFAAiF;AACjF,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,eAAe,EAAsB,MAAM,aAAa,CAAC;AAGvF,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,8FAA8F;AAC9F,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAQxC,uCAAuC;AACvC,MAAM,CAAC,KAAK,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ai/auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "SOFTURE AI auth module: users, scrypt passwords, database sessions, register with consent, login, logout, password change, roles, a proxy route guard, pages and forms for Next.js, and a password reset sender over @softure-ai/mailing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"build": "tsc -p tsconfig.build.json"
|
|
73
73
|
},
|
|
74
74
|
"dependencies": {
|
|
75
|
-
"@softure-ai/core": "^0.1.
|
|
75
|
+
"@softure-ai/core": "^0.1.8",
|
|
76
76
|
"@softure-ai/db": "^0.1.0",
|
|
77
77
|
"@softure-ai/security": "^0.1.0",
|
|
78
78
|
"zod": "^4.6.5"
|
package/src/index.ts
CHANGED
|
@@ -41,7 +41,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
|
|
|
41
41
|
export const auth = defineModule({
|
|
42
42
|
manifest: {
|
|
43
43
|
id: MODULE_ID,
|
|
44
|
-
version: "0.1.
|
|
44
|
+
version: "0.1.11",
|
|
45
45
|
dependsOn: { security: "^0.1.0", mailing: "^0.1.0?", ops: "^0.1.0?" },
|
|
46
46
|
dbSchema: "auth",
|
|
47
47
|
tables: ["users", "sessions", "user_roles", "password_resets"],
|
package/src/proxy/index.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// anonymous visitors off private pages without a database round trip in the proxy. Pages and
|
|
8
8
|
// actions still call `requireUser`, which reads the session row. An app that is private by default
|
|
9
9
|
// protects "/" and lists its public paths in `exclude`; auth's own public pages are never guarded.
|
|
10
|
-
import type
|
|
10
|
+
import { parseOrigin, resolveAppOrigin, type SoftureConfig } from "@softure-ai/core";
|
|
11
11
|
import { getAuthOptions, getAuthRoutes } from "../server/options.js";
|
|
12
12
|
import { getSessionCookie, readSessionToken } from "../session-cookie.js";
|
|
13
13
|
|
|
@@ -20,9 +20,10 @@ export interface AuthGuardOptions {
|
|
|
20
20
|
*/
|
|
21
21
|
readonly exclude?: readonly string[];
|
|
22
22
|
/**
|
|
23
|
-
* Origins besides `appOrigin` that the login redirect may stay on
|
|
24
|
-
*
|
|
25
|
-
* else `Host`
|
|
23
|
+
* Origins besides `appOrigin` and the config's `origins.trustedOrigins` that the login redirect may stay on
|
|
24
|
+
* (`https://example.com`). Prefer the config's block, which every module reads. The request's public origin
|
|
25
|
+
* (core `resolveAppOrigin`: `X-Forwarded-Host`, else `Host`) is used only when it is listed; any other goes to
|
|
26
|
+
* `appOrigin`.
|
|
26
27
|
*/
|
|
27
28
|
readonly trustedOrigins?: readonly string[];
|
|
28
29
|
}
|
|
@@ -43,7 +44,7 @@ export function createAuthGuard(config: SoftureConfig, options: AuthGuardOptions
|
|
|
43
44
|
normalizePrefix(path, "excluded"),
|
|
44
45
|
);
|
|
45
46
|
const excluded = (options.exclude ?? []).map((prefix) => normalizePrefix(prefix, "excluded"));
|
|
46
|
-
const trustedOrigins =
|
|
47
|
+
const trustedOrigins = (options.trustedOrigins ?? []).map(normalizeOrigin);
|
|
47
48
|
|
|
48
49
|
const isGuarded = (path: string): boolean => {
|
|
49
50
|
if (isUnder(path, changePassword)) return true;
|
|
@@ -61,8 +62,7 @@ export function createAuthGuard(config: SoftureConfig, options: AuthGuardOptions
|
|
|
61
62
|
if (cookieNames.some((name) => readSessionToken(cookieHeader, name) !== null)) return null;
|
|
62
63
|
// Built on appOrigin, or on the request's public origin when the app trusts it: behind a proxy
|
|
63
64
|
// the request URL carries an internal host, and request headers alone are never trusted.
|
|
64
|
-
const
|
|
65
|
-
const login = new URL(routes.login, origin !== null && trustedOrigins.has(origin) ? origin : config.appOrigin);
|
|
65
|
+
const login = new URL(routes.login, resolveAppOrigin(config, request, { trustedOrigins }));
|
|
66
66
|
login.searchParams.set("next", `${url.pathname}${url.search}`);
|
|
67
67
|
return Response.redirect(login, 307);
|
|
68
68
|
};
|
|
@@ -78,36 +78,13 @@ function normalizePrefix(prefix: string, kind: "protected" | "excluded"): string
|
|
|
78
78
|
|
|
79
79
|
/** `entry` as an origin (`https://example.com`, a trailing `/` dropped); anything else is a config error. */
|
|
80
80
|
function normalizeOrigin(entry: string): string {
|
|
81
|
-
const parsed = parseOrigin(entry
|
|
81
|
+
const parsed = parseOrigin(entry);
|
|
82
82
|
if (parsed === null) {
|
|
83
83
|
throw new Error(`createAuthGuard: trusted origin "${entry}" must be an http(s) origin such as https://example.com`);
|
|
84
84
|
}
|
|
85
85
|
return parsed;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
/** The origin the browser used: the front proxy's forwarded scheme and host, else `Host` and the URL's scheme. */
|
|
89
|
-
function findPublicOrigin(request: Request, url: URL): string | null {
|
|
90
|
-
const host = firstValue(request.headers.get("x-forwarded-host")) ?? firstValue(request.headers.get("host")) ?? url.host;
|
|
91
|
-
const scheme = firstValue(request.headers.get("x-forwarded-proto")) ?? url.protocol.slice(0, -1);
|
|
92
|
-
return parseOrigin(`${scheme}://${host}`);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function firstValue(header: string | null): string | null {
|
|
96
|
-
const value = header?.split(",")[0]?.trim();
|
|
97
|
-
return value === undefined || value === "" ? null : value;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** The normalized origin of `candidate` when it is exactly `http(s)://host[:port]`, else null. */
|
|
101
|
-
function parseOrigin(candidate: string): string | null {
|
|
102
|
-
// Checked on the raw text, so a path, query, fragment or credentials make it no origin.
|
|
103
|
-
if (!/^https?:\/\/[^/\\?#@\s]+$/i.test(candidate)) return null;
|
|
104
|
-
try {
|
|
105
|
-
return new URL(candidate).origin;
|
|
106
|
-
} catch {
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
88
|
function decodePath(pathname: string): string | null {
|
|
112
89
|
try {
|
|
113
90
|
return decodeURIComponent(pathname).toLowerCase();
|
package/src/server/password.ts
CHANGED
|
@@ -89,9 +89,32 @@ function deriveKey(password: string, salt: Buffer, params: ScryptParams): Promis
|
|
|
89
89
|
});
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
const BASE64URL = /^[A-Za-z0-9_-]+$/;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether `value` is a hash `hashPassword` could have written: `scrypt$N$r$p$salt$key` with a cost scrypt accepts
|
|
96
|
+
* (a power of two above 1) and an unpadded base64url salt and key of exactly `SALT_BYTES` and `KEY_BYTES`. A hash
|
|
97
|
+
* cut or padded in transport fails here instead of being stored and failing every login. It checks the format, not
|
|
98
|
+
* integrity: a character swapped for another base64url character still passes, and so does a last character that
|
|
99
|
+
* differs only in unused bits (it decodes to the same bytes, so the hash still verifies).
|
|
100
|
+
*/
|
|
93
101
|
export function isPasswordHash(value: string): boolean {
|
|
94
|
-
|
|
102
|
+
const parts = readHashParts(value);
|
|
103
|
+
return (
|
|
104
|
+
parts !== null &&
|
|
105
|
+
isScryptCost(parts.params.cost) &&
|
|
106
|
+
isBase64urlOf(parts.salt, SALT_BYTES) &&
|
|
107
|
+
isBase64urlOf(parts.key, KEY_BYTES)
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isScryptCost(cost: number): boolean {
|
|
112
|
+
// Not a bitwise test: a safe integer can exceed the 32 bits `&` works on.
|
|
113
|
+
return cost > 1 && Number.isInteger(Math.log2(cost));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isBase64urlOf(value: string, bytes: number): boolean {
|
|
117
|
+
return value.length === Math.ceil((bytes * 4) / 3) && BASE64URL.test(value);
|
|
95
118
|
}
|
|
96
119
|
|
|
97
120
|
function readHashParts(value: string): { params: ScryptParams; salt: string; key: string } | null {
|