A pure Python implementation of ZCAP-LD (Authorization Capabilities for Linked Data)
Project description
zcap - Python ZCAP-LD Implementation
A pure Python implementation of ZCAP-LD (Authorization Capabilities for Linked Data) for decentralized indentity and access control. This library provides a complete implementation of capability-based access control using the ZCAP-LD specification.
⚠️ WARNING ⚠️
zcap is currently in early development and is not suitable for production use. The library has not yet undergone an independent security review or audit.
Use at your own risk for experimental or research purposes only.
Features
-
Capability Creation: Generate JSON-LD capabilities with full structure:
- Controller and invoker identification
- Allowed actions with parameters
- Target resource specification
- Cryptographic proofs
- Expiration and caveats
-
Delegation: Chain capabilities via delegation with verifiable cryptographic proofs
- Subset of parent actions
- Additional caveats
- Proof chain validation
-
Invocation: Secure invocation flow with capability verification
- Action validation
- Proof verification
- Caveat enforcement
-
Revocation: In-memory tracking of revoked capabilities
- Immediate effect on delegation chain
- Prevents use of revoked capabilities
Installation
pip install zcap
Quick Start
from zcap import create_capability, delegate_capability, invoke_capability
from cryptography.hazmat.primitives.asymmetric import ed25519
# Generate keys
alice_key = ed25519.Ed25519PrivateKey.generate()
bob_key = ed25519.Ed25519PrivateKey.generate()
# Create a capability
capability = create_capability(
controller="did:example:alice",
invoker="did:example:bob",
actions=[{"name": "read"}],
target={
"id": "https://example.com/resource/123",
"type": "Document"
},
controller_key=alice_key
)
# Delegate the capability
delegated = delegate_capability(
parent_capability=capability,
delegator_key=bob_key,
new_invoker="did:example:charlie",
actions=[{"name": "read"}]
)
# Invoke the capability
success = invoke_capability(
capability=delegated,
action="read",
invoker_key=charlie_key
)
Core Concepts
Capabilities
A capability is a token that grants specific permissions to access a resource. It contains:
- Controller: The entity that created the capability
- Invoker: The entity allowed to use the capability
- Actions: The allowed operations
- Target: The resource the capability applies to
- Proof: Cryptographic proof of authenticity
- Caveats: Additional constraints
Delegation
Capabilities can be delegated, creating a chain of trust:
- A capability holder can delegate a subset of their permissions
- Each delegation adds to the proof chain
- Delegated capabilities can add more restrictive caveats
- Revocation affects the entire delegation chain
Cryptographic Proofs
The library uses Ed25519 signatures for capability proofs:
- Capabilities are signed by their controller
- Delegations are signed by the delegator
- Invocations verify the entire proof chain
- JSON-LD normalization ensures consistent signing
Caveats
Caveats are constraints that limit when and how a capability can be used. They are a powerful mechanism for fine-grained authorization control:
- Evaluation Time: Caveats are evaluated during both verification and invocation
- Delegation Chain: All caveats in the entire delegation chain are enforced
- Extensible: The caveat system is designed to be extensible with custom caveat types
Supported Caveat Types
-
Time-based Caveats
ValidUntil: The capability is only valid until a specific timeValidAfter: The capability is only valid after a specific time- Example:
{"type": "ValidUntil", "date": "2023-12-31T23:59:59Z"}
-
Action-specific Caveats
AllowedAction: Restricts which actions can be performedRequireParameter: Requires specific parameter values for actions- Example:
{"type": "AllowedAction", "actions": ["read"]}
-
Conditional Caveats
ValidWhileTrue: The capability is valid as long as a condition remains true- Example:
{"type": "ValidWhileTrue", "conditionId": "condition:example:active"}
-
Usage Caveats
MaxUses: Limits the number of times a capability can be used- Example:
{"type": "MaxUses", "limit": 5}
-
Network Caveats
AllowedNetwork: Restricts capability use to specific networks- Example:
{"type": "AllowedNetwork", "networks": ["192.168.1.0/24"]}
Example: Combining Caveats
# Create a capability with multiple caveats
capability = create_capability(
controller="did:example:alice",
invoker="did:example:bob",
actions=[{"name": "read"}, {"name": "write"}],
target={"id": "https://example.com/resource/123", "type": "Document"},
controller_key=alice_key,
caveats=[
{"type": "ValidUntil", "date": (datetime.utcnow() + timedelta(days=30)).isoformat()},
{"type": "AllowedAction", "actions": ["read"]},
{"type": "ValidWhileTrue", "conditionId": "subscription:active"}
]
)
Adding Caveats During Delegation
# Add more restrictive caveats during delegation
delegated = delegate_capability(
parent_capability=capability,
delegator_key=bob_key,
new_invoker="did:example:charlie",
caveats=[
{"type": "RequireParameter", "parameter": "mode", "value": "secure"},
{"type": "MaxUses", "limit": 3}
]
)
Examples
The examples/ directory contains detailed examples:
basic_usage.py: Simple capability creation and usagedocument_sharing.py: Document sharing system with delegationcrypto_operations.py: Detailed cryptographic operations
API Reference
Creating Capabilities
def create_capability(
controller: str,
invoker: str,
actions: List[Dict[str, Any]],
target: Dict[str, Any],
controller_key: ed25519.Ed25519PrivateKey,
expires: Optional[datetime] = None,
caveats: Optional[List[Dict[str, Any]]] = None
) -> Capability:
"""Create a new capability with the specified parameters."""
Delegating Capabilities
def delegate_capability(
parent_capability: Capability,
delegator_key: ed25519.Ed25519PrivateKey,
new_invoker: str,
actions: Optional[List[Dict[str, Any]]] = None,
expires: Optional[datetime] = None,
caveats: Optional[List[Dict[str, Any]]] = None
) -> Capability:
"""Create a delegated capability from a parent capability."""
Invoking Capabilities
def invoke_capability(
capability: Capability,
action: str,
invoker_key: ed25519.Ed25519PrivateKey,
parameters: Optional[Dict[str, Any]] = None
) -> bool:
"""Invoke a capability to perform an action."""
Verifying Capabilities
def verify_capability(capability: Capability) -> bool:
"""Verify a capability and its entire delegation chain."""
Revoking Capabilities
def revoke_capability(capability_id: str) -> None:
"""Revoke a capability by its ID."""
Development
Requirements:
- Python 3.10+
- PDM (Python package manager)
Setup:
pdm install
Run tests:
pdm run pytest
Security Considerations
- Key Management: Securely store and manage private keys
- Proof Verification: Always verify the complete delegation chain
- Expiration: Use appropriate expiration times
- Caveats: Implement and enforce appropriate constraints
- Revocation: Consider using external revocation registries for production
License
Apache License 2.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zcap-0.1.0.tar.gz.
File metadata
- Download URL: zcap-0.1.0.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfce8e5bb9d80f9895b1a50cbdca4e6643b82d47320babb986d23e6d544e7ef3
|
|
| MD5 |
9a3789823f6a263535fcc6c8fa98637b
|
|
| BLAKE2b-256 |
0ec8a6f2a961496af2052bc354f805faed9580d96b88eb938d8044b8114e4cdf
|
Provenance
The following attestation bundles were made for zcap-0.1.0.tar.gz:
Publisher:
publish.yml on lukehinds/pyzcap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zcap-0.1.0.tar.gz -
Subject digest:
bfce8e5bb9d80f9895b1a50cbdca4e6643b82d47320babb986d23e6d544e7ef3 - Sigstore transparency entry: 226885966
- Sigstore integration time:
-
Permalink:
lukehinds/pyzcap@0c091972a8635ad6a051023f454d80a15d15576d -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/lukehinds
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0c091972a8635ad6a051023f454d80a15d15576d -
Trigger Event:
release
-
Statement type:
File details
Details for the file zcap-0.1.0-py3-none-any.whl.
File metadata
- Download URL: zcap-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d499eeb854849678c0833b884838c3158d2c091b030ca0f207c8f266377616f8
|
|
| MD5 |
c6bb03a40c49b622e7f12d70e6fdd2c7
|
|
| BLAKE2b-256 |
1644736a8c2a190ff471c1423efd9adfd4e358da4764503306608c3c35fd8678
|
Provenance
The following attestation bundles were made for zcap-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on lukehinds/pyzcap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zcap-0.1.0-py3-none-any.whl -
Subject digest:
d499eeb854849678c0833b884838c3158d2c091b030ca0f207c8f266377616f8 - Sigstore transparency entry: 226885967
- Sigstore integration time:
-
Permalink:
lukehinds/pyzcap@0c091972a8635ad6a051023f454d80a15d15576d -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/lukehinds
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0c091972a8635ad6a051023f454d80a15d15576d -
Trigger Event:
release
-
Statement type: