@arkstack/auth 0.12.18 → 0.12.20

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/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import { Exception } from "@arkstack/common";
3
3
  import { Request, RequestSource, Response, ResponseSource, Session } from "@arkstack/http";
4
4
  import * as _$otpauth from "otpauth";
5
5
  import { Model } from "@arkstack/database";
6
+ import { User as User$1 } from "@app/models/User";
6
7
 
7
8
  //#region src/Contracts/PersonalAccessToken.d.ts
8
9
  declare abstract class PersonalAccessToken extends Model {
@@ -40,17 +41,6 @@ declare class AuthSession extends Session {
40
41
  token(): Promise<PersonalAccessToken | null>;
41
42
  }
42
43
  //#endregion
43
- //#region src/Contracts/User.d.ts
44
- declare abstract class User extends Model {
45
- [key: string]: any;
46
- email: string;
47
- name: string;
48
- password: string;
49
- createdAt: Date;
50
- updatedAt: Date;
51
- protected static table?: string | undefined;
52
- }
53
- //#endregion
54
44
  //#region src/Contracts/AuthContract.d.ts
55
45
  /**
56
46
  * The Auth class provides methods for user authentication, including verifying
@@ -65,20 +55,20 @@ declare abstract class AuthContract {
65
55
  * @param req The HTTP request instance to be set.
66
56
  * @returns The Auth instance itself for method chaining.
67
57
  */
68
- abstract setRequest(req: Request<User> | RequestSource<User>): this;
58
+ abstract setRequest(req: Request<User$1> | RequestSource<User$1>): this;
69
59
  /**
70
60
  * Get the current HTTP request instance being processed, which may contain
71
61
  * user information and other request-specific data relevant to authentication operations.
72
62
  *
73
63
  * @returns The current HTTP request instance or undefined if not set.
74
64
  */
75
- abstract getRequest(): Request<User> | undefined;
65
+ abstract getRequest(): Request<User$1> | undefined;
76
66
  /**
77
67
  * Get the currently authenticated user
78
68
  *
79
69
  * @returns The currently authenticated user or null if not authenticated.
80
70
  */
81
- abstract user(): User | null;
71
+ abstract user(): User$1 | null;
82
72
  /**
83
73
  * Verify user credentials
84
74
  *
@@ -94,7 +84,7 @@ declare abstract class AuthContract {
94
84
  * @param password
95
85
  * @returns
96
86
  */
97
- abstract attempt(email: string, password: string): Promise<User>;
87
+ abstract attempt(email: string, password: string): Promise<User$1>;
98
88
  /**
99
89
  * Login a user and create a personal access token
100
90
  *
@@ -112,7 +102,7 @@ declare abstract class AuthContract {
112
102
  * @param expiresIn
113
103
  * @returns
114
104
  */
115
- abstract createTemporaryToken(user: User, purpose: string, expiresIn?: string): Promise<string>;
105
+ abstract createTemporaryToken(user: User$1, purpose: string, expiresIn?: string): Promise<string>;
116
106
  /**
117
107
  * Authorize a temporary token and return the associated user if the token is
118
108
  * valid and matches the expected purpose.
@@ -121,7 +111,7 @@ declare abstract class AuthContract {
121
111
  * @param purpose
122
112
  * @returns
123
113
  */
124
- abstract authorizeTemporaryToken(token: string, purpose: string): Promise<User>;
114
+ abstract authorizeTemporaryToken(token: string, purpose: string): Promise<User$1>;
125
115
  /**
126
116
  * Logout the currently authenticated user and delete all their personal access tokens
127
117
  *
@@ -147,14 +137,14 @@ declare abstract class AuthContract {
147
137
  * @param user
148
138
  * @returns
149
139
  */
150
- abstract create(user: User): Promise<PersonalAccessToken>;
140
+ abstract create(user: User$1): Promise<PersonalAccessToken>;
151
141
  /**
152
142
  * Authorize a token and return the associated user
153
143
  *
154
144
  * @param token
155
145
  * @returns
156
146
  */
157
- abstract authorizeToken(token: string): Promise<User>;
147
+ abstract authorizeToken(token: string): Promise<User$1>;
158
148
  }
