@t4h.framework/jwks 0.5.2 → 0.7.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.
@@ -27,6 +27,7 @@ Package path: `framework/packages/jwks`. See [README.md](../../../README.md) for
27
27
  |-------|---------|
28
28
  | `@t4h.framework/jwks` | `JWKSAuthorization`, `JWKSAuthorizationContext`, `JWKSAuthorizationManifest`, `JWKSAlgorithm`, re-exports authorize module |
29
29
  | `@t4h.framework/jwks/authorize` | Default async handler: `(context) => Promise<boolean>` |
30
+ | `@t4h.framework/jwks/callback` | Default export `jwksCallback`: `(auth, delivery) => Promise<boolean>`, the authorizer hosts pass to the core `AsyncActivityCallback.authorize` for `jwt` (bearer JWT) and `jwks` (detached signature over the body, key resolved by `kid` from a JWKS URL) |
30
31
 
31
32
  ```typescript
32
33
  import {
@@ -132,8 +133,8 @@ Behavior (from `jwks-authorize.ts` and tests):
132
133
  Uses Node `crypto.createVerify` with public key from JWKS:
133
134
 
134
135
  ```typescript
135
- verifier.update(options.rawPayload)
136
- verifier.verify(signingKey, options.signature, signatureFormat)
136
+ authorizer.update(options.rawPayload)
137
+ authorizer.verify(signingKey, options.signature, signatureFormat)
137
138
  ```
138
139
 
139
140
  Throws if any required field is missing:
package/CHANGELOG.md CHANGED
@@ -1,5 +1,75 @@
1
1
  # @t4h.framework/jwks
2
2
 
3
+ ## 0.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#170](https://github.com/tech4humans-brasil/framework/pull/170) [`aa72268`](https://github.com/tech4humans-brasil/framework/commit/aa72268d040a743743cac31cc14e26b8620b0e3f) Thanks [@gusteycamargo](https://github.com/gusteycamargo)! - Adds the `jwks` callback auth: a detached signature over the callback body,
8
+ verified with the public key the `kid` resolves to at a JWKS URL — the way Amber
9
+ signs its webhooks.
10
+
11
+ **core**: `AsyncActivityCallbackAuthJwks` joins `AsyncActivityCallbackAuth`
12
+ (`jwksUri`, `header`, `kid`/`kidHeader`, `algorithm`/`algorithmHeader`,
13
+ `signatureFormat`/`signatureFormatHeader`, `timestampHeader`,
14
+ `toleranceInSeconds`, `signedPayload` as a template over `{timestamp}` and
15
+ `{body}`).
16
+
17
+ **jwks**: `jwksCallback` (default of `@t4h.framework/jwks/callback`) now serves
18
+ both `jwt` and `jwks`. Hosts pass it as `{ jwt: jwksCallback, jwks: jwksCallback }`.
19
+
20
+ **dev**: loads `@t4h.framework/jwks` for the `jwks` type as well.
21
+
22
+ **webhook**: documents the new type.
23
+
24
+ ### Patch Changes
25
+
26
+ - Updated dependencies [[`aa72268`](https://github.com/tech4humans-brasil/framework/commit/aa72268d040a743743cac31cc14e26b8620b0e3f)]:
27
+ - @t4h.framework/core@0.14.0
28
+
29
+ ## 0.6.0
30
+
31
+ ### Minor Changes
32
+
33
+ - [#167](https://github.com/tech4humans-brasil/framework/pull/167) [`1b1509f`](https://github.com/tech4humans-brasil/framework/commit/1b1509f9b6d2554dfab1f7633c3d022249e4fa01) Thanks [@gusteycamargo](https://github.com/gusteycamargo)! - Moves the callbacks route helpers from `@t4h.framework/webhook` to
34
+ `@t4h.framework/core`. The route advances any pending async activity, not only
35
+ `webhook()`, and the core already owned its contract (`AsyncActivityCallbackAuth`,
36
+ `GetActivityCallbackUrlClaim`, `AsyncActivityAdvanceContext`).
37
+
38
+ **core**: new `AsyncActivityCallback` namespace with `deriveToken`, `isTokenValid`,
39
+ `parseBody`, `getHeader`, `getBearerToken`, `flattenHeaders`, `authorize` and
40
+ the types `AsyncActivityCallback.Delivery`, `AsyncActivityCallback.Headers`, `AsyncActivityCallback.Json`,
41
+ `AsyncActivityCallback.AuthType`, `AsyncActivityCallback.Authorizer`, `AsyncActivityCallback.Authorizers`.
42
+ `AsyncActivityCallback.authorize(auth, request, authorizers?)` ships `basic`, `header` and
43
+ `bearer`; `hmac` and `jwt` are authorizers the host passes in, so the core does not
44
+ depend on `@t4h.framework/hmac` or `@t4h.framework/jwks`. A declared type without
45
+ a authorizer throws `AsyncActivityCallbackAuthorizerMissingException`; an unknown type throws
46
+ `AsyncActivityCallbackAuthUnsupportedException`.
47
+
48
+ **hmac**: new `@t4h.framework/hmac/callback` entry whose default export is the
49
+ `hmac` callback authorizer, `hmacCallback`.
50
+
51
+ **jwks**: new `@t4h.framework/jwks/callback` entry whose default export is the
52
+ `jwt` callback authorizer, `jwksCallback`.
53
+
54
+ **webhook**: no longer exports `deriveCallbackToken`, `isCallbackTokenValid`,
55
+ `verifyCallbackAuth`, `parseCallbackBody`, `getHeader`, `flattenHeaders` nor
56
+ `CallbackRequest`/`CallbackHeaders`, and no longer depends on `hmac` and `jwks`;
57
+ `WebhookJson` is now an alias of the core `AsyncActivityCallback.Json`. Hosts import the
58
+ helpers from the core `AsyncActivityCallback` namespace and the authorizers from the two entries.
59
+
60
+ **dev**: `@t4h.framework/webhook` becomes an optional peer like the other
61
+ integrations. Only `@t4h.framework/core` is required again, which fixes
62
+ `ERR_MODULE_NOT_FOUND: Cannot find package '@t4h.framework/webhook'` on
63
+ projects that never call `webhook()`. `hmac` and `jwt` callback authorizers are
64
+ loaded from the project when `@t4h.framework/hmac` / `@t4h.framework/jwks` are
65
+ installed; a callback whose declared `auth` needs a missing package answers 500
66
+ naming the package to install.
67
+
68
+ ### Patch Changes
69
+
70
+ - Updated dependencies [[`1b1509f`](https://github.com/tech4humans-brasil/framework/commit/1b1509f9b6d2554dfab1f7633c3d022249e4fa01), [`1b1509f`](https://github.com/tech4humans-brasil/framework/commit/1b1509f9b6d2554dfab1f7633c3d022249e4fa01)]:
71
+ - @t4h.framework/core@0.13.0
72
+
3
73
  ## 0.5.2
4
74
 
5
75
  ### Patch Changes
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Tech for humans
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -12,6 +12,7 @@ Requires `@t4h.framework/core` (same major line as this package).
12
12
  |------------------|---------|
13
13
  | `@t4h.framework/jwks` | `JWKSAuthorization`, `JWKSAuthorizationContext`, and related types |
14
14
  | `@t4h.framework/jwks/authorize` | Default async handler invoked by the runtime with the manifest context |
15
+ | `@t4h.framework/jwks/callback` | Default export `jwksCallback`: the `jwt` (bearer JWT) and `jwks` (detached signature verified with a JWKS key) callback authorizer for the core `AsyncActivityCallback.authorize` |
15
16
 
16
17
  ## `JWKSAuthorization`
17
18
 
@@ -0,0 +1,3 @@
1
+ import { AsyncActivityCallback, type AsyncActivityCallbackAuthJwks, type AsyncActivityCallbackAuthJwt } from '@t4h.framework/core';
2
+ export default function jwksCallback(auth: AsyncActivityCallbackAuthJwt | AsyncActivityCallbackAuthJwks, delivery: AsyncActivityCallback.Delivery): Promise<boolean>;
3
+ //# sourceMappingURL=jwks-callback.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwks-callback.d.ts","sourceRoot":"","sources":["../src/jwks-callback.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,KAAK,6BAA6B,EAClC,KAAK,4BAA4B,EAClC,MAAM,qBAAqB,CAAA;AAyG5B,wBAA8B,YAAY,CACxC,IAAI,EAAE,4BAA4B,GAAG,6BAA6B,EAClE,QAAQ,EAAE,qBAAqB,CAAC,QAAQ,GACvC,OAAO,CAAC,OAAO,CAAC,CAIlB"}
@@ -0,0 +1,75 @@
1
+ import { AsyncActivityCallback, } from '@t4h.framework/core';
2
+ import jwks from './jwks-authorize.js';
3
+ function isWithinTolerance(timestamp, toleranceInSeconds) {
4
+ const value = timestamp.trim();
5
+ if (!value)
6
+ return false;
7
+ const signedAt = /^\d+$/.test(value)
8
+ ? value.length >= 13
9
+ ? Number(value)
10
+ : Number(value) * 1000
11
+ : Date.parse(value);
12
+ if (Number.isNaN(signedAt))
13
+ return false;
14
+ return Math.abs(Date.now() - signedAt) / 1000 <= toleranceInSeconds;
15
+ }
16
+ function fromHeaderOrValue(headers, value, header) {
17
+ if (value !== undefined)
18
+ return value;
19
+ if (!header)
20
+ return undefined;
21
+ return AsyncActivityCallback.getHeader(headers, header);
22
+ }
23
+ async function verifyJwt(auth, delivery) {
24
+ const token = AsyncActivityCallback.getBearerToken(delivery.headers, auth.header);
25
+ if (!token)
26
+ return false;
27
+ try {
28
+ return await jwks({
29
+ url: auth.jwksUri,
30
+ signature: token,
31
+ rawPayload: '',
32
+ issuer: auth.issuer,
33
+ audience: auth.audience,
34
+ });
35
+ }
36
+ catch {
37
+ return false;
38
+ }
39
+ }
40
+ async function verifyDetachedSignature(auth, delivery) {
41
+ const { headers } = delivery;
42
+ const signature = AsyncActivityCallback.getHeader(headers, auth.header);
43
+ if (!signature)
44
+ return false;
45
+ const timestamp = auth.timestampHeader
46
+ ? AsyncActivityCallback.getHeader(headers, auth.timestampHeader)
47
+ : undefined;
48
+ if (auth.timestampHeader && !timestamp)
49
+ return false;
50
+ if (auth.toleranceInSeconds !== undefined &&
51
+ !isWithinTolerance(timestamp ?? '', auth.toleranceInSeconds))
52
+ return false;
53
+ const rawPayload = (auth.signedPayload ?? '{body}')
54
+ .replaceAll('{timestamp}', timestamp ?? '')
55
+ .replaceAll('{body}', delivery.rawBody);
56
+ try {
57
+ return await jwks({
58
+ url: auth.jwksUri,
59
+ signature,
60
+ rawPayload,
61
+ kid: fromHeaderOrValue(headers, auth.kid, auth.kidHeader),
62
+ algorithm: fromHeaderOrValue(headers, auth.algorithm, auth.algorithmHeader),
63
+ signatureFormat: fromHeaderOrValue(headers, auth.signatureFormat, auth.signatureFormatHeader) ?? 'hex',
64
+ });
65
+ }
66
+ catch {
67
+ return false;
68
+ }
69
+ }
70
+ export default async function jwksCallback(auth, delivery) {
71
+ if (auth.type === 'jwks')
72
+ return verifyDetachedSignature(auth, delivery);
73
+ return verifyJwt(auth, delivery);
74
+ }
75
+ //# sourceMappingURL=jwks-callback.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwks-callback.js","sourceRoot":"","sources":["../src/jwks-callback.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,GAGtB,MAAM,qBAAqB,CAAA;AAE5B,OAAO,IAAI,MAAM,qBAAqB,CAAA;AAEtC,SAAS,iBAAiB,CAAC,SAAiB,EAAE,kBAA0B;IACtE,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,EAAE,CAAA;IAE9B,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAA;IAExB,MAAM,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;QAClC,CAAC,CAAC,KAAK,CAAC,MAAM,IAAI,EAAE;YAClB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;YACf,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI;QACxB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;IAErB,IAAI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC;QAAE,OAAO,KAAK,CAAA;IAExC,OAAO,IAAI,CAAC,GAAG,CAAC,IA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SAuthorization.js';
3
4
  //# sourceMappingURL=jwks.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"jwks.d.ts","sourceRoot":"","sources":["../src/jwks.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,+BAA+B,CAAA"}
1
+ {"version":3,"file":"jwks.d.ts","sourceRoot":"","sources":["../src/jwks.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,+BAA+B,CAAA"}
package/dist/jwks.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './jwks-authorize.js';
2
+ export * from './jwks-callback.js';
2
3
  export * from './models/JWKSAuthorization.js';
3
4
  //# sourceMappingURL=jwks.js.map
package/dist/jwks.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"jwks.js","sourceRoot":"","sources":["../src/jwks.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,+BAA+B,CAAA"}
1
+ {"version":3,"file":"jwks.js","sourceRoot":"","sources":["../src/jwks.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,+BAA+B,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t4h.framework/jwks",
3
- "version": "0.5.2",
3
+ "version": "0.7.0",
4
4
  "description": "JWKS module for the T4H Framework authorization",
5
5
  "homepage": "https://github.com/tech4humans-brasil/framework/tree/main/packages/jwks",
6
6
  "bugs": "https://github.com/tech4humans-brasil/framework/issues",
@@ -20,6 +20,10 @@
20
20
  "./authorize": {
21
21
  "types": "./dist/jwks-authorize.d.ts",
22
22
  "import": "./dist/jwks-authorize.js"
23
+ },
24
+ "./callback": {
25
+ "types": "./dist/jwks-callback.d.ts",
26
+ "import": "./dist/jwks-callback.js"
23
27
  }
24
28
  },
25
29
  "module": "./dist/jwks.js",
@@ -37,13 +41,13 @@
37
41
  "test:watch": "vitest"
38
42
  },
39
43
  "devDependencies": {
40
- "@t4h.framework/core": "^0.12.0",
44
+ "@t4h.framework/core": "^0.14.0",
41
45
  "@types/jsonwebtoken": "^9",
42
46
  "typescript": "^5.9.3",
43
47
  "vitest": "^4.0.18"
44
48
  },
45
49
  "peerDependencies": {
46
- "@t4h.framework/core": "^0.12.0"
50
+ "@t4h.framework/core": "^0.14.0"
47
51
  },
48
52
  "packageManager": "yarn@4.12.0",
49
53
  "engines": {