@nest-my-admin/auth 0.9.1 → 0.10.3

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.
@@ -33,6 +33,8 @@ export interface BuiltinAuthOptions {
33
33
  secretKey?: string;
34
34
  /** The name authenticator apps show next to the code. Default `nest-my-admin`. */
35
35
  issuer?: string;
36
+ /** An http(s) URL of the logo, sent as `image=` in the two-factor QR code. Only some authenticator apps read it; Ente Auth and others use their own icons, matched on `issuer`. */
37
+ issuerImage?: string;
36
38
  /** @internal Tests: the clock and a cheaper hash. */
37
39
  now?: () => Date;
38
40
  /** @internal */
@@ -8,6 +8,14 @@ import { SCRYPT, hashPassword, passwordProblems, verifyPassword } from './passwo
8
8
  import { RateLimiter } from './rate-limit.js';
9
9
  import { SecretBox } from './secret-box.js';
10
10
  import { base32Decode, base32Encode, newTotpSecret, otpauthUrl, verifyTotp } from './totp.js';
11
+ function checkIssuerImage(image) {
12
+ if (image === undefined)
13
+ return undefined;
14
+ if (!URL.canParse(image) || !['http:', 'https:'].includes(new URL(image).protocol)) {
15
+ throw new Error(`builtinAuth: issuerImage must be an absolute http(s) URL, got "${image}"`);
16
+ }
17
+ return image;
18
+ }
11
19
  const WRONG = 'Wrong username or password';
12
20
  const WRONG_CODE = 'The code is wrong or was already used';
13
21
  const RECOVERY_CODES = 10;
@@ -52,6 +60,7 @@ export class BuiltinAuthAdapter {
52
60
  now: options.now ?? (() => new Date()),
53
61
  scrypt: options.scrypt ?? SCRYPT,
54
62
  issuer: options.issuer ?? 'nest-my-admin',
63
+ issuerImage: checkIssuerImage(options.issuerImage),
55
64
  };
56
65
  this.box = new SecretBox(options.secretKey);
57
66
  this.limiter = new RateLimiter(this.options.loginAttemptsPerMinute, 60_000);
@@ -229,7 +238,7 @@ export class BuiltinAuthAdapter {
229
238
  this.warnOnce('two-factor keys are stored unencrypted; set builtinAuth({ secretKey }) to encrypt them');
230
239
  const secret = newTotpSecret();
231
240
  await this.users.update({ id: user.id }, { totpPending: this.box.seal(base32Encode(secret)) });
232
- return { secret: base32Encode(secret), otpauthUrl: otpauthUrl(this.options.issuer, user.username, secret) };
241
+ return { secret: base32Encode(secret), otpauthUrl: otpauthUrl(this.options.issuer, user.username, secret, this.options.issuerImage) };
233
242
  }
234
243
  async confirmTwoFactor(principal, code) {
235
244
  const user = await this.userOf(principal);
package/dist/totp.d.ts CHANGED
@@ -20,6 +20,6 @@ export declare function verifyTotp(secret: Uint8Array, code: string, now: number
20
20
  /** 20 random bytes, the size RFC 4226 recommends for SHA-1. */
21
21
  export declare function newTotpSecret(): Buffer;
22
22
  /** The `otpauth://` URL authenticator apps scan. */
23
- export declare function otpauthUrl(issuer: string, account: string, secret: Uint8Array): string;
23
+ export declare function otpauthUrl(issuer: string, account: string, secret: Uint8Array, image?: string): string;
24
24
  /** The current code for a base32 key (what the authenticator app shows): for tests and scripts of your own. */
25
25
  export declare function totpCode(secret: string, at?: Date): string;
package/dist/totp.js CHANGED
@@ -77,9 +77,11 @@ export function newTotpSecret() {
77
77
  return randomBytes(20);
78
78
  }
79
79
  /** The `otpauth://` URL authenticator apps scan. */
80
- export function otpauthUrl(issuer, account, secret) {
80
+ export function otpauthUrl(issuer, account, secret, image) {
81
81
  const label = `${encodeURIComponent(issuer)}:${encodeURIComponent(account)}`;
82
82
  const params = new URLSearchParams({ secret: base32Encode(secret), issuer, algorithm: 'SHA1', digits: '6', period: String(TOTP_PERIOD) });
83
+ if (image)
84
+ params.set('image', image);
83
85
  return `otpauth://totp/${label}?${params.toString()}`;
84
86
  }
85
87
  /** The current code for a base32 key (what the authenticator app shows): for tests and scripts of your own. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nest-my-admin/auth",
3
- "version": "0.9.1",
3
+ "version": "0.10.3",
4
4
  "description": "Built-in sign-in for nest-my-admin: admin users, hashed passwords, revocable sessions",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -30,14 +30,14 @@
30
30
  "typecheck": "tsc -p tsconfig.json --noEmit"
31
31
  },
32
32
  "peerDependencies": {
33
- "@nest-my-admin/core": "0.9.1",
33
+ "@nest-my-admin/core": "0.10.3",
34
34
  "@nestjs/common": "^11.0.0 || ^12.0.0",
35
35
  "@nestjs/core": "^11.0.0 || ^12.0.0",
36
36
  "@nestjs/typeorm": "^11.0.0 || ^12.0.0",
37
37
  "typeorm": "^0.3.20 || ^1.0.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@nest-my-admin/core": "0.9.1",
40
+ "@nest-my-admin/core": "0.10.3",
41
41
  "@nestjs/common": "12.1.1",
42
42
  "@nestjs/core": "12.1.1",
43
43
  "@nestjs/platform-express": "12.1.1",