sessions

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package sessions provides session token generation, cookie helpers, session policy, and request URL/host detection. Pure utility code, no DB.

Index

Constants

View Source
const (
	DefaultSessionTTL              = 7 * 24 * time.Hour
	DefaultSessionRefreshThreshold = 24 * time.Hour
	DefaultSessionAbsoluteTTL      = 30 * 24 * time.Hour
)
View Source
const (
	// TTL is the default session lifetime.
	TTL = 30 * 24 * time.Hour
)

Variables

This section is empty.

Functions

func ClearCookie

func ClearCookie(w http.ResponseWriter, r *http.Request)

ClearCookie deletes the session cookie under both dev and prod names.

func ClearTransientCookie

func ClearTransientCookie(w http.ResponseWriter, name string, secure bool)

ClearTransientCookie deletes a cookie previously set via SetTransientCookie.

func HashFromB64

func HashFromB64(tok string) ([]byte, error)

HashFromB64 decodes the base64url token and returns the SHA-256 of the raw bytes. Same convention as NewToken — used to look up a stored hash.

func HashFromRequest

func HashFromRequest(r *http.Request) ([]byte, error)

HashFromRequest reads the session cookie from r and returns its hash.

func IsLocalhost

func IsLocalhost(h string) bool

IsLocalhost reports whether host (which may include :port) is loopback.

func NewToken

func NewToken() (token string, hash []byte, err error)

NewToken returns a base64url-encoded random token (32 bytes of entropy) and the SHA-256 hash of the raw bytes. Store the hash; expose the token.

func NextSessionExpiry added in v0.1.2

func NextSessionExpiry(now, createdAt, currentExpiry time.Time, policy SessionPolicy) (time.Time, bool)

func ParseAllowlistCSV added in v0.1.2

func ParseAllowlistCSV(csv string) map[string]struct{}

ParseAllowlistCSV parses a comma-separated list of emails into a lowercase set.

func ReadCookie

func ReadCookie(r *http.Request) (string, error)

ReadCookie returns the raw session cookie value, falling back across dev/prod cookie names.

func RequestBaseURL

func RequestBaseURL(r *http.Request) string

RequestBaseURL derives the public base URL from an inbound request.

func RequestHost

func RequestHost(r *http.Request) string

RequestHost returns the effective host: X-Forwarded-Host (last entry) when the peer is trusted, else r.Host.

func RequestIsHTTPS

func RequestIsHTTPS(r *http.Request) bool

RequestIsHTTPS reports whether the request is HTTPS, honouring X-Forwarded-Proto (last entry) when the peer is trusted.

func SetCookie

func SetCookie(w http.ResponseWriter, r *http.Request, token string, exp time.Time)

SetCookie writes a session cookie. The cookie name and Secure flag depend on whether the request is HTTPS and not localhost (__Host- prefix in prod).

func SetTransientCookie

func SetTransientCookie(w http.ResponseWriter, name, value string, exp time.Time, secure bool)

SetTransientCookie writes a short-lived HttpOnly cookie under an arbitrary name. Used for OAuth state and PKCE verifier cookies (and similar one-shot flows). The caller decides Secure based on its own HTTPS detection.

Types

type SessionPolicy added in v0.1.2

type SessionPolicy struct {
	TTL              time.Duration
	RefreshThreshold time.Duration
	AbsoluteTTL      time.Duration
}

func NewSessionPolicy added in v0.1.2

func NewSessionPolicy(sessionTTL, refreshThreshold, absoluteTTL time.Duration) (SessionPolicy, error)

Jump to

Keyboard shortcuts

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