Documentation
¶
Overview ¶
Package jwt provides a small framework for issuing and parsing JWT tokens with a generic claims payload, plus signer/verifier interfaces that allow swapping the signing algorithm without changing call sites.
The HS256 implementation lives in core/jwt/hs256. Future algorithm subpackages (RS256, EdDSA, JWKS) implement the Signer/Verifier interfaces and are dropped in without breaking AuthService callers.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthClaims ¶
type AuthClaims[T any] struct { jwt.RegisteredClaims AuthInfo T `json:"authInfo"` }
AuthClaims embeds the standard jwt.RegisteredClaims and adds a caller-defined typed payload accessible as AuthInfo.
type AuthService ¶
type AuthService[T any] struct { // contains filtered or unexported fields }
AuthService issues and parses tokens carrying a typed AuthInfo payload. Construct via NewAuthService with a Signer and Verifier from one of the algorithm subpackages.
func NewAuthService ¶
func NewAuthService[T any]( signer Signer, verifier Verifier, serviceName, claimSubject string, tokenTTL time.Duration, ) *AuthService[T]
NewAuthService builds an AuthService.
signer and verifier come from the chosen algorithm subpackage, e.g.:
signer := hs256.NewSigner([]byte("secret"))
verifier := hs256.NewVerifier([]byte("secret"))
svc := jwt.NewAuthService[MyAuthInfo](signer, verifier, "my-service", "user", time.Hour)
serviceName is used as the Issuer and Audience claims; claimSubject becomes the Subject claim; tokenTTL bounds the ExpiresAt claim.
func (*AuthService[T]) CreateToken ¶
func (s *AuthService[T]) CreateToken(authInfo T) (string, error)
CreateToken issues a fresh signed token containing authInfo. Sets all standard claims (iss, sub, aud, iat, nbf, exp, jti).
func (*AuthService[T]) ParseToken ¶
func (s *AuthService[T]) ParseToken(tokenString string) (T, error)
ParseToken validates the given JWT and returns its AuthInfo payload.
type Signer ¶ added in v1.3.0
type Signer interface {
// Sign returns the signed JWT string.
Sign(claims jwt.Claims) (string, error)
}
Signer issues a signed JWT for the given claims. Implemented per algorithm in core/jwt/<algo> subpackages.