cookies

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

View Source
const (
	DefaultCookieName    = "access_token"
	DefaultCookieNameKey = "jwt.cookie.name"
)

Variables

View Source
var (
	ErrNilJWTService    = errors.New("cookies: jwt service is required")
	ErrMissingCookieKey = errors.New("cookies: cookie name is required")
	ErrNilRequest       = errors.New("cookies: request is required")
	ErrMissingCookie    = errors.New("cookies: auth cookie is required")
)

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

type Option func(*Service) error

Option configures a cookie auth Service.

func WithCookieName

func WithCookieName(cookieName string) Option

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 New

func New(options ...Option) (*Service, error)

New builds a cookie auth service from options.

func NewConfiguredService

func NewConfiguredService(input ConfigServiceInput) (*Service, error)

NewConfiguredService builds a cookie auth service from viper-backed JWT and cookie configuration.

func (*Service) CookieName

func (service *Service) CookieName() string

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

func (service *Service) Read(request *http.Request, destination any) error

Read validates the token stored in the configured cookie and decodes its claims into destination.

func (*Service) TokenFromRequest

func (service *Service) TokenFromRequest(request *http.Request) (string, error)

TokenFromRequest extracts the raw JWT token from the configured request cookie.

func (*Service) ValidateRequest

func (service *Service) ValidateRequest(request *http.Request) error

ValidateRequest validates the JWT signature from the configured request cookie without decoding claims.

Jump to

Keyboard shortcuts

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