Skip to content

Commit 82d33f1

Browse files
committed
feat(validateDelegationAuthSig): add function to validate delegation auth signature and check expiration
1 parent 1d2dbe0 commit 82d33f1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { AuthSig, LitResourceAbilityRequest } from '@lit-protocol/types';
2+
3+
/**
4+
* Validates that the provided delegation auth sig hasn't expired and
5+
* references the expected session key URI.
6+
* Throws an error if validation fails.
7+
*/
8+
export function validateDelegationAuthSig(params: {
9+
delegationAuthSig: AuthSig;
10+
requiredResources: LitResourceAbilityRequest[];
11+
sessionKeyUri: string;
12+
}) {
13+
const { delegationAuthSig, sessionKeyUri } = params;
14+
15+
try {
16+
const siweMessage = delegationAuthSig.signedMessage;
17+
18+
// Check expiration if it exists in the SIWE message
19+
const expirationMatch = siweMessage.match(/^Expiration Time: (.*)$/m);
20+
if (expirationMatch?.[1]) {
21+
const expiration = new Date(expirationMatch[1].trim());
22+
if (Number.isNaN(expiration.getTime())) {
23+
throw new Error(
24+
'Delegation signature contains an invalid expiration timestamp'
25+
);
26+
}
27+
if (expiration.getTime() <= Date.now()) {
28+
throw new Error(
29+
`Delegation signature has expired at ${expiration.toISOString()}`
30+
);
31+
}
32+
}
33+
34+
// Validate session key URI matches
35+
if (!siweMessage.includes(sessionKeyUri)) {
36+
throw new Error(
37+
'Session key URI in delegation signature does not match provided session key pair'
38+
);
39+
}
40+
41+
// TODO: Add resource validation by parsing the RECAP URN when available.
42+
} catch (error) {
43+
throw new Error(
44+
`Invalid delegation signature: ${
45+
error instanceof Error ? error.message : 'Unknown error'
46+
}`
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)