identity

package
v1.0.0-beta.151 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package identity provides local DID-based cryptographic identity primitives.

Index

Constants

View Source
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

View Source
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

View Source
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"
)

Common DID methods

Variables

View Source
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

View Source
var (
	// ContextVC is the standard W3C VC context.
	ContextVC = "https://www.w3.org/2018/credentials/v1"
)

Default context URLs

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 local DID and credentials of an agent. 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 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 (for example, "web" or "key").
	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 NewKeyDID

func NewKeyDID(multibaseKey string) *DID

NewKeyDID creates a new did:key DID from a public key multibase encoding.

func NewWebDID

func NewWebDID(domain string, paths ...string) *DID

NewWebDID creates a new did:web DID from a domain name. The domain should be URL-encoded for special characters.

func ParseDID

func ParseDID(s string) (*DID, error)

ParseDID parses a DID string into a DID struct. Format: did:method:method-specific-id[#fragment]

func (*DID) Equal

func (d *DID) Equal(other *DID) bool

Equal checks if two DIDs are equal (including fragment).

func (*DID) EqualIgnoreFragment

func (d *DID) EqualIgnoreFragment(other *DID) bool

EqualIgnoreFragment checks if two DIDs are equal ignoring the fragment.

func (*DID) IsMethod

func (d *DID) IsMethod(method string) bool

IsMethod checks if the DID uses the specified method.

func (DID) MarshalText

func (d DID) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*DID) String

func (d *DID) String() string

String returns the canonical string representation of the DID.

func (*DID) UnmarshalText

func (d *DID) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (*DID) Validate

func (d *DID) Validate() error

Validate checks if the DID is valid.

func (*DID) WithFragment

func (d *DID) WithFragment(fragment string) DID

WithFragment returns a copy of the DID with the specified fragment.

func (*DID) WithoutFragment

func (d *DID) WithoutFragment() DID

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. The core implementation is "local".
	ProviderType string `json:"provider_type"`

	// IssuerDID is the DID used for issuing credentials.
	IssuerDID string `json:"issuer_did,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)
	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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL