Documentation
¶
Overview ¶
Package cookies provides cookie-based authentication helpers built on top of the JWT service from the security module.
It extracts a JWT from an HTTP cookie, validates it through a jwt.Service, and exposes helpers that can be reused by middleware or application code.
Main entry points:
- New to build a Service from explicit options
- NewConfiguredService to build a Service from viper-backed configuration
- TokenFromRequest to extract the raw token from an HTTP request
- Decode to validate the token and unmarshal its claims
Index ¶
- Constants
- Variables
- type ConfigServiceInput
- type Option
- type Service
- func (service *Service) CookieName() string
- func (service *Service) Decode(ctx context.Context, request *http.Request, destination any, ...) (jwtservice.Token, error)
- func (service *Service) Read(request *http.Request, destination any) error
- func (service *Service) TokenFromRequest(request *http.Request) (string, error)
- func (service *Service) ValidateRequest(request *http.Request) error
Constants ¶
const ( DefaultCookieName = "access_token" DefaultCookieNameKey = "jwt.cookie.name" )
Variables ¶
Functions ¶
This section is empty.
Types ¶
type ConfigServiceInput ¶
type ConfigServiceInput struct {
CookieName string
CookieNameKey string
JWT jwtservice.ConfigServiceInput
}
ConfigServiceInput configures NewConfiguredService. When CookieName is empty, it is loaded from viper using CookieNameKey, and then falls back to DefaultCookieName.
type Option ¶
Option configures a cookie auth Service.
func WithCookieName ¶
WithCookieName configures the cookie name used to extract the auth token.
func WithJWTService ¶
func WithJWTService(service *jwtservice.Service) Option
WithJWTService injects the JWT service used to validate cookie values.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service validates JWT auth tokens extracted from HTTP cookies.
func NewConfiguredService ¶
func NewConfiguredService(input ConfigServiceInput) (*Service, error)
NewConfiguredService builds a cookie auth service from viper-backed JWT and cookie configuration.
func (*Service) CookieName ¶
CookieName returns the configured cookie name.
func (*Service) Decode ¶
func (service *Service) Decode(ctx context.Context, request *http.Request, destination any, validators ...jwtservice.Validator) (jwtservice.Token, error)
Decode extracts the token from the configured cookie, validates it, decodes its claims into destination, and runs optional validators.
func (*Service) Read ¶
Read validates the token stored in the configured cookie and decodes its claims into destination.
func (*Service) TokenFromRequest ¶
TokenFromRequest extracts the raw JWT token from the configured request cookie.