Documentation
ΒΆ
Index ΒΆ
- Variables
- func GenerateEdDSAKey() (ed25519.PublicKey, ed25519.PrivateKey, error)
- type Algorithm
- func NewES256(privateKey *ecdsa.PrivateKey) Algorithm
- func NewES256WithPublicKey(publicKey *ecdsa.PublicKey) Algorithm
- func NewES384(privateKey *ecdsa.PrivateKey) Algorithm
- func NewES384WithPublicKey(publicKey *ecdsa.PublicKey) Algorithm
- func NewES512(privateKey *ecdsa.PrivateKey) Algorithm
- func NewES512WithPublicKey(publicKey *ecdsa.PublicKey) Algorithm
- func NewEdDSA(privateKey ed25519.PrivateKey) Algorithm
- func NewEdDSAWithPublicKey(publicKey ed25519.PublicKey) Algorithm
- func NewHS256(secret string) Algorithm
- func NewHS384(secret string) Algorithm
- func NewHS512(secret string) Algorithm
- func NewRS256(privateKey *rsa.PrivateKey) Algorithm
- func NewRS256WithPublicKey(publicKey *rsa.PublicKey) Algorithm
- func NewRS384(privateKey *rsa.PrivateKey) Algorithm
- func NewRS512(privateKey *rsa.PrivateKey) Algorithm
- type ClaimsValidator
- type ECDSAAlgorithm
- type EdDSAAlgorithm
- type HMACAlgorithm
- type RSAAlgorithm
- type Signer
- type StandardClaims
- type TokenSigner
- type TokenVerifier
- type Verifier
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( ErrInvalidToken = errors.New("invalid token") ErrExpiredToken = errors.New("token has expired") ErrInvalidSignature = errors.New("invalid token signature") ErrInvalidClaims = errors.New("invalid claims structure") ErrEmptySubject = errors.New("subject cannot be empty") ErrInvalidDuration = errors.New("duration must be positive") )
var (
ErrInvalidKeyType = errors.New("invalid key type for algorithm")
)
Functions ΒΆ
func GenerateEdDSAKey ΒΆ
func GenerateEdDSAKey() (ed25519.PublicKey, ed25519.PrivateKey, error)
GenerateEdDSAKey generates a new Ed25519 key pair
Types ΒΆ
type Algorithm ΒΆ
type Algorithm interface { // Name returns the algorithm name for JWT header (e.g., "HS256", "RS256", "ES256") Name() string // Sign creates a signature for the given payload Sign(payload []byte) ([]byte, error) // Verify checks if the signature is valid for the given payload Verify(payload []byte, signature []byte) error // Header returns the JWT header as JSON bytes Header() []byte }
Algorithm defines the interface for JWT signing algorithms
func NewES256 ΒΆ
func NewES256(privateKey *ecdsa.PrivateKey) Algorithm
NewES256 creates a new ECDSA-SHA256 algorithm instance (P-256 curve)
func NewES256WithPublicKey ΒΆ
NewES256WithPublicKey creates a verification-only ES256 instance
func NewES384 ΒΆ
func NewES384(privateKey *ecdsa.PrivateKey) Algorithm
NewES384 creates a new ECDSA-SHA384 algorithm instance (P-384 curve)
func NewES384WithPublicKey ΒΆ
NewES384WithPublicKey creates a verification-only ES384 instance
func NewES512 ΒΆ
func NewES512(privateKey *ecdsa.PrivateKey) Algorithm
NewES512 creates a new ECDSA-SHA512 algorithm instance (P-521 curve)
func NewES512WithPublicKey ΒΆ
NewES512WithPublicKey creates a verification-only ES512 instance
func NewEdDSA ΒΆ
func NewEdDSA(privateKey ed25519.PrivateKey) Algorithm
NewEdDSA creates a new EdDSA algorithm instance
func NewEdDSAWithPublicKey ΒΆ
NewEdDSAWithPublicKey creates a verification-only EdDSA instance
func NewRS256 ΒΆ
func NewRS256(privateKey *rsa.PrivateKey) Algorithm
NewRS256 creates a new RSA-SHA256 algorithm instance
func NewRS256WithPublicKey ΒΆ
NewRS256WithPublicKey creates a verification-only RS256 instance
func NewRS384 ΒΆ
func NewRS384(privateKey *rsa.PrivateKey) Algorithm
NewRS384 creates a new RSA-SHA384 algorithm instance
func NewRS512 ΒΆ
func NewRS512(privateKey *rsa.PrivateKey) Algorithm
NewRS512 creates a new RSA-SHA512 algorithm instance
type ClaimsValidator ΒΆ
type ClaimsValidator interface {
Validate() error
}
ClaimsValidator interface for custom claims validation
type ECDSAAlgorithm ΒΆ
type ECDSAAlgorithm struct {
// contains filtered or unexported fields
}
ECDSAAlgorithm implements ECDSA-based JWT signing
func (*ECDSAAlgorithm) Header ΒΆ
func (e *ECDSAAlgorithm) Header() []byte
Header returns the algorithm header
func (*ECDSAAlgorithm) Name ΒΆ
func (e *ECDSAAlgorithm) Name() string
Name returns the algorithm name
func (*ECDSAAlgorithm) Sign ΒΆ
func (e *ECDSAAlgorithm) Sign(payload []byte) ([]byte, error)
Sign signs the payload using ECDSA
func (*ECDSAAlgorithm) Verify ΒΆ
func (e *ECDSAAlgorithm) Verify(payload, signature []byte) error
Verify verifies the signature using ECDSA
type EdDSAAlgorithm ΒΆ
type EdDSAAlgorithm struct {
// contains filtered or unexported fields
}
EdDSAAlgorithm implements EdDSA-based JWT signing (Ed25519)
func (*EdDSAAlgorithm) Header ΒΆ
func (e *EdDSAAlgorithm) Header() []byte
Header returns the algorithm header
func (*EdDSAAlgorithm) Name ΒΆ
func (e *EdDSAAlgorithm) Name() string
Name returns the algorithm name
func (*EdDSAAlgorithm) Sign ΒΆ
func (e *EdDSAAlgorithm) Sign(payload []byte) ([]byte, error)
Sign signs the payload using EdDSA
func (*EdDSAAlgorithm) Verify ΒΆ
func (e *EdDSAAlgorithm) Verify(payload, signature []byte) error
Verify verifies the signature using EdDSA
type HMACAlgorithm ΒΆ
type HMACAlgorithm struct {
// contains filtered or unexported fields
}
HMACAlgorithm implements HMAC-based JWT signing
func (*HMACAlgorithm) Header ΒΆ
func (h *HMACAlgorithm) Header() []byte
Header returns the algorithm header
func (*HMACAlgorithm) Sign ΒΆ
func (h *HMACAlgorithm) Sign(payload []byte) ([]byte, error)
Sign signs the payload using HMAC
func (*HMACAlgorithm) Verify ΒΆ
func (h *HMACAlgorithm) Verify(payload, signature []byte) error
Verify verifies the signature using HMAC
type RSAAlgorithm ΒΆ
type RSAAlgorithm struct {
// contains filtered or unexported fields
}
RSAAlgorithm implements RSA-based JWT signing
func (*RSAAlgorithm) Header ΒΆ
func (r *RSAAlgorithm) Header() []byte
Header returns the algorithm header
func (*RSAAlgorithm) Sign ΒΆ
func (r *RSAAlgorithm) Sign(payload []byte) ([]byte, error)
Sign signs the payload using RSA
func (*RSAAlgorithm) Verify ΒΆ
func (r *RSAAlgorithm) Verify(payload, signature []byte) error
Verify verifies the signature using RSA
type StandardClaims ΒΆ
type StandardClaims struct { Subject string `json:"sub,omitempty"` IssuedAt int64 `json:"iat"` ExpiresAt int64 `json:"exp"` NotBefore int64 `json:"nbf,omitempty"` Issuer string `json:"iss,omitempty"` Audience string `json:"aud,omitempty"` JwtID string `json:"jti,omitempty"` }
StandardClaims represents standard JWT claims that are always present
type TokenSigner ΒΆ
type TokenSigner[T any] struct { // contains filtered or unexported fields }
TokenSigner represents a JWT token signer (Auth Service)
func NewTokenSigner ΒΆ
func NewTokenSigner[T any](algorithm Algorithm, issuer string) (*TokenSigner[T], error)
NewTokenSigner creates a new token signer for generating JWTs
func (*TokenSigner[T]) GenerateToken ΒΆ
func (s *TokenSigner[T]) GenerateToken(subject string, duration time.Duration, customClaims T) (string, error)
GenerateToken creates a new JWT token with custom claims
type TokenVerifier ΒΆ
type TokenVerifier[T any] struct { // contains filtered or unexported fields }
TokenVerifier represents a JWT token verifier (Client Service)
func NewTokenVerifier ΒΆ
func NewTokenVerifier[T any](algorithm Algorithm, issuer string) (*TokenVerifier[T], error)
NewTokenVerifier creates a new token verifier for validating JWTs
func (*TokenVerifier[T]) VerifyToken ΒΆ
func (v *TokenVerifier[T]) VerifyToken(tokenString string) (*StandardClaims, *T, error)
VerifyToken validates a JWT token and returns the claims