-
Notifications
You must be signed in to change notification settings - Fork 119
feat: Implementation of AgentCardSignature feature #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
guglielmo-san
wants to merge
9
commits into
a2aproject:main
Choose a base branch
from
guglielmo-san:guglielmoc/implement_signature_verifier_client_side
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
669cac1
wip
guglielmo-san 1a37954
wip
guglielmo-san 8a2de36
fix lint error
guglielmo-san 99b8da0
revert package-lock
guglielmo-san 031e70b
regenerate package-lock
guglielmo-san a363349
use flattenedJws instead of CompactSing
guglielmo-san 407de07
improve signature function
guglielmo-san a82b4a4
run linter
guglielmo-san b0b27a8
fix typo
guglielmo-san File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { AgentCard, AgentCardSignature } from './types.js'; | ||
| import * as jose from 'jose'; | ||
|
|
||
| export type AgentCardSignatureGenerator = (agentCard: AgentCard) => Promise<AgentCard>; | ||
|
|
||
| export function generateAgentCardSignature( | ||
| privateKey: jose.CryptoKey | jose.KeyObject | jose.JWK, | ||
| protectedHeader: jose.JWSHeaderParameters, | ||
| header?: jose.JWSHeaderParameters | ||
| ): AgentCardSignatureGenerator { | ||
| return async (agentCard: AgentCard): Promise<AgentCard> => { | ||
| const cardCopy = JSON.parse(JSON.stringify(agentCard)); | ||
| delete cardCopy.signatures; | ||
| const canonicalPayload = canonicalizeAgentCard(cardCopy); | ||
|
|
||
| const jws = await new jose.FlattenedSign(new TextEncoder().encode(canonicalPayload)) | ||
| .setProtectedHeader(protectedHeader) | ||
| .setUnprotectedHeader(header) | ||
| .sign(privateKey); | ||
|
|
||
| const agentCardSignature: AgentCardSignature = { | ||
| protected: jws.protected, | ||
| signature: jws.signature, | ||
| header: jws.header, | ||
| }; | ||
|
|
||
| if (!agentCard.signatures) agentCard.signatures = []; | ||
| agentCard.signatures.push(agentCardSignature); | ||
|
|
||
| return agentCard; | ||
| }; | ||
| } | ||
|
|
||
| export type AgentCardSignatureVerifier = (agentCard: AgentCard) => Promise<void>; | ||
|
|
||
| export function verifyAgentCardSignature( | ||
| retrievePublicKey: ( | ||
| kid: string, | ||
| jku?: string | ||
| ) => Promise<jose.CryptoKey | jose.KeyObject | jose.JWK> | ||
| ): AgentCardSignatureVerifier { | ||
| return async (agentCard: AgentCard): Promise<void> => { | ||
| if (!agentCard.signatures?.length) { | ||
| throw new Error('No signatures found on agent card to verify.'); | ||
| } | ||
| const cardCopy = JSON.parse(JSON.stringify(agentCard)); | ||
| delete cardCopy.signatures; | ||
| const canonicalPayload = canonicalizeAgentCard(cardCopy); | ||
| const payloadBytes = new TextEncoder().encode(canonicalPayload); | ||
| const encodedPayload = jose.base64url.encode(payloadBytes); | ||
|
|
||
| for (const signatureEntry of agentCard.signatures) { | ||
| try { | ||
| const protectedHeader = jose.decodeProtectedHeader(signatureEntry); | ||
| if (!protectedHeader.kid || !protectedHeader.typ || !protectedHeader.alg) { | ||
| throw new Error('Missing required header parameters (kid, typ, alg)'); | ||
| } | ||
| const publicKey = await retrievePublicKey(protectedHeader.kid, protectedHeader.jku); | ||
| const jws: jose.FlattenedJWS = { | ||
| payload: encodedPayload, | ||
| protected: signatureEntry.protected, | ||
| signature: signatureEntry.signature, | ||
| header: signatureEntry.header, | ||
| }; | ||
| await jose.flattenedVerify(jws, publicKey); | ||
| return; | ||
| } catch (error) { | ||
| console.warn('Signature verification failed:', error); | ||
| } | ||
| } | ||
| throw new Error('No valid signatures found on agent card.'); | ||
| }; | ||
| } | ||
|
|
||
| function cleanEmpty(d: unknown): unknown { | ||
| if (d === '' || d === null || d === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| if (Array.isArray(d)) { | ||
| const cleanedList = d.map((v) => cleanEmpty(v)).filter((v) => v !== null); | ||
| return cleanedList.length > 0 ? cleanedList : null; | ||
| } | ||
|
|
||
| if (typeof d === 'object') { | ||
| if (d instanceof Date) return d; | ||
| const cleanedDict: { [key: string]: unknown } = {}; | ||
| for (const [key, v] of Object.entries(d)) { | ||
| const cleanedValue = cleanEmpty(v); | ||
| if (cleanedValue !== null) { | ||
| cleanedDict[key] = cleanedValue; | ||
| } | ||
| } | ||
| return Object.keys(cleanedDict).length > 0 ? cleanedDict : null; | ||
| } | ||
|
|
||
| return d; | ||
| } | ||
|
|
||
| /** | ||
| * JCS Canonicalization (RFC 8785) | ||
| * Sorts keys recursively and serializes to string. | ||
| */ | ||
| function jcsStringify(value: unknown): string { | ||
| if (value === null || typeof value !== 'object') { | ||
| return JSON.stringify(value); | ||
| } | ||
|
|
||
| if (Array.isArray(value)) { | ||
| return '[' + value.map((item) => jcsStringify(item)).join(',') + ']'; | ||
| } | ||
|
|
||
| const record = value as Record<string, unknown>; | ||
| const keys = Object.keys(record).sort(); | ||
| const parts = keys.map((key) => { | ||
| return `${JSON.stringify(key)}:${jcsStringify(record[key])}`; | ||
| }); | ||
|
|
||
| return '{' + parts.join(',') + '}'; | ||
| } | ||
|
|
||
| export function canonicalizeAgentCard(agentCard: Omit<AgentCard, 'signatures'>): string { | ||
| const cleaned = cleanEmpty(agentCard); | ||
| if (!cleaned) { | ||
| return '{}'; | ||
| } | ||
|
|
||
| return jcsStringify(cleaned); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of logging verification failures to the console with
console.warn, it would be more helpful for the library consumer if you collect all verification errors. Then, if no valid signatures are found, you can throw an error that includes the details of why each signature verification failed. This provides better debugging information without polluting the console.You would need to declare an array for errors before the loop, push errors in this
catchblock, and then use that array in the finalthrowstatement after the loop.