159
149
  //#endregion
160
150
  //#region src/Auth.d.ts
@@ -166,9 +156,9 @@ declare abstract class AuthContract {
166
156
  */
167
157
  declare class Auth extends AuthContract {
168
158
  #private;
169
- protected static req?: Request<User>;
159
+ protected static req?: Request<User$1>;
170
160
  private configuredSecret?;
171
- constructor(secret?: string, req?: Request<User> | RequestSource<User>);
161
+ constructor(secret?: string, req?: Request<User$1> | RequestSource<User$1>);
172
162
  /**
173
163
  * Create a new instance of the Auth class with an optional secret for JWT
174
164
  * signing and verification.
@@ -183,27 +173,27 @@ declare class Auth extends AuthContract {
183
173
  * @param req The HTTP request instance to be set.
184
174
  * @returns The Auth class itself for method chaining.
185
175
  */
186
- static setRequest(req: Request<User> | RequestSource<User>): typeof Auth;
176
+ static setRequest(req: Request<User$1> | RequestSource<User$1>): typeof Auth;
187
177
  /**
188
178
  * Set the current HTTP request instance being processed.
189
179
  *
190
180
  * @param req The HTTP request instance to be set.
191
181
  * @returns The Auth instance itself for method chaining.
192
182
  */
193
- setRequest(req: Request<User> | RequestSource<User>): this;
183
+ setRequest(req: Request<User$1> | RequestSource<User$1>): this;
194
184
  /**
195
185
  * Get the current HTTP request instance being processed, which may contain
196
186
  * user information and other request-specific data relevant to authentication operations.
197
187
  *
198
188
  * @returns The current HTTP request instance or undefined if not set.
199
189
  */
200
- getRequest(): Request<User> | undefined;
190
+ getRequest(): Request<User$1> | undefined;
201
191
  /**
202
192
  * Get the currently authenticated user
203
193
  *
204
194
  * @returns The currently authenticated user or null if not authenticated.
205
195
  */
206
- user(): User | null;
196
+ user(): User$1 | null;
207
197
  /**
208
198
  * Verify user credentials
209
199
  *
@@ -219,7 +209,7 @@ declare class Auth extends AuthContract {
219
209
  * @param password
220
210
  * @returns
221
211
  */
222
- attempt(email: string, password: string): Promise<User>;
212
+ attempt(email: string, password: string): Promise<User$1>;
223
213
  /**
224
214
  * Login a user and create a personal access token
225
215
  *
@@ -237,7 +227,7 @@ declare class Auth extends AuthContract {
237
227
  * @param expiresIn
238
228
  * @returns
239
229
  */
240
- createTemporaryToken(user: User, purpose: string, expiresIn?: string): Promise<string>;
230
+ createTemporaryToken(user: User$1, purpose: string, expiresIn?: string): Promise<string>;
241
231
  /**
242
232
  * Authorize a temporary token and return the associated user if the token is
243
233
  * valid and matches the expected purpose.
@@ -246,7 +236,7 @@ declare class Auth extends AuthContract {
246
236
  * @param purpose
247
237
  * @returns
248
238
  */
249
- authorizeTemporaryToken(token: string, purpose: string): Promise<User>;
239
+ authorizeTemporaryToken(token: string, purpose: string): Promise<User$1>;
250
240
  /**
251
241
  * Logout the currently authenticated user and delete all their personal access tokens
252
242
  *
@@ -272,7 +262,7 @@ declare class Auth extends AuthContract {
272
262
  * @param user
273
263
  * @returns
274
264
  */
275
- create(user: User): Promise<PersonalAccessToken>;
265
+ create(user: User$1): Promise<PersonalAccessToken>;
276
266
  /**
277
267
  * Create or replace the personal access token for the same user and device
278
268
  * while keeping a single active session record for that device.
@@ -288,7 +278,7 @@ declare class Auth extends AuthContract {
288
278
  * @param token
289
279
  * @returns
290
280
  */
291
- authorizeToken(token: string): Promise<User>;
281
+ authorizeToken(token: string): Promise<User$1>;
292
282
  /**
293
283
  * Create a JWT token
294
284
  *
@@ -304,6 +294,7 @@ declare class Auth extends AuthContract {
304
294
  */
305
295
  private verifyJWT;
306
296
  private getSecret;
297
+ private setAuthenticated;
307
298
  /**
308
299
  * Update the last used timestamp and device information of a personal
309
300
  * access token to keep the session active and reflect the latest device details.
@@ -467,7 +458,7 @@ declare class TwoFactor {
467
458
  * @param user
468
459
  * @returns
469
460
  */
470
- static getLabel(user: User): string;
461
+ static getLabel(user: User$1): string;
471
462
  /**
472
463
  * Create the per-user TOTP instance for setup and verification.
473
464
  *
@@ -475,7 +466,7 @@ declare class TwoFactor {
475
466
  * @param secret
476
467
  * @returns
477
468
  */
478
- static getTotp(user: User, secret: string): _$otpauth.TOTP;
469
+ static getTotp(user: User$1, secret: string): _$otpauth.TOTP;
479
470
  /**
480
471
  * Generate a new shared secret for authenticator-based 2FA.
481
472
  *
@@ -489,7 +480,7 @@ declare class TwoFactor {
489
480
  * @param secret Optional existing secret to use for the setup.
490
481
  * @returns An object containing the secret and the OTPAuth URL.
491
482
  */
492
- static createSetup(user: User, secret?: string): TwoFactorSetup;
483
+ static createSetup(user: User$1, secret?: string): TwoFactorSetup;
493
484
  /**
494
485
  * Verify a 6-digit authenticator code for a user.
495
486
  *
@@ -498,44 +489,44 @@ declare class TwoFactor {
498
489
  * @param code The 6-digit code to verify.
499
490
  * @returns True if the code is valid, false otherwise.
500
491
  */
501
- static verifyCode(user: User, secret: string, code: string): boolean;
502
- static getMethod(userId: User['id']): Promise<TwoFactorMethod | null>;
503
- static setMethod(userId: User['id'], method: TwoFactorMethod): Promise<void>;
492
+ static verifyCode(user: User$1, secret: string, code: string): boolean;
493
+ static getMethod(userId: User$1['id']): Promise<TwoFactorMethod | null>;
494
+ static setMethod(userId: User$1['id'], method: TwoFactorMethod): Promise<void>;
504
495
  /**
505
496
  * Read the setup secret stored for a user.
506
497
  *
507
498
  * @param userId The ID of the user.
508
499
  * @returns The stored secret, or null if not found.
509
500
  */
510
- static getSecret(userId: User['id']): Promise<string | null>;
501
+ static getSecret(userId: User$1['id']): Promise<string | null>;
511
502
  /**
512
503
  * Store the setup secret for a user.
513
504
  *
514
505
  * @param userId The ID of the user.
515
506
  * @param secret The secret to store.
516
507
  */
517
- static setSecret(userId: User['id'], secret: string): Promise<void>;
518
- static clearSecret(userId: User['id']): Promise<void>;
508
+ static setSecret(userId: User$1['id'], secret: string): Promise<void>;
509
+ static clearSecret(userId: User$1['id']): Promise<void>;
519
510
  /**
520
511
  * Read the timestamp indicating whether 2FA is enabled.
521
512
  *
522
513
  * @param userId The ID of the user.
523
514
  * @returns The timestamp when 2FA was enabled, or null if not enabled.
524
515
  */
525
- static getEnabledAt(userId: User['id']): Promise<string | null>;
516
+ static getEnabledAt(userId: User$1['id']): Promise<string | null>;
526
517
  /**
527
518
  * Persist the timestamp marking 2FA as enabled.
528
519
  *
529
520
  * @param userId The ID of the user.
530
521
  * @param enabledAt The timestamp to store.
531
522
  */
532
- static setEnabledAt(userId: User['id'], enabledAt?: string | Date): Promise<void>;
523
+ static setEnabledAt(userId: User$1['id'], enabledAt?: string | Date): Promise<void>;
533
524
  /**
534
525
  * Remove all persisted 2FA state for a user.
535
526
  *
536
527
  * @param userId The ID of the user.
537
528
  */
538
- static clear(userId: User['id']): Promise<void>;
529
+ static clear(userId: User$1['id']): Promise<void>;
539
530
  /**
540
531
  * Generate one-time recovery codes shown when 2FA is enabled.
541
532
  *
@@ -555,14 +546,14 @@ declare class TwoFactor {
555
546
  * @param userId The ID of the user.
556
547
  * @returns An array of recovery-code hashes.
557
548
  */
558
- static readRecoveryCodeHashes(userId: User['id']): Promise<string[]>;
549
+ static readRecoveryCodeHashes(userId: User$1['id']): Promise<string[]>;
559
550
  /**
560
551
  * Persist recovery-code hashes on the user's dedicated 2FA record.
561
552
  *
562
553
  * @param userId
563
554
  * @param hashes
564
555
  */
565
- static writeRecoveryCodeHashes(userId: User['id'], hashes: string[]): Promise<void>;
556
+ static writeRecoveryCodeHashes(userId: User$1['id'], hashes: string[]): Promise<void>;
566
557
  /**
567
558
  * Consume a valid recovery code and invalidate it immediately.
568
559
  *
@@ -570,14 +561,14 @@ declare class TwoFactor {
570
561
  * @param recoveryCode The recovery code to consume.
571
562
  * @returns True if the recovery code was valid and consumed, false otherwise.
572
563
  */
573
- static consumeRecoveryCode(userId: User['id'], recoveryCode: string): Promise<boolean>;
564
+ static consumeRecoveryCode(userId: User$1['id'], recoveryCode: string): Promise<boolean>;
574
565
  /**
575
566
  * Return the public 2FA status payload for a user.
576
567
  *
577
568
  * @param userId The ID of the user.
578
569
  * @returns An object containing the 2FA status and recovery codes remaining.
579
570
  */
580
- static readStatus(userId: User['id']): Promise<TwoFactorStatus>;
571
+ static readStatus(userId: User$1['id']): Promise<TwoFactorStatus>;
581
572
  static createSmsCode(): string;
582
573
  /**
583
574
  * Issue a new SMS code for the given user and send it via SMS for the specified purpose.
@@ -585,8 +576,8 @@ declare class TwoFactor {
585
576
  * @param user
586
577
  * @param purpose
587
578
  */
588
- static issueSmsCode(user: User, purpose: SmsCodePurpose): Promise<IssuedSmsCode>;
589
- static clearSmsCode(userId: User['id']): Promise<void>;
579
+ static issueSmsCode(user: User$1, purpose: SmsCodePurpose): Promise<IssuedSmsCode>;
580
+ static clearSmsCode(userId: User$1['id']): Promise<void>;
590
581
  /**
591
582
  * Verify a submitted SMS code for a user and purpose, consuming the code if valid.
592
583
  *
@@ -595,13 +586,24 @@ declare class TwoFactor {
595
586
  * @param purpose
596
587
  * @returns
597
588
  */
598
- static verifySmsCode(userId: User['id'], code: string, purpose: SmsCodePurpose): Promise<boolean>;
589
+ static verifySmsCode(userId: User$1['id'], code: string, purpose: SmsCodePurpose): Promise<boolean>;
590
+ }
591
+ //#endregion
592
+ //#region src/Contracts/User.d.ts
593
+ declare abstract class User extends Model {
594
+ [key: string]: any;
595
+ email: string;
596
+ name: string;
597
+ password: string;
598
+ createdAt: Date;
599
+ updatedAt: Date;
600
+ protected static table?: string | undefined;
599
601
  }
600
602
  //#endregion
601
603
  //#region src/Contracts/UserTwoFactor.d.ts
602
604
  declare abstract class UserTwoFactor extends Model {
603
605
  [key: string]: any;
604
- userId: User['id'];
606
+ userId: User$1['id'];
605
607
  method: TwoFactorMethod | null;
606
608
  secretCiphertext: string | null;
607
609
  smsCodeHash: string | null;
package/dist/index.js CHANGED
@@ -356,8 +356,7 @@ var Auth = class Auth extends AuthContract {
356
356
  status: 422,
357
357
  errors: { password: ["Invalid password"] }
358
358
  });
359
- Auth.req?.setUser(user);
360
- this.#user = user;
359
+ this.setAuthenticated(user);
361
360
  return user;
362
361
  }
