Documentation
¶
Overview ¶
Package identity provides DID-based cryptographic identity for AGNTCY integration.
Package identity provides DID-based cryptographic identity for AGNTCY integration.
Overview ¶
This package implements the identity layer for agents in the AGNTCY (Internet of Agents) ecosystem. It provides:
- DID (Decentralized Identifier) support following W3C DID Core specification
- Verifiable Credentials for capability and delegation attestation
- Identity providers for local and AGNTCY-based identity management
DID Support ¶
DIDs are self-sovereign identifiers that don't depend on centralized registries. This package supports multiple DID methods:
- did:key - Public key based DIDs (default for local provider)
- did:web - DNS-based DIDs
- did:agntcy - AGNTCY-specific DIDs
Example:
did, err := identity.ParseDID("did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK")
if err != nil {
log.Fatal(err)
}
fmt.Println(did.Method) // "key"
Verifiable Credentials ¶
Credentials follow the W3C Verifiable Credentials data model and are used to:
- Attest agent capabilities (AgentCapabilityCredential)
- Delegate authority between agents (AgentDelegationCredential)
- Verify agent identity (AgentIdentityCredential)
Example:
cred, err := identity.NewAgentCapabilityCredential(
"urn:uuid:123",
issuerDID,
agentDID,
"code-review",
0.95,
)
Identity Providers ¶
Identity providers manage the lifecycle of agent identities:
LocalProvider: For development and single-node deployments
provider, _ := identity.NewLocalProvider(identity.ProviderConfig{})
identity, _ := provider.CreateIdentity(ctx, identity.CreateIdentityOptions{
DisplayName: "Code Reviewer",
InternalRole: "reviewer",
})
AgntcyProvider: For AGNTCY service integration (requires AGNTCY SDK)
provider, _ := identity.NewAgntcyProvider(identity.ProviderConfig{
AgntcyURL: "https://directory.agntcy.org",
})
Agent Identity ¶
AgentIdentity combines a DID with credentials and metadata:
identity := NewAgentIdentity(did, "Architect Agent") identity.InternalRole = "architect" identity.AddCredential(capabilityCredential)
Integration with LoopEntity ¶
LoopEntity includes an optional Identity field for AGNTCY integration:
loop := agentic.LoopEntity{
ID: "loop-123",
Identity: &identity.AgentIdentity{
DID: did,
DisplayName: "Architect",
},
}
See Also ¶
- ADR-019: AGNTCY Integration
- docs/concepts/21-did-identity.md
- W3C DID Core: https://www.w3.org/TR/did-core/
- W3C VC Data Model: https://www.w3.org/TR/vc-data-model/
Index ¶
- Constants
- Variables
- type AgentCapabilitySubject
- type AgentDelegationSubject
- type AgentIdentity
- func (ai *AgentIdentity) AddCredential(cred VerifiableCredential)
- func (ai *AgentIdentity) DIDString() string
- func (ai *AgentIdentity) GetCapabilities() []string
- func (ai *AgentIdentity) GetCredential(credID string) *VerifiableCredential
- func (ai *AgentIdentity) GetCredentialsByType(credType string) []VerifiableCredential
- func (ai *AgentIdentity) GetMetadata(key string) (any, bool)
- func (ai *AgentIdentity) GetValidCredentials() []VerifiableCredential
- func (ai *AgentIdentity) HasCapability(capability string) bool
- func (ai *AgentIdentity) RemoveCredential(credID string) bool
- func (ai *AgentIdentity) SetMetadata(key string, value any)
- func (ai *AgentIdentity) Validate() error
- func (ai *AgentIdentity) WithInternalRole(role string) *AgentIdentity
- type AgntcyProvider
- func (p *AgntcyProvider) CreateIdentity(_ context.Context, _ CreateIdentityOptions) (*AgentIdentity, error)
- func (p *AgntcyProvider) DeleteIdentity(_ context.Context, _ DID) error
- func (p *AgntcyProvider) IssueCredential(_ context.Context, _ DID, _ string, _ any) (*VerifiableCredential, error)
- func (p *AgntcyProvider) ResolveIdentity(_ context.Context, _ DID) (*AgentIdentity, error)
- func (p *AgntcyProvider) UpdateIdentity(_ context.Context, _ *AgentIdentity) error
- func (p *AgntcyProvider) VerifyCredential(_ context.Context, _ *VerifiableCredential) (bool, error)
- type CreateIdentityOptions
- type CredentialStatus
- type DID
- func (d *DID) Equal(other *DID) bool
- func (d *DID) EqualIgnoreFragment(other *DID) bool
- func (d *DID) IsMethod(method string) bool
- func (d DID) MarshalText() ([]byte, error)
- func (d *DID) String() string
- func (d *DID) UnmarshalText(text []byte) error
- func (d *DID) Validate() error
- func (d *DID) WithFragment(fragment string) DID
- func (d *DID) WithoutFragment() DID
- type LocalProvider
- func (p *LocalProvider) CreateIdentity(ctx context.Context, opts CreateIdentityOptions) (*AgentIdentity, error)
- func (p *LocalProvider) DeleteIdentity(_ context.Context, did DID) error
- func (p *LocalProvider) GetIssuerDID() *DID
- func (p *LocalProvider) IssueCredential(_ context.Context, _ DID, credType string, claims any) (*VerifiableCredential, error)
- func (p *LocalProvider) ResolveIdentity(_ context.Context, did DID) (*AgentIdentity, error)
- func (p *LocalProvider) UpdateIdentity(_ context.Context, identity *AgentIdentity) error
- func (p *LocalProvider) VerifyCredential(_ context.Context, cred *VerifiableCredential) (bool, error)
- type Proof
- type Provider
- type ProviderConfig
- type ProviderFactory
- type VerifiableCredential
- func NewAgentCapabilityCredential(id, issuer, agentDID, capability string, confidence float64) (*VerifiableCredential, error)
- func NewAgentDelegationCredential(id, issuer, delegateDID, delegatorDID string, capabilities []string) (*VerifiableCredential, error)
- func NewVerifiableCredential(id, issuer string, credType string, subject any) (*VerifiableCredential, error)
- func (vc *VerifiableCredential) GetSubject(v any) error
- func (vc *VerifiableCredential) HasType(credType string) bool
- func (vc *VerifiableCredential) IsExpired() bool
- func (vc *VerifiableCredential) SetSubject(v any) error
- func (vc *VerifiableCredential) Validate() error
- func (vc *VerifiableCredential) WithExpiration(exp time.Time) *VerifiableCredential
- func (vc *VerifiableCredential) WithProof(proof *Proof) *VerifiableCredential
Constants ¶
const ( // TypeVerifiableCredential is the base type for all VCs. TypeVerifiableCredential = "VerifiableCredential" // TypeAgentCapabilityCredential is for agent capability attestations. TypeAgentCapabilityCredential = "AgentCapabilityCredential" // TypeAgentDelegationCredential is for delegation authority. TypeAgentDelegationCredential = "AgentDelegationCredential" // TypeAgentIdentityCredential is for identity verification. TypeAgentIdentityCredential = "AgentIdentityCredential" )
Common credential types
const ( // PurposeAssertionMethod means the proof asserts a claim. PurposeAssertionMethod = "assertionMethod" // PurposeAuthentication means the proof authenticates the subject. PurposeAuthentication = "authentication" // PurposeCapabilityDelegation means the proof delegates a capability. PurposeCapabilityDelegation = "capabilityDelegation" // PurposeCapabilityInvocation means the proof invokes a capability. PurposeCapabilityInvocation = "capabilityInvocation" )
Common proof purposes
const ( // MethodKey is the did:key method using public key encoding. MethodKey = "key" // MethodWeb is the did:web method using DNS domain names. MethodWeb = "web" // MethodAgntcy is the AGNTCY-specific DID method. MethodAgntcy = "agntcy" )
Common DID methods
Variables ¶
var ( // ContextVC is the standard W3C VC context. ContextVC = "https://www.w3.org/2018/credentials/v1" // ContextAgntcy is the AGNTCY-specific context. ContextAgntcy = "https://agntcy.org/credentials/v1" )
Default context URLs
var ( // ErrDisplayNameRequired indicates display name is required. ErrDisplayNameRequired = errors.New("display name is required") // ErrUnknownProviderType indicates an unknown provider type. ErrUnknownProviderType = errors.New("unknown provider type") // ErrIdentityNotFound indicates the identity was not found. ErrIdentityNotFound = errors.New("identity not found") // ErrCredentialInvalid indicates the credential is invalid. ErrCredentialInvalid = errors.New("credential is invalid") // ErrCredentialExpired indicates the credential has expired. ErrCredentialExpired = errors.New("credential has expired") // ErrSignatureInvalid indicates the signature verification failed. ErrSignatureInvalid = errors.New("signature verification failed") // ErrKeyNotFound indicates the key was not found. ErrKeyNotFound = errors.New("key not found") // ErrProviderNotConfigured indicates the provider is not configured. ErrProviderNotConfigured = errors.New("provider not configured") // ErrUnsupportedMethod indicates the DID method is not supported. ErrUnsupportedMethod = errors.New("unsupported DID method") )
Identity-related errors
Functions ¶
This section is empty.
Types ¶
type AgentCapabilitySubject ¶
type AgentCapabilitySubject struct {
// ID is the DID of the agent.
ID string `json:"id"`
// Capability is the capability being attested.
Capability string `json:"capability"`
// Confidence is the confidence level (0.0-1.0).
Confidence float64 `json:"confidence,omitempty"`
// Scope limits where the capability applies.
Scope string `json:"scope,omitempty"`
}
AgentCapabilitySubject represents the subject of an agent capability credential.
type AgentDelegationSubject ¶
type AgentDelegationSubject struct {
// ID is the DID of the delegate (agent receiving authority).
ID string `json:"id"`
// Delegator is the DID of the delegating agent.
Delegator string `json:"delegator"`
// Capabilities are the delegated capabilities.
Capabilities []string `json:"capabilities"`
// Scope limits where the delegation applies.
Scope string `json:"scope,omitempty"`
// ValidUntil is when the delegation expires.
ValidUntil *time.Time `json:"validUntil,omitempty"`
}
AgentDelegationSubject represents the subject of an agent delegation credential.
type AgentIdentity ¶
type AgentIdentity struct {
// DID is the decentralized identifier for this agent.
DID DID `json:"did"`
// DisplayName is a human-readable name for the agent.
DisplayName string `json:"display_name"`
// Credentials are verifiable credentials held by this agent.
Credentials []VerifiableCredential `json:"credentials,omitempty"`
// InternalRole is the agent's role in the local system (preserved for compatibility).
// Example values: "architect", "editor", "reviewer"
InternalRole string `json:"internal_role,omitempty"`
// Created is when this identity was created.
Created time.Time `json:"created,omitempty"`
// Updated is when this identity was last updated.
Updated time.Time `json:"updated,omitempty"`
// Metadata contains additional identity metadata.
Metadata map[string]any `json:"metadata,omitempty"`
}
AgentIdentity represents the complete identity of an agent in the AGNTCY ecosystem. It combines a DID with associated credentials and metadata.
func NewAgentIdentity ¶
func NewAgentIdentity(did DID, displayName string) *AgentIdentity
NewAgentIdentity creates a new agent identity.
func (*AgentIdentity) AddCredential ¶
func (ai *AgentIdentity) AddCredential(cred VerifiableCredential)
AddCredential adds a credential to the identity.
func (*AgentIdentity) DIDString ¶
func (ai *AgentIdentity) DIDString() string
DIDString returns the string representation of the agent's DID.
func (*AgentIdentity) GetCapabilities ¶
func (ai *AgentIdentity) GetCapabilities() []string
GetCapabilities returns all capabilities from valid capability credentials.
func (*AgentIdentity) GetCredential ¶
func (ai *AgentIdentity) GetCredential(credID string) *VerifiableCredential
GetCredential returns a credential by ID.
func (*AgentIdentity) GetCredentialsByType ¶
func (ai *AgentIdentity) GetCredentialsByType(credType string) []VerifiableCredential
GetCredentialsByType returns all credentials of the specified type.
func (*AgentIdentity) GetMetadata ¶
func (ai *AgentIdentity) GetMetadata(key string) (any, bool)
GetMetadata gets a metadata value.
func (*AgentIdentity) GetValidCredentials ¶
func (ai *AgentIdentity) GetValidCredentials() []VerifiableCredential
GetValidCredentials returns all non-expired credentials.
func (*AgentIdentity) HasCapability ¶
func (ai *AgentIdentity) HasCapability(capability string) bool
HasCapability checks if the agent has a capability credential for the given capability.
func (*AgentIdentity) RemoveCredential ¶
func (ai *AgentIdentity) RemoveCredential(credID string) bool
RemoveCredential removes a credential by ID.
func (*AgentIdentity) SetMetadata ¶
func (ai *AgentIdentity) SetMetadata(key string, value any)
SetMetadata sets a metadata value.
func (*AgentIdentity) Validate ¶
func (ai *AgentIdentity) Validate() error
Validate checks if the agent identity is valid.
func (*AgentIdentity) WithInternalRole ¶
func (ai *AgentIdentity) WithInternalRole(role string) *AgentIdentity
WithInternalRole returns a copy with the specified internal role.
type AgntcyProvider ¶
type AgntcyProvider struct {
// contains filtered or unexported fields
}
AgntcyProvider implements Provider using the AGNTCY service. This is a stub implementation - full integration requires the AGNTCY SDK.
func NewAgntcyProvider ¶
func NewAgntcyProvider(config ProviderConfig) (*AgntcyProvider, error)
NewAgntcyProvider creates a new AGNTCY identity provider.
func (*AgntcyProvider) CreateIdentity ¶
func (p *AgntcyProvider) CreateIdentity(_ context.Context, _ CreateIdentityOptions) (*AgentIdentity, error)
CreateIdentity creates a new agent identity via AGNTCY service.
func (*AgntcyProvider) DeleteIdentity ¶
func (p *AgntcyProvider) DeleteIdentity(_ context.Context, _ DID) error
DeleteIdentity removes an identity via AGNTCY service.
func (*AgntcyProvider) IssueCredential ¶
func (p *AgntcyProvider) IssueCredential(_ context.Context, _ DID, _ string, _ any) (*VerifiableCredential, error)
IssueCredential issues a verifiable credential via AGNTCY service.
func (*AgntcyProvider) ResolveIdentity ¶
func (p *AgntcyProvider) ResolveIdentity(_ context.Context, _ DID) (*AgentIdentity, error)
ResolveIdentity resolves a DID to an agent identity via AGNTCY service.
func (*AgntcyProvider) UpdateIdentity ¶
func (p *AgntcyProvider) UpdateIdentity(_ context.Context, _ *AgentIdentity) error
UpdateIdentity updates an existing identity via AGNTCY service.
func (*AgntcyProvider) VerifyCredential ¶
func (p *AgntcyProvider) VerifyCredential(_ context.Context, _ *VerifiableCredential) (bool, error)
VerifyCredential verifies a credential via AGNTCY service.
type CreateIdentityOptions ¶
type CreateIdentityOptions struct {
// DisplayName is the human-readable name for the agent.
DisplayName string
// InternalRole is the agent's role in the local system.
InternalRole string
// Method specifies which DID method to use.
// Defaults to "key" for local provider.
Method string
// Metadata contains additional identity metadata.
Metadata map[string]any
// InitialCapabilities are capabilities to attest via credentials.
InitialCapabilities []string
}
CreateIdentityOptions configures identity creation.
func (*CreateIdentityOptions) Validate ¶
func (o *CreateIdentityOptions) Validate() error
Validate validates the options.
type CredentialStatus ¶
type CredentialStatus struct {
// ID is the URL for checking credential status.
ID string `json:"id"`
// Type specifies the status method type.
Type string `json:"type"`
}
CredentialStatus represents the status of a credential (e.g., revocation).
type DID ¶
type DID struct {
// Method identifies the DID method (e.g., "web", "key", "agntcy").
Method string `json:"method"`
// ID is the method-specific identifier.
ID string `json:"id"`
// Fragment is an optional fragment reference (e.g., key reference).
Fragment string `json:"fragment,omitempty"`
}
DID represents a Decentralized Identifier as specified by W3C DID Core. See: https://www.w3.org/TR/did-core/
func NewAgntcyDID ¶
NewAgntcyDID creates a new did:agntcy DID.
func NewWebDID ¶
NewWebDID creates a new did:web DID from a domain name. The domain should be URL-encoded for special characters.
func ParseDID ¶
ParseDID parses a DID string into a DID struct. Format: did:method:method-specific-id[#fragment]
func (*DID) EqualIgnoreFragment ¶
EqualIgnoreFragment checks if two DIDs are equal ignoring the fragment.
func (DID) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*DID) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
func (*DID) WithFragment ¶
WithFragment returns a copy of the DID with the specified fragment.
func (*DID) WithoutFragment ¶
WithoutFragment returns a copy of the DID without any fragment.
type LocalProvider ¶
type LocalProvider struct {
// contains filtered or unexported fields
}
LocalProvider implements Provider using local key generation. This is suitable for development, testing, and single-node deployments.
func NewLocalProvider ¶
func NewLocalProvider(config ProviderConfig) (*LocalProvider, error)
NewLocalProvider creates a new local identity provider.
func (*LocalProvider) CreateIdentity ¶
func (p *LocalProvider) CreateIdentity(ctx context.Context, opts CreateIdentityOptions) (*AgentIdentity, error)
CreateIdentity creates a new agent identity with a locally generated DID.
func (*LocalProvider) DeleteIdentity ¶
func (p *LocalProvider) DeleteIdentity(_ context.Context, did DID) error
DeleteIdentity removes an identity.
func (*LocalProvider) GetIssuerDID ¶
func (p *LocalProvider) GetIssuerDID() *DID
GetIssuerDID returns the provider's issuer DID.
func (*LocalProvider) IssueCredential ¶
func (p *LocalProvider) IssueCredential(_ context.Context, _ DID, credType string, claims any) (*VerifiableCredential, error)
IssueCredential issues a verifiable credential for the subject.
func (*LocalProvider) ResolveIdentity ¶
func (p *LocalProvider) ResolveIdentity(_ context.Context, did DID) (*AgentIdentity, error)
ResolveIdentity resolves a DID to an agent identity.
func (*LocalProvider) UpdateIdentity ¶
func (p *LocalProvider) UpdateIdentity(_ context.Context, identity *AgentIdentity) error
UpdateIdentity updates an existing identity.
func (*LocalProvider) VerifyCredential ¶
func (p *LocalProvider) VerifyCredential(_ context.Context, cred *VerifiableCredential) (bool, error)
VerifyCredential verifies a credential's signature and validity.
type Proof ¶
type Proof struct {
// Type specifies the proof type (e.g., "Ed25519Signature2020").
Type string `json:"type"`
// Created is when the proof was created.
Created time.Time `json:"created"`
// VerificationMethod is the DID URL of the key used for the proof.
VerificationMethod string `json:"verificationMethod"`
// ProofPurpose describes the purpose of the proof.
ProofPurpose string `json:"proofPurpose"`
// ProofValue is the actual cryptographic signature.
ProofValue string `json:"proofValue,omitempty"`
// JWS is an alternative proof format using JSON Web Signature.
JWS string `json:"jws,omitempty"`
}
Proof represents a cryptographic proof for a verifiable credential.
type Provider ¶
type Provider interface {
// CreateIdentity creates a new agent identity.
CreateIdentity(ctx context.Context, opts CreateIdentityOptions) (*AgentIdentity, error)
// ResolveIdentity resolves a DID to an agent identity.
ResolveIdentity(ctx context.Context, did DID) (*AgentIdentity, error)
// IssueCredential issues a verifiable credential for the subject.
IssueCredential(ctx context.Context, subject DID, credType string, claims any) (*VerifiableCredential, error)
// VerifyCredential verifies a credential's signature and validity.
VerifyCredential(ctx context.Context, cred *VerifiableCredential) (bool, error)
// UpdateIdentity updates an existing identity.
UpdateIdentity(ctx context.Context, identity *AgentIdentity) error
// DeleteIdentity removes an identity.
DeleteIdentity(ctx context.Context, did DID) error
}
Provider defines the interface for creating and managing agent identities.
type ProviderConfig ¶
type ProviderConfig struct {
// ProviderType identifies the provider ("local", "agntcy").
ProviderType string `json:"provider_type"`
// IssuerDID is the DID used for issuing credentials (for AGNTCY provider).
IssuerDID string `json:"issuer_did,omitempty"`
// AgntcyURL is the AGNTCY service URL (for AGNTCY provider).
AgntcyURL string `json:"agntcy_url,omitempty"`
// KeyStorePath is the path to store private keys (for local provider).
KeyStorePath string `json:"key_store_path,omitempty"`
}
ProviderConfig holds configuration for identity providers.
type ProviderFactory ¶
type ProviderFactory func(config ProviderConfig) (Provider, error)
ProviderFactory creates an identity provider based on configuration.
var DefaultProviderFactory ProviderFactory = func(config ProviderConfig) (Provider, error) { switch config.ProviderType { case "local", "": return NewLocalProvider(config) case "agntcy": return NewAgntcyProvider(config) default: return nil, ErrUnknownProviderType } }
DefaultProviderFactory creates providers based on provider type.
type VerifiableCredential ¶
type VerifiableCredential struct {
// Context specifies the JSON-LD context(s).
Context []string `json:"@context"`
// ID is the unique identifier for this credential.
ID string `json:"id"`
// Type specifies the credential type(s).
Type []string `json:"type"`
// Issuer is the DID of the entity that issued this credential.
Issuer string `json:"issuer"`
// IssuanceDate is when the credential was issued.
IssuanceDate time.Time `json:"issuanceDate"`
// ExpirationDate is when the credential expires (optional).
ExpirationDate *time.Time `json:"expirationDate,omitempty"`
// CredentialSubject contains the claims about the subject.
CredentialSubject json.RawMessage `json:"credentialSubject"`
// Proof contains the cryptographic proof (optional).
Proof *Proof `json:"proof,omitempty"`
// CredentialStatus contains revocation/status information (optional).
CredentialStatus *CredentialStatus `json:"credentialStatus,omitempty"`
}
VerifiableCredential represents a W3C Verifiable Credential. See: https://www.w3.org/TR/vc-data-model/
func NewAgentCapabilityCredential ¶
func NewAgentCapabilityCredential(id, issuer, agentDID, capability string, confidence float64) (*VerifiableCredential, error)
NewAgentCapabilityCredential creates a new agent capability credential.
func NewAgentDelegationCredential ¶
func NewAgentDelegationCredential(id, issuer, delegateDID, delegatorDID string, capabilities []string) (*VerifiableCredential, error)
NewAgentDelegationCredential creates a new agent delegation credential.
func NewVerifiableCredential ¶
func NewVerifiableCredential(id, issuer string, credType string, subject any) (*VerifiableCredential, error)
NewVerifiableCredential creates a new verifiable credential.
func (*VerifiableCredential) GetSubject ¶
func (vc *VerifiableCredential) GetSubject(v any) error
GetSubject unmarshals the credential subject into the provided value.
func (*VerifiableCredential) HasType ¶
func (vc *VerifiableCredential) HasType(credType string) bool
HasType checks if the credential has the specified type.
func (*VerifiableCredential) IsExpired ¶
func (vc *VerifiableCredential) IsExpired() bool
IsExpired checks if the credential has expired.
func (*VerifiableCredential) SetSubject ¶
func (vc *VerifiableCredential) SetSubject(v any) error
SetSubject marshals and sets the credential subject.
func (*VerifiableCredential) Validate ¶
func (vc *VerifiableCredential) Validate() error
Validate checks if the credential is structurally valid.
func (*VerifiableCredential) WithExpiration ¶
func (vc *VerifiableCredential) WithExpiration(exp time.Time) *VerifiableCredential
WithExpiration returns a copy with the specified expiration date.
func (*VerifiableCredential) WithProof ¶
func (vc *VerifiableCredential) WithProof(proof *Proof) *VerifiableCredential
WithProof returns a copy with the specified proof.