@bymax-one/nest-auth 1.0.3 → 1.0.5
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 +29 -0
- package/dist/server/index.cjs +67 -8
- package/dist/server/index.d.cts +53 -0
- package/dist/server/index.d.ts +53 -0
- package/dist/server/index.mjs +67 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.0.5] - 2026-05-25
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **Default `cookies.sameSite` lowered from `'strict'` to `'lax'`** ([`src/server/config/default-options.ts`](src/server/config/default-options.ts), [`src/server/services/token-delivery.service.ts`](src/server/services/token-delivery.service.ts)). The previous `'strict'` default broke the OAuth return-trip: Chromium does not include `SameSite=Strict` cookies on the very first request after a cross-site-initiated navigation (e.g. `accounts.google.com → /auth/oauth/google/callback → /dashboard`), so the freshly-issued auth cookies never reached the destination route. The `'lax'` default mirrors what Chromium applies to cookies that omit the attribute entirely and is the industry posture for browser auth cookies (Passport, Auth0, NextAuth all default to or expect Lax). The CSRF margin loss is negligible — POST endpoints are still cross-site safe under Lax, and the lib already short-circuits forgery vectors with origin checks at the controller boundary.
|
|
15
|
+
|
|
16
|
+
This is a **behavior change** for consumers that relied on the `'strict'` posture; they can restore it explicitly with `cookies.sameSite: 'strict'`. SemVer-wise this is a patch because the previous behavior broke a documented feature (OAuth callbacks).
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- **`cookies.sameSite?: 'lax' | 'strict' | 'none'` configuration option** ([`src/server/interfaces/auth-module-options.interface.ts`](src/server/interfaces/auth-module-options.interface.ts)). Lets consumers explicitly pick the SameSite posture for every cookie the module issues (access token, refresh token, session signal). Defaults to `'lax'` (see Changed above).
|
|
21
|
+
|
|
22
|
+
Validated at startup by [`src/server/config/resolved-options.ts`](src/server/config/resolved-options.ts): `cookies.sameSite: 'none'` requires `secureCookies: true` (browser-spec rule — `SameSite=None` cookies without `Secure` are silently dropped, which would surface as "auth doesn't work" instead of a configuration error).
|
|
23
|
+
|
|
24
|
+
Covered by 3 new unit tests in [`src/server/services/token-delivery.service.spec.ts`](src/server/services/token-delivery.service.spec.ts) (default propagates, explicit `'strict'` overrides, explicit `'none'` propagates) and 5 new validator tests in [`src/server/config/resolved-options.spec.ts`](src/server/config/resolved-options.spec.ts) (default to `'lax'`, explicit override, `'none' + secureCookies: true` accepted, `'none' + secureCookies: false` rejected, `'none'` accepted in production via `NODE_ENV`-driven default). One existing e2e cookie-header assertion in [`test/e2e/auth-flow.e2e-spec.ts`](test/e2e/auth-flow.e2e-spec.ts) updated to match the new default; the rest of the suite continues to pass unmodified.
|
|
25
|
+
|
|
26
|
+
## [1.0.4] - 2026-05-25
|
|
27
|
+
|
|
28
|
+
### Added
|
|
29
|
+
|
|
30
|
+
- **`oauth.successRedirectUrl?: string` configuration option** ([`src/server/interfaces/auth-module-options.interface.ts`](src/server/interfaces/auth-module-options.interface.ts), [`src/server/oauth/oauth.controller.ts`](src/server/oauth/oauth.controller.ts)). When set, `GET /auth/oauth/:provider/callback` issues a `302` redirect to the configured URL after delivering tokens, instead of returning the JSON body that API/SPA consumers expect. Cookies are still set on the same response — the destination page lands fully authenticated. This closes a UX gap that left browser users on the JSON payload after a successful OAuth round-trip (every consumer was forced to reimplement the OAuth controller just to get a redirect). The option is opt-in and the legacy JSON-body contract is preserved when it is omitted. Aligns the lib with `passport.successRedirect`, `next-auth.callbackUrl`, and `auth0.returnTo`.
|
|
31
|
+
|
|
32
|
+
Validated at startup by [`src/server/config/resolved-options.ts`](src/server/config/resolved-options.ts) with three rules:
|
|
33
|
+
1. Must be a non-empty string when set.
|
|
34
|
+
2. Must use `https://` or be a same-origin path (`/...`) in production — HTTP rejected so the post-callback leg cannot strip cookie `Secure` guarantees.
|
|
35
|
+
3. Requires `tokenDelivery: 'cookie'` or `'both'` — `bearer` is rejected because the 302 would discard the JSON body that carries the access token.
|
|
36
|
+
|
|
37
|
+
Covered by 3 new unit tests in [`src/server/oauth/oauth.controller.spec.ts`](src/server/oauth/oauth.controller.spec.ts), 7 new validator tests in [`src/server/config/resolved-options.spec.ts`](src/server/config/resolved-options.spec.ts), and 2 new e2e scenarios in [`test/e2e/oauth-flow.e2e-spec.ts`](test/e2e/oauth-flow.e2e-spec.ts) (cookie-attached 302 to the configured URL + Set-Cookie headers on the redirect response).
|
|
38
|
+
|
|
10
39
|
## [1.0.3] - 2026-05-25
|
|
11
40
|
|
|
12
41
|
### Fixed
|
package/dist/server/index.cjs
CHANGED
|
@@ -45,7 +45,13 @@ var DEFAULT_OPTIONS = {
|
|
|
45
45
|
accessTokenName: "access_token",
|
|
46
46
|
refreshTokenName: "refresh_token",
|
|
47
47
|
sessionSignalName: "has_session",
|
|
48
|
-
refreshCookiePath: "/auth"
|
|
48
|
+
refreshCookiePath: "/auth",
|
|
49
|
+
// 'lax' matches Chromium's default for cookies that omit SameSite. It
|
|
50
|
+
// delivers auth cookies on top-level cross-site GET navigations — the
|
|
51
|
+
// case that makes OAuth redirects (Google → callback → dashboard) work
|
|
52
|
+
// without dropping the freshly-issued session on the first hop. Consumers
|
|
53
|
+
// that want the stricter posture can override via `cookies.sameSite`.
|
|
54
|
+
sameSite: "lax"
|
|
49
55
|
},
|
|
50
56
|
mfa: {
|
|
51
57
|
recoveryCodeCount: 8,
|
|
@@ -142,7 +148,9 @@ function resolveOptions(userOptions) {
|
|
|
142
148
|
validatePasswordResetOtpLength(userOptions.passwordReset);
|
|
143
149
|
validatePasswordCostFactor(userOptions.password);
|
|
144
150
|
validateOAuthProviders(userOptions.oauth);
|
|
151
|
+
validateOAuthSuccessRedirectUrl(userOptions);
|
|
145
152
|
validateRefreshCookiePath(userOptions.routePrefix, userOptions.cookies);
|
|
153
|
+
validateSameSiteNoneRequiresSecure(userOptions);
|
|
146
154
|
validateRefreshGraceWindow(userOptions.jwt);
|
|
147
155
|
const { mfa: _mfa, ...userOptionsWithoutMfa } = userOptions;
|
|
148
156
|
const resolved = {
|
|
@@ -304,7 +312,9 @@ var REQUIRED_OAUTH_FIELDS = [
|
|
|
304
312
|
];
|
|
305
313
|
function validateOAuthProviders(oauth) {
|
|
306
314
|
if (!oauth) return;
|
|
307
|
-
for (const [
|
|
315
|
+
for (const [providerOrField, rawConfig] of Object.entries(oauth)) {
|
|
316
|
+
if (providerOrField === "successRedirectUrl") continue;
|
|
317
|
+
const provider = providerOrField;
|
|
308
318
|
const config = rawConfig;
|
|
309
319
|
for (const field of REQUIRED_OAUTH_FIELDS) {
|
|
310
320
|
if (!config[field]) {
|
|
@@ -318,6 +328,22 @@ function validateOAuthProviders(oauth) {
|
|
|
318
328
|
}
|
|
319
329
|
}
|
|
320
330
|
__name(validateOAuthProviders, "validateOAuthProviders");
|
|
331
|
+
function validateOAuthSuccessRedirectUrl(userOptions) {
|
|
332
|
+
const url = userOptions.oauth?.successRedirectUrl;
|
|
333
|
+
if (url === void 0) return;
|
|
334
|
+
if (typeof url !== "string" || url.length === 0) {
|
|
335
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must be a non-empty string when set.`);
|
|
336
|
+
}
|
|
337
|
+
const isProduction = process.env["NODE_ENV"] === "production";
|
|
338
|
+
const isSafe = url.startsWith("/") || url.startsWith("https://");
|
|
339
|
+
if (isProduction && !isSafe) {
|
|
340
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (got: '${url}').`);
|
|
341
|
+
}
|
|
342
|
+
if (userOptions.tokenDelivery === "bearer") {
|
|
343
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl is set but tokenDelivery is 'bearer'. A redirect discards the JSON response body, so the access token would never reach the client. Use tokenDelivery: 'cookie' or 'both' when configuring a successRedirectUrl.`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
__name(validateOAuthSuccessRedirectUrl, "validateOAuthSuccessRedirectUrl");
|
|
321
347
|
function validateRefreshCookiePath(routePrefix, cookies) {
|
|
322
348
|
const prefix = routePrefix ?? DEFAULT_OPTIONS.routePrefix;
|
|
323
349
|
if (prefix !== "auth" && !cookies?.refreshCookiePath) {
|
|
@@ -325,6 +351,15 @@ function validateRefreshCookiePath(routePrefix, cookies) {
|
|
|
325
351
|
}
|
|
326
352
|
}
|
|
327
353
|
__name(validateRefreshCookiePath, "validateRefreshCookiePath");
|
|
354
|
+
function validateSameSiteNoneRequiresSecure(userOptions) {
|
|
355
|
+
const sameSite = userOptions.cookies?.sameSite;
|
|
356
|
+
if (sameSite !== "none") return;
|
|
357
|
+
const secureCookies = userOptions.secureCookies ?? process.env["NODE_ENV"] === "production";
|
|
358
|
+
if (!secureCookies) {
|
|
359
|
+
throw new Error(`[BymaxAuthModule] cookies.sameSite is 'none' but secureCookies is false. Browsers reject SameSite=None cookies without the Secure attribute, so the auth cookies would never be stored. Set secureCookies: true (and serve over HTTPS) or use cookies.sameSite: 'lax' / 'strict'.`);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
__name(validateSameSiteNoneRequiresSecure, "validateSameSiteNoneRequiresSecure");
|
|
328
363
|
function validateRefreshGraceWindow(jwt) {
|
|
329
364
|
const graceSeconds = jwt.refreshGraceWindowSeconds ?? DEFAULT_OPTIONS.jwt.refreshGraceWindowSeconds;
|
|
330
365
|
const refreshExpiresInDays = jwt.refreshExpiresInDays ?? DEFAULT_OPTIONS.jwt.refreshExpiresInDays;
|
|
@@ -1439,9 +1474,16 @@ var TokenDeliveryService = class {
|
|
|
1439
1474
|
maxAge: refreshMaxAge
|
|
1440
1475
|
});
|
|
1441
1476
|
}
|
|
1477
|
+
/**
|
|
1478
|
+
* Returns the shared cookie attributes applied to every cookie issued by
|
|
1479
|
+
* this service. `sameSite` comes from the resolved options (defaults to
|
|
1480
|
+
* `'lax'`); the `domain` is folded in only when the consumer supplies a
|
|
1481
|
+
* resolver. Other per-cookie attributes (`httpOnly`, `secure`, `path`,
|
|
1482
|
+
* `maxAge`) are spread on top of this base by the callers.
|
|
1483
|
+
*/
|
|
1442
1484
|
baseCookieOptions(domain) {
|
|
1443
1485
|
return {
|
|
1444
|
-
sameSite:
|
|
1486
|
+
sameSite: this.options.cookies.sameSite,
|
|
1445
1487
|
...domain ? {
|
|
1446
1488
|
domain
|
|
1447
1489
|
} : {}
|
|
@@ -7285,9 +7327,11 @@ var OAuthController = class {
|
|
|
7285
7327
|
}
|
|
7286
7328
|
oauthService;
|
|
7287
7329
|
tokenDelivery;
|
|
7288
|
-
|
|
7330
|
+
options;
|
|
7331
|
+
constructor(oauthService, tokenDelivery, options) {
|
|
7289
7332
|
this.oauthService = oauthService;
|
|
7290
7333
|
this.tokenDelivery = tokenDelivery;
|
|
7334
|
+
this.options = options;
|
|
7291
7335
|
}
|
|
7292
7336
|
// ---------------------------------------------------------------------------
|
|
7293
7337
|
// GET /oauth/:provider
|
|
@@ -7321,17 +7365,30 @@ var OAuthController = class {
|
|
|
7321
7365
|
* or oversized values are rejected before reaching the service layer or the
|
|
7322
7366
|
* token-exchange HTTP call.
|
|
7323
7367
|
*
|
|
7368
|
+
* If `oauth.successRedirectUrl` is configured, the response is a `302` redirect
|
|
7369
|
+
* to that URL with cookies already set in the same response. This is the
|
|
7370
|
+
* standard browser OAuth flow — without it the browser lands on the JSON
|
|
7371
|
+
* payload returned to API consumers. The redirect is issued by calling
|
|
7372
|
+
* `res.redirect()` and returning `undefined`, which Nest passes through
|
|
7373
|
+
* unchanged (passthrough mode is active).
|
|
7374
|
+
*
|
|
7324
7375
|
* @param provider - Provider name from the URL path (e.g. `'google'`).
|
|
7325
7376
|
* @param query - Validated query parameters (contains `code` and `state`).
|
|
7326
7377
|
* @param req - Incoming request (IP, User-Agent, and cookie context for token delivery).
|
|
7327
|
-
* @param res - Express response in passthrough mode (used for cookie delivery).
|
|
7328
|
-
* @returns
|
|
7378
|
+
* @param res - Express response in passthrough mode (used for cookie delivery and the optional redirect).
|
|
7379
|
+
* @returns The auth response shaped by `tokenDelivery`, or `undefined` when a `successRedirectUrl` redirect was issued.
|
|
7329
7380
|
*/
|
|
7330
7381
|
async callback(provider, query, req, res) {
|
|
7331
7382
|
const ip = (req.ip ?? "").slice(0, 64);
|
|
7332
7383
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
7333
7384
|
const result = await this.oauthService.handleCallback(provider, query.code, query.state, ip, userAgent, req.headers);
|
|
7334
|
-
|
|
7385
|
+
const body = this.tokenDelivery.deliverAuthResponse(res, result, req);
|
|
7386
|
+
const redirectUrl = this.options.oauth?.successRedirectUrl;
|
|
7387
|
+
if (redirectUrl !== void 0) {
|
|
7388
|
+
res.redirect(redirectUrl);
|
|
7389
|
+
return void 0;
|
|
7390
|
+
}
|
|
7391
|
+
return body;
|
|
7335
7392
|
}
|
|
7336
7393
|
};
|
|
7337
7394
|
_ts_decorate43([
|
|
@@ -7378,10 +7435,12 @@ OAuthController = _ts_decorate43([
|
|
|
7378
7435
|
forbidNonWhitelisted: true,
|
|
7379
7436
|
forbidUnknownValues: true
|
|
7380
7437
|
})),
|
|
7438
|
+
_ts_param24(2, common.Inject(BYMAX_AUTH_OPTIONS)),
|
|
7381
7439
|
_ts_metadata42("design:type", Function),
|
|
7382
7440
|
_ts_metadata42("design:paramtypes", [
|
|
7383
7441
|
typeof exports.OAuthService === "undefined" ? Object : exports.OAuthService,
|
|
7384
|
-
typeof TokenDeliveryService === "undefined" ? Object : TokenDeliveryService
|
|
7442
|
+
typeof TokenDeliveryService === "undefined" ? Object : TokenDeliveryService,
|
|
7443
|
+
typeof ResolvedOptions === "undefined" ? Object : ResolvedOptions
|
|
7385
7444
|
])
|
|
7386
7445
|
], OAuthController);
|
|
7387
7446
|
|
package/dist/server/index.d.cts
CHANGED
|
@@ -422,6 +422,26 @@ interface BymaxAuthModuleOptions {
|
|
|
422
422
|
* @returns Array of domain strings where cookies should be set (e.g. `['.example.com']`).
|
|
423
423
|
*/
|
|
424
424
|
resolveDomains?: (requestDomain: string) => string[];
|
|
425
|
+
/**
|
|
426
|
+
* `SameSite` attribute applied to every cookie issued by the module
|
|
427
|
+
* (access token, refresh token, session signal). Default: `'lax'`.
|
|
428
|
+
*
|
|
429
|
+
* Trade-offs:
|
|
430
|
+
* - `'lax'` (default): cookies travel on top-level cross-site GET navigations.
|
|
431
|
+
* This is the standard posture for browser auth — OAuth redirects from a
|
|
432
|
+
* provider (Google, GitHub, …) back to the app deliver the auth cookies on
|
|
433
|
+
* the first navigation, which is exactly what users expect after sign-in.
|
|
434
|
+
* Aligns with Chromium's behavior for cookies that omit `SameSite`.
|
|
435
|
+
* - `'strict'`: cookies are withheld on every cross-site request, including
|
|
436
|
+
* the OAuth return-trip. Choose this only when the app does not use
|
|
437
|
+
* third-party identity providers and the extra CSRF margin justifies the
|
|
438
|
+
* broken OAuth flow.
|
|
439
|
+
* - `'none'`: cookies are sent on every cross-site request. The browser
|
|
440
|
+
* requires `Secure` for `SameSite=None`, so the resolver throws at startup
|
|
441
|
+
* when this combination would be unset (`secureCookies` must be `true`).
|
|
442
|
+
* Pick this for embedded scenarios (iframes, third-party widgets).
|
|
443
|
+
*/
|
|
444
|
+
sameSite?: 'lax' | 'strict' | 'none';
|
|
425
445
|
};
|
|
426
446
|
/**
|
|
427
447
|
* Multi-factor authentication (MFA/TOTP) configuration.
|
|
@@ -626,6 +646,32 @@ interface BymaxAuthModuleOptions {
|
|
|
626
646
|
* Each provider block is optional — only configured providers are registered.
|
|
627
647
|
*/
|
|
628
648
|
oauth?: {
|
|
649
|
+
/**
|
|
650
|
+
* URL the browser is redirected to after a successful OAuth callback.
|
|
651
|
+
*
|
|
652
|
+
* When set, the OAuth callback endpoint issues a `302` to this URL after
|
|
653
|
+
* delivering tokens (cookies are still set via the same response). When
|
|
654
|
+
* omitted, the callback returns a JSON body — appropriate for API/SPA
|
|
655
|
+
* consumers that XHR/fetch the callback URL, but it leaves browser users
|
|
656
|
+
* staring at a JSON page. Set this for any consumer where the OAuth flow
|
|
657
|
+
* is initiated by a full-page browser navigation.
|
|
658
|
+
*
|
|
659
|
+
* Requires `tokenDelivery: 'cookie'` or `'both'` — bearer-only delivery is
|
|
660
|
+
* incompatible with a redirect because the access token would be discarded
|
|
661
|
+
* in the redirect response body. The module throws at startup if the
|
|
662
|
+
* combination is misconfigured.
|
|
663
|
+
*
|
|
664
|
+
* In production (`NODE_ENV === 'production'`), the URL must use HTTPS or
|
|
665
|
+
* be a relative path (starts with `/`) — same posture as `callbackUrl`.
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```typescript
|
|
669
|
+
* successRedirectUrl: 'https://app.example.com/dashboard'
|
|
670
|
+
* // or, for same-origin deployments:
|
|
671
|
+
* successRedirectUrl: '/dashboard'
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
successRedirectUrl?: string;
|
|
629
675
|
/**
|
|
630
676
|
* Google OAuth 2.0 configuration.
|
|
631
677
|
* All three fields are required to enable Google login.
|
|
@@ -2855,6 +2901,13 @@ declare class TokenDeliveryService {
|
|
|
2855
2901
|
*/
|
|
2856
2902
|
extractDomain(req: Request): string | undefined;
|
|
2857
2903
|
private setAuthCookies;
|
|
2904
|
+
/**
|
|
2905
|
+
* Returns the shared cookie attributes applied to every cookie issued by
|
|
2906
|
+
* this service. `sameSite` comes from the resolved options (defaults to
|
|
2907
|
+
* `'lax'`); the `domain` is folded in only when the consumer supplies a
|
|
2908
|
+
* resolver. Other per-cookie attributes (`httpOnly`, `secure`, `path`,
|
|
2909
|
+
* `maxAge`) are spread on top of this base by the callers.
|
|
2910
|
+
*/
|
|
2858
2911
|
private baseCookieOptions;
|
|
2859
2912
|
private readCookie;
|
|
2860
2913
|
private readBearerHeader;
|
package/dist/server/index.d.ts
CHANGED
|
@@ -422,6 +422,26 @@ interface BymaxAuthModuleOptions {
|
|
|
422
422
|
* @returns Array of domain strings where cookies should be set (e.g. `['.example.com']`).
|
|
423
423
|
*/
|
|
424
424
|
resolveDomains?: (requestDomain: string) => string[];
|
|
425
|
+
/**
|
|
426
|
+
* `SameSite` attribute applied to every cookie issued by the module
|
|
427
|
+
* (access token, refresh token, session signal). Default: `'lax'`.
|
|
428
|
+
*
|
|
429
|
+
* Trade-offs:
|
|
430
|
+
* - `'lax'` (default): cookies travel on top-level cross-site GET navigations.
|
|
431
|
+
* This is the standard posture for browser auth — OAuth redirects from a
|
|
432
|
+
* provider (Google, GitHub, …) back to the app deliver the auth cookies on
|
|
433
|
+
* the first navigation, which is exactly what users expect after sign-in.
|
|
434
|
+
* Aligns with Chromium's behavior for cookies that omit `SameSite`.
|
|
435
|
+
* - `'strict'`: cookies are withheld on every cross-site request, including
|
|
436
|
+
* the OAuth return-trip. Choose this only when the app does not use
|
|
437
|
+
* third-party identity providers and the extra CSRF margin justifies the
|
|
438
|
+
* broken OAuth flow.
|
|
439
|
+
* - `'none'`: cookies are sent on every cross-site request. The browser
|
|
440
|
+
* requires `Secure` for `SameSite=None`, so the resolver throws at startup
|
|
441
|
+
* when this combination would be unset (`secureCookies` must be `true`).
|
|
442
|
+
* Pick this for embedded scenarios (iframes, third-party widgets).
|
|
443
|
+
*/
|
|
444
|
+
sameSite?: 'lax' | 'strict' | 'none';
|
|
425
445
|
};
|
|
426
446
|
/**
|
|
427
447
|
* Multi-factor authentication (MFA/TOTP) configuration.
|
|
@@ -626,6 +646,32 @@ interface BymaxAuthModuleOptions {
|
|
|
626
646
|
* Each provider block is optional — only configured providers are registered.
|
|
627
647
|
*/
|
|
628
648
|
oauth?: {
|
|
649
|
+
/**
|
|
650
|
+
* URL the browser is redirected to after a successful OAuth callback.
|
|
651
|
+
*
|
|
652
|
+
* When set, the OAuth callback endpoint issues a `302` to this URL after
|
|
653
|
+
* delivering tokens (cookies are still set via the same response). When
|
|
654
|
+
* omitted, the callback returns a JSON body — appropriate for API/SPA
|
|
655
|
+
* consumers that XHR/fetch the callback URL, but it leaves browser users
|
|
656
|
+
* staring at a JSON page. Set this for any consumer where the OAuth flow
|
|
657
|
+
* is initiated by a full-page browser navigation.
|
|
658
|
+
*
|
|
659
|
+
* Requires `tokenDelivery: 'cookie'` or `'both'` — bearer-only delivery is
|
|
660
|
+
* incompatible with a redirect because the access token would be discarded
|
|
661
|
+
* in the redirect response body. The module throws at startup if the
|
|
662
|
+
* combination is misconfigured.
|
|
663
|
+
*
|
|
664
|
+
* In production (`NODE_ENV === 'production'`), the URL must use HTTPS or
|
|
665
|
+
* be a relative path (starts with `/`) — same posture as `callbackUrl`.
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```typescript
|
|
669
|
+
* successRedirectUrl: 'https://app.example.com/dashboard'
|
|
670
|
+
* // or, for same-origin deployments:
|
|
671
|
+
* successRedirectUrl: '/dashboard'
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
successRedirectUrl?: string;
|
|
629
675
|
/**
|
|
630
676
|
* Google OAuth 2.0 configuration.
|
|
631
677
|
* All three fields are required to enable Google login.
|
|
@@ -2855,6 +2901,13 @@ declare class TokenDeliveryService {
|
|
|
2855
2901
|
*/
|
|
2856
2902
|
extractDomain(req: Request): string | undefined;
|
|
2857
2903
|
private setAuthCookies;
|
|
2904
|
+
/**
|
|
2905
|
+
* Returns the shared cookie attributes applied to every cookie issued by
|
|
2906
|
+
* this service. `sameSite` comes from the resolved options (defaults to
|
|
2907
|
+
* `'lax'`); the `domain` is folded in only when the consumer supplies a
|
|
2908
|
+
* resolver. Other per-cookie attributes (`httpOnly`, `secure`, `path`,
|
|
2909
|
+
* `maxAge`) are spread on top of this base by the callers.
|
|
2910
|
+
*/
|
|
2858
2911
|
private baseCookieOptions;
|
|
2859
2912
|
private readCookie;
|
|
2860
2913
|
private readBearerHeader;
|
package/dist/server/index.mjs
CHANGED
|
@@ -43,7 +43,13 @@ var DEFAULT_OPTIONS = {
|
|
|
43
43
|
accessTokenName: "access_token",
|
|
44
44
|
refreshTokenName: "refresh_token",
|
|
45
45
|
sessionSignalName: "has_session",
|
|
46
|
-
refreshCookiePath: "/auth"
|
|
46
|
+
refreshCookiePath: "/auth",
|
|
47
|
+
// 'lax' matches Chromium's default for cookies that omit SameSite. It
|
|
48
|
+
// delivers auth cookies on top-level cross-site GET navigations — the
|
|
49
|
+
// case that makes OAuth redirects (Google → callback → dashboard) work
|
|
50
|
+
// without dropping the freshly-issued session on the first hop. Consumers
|
|
51
|
+
// that want the stricter posture can override via `cookies.sameSite`.
|
|
52
|
+
sameSite: "lax"
|
|
47
53
|
},
|
|
48
54
|
mfa: {
|
|
49
55
|
recoveryCodeCount: 8,
|
|
@@ -140,7 +146,9 @@ function resolveOptions(userOptions) {
|
|
|
140
146
|
validatePasswordResetOtpLength(userOptions.passwordReset);
|
|
141
147
|
validatePasswordCostFactor(userOptions.password);
|
|
142
148
|
validateOAuthProviders(userOptions.oauth);
|
|
149
|
+
validateOAuthSuccessRedirectUrl(userOptions);
|
|
143
150
|
validateRefreshCookiePath(userOptions.routePrefix, userOptions.cookies);
|
|
151
|
+
validateSameSiteNoneRequiresSecure(userOptions);
|
|
144
152
|
validateRefreshGraceWindow(userOptions.jwt);
|
|
145
153
|
const { mfa: _mfa, ...userOptionsWithoutMfa } = userOptions;
|
|
146
154
|
const resolved = {
|
|
@@ -302,7 +310,9 @@ var REQUIRED_OAUTH_FIELDS = [
|
|
|
302
310
|
];
|
|
303
311
|
function validateOAuthProviders(oauth) {
|
|
304
312
|
if (!oauth) return;
|
|
305
|
-
for (const [
|
|
313
|
+
for (const [providerOrField, rawConfig] of Object.entries(oauth)) {
|
|
314
|
+
if (providerOrField === "successRedirectUrl") continue;
|
|
315
|
+
const provider = providerOrField;
|
|
306
316
|
const config = rawConfig;
|
|
307
317
|
for (const field of REQUIRED_OAUTH_FIELDS) {
|
|
308
318
|
if (!config[field]) {
|
|
@@ -316,6 +326,22 @@ function validateOAuthProviders(oauth) {
|
|
|
316
326
|
}
|
|
317
327
|
}
|
|
318
328
|
__name(validateOAuthProviders, "validateOAuthProviders");
|
|
329
|
+
function validateOAuthSuccessRedirectUrl(userOptions) {
|
|
330
|
+
const url = userOptions.oauth?.successRedirectUrl;
|
|
331
|
+
if (url === void 0) return;
|
|
332
|
+
if (typeof url !== "string" || url.length === 0) {
|
|
333
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must be a non-empty string when set.`);
|
|
334
|
+
}
|
|
335
|
+
const isProduction = process.env["NODE_ENV"] === "production";
|
|
336
|
+
const isSafe = url.startsWith("/") || url.startsWith("https://");
|
|
337
|
+
if (isProduction && !isSafe) {
|
|
338
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (got: '${url}').`);
|
|
339
|
+
}
|
|
340
|
+
if (userOptions.tokenDelivery === "bearer") {
|
|
341
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl is set but tokenDelivery is 'bearer'. A redirect discards the JSON response body, so the access token would never reach the client. Use tokenDelivery: 'cookie' or 'both' when configuring a successRedirectUrl.`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
__name(validateOAuthSuccessRedirectUrl, "validateOAuthSuccessRedirectUrl");
|
|
319
345
|
function validateRefreshCookiePath(routePrefix, cookies) {
|
|
320
346
|
const prefix = routePrefix ?? DEFAULT_OPTIONS.routePrefix;
|
|
321
347
|
if (prefix !== "auth" && !cookies?.refreshCookiePath) {
|
|
@@ -323,6 +349,15 @@ function validateRefreshCookiePath(routePrefix, cookies) {
|
|
|
323
349
|
}
|
|
324
350
|
}
|
|
325
351
|
__name(validateRefreshCookiePath, "validateRefreshCookiePath");
|
|
352
|
+
function validateSameSiteNoneRequiresSecure(userOptions) {
|
|
353
|
+
const sameSite = userOptions.cookies?.sameSite;
|
|
354
|
+
if (sameSite !== "none") return;
|
|
355
|
+
const secureCookies = userOptions.secureCookies ?? process.env["NODE_ENV"] === "production";
|
|
356
|
+
if (!secureCookies) {
|
|
357
|
+
throw new Error(`[BymaxAuthModule] cookies.sameSite is 'none' but secureCookies is false. Browsers reject SameSite=None cookies without the Secure attribute, so the auth cookies would never be stored. Set secureCookies: true (and serve over HTTPS) or use cookies.sameSite: 'lax' / 'strict'.`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
__name(validateSameSiteNoneRequiresSecure, "validateSameSiteNoneRequiresSecure");
|
|
326
361
|
function validateRefreshGraceWindow(jwt) {
|
|
327
362
|
const graceSeconds = jwt.refreshGraceWindowSeconds ?? DEFAULT_OPTIONS.jwt.refreshGraceWindowSeconds;
|
|
328
363
|
const refreshExpiresInDays = jwt.refreshExpiresInDays ?? DEFAULT_OPTIONS.jwt.refreshExpiresInDays;
|
|
@@ -1437,9 +1472,16 @@ var TokenDeliveryService = class {
|
|
|
1437
1472
|
maxAge: refreshMaxAge
|
|
1438
1473
|
});
|
|
1439
1474
|
}
|
|
1475
|
+
/**
|
|
1476
|
+
* Returns the shared cookie attributes applied to every cookie issued by
|
|
1477
|
+
* this service. `sameSite` comes from the resolved options (defaults to
|
|
1478
|
+
* `'lax'`); the `domain` is folded in only when the consumer supplies a
|
|
1479
|
+
* resolver. Other per-cookie attributes (`httpOnly`, `secure`, `path`,
|
|
1480
|
+
* `maxAge`) are spread on top of this base by the callers.
|
|
1481
|
+
*/
|
|
1440
1482
|
baseCookieOptions(domain) {
|
|
1441
1483
|
return {
|
|
1442
|
-
sameSite:
|
|
1484
|
+
sameSite: this.options.cookies.sameSite,
|
|
1443
1485
|
...domain ? {
|
|
1444
1486
|
domain
|
|
1445
1487
|
} : {}
|
|
@@ -7283,9 +7325,11 @@ var OAuthController = class {
|
|
|
7283
7325
|
}
|
|
7284
7326
|
oauthService;
|
|
7285
7327
|
tokenDelivery;
|
|
7286
|
-
|
|
7328
|
+
options;
|
|
7329
|
+
constructor(oauthService, tokenDelivery, options) {
|
|
7287
7330
|
this.oauthService = oauthService;
|
|
7288
7331
|
this.tokenDelivery = tokenDelivery;
|
|
7332
|
+
this.options = options;
|
|
7289
7333
|
}
|
|
7290
7334
|
// ---------------------------------------------------------------------------
|
|
7291
7335
|
// GET /oauth/:provider
|
|
@@ -7319,17 +7363,30 @@ var OAuthController = class {
|
|
|
7319
7363
|
* or oversized values are rejected before reaching the service layer or the
|
|
7320
7364
|
* token-exchange HTTP call.
|
|
7321
7365
|
*
|
|
7366
|
+
* If `oauth.successRedirectUrl` is configured, the response is a `302` redirect
|
|
7367
|
+
* to that URL with cookies already set in the same response. This is the
|
|
7368
|
+
* standard browser OAuth flow — without it the browser lands on the JSON
|
|
7369
|
+
* payload returned to API consumers. The redirect is issued by calling
|
|
7370
|
+
* `res.redirect()` and returning `undefined`, which Nest passes through
|
|
7371
|
+
* unchanged (passthrough mode is active).
|
|
7372
|
+
*
|
|
7322
7373
|
* @param provider - Provider name from the URL path (e.g. `'google'`).
|
|
7323
7374
|
* @param query - Validated query parameters (contains `code` and `state`).
|
|
7324
7375
|
* @param req - Incoming request (IP, User-Agent, and cookie context for token delivery).
|
|
7325
|
-
* @param res - Express response in passthrough mode (used for cookie delivery).
|
|
7326
|
-
* @returns
|
|
7376
|
+
* @param res - Express response in passthrough mode (used for cookie delivery and the optional redirect).
|
|
7377
|
+
* @returns The auth response shaped by `tokenDelivery`, or `undefined` when a `successRedirectUrl` redirect was issued.
|
|
7327
7378
|
*/
|
|
7328
7379
|
async callback(provider, query, req, res) {
|
|
7329
7380
|
const ip = (req.ip ?? "").slice(0, 64);
|
|
7330
7381
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
7331
7382
|
const result = await this.oauthService.handleCallback(provider, query.code, query.state, ip, userAgent, req.headers);
|
|
7332
|
-
|
|
7383
|
+
const body = this.tokenDelivery.deliverAuthResponse(res, result, req);
|
|
7384
|
+
const redirectUrl = this.options.oauth?.successRedirectUrl;
|
|
7385
|
+
if (redirectUrl !== void 0) {
|
|
7386
|
+
res.redirect(redirectUrl);
|
|
7387
|
+
return void 0;
|
|
7388
|
+
}
|
|
7389
|
+
return body;
|
|
7333
7390
|
}
|
|
7334
7391
|
};
|
|
7335
7392
|
_ts_decorate43([
|
|
@@ -7376,10 +7433,12 @@ OAuthController = _ts_decorate43([
|
|
|
7376
7433
|
forbidNonWhitelisted: true,
|
|
7377
7434
|
forbidUnknownValues: true
|
|
7378
7435
|
})),
|
|
7436
|
+
_ts_param24(2, Inject(BYMAX_AUTH_OPTIONS)),
|
|
7379
7437
|
_ts_metadata42("design:type", Function),
|
|
7380
7438
|
_ts_metadata42("design:paramtypes", [
|
|
7381
7439
|
typeof OAuthService === "undefined" ? Object : OAuthService,
|
|
7382
|
-
typeof TokenDeliveryService === "undefined" ? Object : TokenDeliveryService
|
|
7440
|
+
typeof TokenDeliveryService === "undefined" ? Object : TokenDeliveryService,
|
|
7441
|
+
typeof ResolvedOptions === "undefined" ? Object : ResolvedOptions
|
|
7383
7442
|
])
|
|
7384
7443
|
], OAuthController);
|
|
7385
7444
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-auth",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Full-stack authentication and authorization package for NestJS, React and Next.js — JWT, MFA, OAuth, sessions, multi-tenant SaaS ready",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|