@routr/common 2.32.2 → 2.32.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.
package/dist/auth.d.ts CHANGED
@@ -1,6 +1,18 @@
1
1
  import { AuthChallengeResponse } from "./types";
2
2
  export declare const DEFAULT_ALGORITHM = "MD5";
3
- export declare const generateNonce: (algorithm?: string) => string;
3
+ export declare const NONCE_MAX_AGE_SECONDS = 300;
4
+ export declare const generateNonce: (algorithm?: string, now?: () => number) => string;
5
+ /**
6
+ * Whether a nonce was issued by generateNonce within the last maxAgeSeconds.
7
+ * A nonce in another format is never fresh, so a header captured before
8
+ * nonces carried their time can't be replayed either.
9
+ *
10
+ * @param {string} nonce - the nonce from the Authorization header
11
+ * @param {number} maxAgeSeconds - how long a nonce stays valid; 0 means forever
12
+ * @param {Function} now - the clock, injected by tests
13
+ * @return {boolean} true when the nonce is fresh
14
+ */
15
+ export declare const isNonceFresh: (nonce: string | undefined, maxAgeSeconds?: number, now?: () => number) => boolean;
4
16
  export declare const calculateAuthResponse: (res: AuthChallengeResponse, credentials: {
5
17
  username: string;
6
18
  secret: string;
package/dist/auth.js CHANGED
@@ -3,13 +3,41 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getCredentials = exports.calculateAuthResponse = exports.generateNonce = exports.DEFAULT_ALGORITHM = void 0;
6
+ exports.getCredentials = exports.calculateAuthResponse = exports.isNonceFresh = exports.generateNonce = exports.NONCE_MAX_AGE_SECONDS = exports.DEFAULT_ALGORITHM = void 0;
7
7
  const crypto_1 = __importDefault(require("crypto"));
8
8
  exports.DEFAULT_ALGORITHM = "MD5";
9
9
  const md5hex = (str, algorithm = exports.DEFAULT_ALGORITHM) => crypto_1.default.createHash(algorithm).update(str).digest("hex");
10
10
  const decToHex = (dec) => (dec + Math.pow(16, 8)).toString(16).slice(-8);
11
- const generateNonce = (algorithm = exports.DEFAULT_ALGORITHM) => md5hex(`${new Date().getTime()}${Math.random()}`, algorithm);
11
+ // A nonce carries the second it was issued, so a captured Authorization
12
+ // header stops working once NONCE_MAX_AGE_SECONDS have passed (isNonceFresh)
13
+ exports.NONCE_MAX_AGE_SECONDS = 300;
14
+ const generateNonce = (algorithm = exports.DEFAULT_ALGORITHM, now = Date.now) => {
15
+ const issuedAt = Math.floor(now() / 1000).toString(16);
16
+ return `${issuedAt}.${md5hex(`${now()}${Math.random()}`, algorithm)}`;
17
+ };
12
18
  exports.generateNonce = generateNonce;
19
+ /**
20
+ * Whether a nonce was issued by generateNonce within the last maxAgeSeconds.
21
+ * A nonce in another format is never fresh, so a header captured before
22
+ * nonces carried their time can't be replayed either.
23
+ *
24
+ * @param {string} nonce - the nonce from the Authorization header
25
+ * @param {number} maxAgeSeconds - how long a nonce stays valid; 0 means forever
26
+ * @param {Function} now - the clock, injected by tests
27
+ * @return {boolean} true when the nonce is fresh
28
+ */
29
+ const isNonceFresh = (nonce, maxAgeSeconds = exports.NONCE_MAX_AGE_SECONDS, now = Date.now) => {
30
+ if (maxAgeSeconds === 0)
31
+ return true;
32
+ const [issuedAtHex, digest] = (nonce !== null && nonce !== void 0 ? nonce : "").split(".");
33
+ if (!digest || !/^[0-9a-f]+$/.test(issuedAtHex))
34
+ return false;
35
+ // A nonce dated ahead of this clock (another instance's) is only as fresh
36
+ // as it gets: the digest binds it, so no one gains by choosing one
37
+ const age = Math.floor(now() / 1000) - parseInt(issuedAtHex, 16);
38
+ return age <= maxAgeSeconds;
39
+ };
40
+ exports.isNonceFresh = isNonceFresh;
13
41
  const calculateAuthResponse = (res, credentials) => {
14
42
  if (!credentials)
15
43
  return null;
@@ -2,6 +2,7 @@ import { ResponseType } from "./types";
2
2
  export declare const createUnauthorizedResponse: (realm: string, params?: {
3
3
  qop: string;
4
4
  algorithm: string;
5
+ stale?: boolean;
5
6
  }) => {
6
7
  message: {
7
8
  responseType: ResponseType;
@@ -17,6 +18,21 @@ export declare const createUnauthorizedResponse: (realm: string, params?: {
17
18
  };
18
19
  };
19
20
  };
21
+ export declare const createStaleUnauthorizedResponse: (realm: string) => {
22
+ message: {
23
+ responseType: ResponseType;
24
+ reasonPhrase: string;
25
+ wwwAuthenticate: {
26
+ scheme: string;
27
+ realm: string;
28
+ qop: string;
29
+ opaque: string;
30
+ stale: boolean;
31
+ nonce: string;
32
+ algorithm: string;
33
+ };
34
+ };
35
+ };
20
36
  export declare const createUnauthorizedResponseWithoutChallenge: (metadata?: Record<string, string>) => {
21
37
  metadata: Record<string, string>;
22
38
  message: {
package/dist/response.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createNotFoundResponse = exports.createBadRequestResponse = exports.createForbiddenResponse = exports.createServerInternalErrorResponse = exports.createUnauthorizedResponseWithoutChallenge = exports.createUnauthorizedResponse = void 0;
3
+ exports.createNotFoundResponse = exports.createBadRequestResponse = exports.createForbiddenResponse = exports.createServerInternalErrorResponse = exports.createUnauthorizedResponseWithoutChallenge = exports.createStaleUnauthorizedResponse = exports.createUnauthorizedResponse = void 0;
4
4
  /*
5
5
  * Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
6
6
  * http://github.com/fonoster/routr
@@ -22,6 +22,7 @@ exports.createNotFoundResponse = exports.createBadRequestResponse = exports.crea
22
22
  const auth_1 = require("./auth");
23
23
  const types_1 = require("./types");
24
24
  const createUnauthorizedResponse = (realm, params = { qop: "auth", algorithm: auth_1.DEFAULT_ALGORITHM }) => {
25
+ var _a;
25
26
  return {
26
27
  message: {
27
28
  responseType: types_1.ResponseType.UNAUTHORIZED,
@@ -31,7 +32,7 @@ const createUnauthorizedResponse = (realm, params = { qop: "auth", algorithm: au
31
32
  realm: realm,
32
33
  qop: params.qop,
33
34
  opaque: "",
34
- stale: false,
35
+ stale: (_a = params.stale) !== null && _a !== void 0 ? _a : false,
35
36
  nonce: (0, auth_1.generateNonce)(),
36
37
  algorithm: params.algorithm
37
38
  }
@@ -39,6 +40,14 @@ const createUnauthorizedResponse = (realm, params = { qop: "auth", algorithm: au
39
40
  };
40
41
  };
41
42
  exports.createUnauthorizedResponse = createUnauthorizedResponse;
43
+ // The credentials were right but the nonce had expired: a client retries with
44
+ // the new nonce without asking the user again (RFC 7616 section 3.3)
45
+ const createStaleUnauthorizedResponse = (realm) => (0, exports.createUnauthorizedResponse)(realm, {
46
+ qop: "auth",
47
+ algorithm: auth_1.DEFAULT_ALGORITHM,
48
+ stale: true
49
+ });
50
+ exports.createStaleUnauthorizedResponse = createStaleUnauthorizedResponse;
42
51
  const createUnauthorizedResponseWithoutChallenge = (metadata) => {
43
52
  return {
44
53
  metadata,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/common",
3
- "version": "2.32.2",
3
+ "version": "2.32.3",
4
4
  "description": "Common package",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -61,5 +61,5 @@
61
61
  "devDependencies": {
62
62
  "@types/js-yaml": "^4.0.5"
63
63
  },
64
- "gitHead": "4781133850af7e3cf9daa3b538c385836422d27e"
64
+ "gitHead": "88199378914c62dbd44cf187c7892ce8435c3ca3"
65
65
  }