363
362
  /**
@@ -406,8 +405,7 @@ var Auth = class Auth extends AuthContract {
406
405
  req: Auth.req,
407
406
  status: 401
408
407
  });
409
- Auth.req?.setUser(user);
410
- this.#user = user;
408
+ this.setAuthenticated(user, token);
411
409
  return user;
412
410
  }
413
411
  /**
@@ -422,6 +420,7 @@ var Auth = class Auth extends AuthContract {
422
420
  else await token.delete();
423
421
  else await (await getModel("PersonalAccessToken")).query().where({ userId: this.#user.id }).delete();
424
422
  this.#user = null;
423
+ if (Auth.req?.auth === this) Auth.req.clearAuthentication();
425
424
  }
426
425
  /**
427
426
  * Check if the user is authenticated
@@ -450,11 +449,11 @@ var Auth = class Auth extends AuthContract {
450
449
  sub: user.id.toString(),
451
450
  email: user.email
452
451
  };
453
- Auth.req?.setUser(user);
454
452
  const token = await this.createJWT(payload);
455
453
  const deviceInfo = SessionDevice.fromRequest(Auth.req);
456
454
  const pat = await this.upsertDeviceToken(user, token, deviceInfo);
457
455
  pat.setLoadedRelation("user", user);
456
+ this.setAuthenticated(user, token);
458
457
  return pat;
459
458
  }
460
459
  /**
@@ -514,11 +513,10 @@ var Auth = class Auth extends AuthContract {
514
513
  req: Auth.req,
515
514
  status: 401
516
515
  });
517
- Auth.req?.setUser(user);
518
516
  this.touchSession(pat).catch((error) => {
519
517
  if (env("NODE_ENV") === "development") console.error("Failed to update session activity", error);
520
518
  });
521
- this.#user = user;
519
+ this.setAuthenticated(user, token);
522
520
  return user;
523
521
  }
524
522
  /**
@@ -547,6 +545,10 @@ var Auth = class Auth extends AuthContract {
547
545
  getSecret() {
548
546
  return this.configuredSecret ?? env("JWT_SECRET", "default_secret");
549
547
  }
548
+ setAuthenticated(user, token) {
549
+ this.#user = user;
550
+ Auth.req?.setAuthentication(this, user, token);
551
+ }
550
552
  /**
551
553
  * Update the last used timestamp and device information of a personal
552
554
  * access token to keep the session active and reflect the latest device details.
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@arkstack/auth",
3
- "version": "0.12.18",
3
+ "version": "0.12.20",
4
4
  "type": "module",
5
5
  "description": "Authentication module for Arkstack, providing core authentication and identity features.",
6
6
  "homepage": "https://arkstack.toneflix.net/guide/auth",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/arkstack-tmp/arkstack.git",
9
+ "url": "git+https://github.com/arkstack-hq/arkstack.git",
10
10
  "directory": "packages/auth"
11
11
  },
12
12
  "keywords": [
@@ -38,12 +38,12 @@
38
38
  "jose": "^6.2.3",
39
39
  "otpauth": "^9.5.1",
40
40
  "ua-parser-js": "^2.0.9",
41
- "@arkstack/common": "^0.12.18",
42
- "@arkstack/http": "^0.12.18"
41
+ "@arkstack/common": "^0.12.20",
42
+ "@arkstack/http": "^0.12.20"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "@h3ravel/support": "^0.15.11",
46
- "@arkstack/database": "^0.12.18"
46
+ "@arkstack/database": "^0.12.20"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "tsdown --config-loader unrun",