-
Notifications
You must be signed in to change notification settings - Fork 2
Testing PEX proof requets with delegation #446
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf80555
testing proof requests with delegation
maycon-mello 4302cec
remove commented code
maycon-mello e3f9f70
fixing merge issues
maycon-mello da118cc
function to checkPresentationHolder
maycon-mello a64d2f0
updating tests and adding documentation
maycon-mello f92a729
add cedar reference
maycon-mello 8cd549b
updating doc
maycon-mello 0fcee24
Merge branch 'master' into feat/test-cedar-policies
maycon-mello 94be8ed
fixing tests
maycon-mello 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
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,351 @@ | ||
| # Delegatable Credentials | ||
|
|
||
| Delegatable credentials enable a credential issuer (delegator) to delegate their authority to issue specific types of credentials to other entities (delegees). This creates a chain of trust where delegees can issue credentials on behalf of the original issuer, within the bounds of their delegated authority. | ||
|
|
||
| ## Overview | ||
|
|
||
| A delegatable credential allows an issuer to: | ||
| - Grant specific issuance authorities to delegates | ||
| - Control which claims delegates can issue | ||
| - Establish delegation chains with multiple levels | ||
| - Enforce policies using Cedar policy language | ||
| - Verify the entire delegation chain during presentation | ||
|
|
||
| ## Key Concepts | ||
|
|
||
| ### Delegation Chain | ||
|
|
||
| A delegation chain consists of: | ||
| - **Root Credential**: The original delegation issued by the authoritative issuer | ||
| - **Delegated Credentials**: Credentials issued by delegates using their delegated authority | ||
| - **Chain Verification**: Each credential in the chain references its predecessor via `previousCredentialId` and the root via `rootCredentialId` | ||
|
|
||
| ### May Claim | ||
|
|
||
| The `mayClaim` property (IRI: `https://www.dock.io/rdf2020#mayClaim`) specifies which claims a delegate is authorized to issue. For example: | ||
|
|
||
| ```javascript | ||
| credentialSubject: { | ||
| id: 'did:example:delegate', | ||
| creditScore: 760, | ||
| [MAY_CLAIM_IRI]: ['creditScore'], // Delegate can only issue creditScore claims | ||
| } | ||
| ``` | ||
|
|
||
| ### Cedar Policies | ||
|
|
||
| [Cedar](https://docs.cedarpolicy.com/) is a language for defining permissions as policies. Cedar policies provide fine-grained authorization control over delegation chains. They can enforce: | ||
| - Maximum delegation depth | ||
| - Required root issuer | ||
| - Minimum claim values | ||
| - Specific claim values | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### Core Functions | ||
|
|
||
| #### `issueDelegationCredential(keyDoc, options)` | ||
|
|
||
| Issues a root delegation credential that grants authority to a delegate. | ||
|
|
||
| **Parameters:** | ||
| - `keyDoc`: Signing key document of the issuer | ||
| - `options`: | ||
| - `id`: Unique credential identifier | ||
| - `issuer`: DID of the issuer | ||
| - `@context`: JSON-LD context including delegation terms | ||
| - `issuanceDate`: ISO timestamp | ||
| - `type`: Array including 'DelegationCredential' | ||
| - `credentialSubject`: Object containing: | ||
| - `id`: DID of the delegate | ||
| - `[MAY_CLAIM_IRI]`: Array of authorized claim names | ||
| - Additional claims | ||
| - `previousCredentialId`: Should be `null` for root credentials | ||
| - `rootCredentialId`: Should be `null` for root credentials | ||
|
|
||
| **Returns:** Signed delegation credential | ||
|
|
||
| **Example:** | ||
| ```javascript | ||
| const rootCredential = await issueDelegationCredential(issuerKey, { | ||
| id: 'urn:uuid:12345678', | ||
| issuer: issuerDid, | ||
| '@context': CREDIT_SCORE_DELEGATION_CONTEXT, | ||
| issuanceDate: new Date().toISOString(), | ||
| type: [ | ||
| 'VerifiableCredential', | ||
| 'CreditScoreCredential', | ||
| 'DelegationCredential', | ||
| ], | ||
| credentialSubject: { | ||
| id: holderDid, | ||
| creditScore: 760, | ||
| [MAY_CLAIM_IRI]: ['creditScore'], | ||
| }, | ||
| previousCredentialId: null, | ||
| rootCredentialId: null, | ||
| }); | ||
| ``` | ||
|
|
||
| #### `issueDelegatedCredential(keyDoc, options)` | ||
|
|
||
| Issues a credential using delegated authority. | ||
|
|
||
| **Parameters:** | ||
| - `keyDoc`: Signing key document of the delegate | ||
| - `options`: | ||
| - `id`: Unique credential identifier | ||
| - `issuer`: DID of the delegate issuer | ||
| - `@context`: JSON-LD context | ||
| - `issuanceDate`: ISO timestamp | ||
| - `type`: Array of credential types | ||
| - `credentialSubject`: Object containing: | ||
| - `id`: DID of the credential subject | ||
| - Claims being issued | ||
| - `[MAY_CLAIM_IRI]` (optional): For further delegation | ||
| - `previousCredentialId`: ID of the delegating credential | ||
| - `rootCredentialId`: ID of the root credential | ||
|
|
||
| **Returns:** Signed delegated credential | ||
|
|
||
| **Example:** | ||
| ```javascript | ||
| const delegatedCredential = await issueDelegatedCredential(holderKey, { | ||
| id: 'urn:uuid:87654321', | ||
| issuer: holderDid, | ||
| '@context': CREDIT_SCORE_CREDENTIAL_CONTEXT, | ||
| issuanceDate: new Date().toISOString(), | ||
| type: [ | ||
| 'VerifiableCredential', | ||
| 'CreditScoreCredential', | ||
| 'DelegationCredential', | ||
| ], | ||
| credentialSubject: { | ||
| id: agentDid, | ||
| creditScore: 400, | ||
| [MAY_CLAIM_IRI]: ['creditScore'], // For further delegation | ||
| }, | ||
| previousCredentialId: rootCredential.id, | ||
| rootCredentialId: rootCredential.id, | ||
| }); | ||
| ``` | ||
|
|
||
| #### `createSignedPresentation(keyDoc, options)` | ||
|
|
||
| Creates a signed verifiable presentation containing delegated credentials. | ||
|
|
||
| **Parameters:** | ||
| - `keyDoc`: Signing key document of the holder | ||
| - `options`: | ||
| - `credentials`: Array of credentials (must include full delegation chain) | ||
| - `holderDid`: DID of the presentation holder | ||
| - `challenge`: Challenge string for replay protection | ||
| - `domain`: Domain string for binding | ||
|
|
||
| **Returns:** Signed verifiable presentation | ||
|
|
||
| **Example:** | ||
| ```javascript | ||
| const presentation = await createSignedPresentation(agentKey, { | ||
| credentials: [rootCredential, credDelegatedToAgent], | ||
| holderDid: agentDid, | ||
| challenge: 'test-challenge-123', | ||
| domain: 'test.example.com', | ||
| }); | ||
| ``` | ||
|
|
||
| #### `verifyDelegatablePresentation(presentation, options)` | ||
|
|
||
| Verifies a presentation containing delegated credentials. | ||
|
|
||
| **Parameters:** | ||
| - `presentation`: The verifiable presentation to verify | ||
| - `options`: | ||
| - `challenge`: Expected challenge string | ||
| - `domain`: Expected domain string | ||
| - `policies` (optional): Cedar policies object | ||
|
|
||
| **Returns:** Verification result object containing: | ||
| - `verified`: Boolean indicating overall verification status | ||
| - `delegationResult`: Delegation verification result with: | ||
| - `decision`: 'allow' or 'deny' | ||
| - `summaries`: Array of delegation chain summaries | ||
| - `failures`: Array of failure objects (if denied) | ||
| - `credentialResults`: Array of individual credential verification results | ||
|
|
||
| **Example:** | ||
| ```javascript | ||
| const result = await verifyDelegatablePresentation(presentation, { | ||
| challenge: CHALLENGE, | ||
| domain: DOMAIN, | ||
| policies, | ||
| }); | ||
|
|
||
| console.log('Verified:', result.verified); | ||
| console.log('Decision:', result.delegationResult?.decision); | ||
| ``` | ||
|
|
||
| #### `createCedarPolicy(options)` | ||
|
|
||
| Creates Cedar policy rules for delegation authorization. | ||
|
|
||
| **Parameters:** | ||
| - `options`: | ||
| - `maxDepth`: Maximum allowed delegation chain depth | ||
| - `rootIssuer`: Required DID of the root issuer | ||
| - `requiredClaims`: Object mapping claim names to required values: | ||
| - Number values: minimum required value (e.g., `creditScore: 500`) | ||
| - String values: exact required value (e.g., `role: "admin"`) | ||
| - Any value: description of the claim requirement | ||
|
|
||
| **Returns:** Policy object with `staticPolicies` string | ||
|
|
||
| **Example:** | ||
| ```javascript | ||
| const policies = createCedarPolicy({ | ||
| maxDepth: 2, | ||
| rootIssuer: 'did:example:root-issuer', | ||
| requiredClaims: { | ||
| creditScore: 500, // Minimum value | ||
| role: 'admin', // Exact match | ||
| body: 'Issuer of Credits', // Description | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Context Constants | ||
|
|
||
| #### `MAY_CLAIM_IRI` | ||
| The IRI used for the `mayClaim` property in credential subjects. | ||
| ```javascript | ||
| const MAY_CLAIM_IRI = 'https://www.dock.io/rdf2020#mayClaim'; | ||
| ``` | ||
|
|
||
| #### `DELEGATION_CONTEXT_TERMS` | ||
| Base JSON-LD terms for delegation credentials. | ||
|
|
||
| #### `W3C_CREDENTIALS_V1` | ||
| Standard W3C Verifiable Credentials context URL. | ||
|
|
||
|
|
||
| ## Integration with Presentation Exchange (PEX) | ||
|
|
||
| Delegatable credentials can be used with Presentation Exchange to request specific delegation chains: | ||
|
|
||
| ```javascript | ||
| const presentationDefinition = { | ||
| id: 'delegation_test', | ||
| input_descriptors: [ | ||
| { | ||
| id: 'root-credential', | ||
| name: 'Root Credential', | ||
| purpose: 'Must be the root credential issued by the required issuer', | ||
| group: ['1'], | ||
| constraints: { | ||
| fields: [ | ||
| { | ||
| path: ['$.type'], | ||
| filter: { | ||
| type: 'array', | ||
| contains: { const: 'CreditScoreCredential' }, | ||
| }, | ||
| }, | ||
| { | ||
| path: ['$.issuer', '$.iss'], | ||
| filter: { | ||
| type: 'string', | ||
| const: issuerDid, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| { | ||
| id: 'other-credentials', | ||
| name: 'Additional Credentials', | ||
| purpose: 'Any number of additional credentials of the specified type', | ||
| group: ['2'], | ||
| constraints: { | ||
| fields: [ | ||
| { | ||
| path: ['$.type'], | ||
| filter: { | ||
| type: 'array', | ||
| contains: { const: 'CreditScoreCredential' }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| submission_requirements: [ | ||
| { | ||
| from: '1', | ||
| name: 'Root Credential', | ||
| rule: 'pick', | ||
| count: 1, | ||
| }, | ||
| { | ||
| from: '2', | ||
| name: 'Additional Credentials', | ||
| rule: 'pick', | ||
| min: 0, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| // Filter credentials that match the definition | ||
| const filterResult = await credentialService.filterCredentials({ | ||
| credentials: [rootCredential, credDelegatedToAgent], | ||
| presentationDefinition, | ||
| holderDid: agentDid, | ||
| }); | ||
|
|
||
| // Create presentation | ||
| const presentation = await createSignedPresentation(agentKey, { | ||
| credentials: [rootCredential, credDelegatedToAgent], | ||
| holderDid: agentDid, | ||
| challenge: CHALLENGE, | ||
| domain: DOMAIN, | ||
| }); | ||
|
|
||
| // Evaluate presentation against definition | ||
| const validationResults = await pexService.evaluatePresentation({ | ||
| presentation, | ||
| presentationDefinition, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Best Practices | ||
|
|
||
| 1. **Always Include Full Chain**: When creating presentations with delegated credentials, include the complete delegation chain from root to leaf. | ||
|
|
||
| 2. **Use Specific mayClaim Values**: Be explicit about which claims a delegate can issue. Avoid overly broad delegations. | ||
|
|
||
| 3. **Implement Cedar Policies**: Use Cedar policies to enforce business rules like maximum delegation depth and claim requirements. | ||
|
|
||
| 4. **Handle Verification Failures**: Check both the overall `verified` status and the `delegationResult.decision` to understand why verification failed. | ||
|
|
||
|
|
||
| ## Testing | ||
|
|
||
| See `integration-tests/delegatable-credentials.test.ts` for comprehensive test examples including: | ||
|
|
||
| - Issuing valid delegation credentials | ||
| - Issuing delegated credentials with proper chain references | ||
| - Creating and verifying presentations with delegation chains | ||
| - Enforcing authorization with Cedar policies | ||
| - Handling unauthorized delegations | ||
| - Multi-level delegation chains | ||
| - Integration with Presentation Exchange | ||
|
|
||
| Run the tests with: | ||
| ```bash | ||
| npm test integration-tests/delegatable-credentials.test.ts | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| - [W3C Verifiable Credentials](https://www.w3.org/TR/vc-data-model/) | ||
| - [Cedar Policy Language](https://www.cedarpolicy.com/) | ||
| - [Presentation Exchange](https://identity.foundation/presentation-exchange/) |
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.
Uh oh!
There was an error while loading. Please reload this page.