@reallyme/jose 0.4.6 → 0.5.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.
package/README.md CHANGED
@@ -38,8 +38,9 @@ into a `Uint8Array`, and call `wasm.initSync({ module: bytes })` before provider
38
38
  installation. Node.js integrations require version 20.19 or newer.
39
39
 
40
40
  Provider installation is explicit. Calls fail closed when no WASM provider is
41
- installed; applications must select a trusted module. The facade clears the
42
- request and response byte arrays it owns after dispatch. The protobuf encoder
41
+ installed, with the local `provider-not-installed` SDK code. Applications must
42
+ select a trusted module.
43
+ The facade clears the request and response byte arrays it owns after dispatch. The protobuf encoder
43
44
  can make internal copies during buffer growth that JavaScript cannot clear
44
45
  through its public API, so this is best-effort cleanup rather than guaranteed
45
46
  erasure. Callers remain responsible for clearing their own `Uint8Array` values.
@@ -60,6 +61,16 @@ validation, typed errors, size limits, and cleanup rules form the supported
60
61
  API. Cryptographic operations stay inside WASM; the host supplies secure
61
62
  randomness.
62
63
 
64
+ Facade options accept plain or null-prototype records. Unknown option and policy
65
+ fields are rejected so misspelled constraints cannot silently disable a check.
66
+
67
+ JWT verification requires `verification: { mode: "signatureOnly" }`,
68
+ `{ mode: "claims", policy }`, or `{ mode: "temporalOnly", policy }`.
69
+ `ReallyMeJoseJwtClaimsPolicy` binds audience and optional issuer/subject;
70
+ `ReallyMeJoseJwtTemporalPolicy` checks time claims only. Omitted temporal options
71
+ use the schema's strict preset: expiration is required and skew is 60 seconds. Unknown modes, extra
72
+ fields, and the retired `signatureOnly`/`temporalPolicy` options are rejected.
73
+
63
74
  ## License
64
75
 
65
76
  Licensed under either the MIT License or the Apache License, Version 2.0, at your
package/dist/boundary.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
2
2
  //
3
3
  // SPDX-License-Identifier: MIT OR Apache-2.0
4
- import { ReallyMeJoseError } from "./errors.js";
4
+ import { ReallyMeJoseError, ReallyMeJoseSdkErrorCode } from "./errors.js";
5
5
  import { requireReallyMeJoseWasmProvider } from "./provider.js";
6
6
  import { ensureBytes, ensureIndependentBytes, MAX_JOSE_BINARY_REQUEST_BYTES, MAX_JOSE_JSON_REQUEST_BYTES, MAX_JOSE_RESPONSE_BYTES, resourceLimit, } from "./validate.js";
