@tinycloud/sdk-core 3.0.0-beta.18 → 3.0.0-beta.19

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.
@@ -3898,6 +3898,7 @@ var CompactPayloadSchema = z4.object({
3898
3898
  nnc: z4.string().min(1),
3899
3899
  prf: z4.array(z4.string().min(1))
3900
3900
  }).strict();
3901
+ var MAX_POLICY_ROOT_SECONDS = 31 * 24 * 60 * 60;
3901
3902
  function createCompactPolicyInvocation(input) {
3902
3903
  const session = parsePolicySessionUcan(input.sessionAuthorization);
3903
3904
  if (session.cid !== input.sessionCid || session.aud !== input.recipientDid)
@@ -3929,7 +3930,7 @@ function createCompactPolicyInvocation(input) {
3929
3930
  const signature = ed255192.sign(signingInput, input.privateKey);
3930
3931
  return parseCompactUcanAuthorization(`${protectedSegment}.${payloadSegment}.${encodeBase64Url(signature)}`);
3931
3932
  }
3932
- function createCompactPolicyDescendant(input) {
3933
+ function compactPolicyDescendantClaims(input) {
3933
3934
  const parent = parseCompactUcanAuthorization(input.parentAuthorization, input.parentCid);
3934
3935
  if (parent.payload.aud !== input.issuerDid) throw new Error("policy descendant issuer is not the parent audience");
3935
3936
  const parentFact = parent.payload.fct[0];
@@ -3937,23 +3938,33 @@ function createCompactPolicyDescendant(input) {
3937
3938
  if (!Number.isInteger(depth) || depth <= 0) throw new Error("policy descendant depth is exhausted");
3938
3939
  if (!compactAttenuationContains(parent.payload.att, input.attenuation))
3939
3940
  throw new Error("policy descendant exceeds the parent attenuation");
3941
+ const now = Math.max(input.now ?? parent.payload.nbf + 1, parent.payload.nbf + 1);
3942
+ const exp = Math.min(input.expiresAt ?? parent.payload.exp - 1, parent.payload.exp - 1);
3943
+ if (exp <= now) throw new Error("policy parent has no strictly narrower validity window");
3944
+ return {
3945
+ parentCid: parent.cid,
3946
+ fact: { ...parentFact, remainingRedelegationDepth: depth - 1 },
3947
+ notBefore: now,
3948
+ expiresAt: exp,
3949
+ nonce: input.nonce ?? encodeBase64Url(crypto.getRandomValues(new Uint8Array(16)))
3950
+ };
3951
+ }
3952
+ function createCompactPolicyDescendant(input) {
3953
+ const claims = compactPolicyDescendantClaims(input);
3940
3954
  const principal = input.issuerDid.split("#", 1)[0];
3941
3955
  const didMaterial = base58btc.decode(principal.slice("did:key:".length));
3942
3956
  const publicKey = ed255192.getPublicKey(input.privateKey);
3943
3957
  if (didMaterial.length !== 34 || !equalBytes2(publicKey, didMaterial.slice(2))) throw new Error("policy descendant signer does not bind issuer DID");
3944
- const now = Math.max(input.now ?? Math.floor(Date.now() / 1e3), parent.payload.nbf + 1);
3945
- const exp = Math.min(input.expiresAt ?? now + 60, now + 60, parent.payload.exp - 1);
3946
- if (exp <= now) throw new Error("policy parent has no strictly narrower validity window");
3947
3958
  const header = { alg: "EdDSA", jwk: { alg: "EdDSA", crv: "Ed25519", kty: "OKP", x: encodeBase64Url(publicKey) }, typ: "JWT", ucv: "0.10.0" };
3948
3959
  const payload = {
3949
3960
  att: input.attenuation,
3950
3961
  aud: input.audienceDid,
3951
- exp,
3952
- fct: [{ ...parentFact, remainingRedelegationDepth: depth - 1 }],
3962
+ exp: claims.expiresAt,
3963
+ fct: [claims.fact],
3953
3964
  iss: input.issuerDid.includes("#") ? input.issuerDid : `${principal}#${principal.slice("did:key:".length)}`,
3954
- nbf: now,
3955
- nnc: input.nonce ?? encodeBase64Url(crypto.getRandomValues(new Uint8Array(16))),
3956
- prf: [parent.cid]
3965
+ nbf: claims.notBefore,
3966
+ nnc: claims.nonce,
3967
+ prf: [claims.parentCid]
3957
3968
  };
3958
3969
  const protectedSegment = encodeBase64Url(new TextEncoder().encode(jcsCanonicalize(header)));
3959
3970
  const payloadSegment = encodeBase64Url(new TextEncoder().encode(jcsCanonicalize(payload)));
@@ -4029,7 +4040,7 @@ function parsePolicySessionUcan(authorization, expectedProofs) {
4029
4040
  throw new Error("policy session UCAN fact is incomplete");
4030
4041
  if (hasV4AuditFactShape && (!LOWER_SHA256_HEX.test(fact.credentialIdAuditDigestHex) || !LOWER_SHA256_HEX.test(fact.presentationJtiAuditDigestHex)))
4031
4042
  throw new Error("policy session UCAN v4 audit facts are invalid");
4032
- if (fact.profile !== POLICY_SESSION_UCAN_V1_PROFILE || typeof fact.remainingRedelegationDepth !== "number" || !Number.isInteger(fact.remainingRedelegationDepth) || fact.remainingRedelegationDepth < 0 || fact.remainingRedelegationDepth > 8 || parsed.payload.exp - parsed.payload.nbf > 60 || parsed.payload.iss.split("#", 1)[0] !== fact.nodeAudience || parsed.payload.aud !== fact.recipientDid)
4043
+ if (fact.profile !== POLICY_SESSION_UCAN_V1_PROFILE || typeof fact.remainingRedelegationDepth !== "number" || !Number.isInteger(fact.remainingRedelegationDepth) || fact.remainingRedelegationDepth < 0 || fact.remainingRedelegationDepth > 8 || parsed.payload.exp - parsed.payload.nbf > MAX_POLICY_ROOT_SECONDS || parsed.payload.iss.split("#", 1)[0] !== fact.nodeAudience || parsed.payload.aud !== fact.recipientDid)
4033
4044
  throw new Error("policy session UCAN fact is invalid");
4034
4045
  return {
4035
4046
  authorization,