auth

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateHandshakeRequest

func CreateHandshakeRequest(signer Manager, clientID string) (*pb.HandshakeRequest, error)

CreateHandshakeRequest creates a handshake request with signature

func GetPrivateKeyHex

func GetPrivateKeyHex(m Manager) string

GetPrivateKeyHex returns private key as hex (for saving)

func VerifyHandshakeRequest

func VerifyHandshakeRequest(req *pb.HandshakeRequest, verifier Verifier, maxClockDrift time.Duration) error

VerifyHandshakeRequest verifies a handshake request

Types

type Config

type Config struct {
	Enabled        bool              // Enable authentication
	PrivateKey     string            // Hex-encoded private key
	TrustedKeys    map[string]string // ID -> hex public key
	AllowUntrusted bool              // Allow connections from untrusted keys
	RequireAuth    bool              // Require all messages to be signed
}

Config holds auth configuration

type ECDSASigner

type ECDSASigner struct {
	// contains filtered or unexported fields
}

ECDSASigner implements Signer using secp256k1

func GenerateECDSASigner

func GenerateECDSASigner() (*ECDSASigner, error)

GenerateECDSASigner generates a new random signer

func NewECDSASigner

func NewECDSASigner(privateKey *ecdsa.PrivateKey) (*ECDSASigner, error)

NewECDSASigner creates a new signer from a private key

func NewECDSASignerFromHex

func NewECDSASignerFromHex(hexKey string) (*ECDSASigner, error)

NewECDSASignerFromHex creates a signer from hex private key

func (*ECDSASigner) Address

func (s *ECDSASigner) Address() string

Address returns Ethereum address

func (*ECDSASigner) PrivateKeyHex

func (s *ECDSASigner) PrivateKeyHex() string

PrivateKeyHex returns the private key as hex string

func (*ECDSASigner) PublicKeyBytes

func (s *ECDSASigner) PublicKeyBytes() []byte

PublicKeyBytes returns compressed public key

func (*ECDSASigner) Sign

func (s *ECDSASigner) Sign(data []byte) ([]byte, error)

Sign signs data using ECDSA

type ECDSAVerifier

type ECDSAVerifier struct{}

ECDSAVerifier implements signature verification

func NewECDSAVerifier

func NewECDSAVerifier() *ECDSAVerifier

NewECDSAVerifier creates a new verifier

func (*ECDSAVerifier) RecoverPublicKey

func (v *ECDSAVerifier) RecoverPublicKey(data, signature []byte) ([]byte, error)

RecoverPublicKey recovers the public key from signature

func (*ECDSAVerifier) Verify

func (v *ECDSAVerifier) Verify(data, signature, publicKey []byte) error

Verify checks if signature is valid

type Manager

type Manager interface {
	// Sign signs data with the private key
	Sign(data []byte) (signature []byte, err error)

	// Verify verifies a signature against data and public key
	Verify(data, signature, publicKey []byte) error

	// VerifyKnown verifies signature from a known/trusted entity
	// Returns the ID of the signer if trusted, error otherwise
	VerifyKnown(data, signature []byte) (signerID string, err error)

	// RecoverPublicKey recovers public key from signature
	RecoverPublicKey(data, signature []byte) ([]byte, error)

	// AddTrustedKey adds a trusted public key with an identifier
	AddTrustedKey(id string, publicKey []byte) error

	// RemoveTrustedKey removes a trusted key
	RemoveTrustedKey(id string) error

	// IsTrusted checks if a public key is trusted
	IsTrusted(publicKey []byte) bool

	// PublicKeyBytes returns this manager's public key
	PublicKeyBytes() []byte

	// PublicKeyString returns this manager's public key as hex
	PublicKeyString() string

	// Address returns this manager's Ethereum address
	Address() string
}

Manager manages authentication and authorization

func GenerateManager

func GenerateManager() (Manager, error)

GenerateManager generates a new manager with random key

func NewManager

func NewManager(privateKey *ecdsa.PrivateKey) Manager

NewManager creates a new auth manager

func NewManagerFromHex

func NewManagerFromHex(hexKey string) (Manager, error)

NewManagerFromHex creates manager from hex private key

type Signer

type Signer interface {
	Sign(data []byte) ([]byte, error)
	PublicKeyBytes() []byte
	Address() string
}

Signer signs messages with ECDSA

type Verifier

type Verifier interface {
	Verify(data, signature, publicKey []byte) error
	VerifyKnown(data, signature []byte) (string, error)
}

Verifier verifies ECDSA signatures

Jump to

Keyboard shortcuts

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