7
7
  const execute = (provider, request, maximum, json) => {
@@ -13,12 +13,12 @@ const execute = (provider, request, maximum, json) => {
13
13
  ? provider.executeOperationJson(request)
14
14
  : provider.executeOperation(request);
15
15
  if (!(result instanceof Uint8Array)) {
16
- throw new ReallyMeJoseError("provider-failure");
16
+ throw new ReallyMeJoseError(ReallyMeJoseSdkErrorCode.PROVIDER_FAILURE);
17
17
  }
18
18
  ensureIndependentBytes(result, [request]);
19
19
  if (result.length === 0 || result.length > MAX_JOSE_RESPONSE_BYTES) {
20
20
  result.fill(0);
21
- throw new ReallyMeJoseError("provider-failure");
21
+ throw new ReallyMeJoseError(ReallyMeJoseSdkErrorCode.PROVIDER_FAILURE);
22
22
  }
23
23
  return result;
24
24
  };
@@ -0,0 +1,18 @@
1
+ import type { ReallyMeJoseJwtClaimsPolicy } from "./facade.js";
2
+ export type ReallyMeJoseJwtVerification = Readonly<{
3
+ mode: "signatureOnly";
4
+ }> | Readonly<{
5
+ mode: "claims";
6
+ policy: ReallyMeJoseJwtClaimsPolicy;
7
+ }> | Readonly<{
8
+ mode: "temporalOnly";
9
+ policy: ReallyMeJoseJwtTemporalPolicy;
10
+ }>;
11
+ export type ReallyMeJoseJwtTemporalPolicy = Readonly<{
12
+ requireExpiration?: boolean;
13
+ requireNotBefore?: boolean;
14
+ requireIssuedAt?: boolean;
15
+ clockSkewSeconds?: bigint;
16
+ maximumFutureIssuedAtSkewSeconds?: bigint;
17
+ verificationTimeUnixSeconds: bigint;
18
+ }>;
@@ -0,0 +1,4 @@
1
+ // SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
2
+ //
3
+ // SPDX-License-Identifier: MIT OR Apache-2.0
4
+ export {};
package/dist/errors.d.ts CHANGED
@@ -1,9 +1,13 @@
1
- import type { JoseErrorReason } from "./proto/generated/reallyme/jose/v1/jose_pb.js";
2
- export type ReallyMeJoseSdkErrorCode = "invalid-input" | "provider-not-installed" | "provider-failure" | "malformed-provider-response" | "jose-failure";
3
- export type ReallyMeJoseErrorBranch = "primitive" | "provider" | "backend";
1
+ import type { JoseErrorReason, JoseSdkErrorCode } from "./proto/generated/reallyme/jose/v1/jose_pb.js";
2
+ import { ReallyMeJoseSdkErrorCode } from "./proto/contract-values.js";
3
+ import type { ReallyMeJoseErrorBranch } from "./proto/contract-values.js";
4
+ export { ReallyMeJoseSdkErrorCode } from "./proto/contract-values.js";
5
+ export type { ReallyMeJoseErrorBranch } from "./proto/contract-values.js";
4
6
  /** Audit-safe SDK error. It never retains caller input or backend text. */
5
7
  export declare class ReallyMeJoseError extends Error {
6
8
  readonly code: ReallyMeJoseSdkErrorCode;
9
+ /** Stable error code defined in the protobuf contract. */
10
+ readonly schemaCode: JoseSdkErrorCode;
7
11
  readonly branch: ReallyMeJoseErrorBranch | undefined;
8
12
  readonly reason: JoseErrorReason | undefined;
9
13
  constructor(code: ReallyMeJoseSdkErrorCode, branch?: ReallyMeJoseErrorBranch, reason?: JoseErrorReason);
package/dist/errors.js CHANGED
@@ -1,16 +1,28 @@
1
1
  // SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
2
2
  //
3
3
  // SPDX-License-Identifier: MIT OR Apache-2.0
4
+ import { errorBranch, ReallyMeJoseSdkErrorCode, sdkErrorSchemaCodes } from "./proto/contract-values.js";
5
+ export { ReallyMeJoseSdkErrorCode } from "./proto/contract-values.js";
4
6
  /** Audit-safe SDK error. It never retains caller input or backend text. */
5
7
  export class ReallyMeJoseError extends Error {
6
8
  code;
9
+ /** Stable error code defined in the protobuf contract. */
10
+ schemaCode;
7
11
  branch;
8
12
  reason;
9
13
  constructor(code, branch, reason) {
10
- super(code === "jose-failure" ? "JOSE operation failed" : code);
14
+ const knownCode = typeof code === "string" && Object.hasOwn(sdkErrorSchemaCodes, code);
15
+ const requestedCode = knownCode ? code : ReallyMeJoseSdkErrorCode.INVALID_INPUT;
16
+ const validReason = requestedCode === ReallyMeJoseSdkErrorCode.JOSE_FAILURE
17
+ && branch !== undefined && reason !== undefined && errorBranch(reason) === branch;
18
+ const safeCode = requestedCode === ReallyMeJoseSdkErrorCode.JOSE_FAILURE && !validReason
19
+ ? ReallyMeJoseSdkErrorCode.MALFORMED_PROVIDER_RESPONSE
20
+ : requestedCode;
21
+ super(safeCode === ReallyMeJoseSdkErrorCode.JOSE_FAILURE ? "JOSE operation failed" : safeCode);
11
22
  this.name = "ReallyMeJoseError";
12
- this.code = code;
13
- this.branch = branch;
14
- this.reason = reason;
23
+ this.code = safeCode;
24
+ this.schemaCode = sdkErrorSchemaCodes[safeCode];
25
+ this.branch = validReason ? branch : undefined;
26
+ this.reason = validReason ? reason : undefined;
15
27
  }
16
28
  }
@@ -0,0 +1,70 @@
1
+ export declare const SIGN_JWS_FIELDS: Readonly<{
2
+ algorithm: true;
3
+ privateKey: true;
4
+ payload: true;
5
+ }>;
6
+ export declare const VERIFY_JWS_FIELDS: Readonly<{
7
+ algorithm: true;
8
+ compact: true;
9
+ publicKey: true;
10
+ }>;
11
+ export declare const SIGN_JWT_FIELDS: Readonly<{
12
+ claimsJson: true;
13
+ jwkJson: true;
14
+ privateKey: true;
15
+ headerOptions: true;
16
+ }>;
17
+ export declare const VERIFY_JWT_FIELDS: Readonly<{
18
+ compact: true;
19
+ jwkJson: true;
20
+ publicKey: true;
21
+ headerPolicy: true;
22
+ verification: true;
23
+ }>;
24
+ export declare const ENCRYPT_JWE_FIELDS: Readonly<{
25
+ keyManagementAlgorithm: true;
26
+ contentEncryptionAlgorithm: true;
27
+ key: true;
28
+ plaintext: true;
29
+ keyIdentifier: true;
30
+ agreementPartyUInfo: true;
31
+ agreementPartyVInfo: true;
32
+ type: true;
33
+ contentType: true;
34
+ compressionAlgorithm: true;
35
+ }>;
36
+ export declare const DECRYPT_JWE_FIELDS: Readonly<{
37
+ compact: true;
38
+ keyManagementAlgorithm: true;
39
+ contentEncryptionAlgorithm: true;
40
+ key: true;
41
+ headerPolicy: true;
42
+ }>;
43
+ export declare const JWT_HEADER_OPTION_FIELDS: Readonly<{
44
+ type: true;
45
+ }>;
46
+ export declare const JWT_HEADER_POLICY_FIELDS: Readonly<{
47
+ allowMissingType: true;
48
+ allowEmbeddedKeyHeaders: true;
49
+ acceptedTypes: true;
50
+ }>;
51
+ export declare const JWT_CLAIMS_POLICY_FIELDS: Readonly<{
52
+ requireExpiration: true;
53
+ requireNotBefore: true;
54
+ requireIssuedAt: true;
55
+ clockSkewSeconds: true;
56
+ maximumFutureIssuedAtSkewSeconds: true;
57
+ verificationTimeUnixSeconds: true;
58
+ expectedAudience: true;
59
+ expectedIssuer: true;
60
+ expectedSubject: true;
61
+ }>;
62
+ export declare const JWE_HEADER_POLICY_FIELDS: Readonly<{
63
+ requireKeyIdentifier: true;
64
+ expectedKeyIdentifier: true;
65
+ expectedType: true;
66
+ expectedContentType: true;
67
+ expectedAgreementPartyUInfo: true;
68
+ expectedAgreementPartyVInfo: true;
69
+ allowedCompressionAlgorithms: true;
70
+ }>;
@@ -0,0 +1,44 @@
1
+ // SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
2
+ //
3
+ // SPDX-License-Identifier: MIT OR Apache-2.0
4
+ // Keep runtime validation total over each facade view. A new interface property
5
+ // must be classified here before TypeScript will accept the boundary adapter.
6
+ export const SIGN_JWS_FIELDS = Object.freeze({
7
+ algorithm: true, privateKey: true, payload: true,
8
+ });
9
+ export const VERIFY_JWS_FIELDS = Object.freeze({
10
+ algorithm: true, compact: true, publicKey: true,
11
+ });
12
+ export const SIGN_JWT_FIELDS = Object.freeze({
13
+ claimsJson: true, jwkJson: true, privateKey: true, headerOptions: true,
14
+ });
15
+ export const VERIFY_JWT_FIELDS = Object.freeze({
16
+ compact: true, jwkJson: true, publicKey: true, headerPolicy: true,
17
+ verification: true,
18
+ });
19
+ export const ENCRYPT_JWE_FIELDS = Object.freeze({
20
+ keyManagementAlgorithm: true, contentEncryptionAlgorithm: true, key: true,
21
+ plaintext: true, keyIdentifier: true, agreementPartyUInfo: true,
22
+ agreementPartyVInfo: true, type: true, contentType: true, compressionAlgorithm: true,
23
+ });
24
+ export const DECRYPT_JWE_FIELDS = Object.freeze({
25
+ compact: true, keyManagementAlgorithm: true, contentEncryptionAlgorithm: true,
26
+ key: true, headerPolicy: true,
27
+ });
28
+ export const JWT_HEADER_OPTION_FIELDS = Object.freeze({
29
+ type: true,
30
+ });
31
+ export const JWT_HEADER_POLICY_FIELDS = Object.freeze({
32
+ allowMissingType: true, allowEmbeddedKeyHeaders: true, acceptedTypes: true,
33
+ });
34
+ export const JWT_CLAIMS_POLICY_FIELDS = Object.freeze({
35
+ requireExpiration: true, requireNotBefore: true, requireIssuedAt: true,
36
+ clockSkewSeconds: true, maximumFutureIssuedAtSkewSeconds: true,
37
+ verificationTimeUnixSeconds: true, expectedAudience: true,
38
+ expectedIssuer: true, expectedSubject: true,
39
+ });
40
+ export const JWE_HEADER_POLICY_FIELDS = Object.freeze({
41
+ requireKeyIdentifier: true, expectedKeyIdentifier: true, expectedType: true,
42
+ expectedContentType: true, expectedAgreementPartyUInfo: true,
43
+ expectedAgreementPartyVInfo: true, allowedCompressionAlgorithms: true,
44
+ });
@@ -1,9 +1,12 @@
1
1
  import { JoseJweCompressionAlgorithm } from "./proto/generated/reallyme/jose/v1/jose_pb.js";
2
2
  import type { JoseJweDecryptResponse, JoseJwsSignResponse, JoseJwsVerifyResponse, JoseJwtDecodeUnsignedResponse, JoseOperationRequest, JoseOperationResponse } from "./proto/generated/reallyme/jose/v1/jose_pb.js";
3
- import type { ReallyMeJoseJweHeaderPolicy, ReallyMeJoseJwtHeaderPolicy, ReallyMeJoseJwtTemporalPolicy } from "./facade.js";
3
+ import type { ReallyMeJoseJweHeaderPolicy, ReallyMeJoseJwtHeaderPolicy, ReallyMeJoseJwtHeaderOptions, ReallyMeJoseJwtClaimsPolicy } from "./facade.js";
4
4
  export declare const malformedProviderResponse: () => never;
5
5
  export declare const ownedBytes: (value: Uint8Array, owners: Uint8Array[]) => Uint8Array;
6
6
  export declare const optionalString: (value: string | undefined) => string;
7
+ export declare const optionalHeaderString: (value: string | undefined) => string | undefined;
8
+ export declare const jwtHeaderOptions: (options: ReallyMeJoseJwtHeaderOptions | undefined) => import("./proto/generated/reallyme/jose/v1/jose_pb.js").JoseJwtHeaderEncodeOptions | undefined;
9
+ export declare const jwtVerification: (value: import("./contract-verification.js").ReallyMeJoseJwtVerification) => import("./proto/generated/reallyme/jose/v1/jose_pb.js").JoseJwtVerificationPolicy;
7
10
  export declare const optionalBoolean: (value: boolean | undefined, fallback: boolean) => boolean;
8
11
  export declare const validateAlgorithm: (value: number, allowed: ReadonlyArray<number>) => void;
9
12
  export declare const withResponse: <T>(request: JoseOperationRequest, consume: (response: JoseOperationResponse) => T) => T;
@@ -18,8 +21,7 @@ export declare const jwtHeaderPolicy: (policy: ReallyMeJoseJwtHeaderPolicy | und
18
21
  acceptedTypValues: any[];
19
22
  } | undefined;
20
23
  export declare const jwtHeaderPolicyLength: (policy: ReturnType<typeof jwtHeaderPolicy>) => number;
21
- export declare const jwtTemporalPolicyLength: (policy: ReturnType<typeof jwtTemporalPolicy>) => number;
22
- export declare const jwtTemporalPolicy: (policy: ReallyMeJoseJwtTemporalPolicy | undefined) => {
24
+ export declare const jwtClaimsPolicy: (policy: ReallyMeJoseJwtClaimsPolicy | undefined) => {
23
25
  requireExp: boolean;
24
26
  requireNbf: boolean;
25
27
  requireIat: boolean;
@@ -1,15 +1,17 @@
1
1
  // SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
2
2
  //
3
3
  // SPDX-License-Identifier: MIT OR Apache-2.0
4
- import { fromBinary, toBinary } from "@bufbuild/protobuf";
4
+ import { create, fromBinary, toBinary } from "@bufbuild/protobuf";
5
5
  import { executeOperation } from "./boundary.js";
6
- import { ReallyMeJoseError } from "./errors.js";
7
- import { JoseErrorReason, JoseJweCompressionAlgorithm, JoseOperationContractVersion, JoseOperationRequestSchema, JoseOperationResponseSchema, } from "./proto/generated/reallyme/jose/v1/jose_pb.js";
8
- import { ensureAggregateLength, ensureBoolean, ensureBytes, ensureObject, ensureString, ensureUint64, invalidInput, utf8Length, } from "./validate.js";
9
- const MAX_JWT_ACCEPTED_TYPE_VALUES = 32;
6
+ import { ReallyMeJoseError, ReallyMeJoseSdkErrorCode } from "./errors.js";
7
+ import { JoseErrorReason, JoseJweCompressionAlgorithm, JoseJwtHeaderEncodeOptionsSchema, JoseJwtVerificationPolicySchema, JoseOperationContractVersion, JoseOperationRequestSchema, JoseOperationResponseSchema, } from "./proto/generated/reallyme/jose/v1/jose_pb.js";
8
+ import { ensureAggregateLength, ensureBoolean, ensureBytes, ensureObjectFields, ensureString, ensureUint64, invalidInput, resourceLimit, MAX_JOSE_BINARY_REQUEST_BYTES, utf8Length, } from "./validate.js";
9
+ import { JWT_HEADER_OPTION_FIELDS, JWT_HEADER_POLICY_FIELDS, JWT_CLAIMS_POLICY_FIELDS, JWE_HEADER_POLICY_FIELDS, } from "./facade-shapes.js";
10
+ import { ACCEPTED_TYPE_VALUES, STRICT_TEMPORAL_DEFAULTS, errorBranch } from "./proto/contract-values.js";
11
+ const MAX_JWT_ACCEPTED_TYPE_VALUES = ACCEPTED_TYPE_VALUES;
10
12
  const PROTOBUF_REPEATED_STRING_OVERHEAD_BYTES = 5;
11
13
  export const malformedProviderResponse = () => {
12
- throw new ReallyMeJoseError("malformed-provider-response");
14
+ throw new ReallyMeJoseError(ReallyMeJoseSdkErrorCode.MALFORMED_PROVIDER_RESPONSE);
13
15
  };
14
16
  const requireClean = (message) => {
15
17
  if (message.$unknown !== undefined && message.$unknown.length !== 0) {
@@ -31,15 +33,63 @@ export const optionalString = (value) => {
31
33
  ensureString(value);
32
34
  return value;
33
35
  };
36
+ export const optionalHeaderString = (value) => {
37
+ if (value !== undefined)
38
+ ensureString(value);
39
+ return value;
40
+ };
41
+ export const jwtHeaderOptions = (options) => {
42
+ if (options === undefined)
43
+ return undefined;
44
+ ensureObjectFields(options, JWT_HEADER_OPTION_FIELDS);
45
+ const typ = optionalHeaderString(options.type);
46
+ if (typ === "") {
47
+ throw new ReallyMeJoseError(ReallyMeJoseSdkErrorCode.JOSE_FAILURE, "primitive", JoseErrorReason.JWT_INVALID_HEADER);
48
+ }
49
+ return create(JoseJwtHeaderEncodeOptionsSchema, { typ });
50
+ };
51
+ export const jwtVerification = (value) => {
52
+ // Validate the discriminated view before reading its policy. The generated
53
+ // mode vocabulary is exactly the schema oneof, including temporal-only.
54
+ ensureObjectFields(value, { mode: true, policy: true });
55
+ if (value.mode === "signatureOnly") {
56
+ ensureObjectFields(value, { mode: true });
57
+ return create(JoseJwtVerificationPolicySchema, { mode: { case: "signatureOnly", value: {} } });
58
+ }
59
+ if (value.mode === "claims") {
60
+ const claims = jwtClaimsPolicy(value.policy);
61
+ if (claims === undefined)
62
+ return invalidInput();
63
+ return create(JoseJwtVerificationPolicySchema, { mode: { case: "claims", value: claims } });
64
+ }
65
+ if (value.mode === "temporalOnly") {
66
+ ensureObjectFields(value.policy, { requireExpiration: true, requireNotBefore: true, requireIssuedAt: true,
67
+ clockSkewSeconds: true, maximumFutureIssuedAtSkewSeconds: true, verificationTimeUnixSeconds: true });
68
+ ensureUint64(value.policy.verificationTimeUnixSeconds);
69
+ if (value.policy.verificationTimeUnixSeconds === 0n)
70
+ invalidInput();
71
+ return create(JoseJwtVerificationPolicySchema, { mode: { case: "temporalOnly", value: {
72
+ nowUnix: value.policy.verificationTimeUnixSeconds,
73
+ options: {
74
+ requireExp: optionalBoolean(value.policy.requireExpiration, STRICT_TEMPORAL_DEFAULTS.requireExp),
75
+ requireNbf: optionalBoolean(value.policy.requireNotBefore, STRICT_TEMPORAL_DEFAULTS.requireNbf),
76
+ requireIat: optionalBoolean(value.policy.requireIssuedAt, STRICT_TEMPORAL_DEFAULTS.requireIat),
77
+ clockSkewSeconds: optionalUint64(value.policy.clockSkewSeconds, STRICT_TEMPORAL_DEFAULTS.clockSkewSeconds),
78
+ maxFutureIatSkewSeconds: optionalUint64(value.policy.maximumFutureIssuedAtSkewSeconds, STRICT_TEMPORAL_DEFAULTS.maxFutureIatSkewSeconds),
79
+ },
80
+ } } });
81
+ }
82
+ return invalidInput();
83
+ };
34
84
  export const optionalBoolean = (value, fallback) => {
35
85
  if (value === undefined)
36
86
  return fallback;
37
87
  ensureBoolean(value);
38
88
  return value;
39
89
  };
40
- const optionalUint64 = (value) => {
90
+ const optionalUint64 = (value, fallback) => {
41
91
  if (value === undefined)
42
- return 0n;
92
+ return fallback;
43
93
  ensureUint64(value);
44
94
  return value;
45
95
  };
@@ -52,22 +102,19 @@ const throwJoseError = (error) => {
52
102
  if (error.error.case !== undefined)
53
103
  requireClean(error.error.value);
54
104
  const branch = error.error.case;
55
- const failWithReason = (publicBranch, reason, minimum, maximum) => {
56
- if (reason === JoseErrorReason.UNSPECIFIED ||
57
- JoseErrorReason[reason] === undefined ||
58
- reason < minimum ||
59
- reason > maximum) {
105
+ const failWithReason = (publicBranch, reason) => {
106
+ if (errorBranch(reason) !== publicBranch) {
60
107
  malformedProviderResponse();
61
108
  }
62
- throw new ReallyMeJoseError("jose-failure", publicBranch, reason);
109
+ throw new ReallyMeJoseError(ReallyMeJoseSdkErrorCode.JOSE_FAILURE, publicBranch, reason);
63
110
  };
64
111
  switch (branch) {
65
112
  case "primitive":
66
- return failWithReason("primitive", error.error.value.reason, 1, 799);
113
+ return failWithReason("primitive", error.error.value.reason);
67
114
  case "provider":
68
- return failWithReason("provider", error.error.value.reason, 800, 802);
115
+ return failWithReason("provider", error.error.value.reason);
69
116
  case "backend":
70
- return failWithReason("backend", error.error.value.reason, 900, 902);
117
+ return failWithReason("backend", error.error.value.reason);
71
118
  default:
72
119
  return malformedProviderResponse();
73
120
  }
@@ -87,7 +134,7 @@ export const withResponse = (request, consume) => {
87
134
  responseBytes = executeOperation(requestBytes);
88
135
  const response = decodeResponse(responseBytes);
89
136
  requireClean(response);
90
- if (response.contractVersion !== JoseOperationContractVersion.V1) {
137
+ if (response.contractVersion !== JoseOperationContractVersion.V2) {
91
138
  malformedProviderResponse();
92
139
  }
93
140
  if (response.response.case === undefined)
@@ -151,13 +198,13 @@ export const plaintextOutcome = (outcome) => {
151
198
  export const jwtHeaderPolicy = (policy) => {
152
199
  if (policy === undefined)
153
200
  return undefined;
154
- ensureObject(policy);
201
+ ensureObjectFields(policy, JWT_HEADER_POLICY_FIELDS);
202
+ if (policy.acceptedTypes !== undefined && !Array.isArray(policy.acceptedTypes))
203
+ invalidInput();
155
204
  if (policy.acceptedTypes !== undefined &&
156
205
  policy.acceptedTypes.length > MAX_JWT_ACCEPTED_TYPE_VALUES) {
157
- invalidInput();
206
+ resourceLimit();
158
207
  }
159
- if (policy.acceptedTypes !== undefined && !Array.isArray(policy.acceptedTypes))
160
- invalidInput();
161
208
  const acceptedTypes = policy.acceptedTypes === undefined ? [] : [...policy.acceptedTypes];
162
209
  for (const value of acceptedTypes)
163
210
  ensureString(value);
@@ -173,29 +220,19 @@ export const jwtHeaderPolicyLength = (policy) => {
173
220
  const lengths = policy.acceptedTypValues.flatMap((value) => [utf8Length(value), PROTOBUF_REPEATED_STRING_OVERHEAD_BYTES]);
174
221
  return ensureAggregateLength(...lengths);
175
222
  };
176
- export const jwtTemporalPolicyLength = (policy) => {
177
- if (policy === undefined)
178
- return 0;
179
- const lengths = [
180
- utf8Length(policy.expectedAudience),
181
- utf8Length(policy.expectedIssuerConstraint?.value ?? ""),
182
- utf8Length(policy.expectedSubjectConstraint?.value ?? ""),
183
- ];
184
- return ensureAggregateLength(...lengths);
185
- };
186
223
  const expectedClaimConstraint = (value) => {
187
224
  if (value === undefined)
188
225
  return undefined;
189
226
  ensureString(value);
190
227
  if (value.length === 0) {
191
- throw new ReallyMeJoseError("jose-failure", "primitive", JoseErrorReason.JWT_INVALID_VERIFICATION_POLICY);
228
+ throw new ReallyMeJoseError(ReallyMeJoseSdkErrorCode.JOSE_FAILURE, "primitive", JoseErrorReason.JWT_INVALID_VERIFICATION_POLICY);
192
229
  }
193
230
  return { value };
194
231
  };
195
- export const jwtTemporalPolicy = (policy) => {
232
+ export const jwtClaimsPolicy = (policy) => {
196
233
  if (policy === undefined)
197
234
  return undefined;
198
- ensureObject(policy);
235
+ ensureObjectFields(policy, JWT_CLAIMS_POLICY_FIELDS);
199
236
  ensureUint64(policy.verificationTimeUnixSeconds);
200
237
  if (policy.verificationTimeUnixSeconds === 0n)
201
238
  invalidInput();
@@ -203,11 +240,11 @@ export const jwtTemporalPolicy = (policy) => {
203
240
  if (policy.expectedAudience.length === 0)
204
241
  invalidInput();
205
242
  return {
206
- requireExp: optionalBoolean(policy.requireExpiration, true),
207
- requireNbf: optionalBoolean(policy.requireNotBefore, false),
208
- requireIat: optionalBoolean(policy.requireIssuedAt, false),
209
- clockSkewSeconds: optionalUint64(policy.clockSkewSeconds),
210
- maxFutureIatSkewSeconds: optionalUint64(policy.maximumFutureIssuedAtSkewSeconds),
243
+ requireExp: optionalBoolean(policy.requireExpiration, STRICT_TEMPORAL_DEFAULTS.requireExp),
244
+ requireNbf: optionalBoolean(policy.requireNotBefore, STRICT_TEMPORAL_DEFAULTS.requireNbf),
245
+ requireIat: optionalBoolean(policy.requireIssuedAt, STRICT_TEMPORAL_DEFAULTS.requireIat),
246
+ clockSkewSeconds: optionalUint64(policy.clockSkewSeconds, STRICT_TEMPORAL_DEFAULTS.clockSkewSeconds),
247
+ maxFutureIatSkewSeconds: optionalUint64(policy.maximumFutureIssuedAtSkewSeconds, STRICT_TEMPORAL_DEFAULTS.maxFutureIatSkewSeconds),
211
248
  nowUnix: policy.verificationTimeUnixSeconds,
212
249
  expectedAudience: policy.expectedAudience,
213
250
  expectedIssuerConstraint: expectedClaimConstraint(policy.expectedIssuer),
@@ -217,10 +254,14 @@ export const jwtTemporalPolicy = (policy) => {
217
254
  export const jweHeaderPolicy = (policy, ownedApu, ownedApv) => {
218
255
  if (policy === undefined)
219
256
  return undefined;
220
- ensureObject(policy);
221
- const configuredCompressionAlgorithms = policy.allowedCompressionAlgorithms ?? [];
257
+ ensureObjectFields(policy, JWE_HEADER_POLICY_FIELDS);
258
+ const configuredCompressionAlgorithms = policy.allowedCompressionAlgorithms === undefined ? [] : policy.allowedCompressionAlgorithms;
222
259
  if (!Array.isArray(configuredCompressionAlgorithms))
223
260
  invalidInput();
261
+ // Each packed enum needs at least one wire byte. Bound the list before
262
+ // copying it so a sparse attacker-controlled array cannot exhaust the host.
263
+ if (configuredCompressionAlgorithms.length > MAX_JOSE_BINARY_REQUEST_BYTES)
264
+ resourceLimit();
224
265
  const allowedCompressionAlgorithms = [
225
266
  ...configuredCompressionAlgorithms,
226
267
  ];
package/dist/facade.d.ts CHANGED
@@ -4,7 +4,7 @@ export type ReallyMeJoseJwtHeaderPolicy = Readonly<{
4
4
  allowEmbeddedKeyHeaders?: boolean;
5
5
  acceptedTypes?: ReadonlyArray<string>;
6
6
  }>;
7
- export type ReallyMeJoseJwtTemporalPolicy = Readonly<{
7
+ export type ReallyMeJoseJwtClaimsPolicy = Readonly<{
8
8
  requireExpiration?: boolean;
9
9
  requireNotBefore?: boolean;
10
10
  requireIssuedAt?: boolean;
@@ -38,6 +38,9 @@ export type ReallyMeJoseSignJwtOptions = Readonly<{
38
38
  claimsJson: Uint8Array;
39
39
  jwkJson: Uint8Array;
40
40
  privateKey: Uint8Array;
41
+ headerOptions?: ReallyMeJoseJwtHeaderOptions;
42
+ }>;
43
+ export type ReallyMeJoseJwtHeaderOptions = Readonly<{
41
44
  type?: string;
42
45
  }>;
43
46
  export type ReallyMeJoseVerifyJwtOptions = Readonly<{
@@ -45,8 +48,7 @@ export type ReallyMeJoseVerifyJwtOptions = Readonly<{
45
48
  jwkJson: Uint8Array;
46
49
  publicKey: Uint8Array;
47
50
  headerPolicy?: ReallyMeJoseJwtHeaderPolicy;
48
- temporalPolicy?: ReallyMeJoseJwtTemporalPolicy;
49
- signatureOnly?: boolean;
51
+ verification: import("./contract-verification.js").ReallyMeJoseJwtVerification;
50
52
  }>;
51
53
  export type ReallyMeJoseEncryptJweOptions = Readonly<{
52
54
  keyManagementAlgorithm: JoseJweKeyManagementAlgorithm;
package/dist/facade.js CHANGED
@@ -3,12 +3,13 @@
3
3
  // SPDX-License-Identifier: MIT OR Apache-2.0
4
4
  import { create } from "@bufbuild/protobuf";
5
5
  import { JoseJweCompressionAlgorithm, JoseJweContentEncryptionAlgorithm, JoseJweKeyManagementAlgorithm, JoseOperationRequestSchema, JoseSignatureAlgorithm, } from "./proto/generated/reallyme/jose/v1/jose_pb.js";
6
- import { ensureAggregateLength, ensureObject, ensureString, utf8Length, } from "./validate.js";
7
- import { claimsOutcome, compactOutcome, jweHeaderPolicy, jwtHeaderPolicy, jwtHeaderPolicyLength, jwtTemporalPolicy, jwtTemporalPolicyLength, malformedProviderResponse, optionalBoolean, optionalString, ownedBytes, plaintextOutcome, validateAlgorithm, verifyOutcome, withResponse, } from "./facade-support.js";
6
+ import { ensureAggregateLength, ensureObjectFields, ensureString, utf8Length, } from "./validate.js";
7
+ import { SIGN_JWS_FIELDS, VERIFY_JWS_FIELDS, SIGN_JWT_FIELDS, VERIFY_JWT_FIELDS, ENCRYPT_JWE_FIELDS, DECRYPT_JWE_FIELDS, JWE_HEADER_POLICY_FIELDS, } from "./facade-shapes.js";
8
+ import { claimsOutcome, compactOutcome, jweHeaderPolicy, jwtHeaderPolicy, jwtHeaderOptions, jwtHeaderPolicyLength, jwtVerification, malformedProviderResponse, optionalHeaderString, ownedBytes, plaintextOutcome, validateAlgorithm, verifyOutcome, withResponse, } from "./facade-support.js";
8
9
  export const ReallyMeJose = Object.freeze({
9
10
  signJws(options) {
10
- ensureObject(options);
11
- validateAlgorithm(options.algorithm, [JoseSignatureAlgorithm.EDDSA, JoseSignatureAlgorithm.ES256]);
11
+ ensureObjectFields(options, SIGN_JWS_FIELDS);
12
+ validateAlgorithm(options.algorithm, [JoseSignatureAlgorithm.EDDSA, JoseSignatureAlgorithm.ES256, JoseSignatureAlgorithm.ES256K]);
12
13
  const owners = [];
13
14
  try {
14
15
  const privateKey = ownedBytes(options.privateKey, owners);
@@ -30,8 +31,8 @@ export const ReallyMeJose = Object.freeze({
30
31
  }
31
32
  },
32
33
  verifyJws(options) {
33
- ensureObject(options);
34
- validateAlgorithm(options.algorithm, [JoseSignatureAlgorithm.EDDSA, JoseSignatureAlgorithm.ES256]);
34
+ ensureObjectFields(options, VERIFY_JWS_FIELDS);
35
+ validateAlgorithm(options.algorithm, [JoseSignatureAlgorithm.EDDSA, JoseSignatureAlgorithm.ES256, JoseSignatureAlgorithm.ES256K]);
35
36
  ensureString(options.compact);
36
37
  const owners = [];
37
38
  try {
@@ -90,18 +91,18 @@ export const ReallyMeJose = Object.freeze({
90
91
  });
91
92
  },
92
93
  signJwt(options) {
93
- ensureObject(options);
94
+ ensureObjectFields(options, SIGN_JWT_FIELDS);
94
95
  const owners = [];
95
96
  try {
96
97
  const claims = ownedBytes(options.claimsJson, owners);
97
98
  const jwk = ownedBytes(options.jwkJson, owners);
98
99
  const privateKey = ownedBytes(options.privateKey, owners);
99
- const type = optionalString(options.type);
100
- ensureAggregateLength(claims.length, jwk.length, privateKey.length, utf8Length(type));
100
+ const headerOptions = jwtHeaderOptions(options.headerOptions);
101
+ ensureAggregateLength(claims.length, jwk.length, privateKey.length, utf8Length(headerOptions?.typ ?? ""));
101
102
  const request = create(JoseOperationRequestSchema, {
102
103
  operation: {
103
104
  case: "jwtSign",
104
- value: { claimsJson: claims, jwkJson: jwk, privateKey, typ: type },
105
+ value: { claimsJson: claims, jwkJson: jwk, privateKey, headerOptions },
105
106
  },
106
107
  });
107
108
  return withResponse(request, (response) => {
@@ -117,15 +118,17 @@ export const ReallyMeJose = Object.freeze({
117
118
  }
118
119
  },
119
120
  verifyJwt(options) {
120
- ensureObject(options);
121
+ ensureObjectFields(options, VERIFY_JWT_FIELDS);
121
122
  ensureString(options.compact);
122
123
  const owners = [];
123
124
  try {
124
125
  const jwk = ownedBytes(options.jwkJson, owners);
125
126
  const publicKey = ownedBytes(options.publicKey, owners);
126
127
  const headerPolicy = jwtHeaderPolicy(options.headerPolicy);
127
- const temporalPolicy = jwtTemporalPolicy(options.temporalPolicy);
128
- ensureAggregateLength(utf8Length(options.compact), jwk.length, publicKey.length, jwtHeaderPolicyLength(headerPolicy), jwtTemporalPolicyLength(temporalPolicy));
128
+ const verification = jwtVerification(options.verification);
129
+ ensureAggregateLength(utf8Length(options.compact), jwk.length, publicKey.length, jwtHeaderPolicyLength(headerPolicy), verification.mode.case === "claims" ? utf8Length(verification.mode.value.expectedAudience) +
130
+ utf8Length(verification.mode.value.expectedIssuerConstraint?.value ?? "") +
131
+ utf8Length(verification.mode.value.expectedSubjectConstraint?.value ?? "") : 0);
129
132
  const request = create(JoseOperationRequestSchema, {
130
133
  operation: {
131
134
  case: "jwtVerify",
@@ -134,8 +137,7 @@ export const ReallyMeJose = Object.freeze({
134
137
  jwkJson: jwk,
135
138
  publicKey,
136
139
  headerPolicy,
137
- temporalPolicy,
138
- signatureOnly: optionalBoolean(options.signatureOnly, false),
140
+ verification,
139
141
  },
140
142
  },
141
143
  });
@@ -152,7 +154,7 @@ export const ReallyMeJose = Object.freeze({
152
154
  }
153
155
  },
154
156
  encryptJwe(options) {
155
- ensureObject(options);
157
+ ensureObjectFields(options, ENCRYPT_JWE_FIELDS);
156
158
  validateAlgorithm(options.keyManagementAlgorithm, [
157
159
  JoseJweKeyManagementAlgorithm.DIRECT,
158
160
  JoseJweKeyManagementAlgorithm.ECDH_ES_P256,
@@ -171,12 +173,12 @@ export const ReallyMeJose = Object.freeze({
171
173
  try {
172
174
  const key = ownedBytes(options.key, owners);
173
175
  const plaintext = ownedBytes(options.plaintext, owners);
174
- const apu = ownedBytes(options.agreementPartyUInfo === undefined ? new Uint8Array() : options.agreementPartyUInfo, owners);
175
- const apv = ownedBytes(options.agreementPartyVInfo === undefined ? new Uint8Array() : options.agreementPartyVInfo, owners);
176
- const keyIdentifier = optionalString(options.keyIdentifier);
177
- const type = optionalString(options.type);
178
- const contentType = optionalString(options.contentType);
179
- ensureAggregateLength(key.length, plaintext.length, apu.length, apv.length, utf8Length(keyIdentifier), utf8Length(type), utf8Length(contentType));
176
+ const apu = options.agreementPartyUInfo === undefined ? undefined : ownedBytes(options.agreementPartyUInfo, owners);
177
+ const apv = options.agreementPartyVInfo === undefined ? undefined : ownedBytes(options.agreementPartyVInfo, owners);
178
+ const keyIdentifier = optionalHeaderString(options.keyIdentifier);
179
+ const type = optionalHeaderString(options.type);
180
+ const contentType = optionalHeaderString(options.contentType);
181
+ ensureAggregateLength(key.length, plaintext.length, apu?.length ?? 0, apv?.length ?? 0, utf8Length(keyIdentifier ?? ""), utf8Length(type ?? ""), utf8Length(contentType ?? ""));
180
182
  const request = create(JoseOperationRequestSchema, {
181
183
  operation: {
182
184
  case: "jweEncrypt",
@@ -208,10 +210,10 @@ export const ReallyMeJose = Object.freeze({
208
210
  }
209
211
  },
210
212
  decryptJwe(options) {
211
- ensureObject(options);
213
+ ensureObjectFields(options, DECRYPT_JWE_FIELDS);
212
214
  ensureString(options.compact);
213
215
  if (options.headerPolicy !== undefined)
214
- ensureObject(options.headerPolicy);
216
+ ensureObjectFields(options.headerPolicy, JWE_HEADER_POLICY_FIELDS);
215
217
  validateAlgorithm(options.keyManagementAlgorithm, [
216
218
  JoseJweKeyManagementAlgorithm.DIRECT,
217
219
  JoseJweKeyManagementAlgorithm.ECDH_ES_P256,