auth-vir 2.3.9 → 2.4.0

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.
@@ -119,14 +119,16 @@ export declare class BackendAuthClient<DatabaseUser extends AnyObject, UserId ex
119
119
  protected cachedParsedJwtKeys: Record<string, Readonly<JwtKeys>>;
120
120
  constructor(config: BackendAuthClientConfig<DatabaseUser, UserId, AssumedUserParams, CsrfHeaderName>);
121
121
  /** Get all the parameters used for cookie generation. */
122
- protected getCookieParams({ isSignUpCookie, }: {
122
+ protected getCookieParams({ isSignUpCookie, serviceOrigin, }: {
123
123
  /**
124
124
  * Set this to `true` when we are setting the initial cookie right after a user signs up.
125
125
  * This allows them to auto-authorize when they verify their email address.
126
126
  *
127
127
  * This should only be set to `true` when a new user is signing up.
128
128
  */
129
- isSignUpCookie?: boolean | undefined;
129
+ isSignUpCookie: boolean;
130
+ /** Overrides the client's already established `serviceOrigin`. */
131
+ serviceOrigin: string | undefined;
130
132
  }): Promise<Readonly<CookieParams>>;
131
133
  /** Calls the provided `getUserFromDatabase` config. */
132
134
  protected getDatabaseUser({ isSignUpCookie, userId, assumingUser, }: {
@@ -163,14 +165,18 @@ export declare class BackendAuthClient<DatabaseUser extends AnyObject, UserId ex
163
165
  createLogoutHeaders(params: RequireExactlyOne<{
164
166
  allCookies: true;
165
167
  isSignUpCookie: boolean;
168
+ /** Overrides the client's already established `serviceOrigin`. */
169
+ serviceOrigin?: string | undefined;
166
170
  }>): Promise<Partial<Record<CsrfHeaderName, string>> & {
167
171
  'set-cookie': string[];
168
172
  }>;
169
173
  /** Use these headers to log a user in. */
170
- createLoginHeaders({ userId, requestHeaders, isSignUpCookie, }: {
174
+ createLoginHeaders({ userId, requestHeaders, isSignUpCookie, serviceOrigin, }: {
171
175
  userId: UserId;
172
176
  requestHeaders: IncomingHttpHeaders;
173
177
  isSignUpCookie: boolean;
178
+ /** Overrides the client's already established `serviceOrigin`. */
179
+ serviceOrigin?: string | undefined;
174
180
  }): Promise<OutgoingHttpHeaders>;
175
181
  /** Combines `.getInsecureUser()` and `.getSecureUser()` into one method. */
176
182
  getInsecureOrSecureUser(params: {
@@ -24,10 +24,10 @@ export class BackendAuthClient {
24
24
  this.config = config;
25
25
  }
26
26
  /** Get all the parameters used for cookie generation. */
27
- async getCookieParams({ isSignUpCookie, }) {
27
+ async getCookieParams({ isSignUpCookie, serviceOrigin, }) {
28
28
  return {
29
29
  cookieDuration: this.config.userSessionIdleTimeout || defaultSessionIdleTimeout,
30
- hostOrigin: this.config.serviceOrigin,
30
+ hostOrigin: serviceOrigin || this.config.serviceOrigin,
31
31
  jwtParams: await this.getJwtParams(),
32
32
  isDev: this.config.isDev,
33
33
  cookieName: isSignUpCookie ? AuthCookieName.SignUp : AuthCookieName.Auth,
@@ -160,11 +160,13 @@ export class BackendAuthClient {
160
160
  const signUpCookieHeaders = params.allCookies || params.isSignUpCookie
161
161
  ? generateLogoutHeaders(await this.getCookieParams({
162
162
  isSignUpCookie: true,
163
+ serviceOrigin: params.serviceOrigin,
163
164
  }), this.config.overrides)
164
165
  : undefined;
165
166
  const authCookieHeaders = params.allCookies || !params.isSignUpCookie
166
167
  ? generateLogoutHeaders(await this.getCookieParams({
167
168
  isSignUpCookie: false,
169
+ serviceOrigin: params.serviceOrigin,
168
170
  }), this.config.overrides)
169
171
  : undefined;
170
172
  const setCookieHeader = {
@@ -180,16 +182,18 @@ export class BackendAuthClient {
180
182
  };
181
183
  }
182
184
  /** Use these headers to log a user in. */
183
- async createLoginHeaders({ userId, requestHeaders, isSignUpCookie, }) {
185
+ async createLoginHeaders({ userId, requestHeaders, isSignUpCookie, serviceOrigin, }) {
184
186
  const oppositeCookieName = isSignUpCookie ? AuthCookieName.Auth : AuthCookieName.SignUp;
185
187
  const hasExistingOppositeCookie = requestHeaders.cookie?.includes(`${oppositeCookieName}=`);
186
188
  const discardOppositeCookieHeaders = hasExistingOppositeCookie
187
189
  ? generateLogoutHeaders(await this.getCookieParams({
188
190
  isSignUpCookie: !isSignUpCookie,
191
+ serviceOrigin,
189
192
  }), this.config.overrides)
190
193
  : undefined;
191
194
  const newCookieHeaders = await generateSuccessfulLoginHeaders(userId, await this.getCookieParams({
192
195
  isSignUpCookie,
196
+ serviceOrigin,
193
197
  }), this.config.overrides);
194
198
  return {
195
199
  ...newCookieHeaders,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auth-vir",
3
- "version": "2.3.9",
3
+ "version": "2.4.0",
4
4
  "description": "Auth made easy and secure via JWT cookies, CSRF tokens, and password hashing helpers.",
5
5
  "keywords": [
6
6
  "auth",
@@ -168,6 +168,7 @@ export class BackendAuthClient<
168
168
  /** Get all the parameters used for cookie generation. */
169
169
  protected async getCookieParams({
170
170
  isSignUpCookie,
171
+ serviceOrigin,
171
172
  }: {
172
173
  /**
173
174
  * Set this to `true` when we are setting the initial cookie right after a user signs up.
@@ -175,11 +176,13 @@ export class BackendAuthClient<
175
176
  *
176
177
  * This should only be set to `true` when a new user is signing up.
177
178
  */
178
- isSignUpCookie?: boolean | undefined;
179
+ isSignUpCookie: boolean;
180
+ /** Overrides the client's already established `serviceOrigin`. */
181
+ serviceOrigin: string | undefined;
179
182
  }): Promise<Readonly<CookieParams>> {
180
183
  return {
181
184
  cookieDuration: this.config.userSessionIdleTimeout || defaultSessionIdleTimeout,
182
- hostOrigin: this.config.serviceOrigin,
185
+ hostOrigin: serviceOrigin || this.config.serviceOrigin,
183
186
  jwtParams: await this.getJwtParams(),
184
187
  isDev: this.config.isDev,
185
188
  cookieName: isSignUpCookie ? AuthCookieName.SignUp : AuthCookieName.Auth,
@@ -381,6 +384,8 @@ export class BackendAuthClient<
381
384
  params: RequireExactlyOne<{
382
385
  allCookies: true;
383
386
  isSignUpCookie: boolean;
387
+ /** Overrides the client's already established `serviceOrigin`. */
388
+ serviceOrigin?: string | undefined;
384
389
  }>,
385
390
  ): Promise<
386
391
  Partial<Record<CsrfHeaderName, string>> & {
@@ -392,6 +397,7 @@ export class BackendAuthClient<
392
397
  ? (generateLogoutHeaders(
393
398
  await this.getCookieParams({
394
399
  isSignUpCookie: true,
400
+ serviceOrigin: params.serviceOrigin,
395
401
  }),
396
402
  this.config.overrides,
397
403
  ) satisfies Record<CsrfHeaderName, string>)
@@ -401,6 +407,7 @@ export class BackendAuthClient<
401
407
  ? (generateLogoutHeaders(
402
408
  await this.getCookieParams({
403
409
  isSignUpCookie: false,
410
+ serviceOrigin: params.serviceOrigin,
404
411
  }),
405
412
  this.config.overrides,
406
413
  ) satisfies Record<CsrfHeaderName, string>)
@@ -430,10 +437,13 @@ export class BackendAuthClient<
430
437
  userId,
431
438
  requestHeaders,
432
439
  isSignUpCookie,
440
+ serviceOrigin,
433
441
  }: {
434
442
  userId: UserId;
435
443
  requestHeaders: IncomingHttpHeaders;
436
444
  isSignUpCookie: boolean;
445
+ /** Overrides the client's already established `serviceOrigin`. */
446
+ serviceOrigin?: string | undefined;
437
447
  }): Promise<OutgoingHttpHeaders> {
438
448
  const oppositeCookieName = isSignUpCookie ? AuthCookieName.Auth : AuthCookieName.SignUp;
439
449
  const hasExistingOppositeCookie = requestHeaders.cookie?.includes(`${oppositeCookieName}=`);
@@ -442,6 +452,7 @@ export class BackendAuthClient<
442
452
  ? generateLogoutHeaders(
443
453
  await this.getCookieParams({
444
454
  isSignUpCookie: !isSignUpCookie,
455
+ serviceOrigin,
445
456
  }),
446
457
  this.config.overrides,
447
458
  )
@@ -451,6 +462,7 @@ export class BackendAuthClient<
451
462
  userId,
452
463
  await this.getCookieParams({
453
464
  isSignUpCookie,
465
+ serviceOrigin,
454
466
  }),
455
467
  this.config.overrides,
456
468
  );