auth-vir 2.2.0 → 2.3.1

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.
@@ -1,7 +1,7 @@
1
1
  import { type AnyObject, type JsonCompatibleObject, type MaybePromise, type PartialWithUndefined, type RequiredAndNotNull } from '@augment-vir/common';
2
2
  import { type AnyDuration } from 'date-vir';
3
3
  import { type IncomingHttpHeaders, type OutgoingHttpHeaders } from 'node:http';
4
- import { type EmptyObject, type RequireExactlyOne } from 'type-fest';
4
+ import { type EmptyObject, type RequireExactlyOne, type RequireOneOrNone } from 'type-fest';
5
5
  import { type UserIdResult } from '../auth.js';
6
6
  import { type CookieParams } from '../cookie.js';
7
7
  import { AuthHeaderName } from '../headers.js';
@@ -166,12 +166,26 @@ export declare class BackendAuthClient<DatabaseUser extends AnyObject, UserId ex
166
166
  requestHeaders: IncomingHttpHeaders;
167
167
  isSignUpCookie: boolean;
168
168
  }): Promise<Pick<RequiredAndNotNull<OutgoingHttpHeaders>, 'set-cookie'> & Record<CsrfHeaderName, string>>;
169
+ /** Combines `.getInsecureUser()` and `.getSecureUser()` into one method. */
170
+ getInsecureOrSecureUser(params: {
171
+ requestHeaders: IncomingHttpHeaders;
172
+ isSignUpCookie?: boolean | undefined;
173
+ }): Promise<RequireOneOrNone<{
174
+ secureUser: GetUserResult<DatabaseUser>;
175
+ /**
176
+ * @deprecated This only half authenticates the user. It should only be used in
177
+ * circumstances where JavaScript cannot be used to attach the CSRF token header to
178
+ * the request (like when opening a PDF file). Use `.getSecureUser()` instead,
179
+ * whenever possible.
180
+ */
181
+ insecureUser: GetUserResult<DatabaseUser>;
182
+ }>>;
169
183
  /**
170
184
  * @deprecated This only half authenticates the user. It should only be used in circumstances
171
185
  * where JavaScript cannot be used to attach the CSRF token header to the request (like when
172
186
  * opening a PDF file). Use `.getSecureUser()` instead, whenever possible.
173
187
  */
174
- getInsecureUser({ headers, }: {
175
- headers: IncomingHttpHeaders;
188
+ getInsecureUser({ requestHeaders, }: {
189
+ requestHeaders: IncomingHttpHeaders;
176
190
  }): Promise<GetUserResult<DatabaseUser> | undefined>;
177
191
  }
@@ -200,14 +200,24 @@ export class BackendAuthClient {
200
200
  : {}),
201
201
  };
202
202
  }
203
+ /** Combines `.getInsecureUser()` and `.getSecureUser()` into one method. */
204
+ async getInsecureOrSecureUser(params) {
205
+ const secureUser = await this.getSecureUser(params);
206
+ if (secureUser) {
207
+ return { secureUser };
208
+ }
209
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
210
+ const insecureUser = await this.getInsecureUser(params);
211
+ return insecureUser ? { insecureUser } : {};
212
+ }
203
213
  /**
204
214
  * @deprecated This only half authenticates the user. It should only be used in circumstances
205
215
  * where JavaScript cannot be used to attach the CSRF token header to the request (like when
206
216
  * opening a PDF file). Use `.getSecureUser()` instead, whenever possible.
207
217
  */
208
- async getInsecureUser({ headers, }) {
218
+ async getInsecureUser({ requestHeaders, }) {
209
219
  // eslint-disable-next-line @typescript-eslint/no-deprecated
210
- const userIdResult = await insecureExtractUserIdFromCookieAlone(headers, await this.getJwtParams(), AuthCookieName.Auth);
220
+ const userIdResult = await insecureExtractUserIdFromCookieAlone(requestHeaders, await this.getJwtParams(), AuthCookieName.Auth);
211
221
  if (!userIdResult) {
212
222
  return undefined;
213
223
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auth-vir",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Auth made easy and secure via JWT cookies, CSRF tokens, and password hashing helpers.",
5
5
  "keywords": [
6
6
  "auth",
@@ -8,7 +8,7 @@ import {
8
8
  } from '@augment-vir/common';
9
9
  import {calculateRelativeDate, getNowInUtcTimezone, isDateAfter, type AnyDuration} from 'date-vir';
10
10
  import {type IncomingHttpHeaders, type OutgoingHttpHeaders} from 'node:http';
11
- import {type EmptyObject, type RequireExactlyOne} from 'type-fest';
11
+ import {type EmptyObject, type RequireExactlyOne, type RequireOneOrNone} from 'type-fest';
12
12
  import {
13
13
  extractUserIdFromRequestHeaders,
14
14
  generateLogoutHeaders,
@@ -463,19 +463,47 @@ export class BackendAuthClient<
463
463
  };
464
464
  }
465
465
 
466
+ /** Combines `.getInsecureUser()` and `.getSecureUser()` into one method. */
467
+ public async getInsecureOrSecureUser(params: {
468
+ requestHeaders: IncomingHttpHeaders;
469
+ isSignUpCookie?: boolean | undefined;
470
+ }): Promise<
471
+ RequireOneOrNone<{
472
+ secureUser: GetUserResult<DatabaseUser>;
473
+ /**
474
+ * @deprecated This only half authenticates the user. It should only be used in
475
+ * circumstances where JavaScript cannot be used to attach the CSRF token header to
476
+ * the request (like when opening a PDF file). Use `.getSecureUser()` instead,
477
+ * whenever possible.
478
+ */
479
+ insecureUser: GetUserResult<DatabaseUser>;
480
+ }>
481
+ > {
482
+ const secureUser = await this.getSecureUser(params);
483
+
484
+ if (secureUser) {
485
+ return {secureUser};
486
+ }
487
+
488
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
489
+ const insecureUser = await this.getInsecureUser(params);
490
+
491
+ return insecureUser ? {insecureUser} : {};
492
+ }
493
+
466
494
  /**
467
495
  * @deprecated This only half authenticates the user. It should only be used in circumstances
468
496
  * where JavaScript cannot be used to attach the CSRF token header to the request (like when
469
497
  * opening a PDF file). Use `.getSecureUser()` instead, whenever possible.
470
498
  */
471
499
  public async getInsecureUser({
472
- headers,
500
+ requestHeaders,
473
501
  }: {
474
- headers: IncomingHttpHeaders;
502
+ requestHeaders: IncomingHttpHeaders;
475
503
  }): Promise<GetUserResult<DatabaseUser> | undefined> {
476
504
  // eslint-disable-next-line @typescript-eslint/no-deprecated
477
505
  const userIdResult = await insecureExtractUserIdFromCookieAlone<UserId>(
478
- headers,
506
+ requestHeaders,
479
507
  await this.getJwtParams(),
480
508
  AuthCookieName.Auth,
481
509
  );