cookie

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT, MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cookie implements all cookie handling for the session package

Index

Constants

View Source
const (
	// SessionID is the key used to store the SessionID in a Secure Cookie
	SessionID cookie.Key = "sessionID"

	// SameSiteStrict is the key used to store the sameSiteStrict cookie setting
	SameSiteStrict cookie.Key = "sameSiteStrict"

	// OIDCState is the key used to store the state
	OIDCState cookie.Key = "state"
	// Registration is the key under which a registration's state cookie names the
	// registration that wrote it, so a callback completes only the login its own
	// registration began.
	Registration cookie.Key = "registration"

	// OIDCPkceVerifier is the key used to store the PKCE verifier
	OIDCPkceVerifier cookie.Key = "pkceVerifier"

	// ReturnURL is the key used to store the return URL
	ReturnURL cookie.Key = "returnURL"
	// WorkOSOrganization is the key under which a WorkOS login's state cookie carries the
	// organization the login requested, so the callback refuses a profile from another.
	WorkOSOrganization cookie.Key = "organization"
)
View Source
const (
	// AuthCookieName is the cookie name of the Secure Cookie
	AuthCookieName = "auth"

	// XSRFCookieName is the cookie name of the XSRF Token Cookie
	XSRFCookieName = "XSRF-TOKEN"

	// StateCookiePrefix begins the name of a registration's state cookie: the core names
	// the cookie OIDC-<session cookie name>-<registration> (StateCookieName), so two
	// registrations of one method, logins with different providers in one browser, and
	// two Auths on one host with a registration of the same name never read each
	// other's state.
	StateCookiePrefix = "OIDC-"

	// XSRFHeaderName is the header name of the XSRF Token Cookie
	XSRFHeaderName = "X-XSRF-TOKEN"

	// OIDCCookieExpiration is the default expiration for the OIDC Cookie
	OIDCCookieExpiration = 10 * time.Minute
)

Variables

View Source
var SafeMethods = methods([]string{"GET", "HEAD", "OPTIONS", "TRACE"})

SafeMethods are Idempotent methods as defined by RFC7231 section 4.2.2.

Functions

func RedirectToReturnURL

func RedirectToReturnURL(w http.ResponseWriter, returnURL string, code int)

RedirectToReturnURL redirects to returnURL once sanitized, through WriteRedirect.

func SanitizeReturnURL

func SanitizeReturnURL(returnURL string) string

SanitizeReturnURL constrains a post-login return URL to a path local to the application, falling back to "/" for anything else. A value is kept only when it is already in canonical form, so no redirect helper, proxy or browser can later normalize it into another host. It must start with a single "/", be printable ASCII with no backslash (raw or %5C), use only RFC 3986 path characters in its path, and have no empty, "." or ".." segment, as written or percent-decoded. The query and fragment otherwise pass through. docs/security-invariants.md (S6) lists the full rule.

func StateCookieName

func StateCookieName(auth, registration string) string

StateCookieName is the name of the state cookie of the registration named registration on the Auth whose session cookie is named auth: StateCookiePrefix, the Auth's cookie name and the registration's, so two Auths on one host with a registration of the same name never read each other's login state.

func ValidSessionID

func ValidSessionID(sessionID string) (impulse.UUID, bool)

ValidSessionID checks that the sessionID is a valid uuid

func WriteRedirect

func WriteRedirect(w http.ResponseWriter, target string, code int)

WriteRedirect answers with code and a Location header of exactly target. It does not use http.Redirect, whose path cleaning can turn a local path into a scheme-relative URL ("/./\host" becomes "/\host", which browsers read as "//host"). target must be a return URL SanitizeReturnURL kept or the application's own configuration.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client implements all cookie management for session package

func NewCookieClient

func NewCookieClient(masterKeyBase64 string, opts ...Option) (*Client, error)

NewCookieClient returns a new CookieClient

func (*Client) Cookie

func (c *Client) Cookie() *cookie.Client

Cookie returns the underlying cookie.Client

func (*Client) CreateXSRFTokenCookie

func (c *Client) CreateXSRFTokenCookie(w http.ResponseWriter, sessionID impulse.UUID)

CreateXSRFTokenCookie sets a new cookie

func (*Client) DeleteStateCookie

func (c *Client) DeleteStateCookie(w http.ResponseWriter, name string)

DeleteStateCookie deletes the state cookie named name. A state cookie is single-use.

func (*Client) ForRegistration

func (c *Client) ForRegistration(registration string) RegistrationStateCookies

ForRegistration returns the state cookie of the registration named registration.

func (*Client) HasValidXSRFToken

func (c *Client) HasValidXSRFToken(r *http.Request) (bool, error)

HasValidXSRFToken checks if the XSRF token is valid

func (*Client) NewAuthCookie

func (c *Client) NewAuthCookie(w http.ResponseWriter, sameSiteStrict bool, sessionID impulse.UUID) *cookie.Values

NewAuthCookie writes a new Auth Cookie for given sessionID

func (*Client) ReadAuthCookie

func (c *Client) ReadAuthCookie(r *http.Request) (values *cookie.Values, found bool, err error)

ReadAuthCookie reads the Auth cookie from the request

func (*Client) ReadStateCookie

func (c *Client) ReadStateCookie(r *http.Request, name string) (values *cookie.Values, readName string, err error)

ReadStateCookie reads the state cookie named name. readName is the name of the cookie read, or "" when the browser carries none. Pass it to DeleteStateCookie.

func (*Client) RefreshXSRFTokenCookie

func (c *Client) RefreshXSRFTokenCookie(w http.ResponseWriter, r *http.Request, sessionID impulse.UUID) (set bool, err error)

RefreshXSRFTokenCookie updates the cookie when it is close to expiration, or sets it if it does not exist.

func (*Client) WriteAuthCookie

func (c *Client) WriteAuthCookie(w http.ResponseWriter, sameSiteStrict bool, values *cookie.Values)

WriteAuthCookie writes the Auth cookie to the response

func (*Client) WriteStateCookie

func (c *Client) WriteStateCookie(w http.ResponseWriter, name string, values *cookie.Values)

WriteStateCookie writes an OIDC login's state cookie under name (a registration's, StateCookieName), so logins with different providers in one browser never overwrite each other's state. It carries the state, PKCE verifier and return URL between the redirect to the identity provider and the callback. No script reads it and the server reads it once at the callback, so it is HttpOnly. SameSite stays the default: a strict setting would keep it from coming back with the provider's redirect.

type Handler

type Handler interface {
	NewAuthCookie(w http.ResponseWriter, sameSiteStrict bool, sessionID impulse.UUID) *cookie.Values
	ReadAuthCookie(r *http.Request) (values *cookie.Values, found bool, err error)
	WriteAuthCookie(w http.ResponseWriter, sameSiteStrict bool, values *cookie.Values)
	RefreshXSRFTokenCookie(w http.ResponseWriter, r *http.Request, sessionID impulse.UUID) (set bool, err error)
	CreateXSRFTokenCookie(w http.ResponseWriter, sessionID impulse.UUID)
	HasValidXSRFToken(r *http.Request) (bool, error)
	Cookie() *cookie.Client
}

Handler Interface included for testability

type Option

type Option func(*cookieOptions)

Option defines a function signature for setting cookie client options.

func WithCookieDomain

func WithCookieDomain(domain string) Option

WithCookieDomain sets the domain for the session cookie.

func WithCookieName

func WithCookieName(name string) Option

WithCookieName sets the cookie name for the session cookie.

func WithXSRFCookieName

func WithXSRFCookieName(name string) Option

WithXSRFCookieName sets the cookie name for the XSRF cookie.

func WithXSRFHeaderName

func WithXSRFHeaderName(name string) Option

WithXSRFHeaderName sets the header name for the XSRF header.

type RegistrationStateCookies

type RegistrationStateCookies struct {
	// contains filtered or unexported fields
}

RegistrationStateCookies is one registration's state cookie, as the core hands it to the method it registers: named for the Auth and the registration (OIDC-<session cookie name>-<registration>), so two registrations of one method, logins with different providers in one browser, and two Auths on one host with a registration of the same name never read each other's state; and carrying the registration's name in its values, so a cookie another registration wrote reads as absent and the callback that finds it is refused. It implements signin.StateCookies.

func (RegistrationStateCookies) DeleteStateCookie

func (s RegistrationStateCookies) DeleteStateCookie(w http.ResponseWriter, name string)

DeleteStateCookie deletes the state cookie named name, the name ReadStateCookie returned.

func (RegistrationStateCookies) Name

Name is the cookie's name, OIDC-<session cookie name>-<registration>.

func (RegistrationStateCookies) ReadStateCookie

func (s RegistrationStateCookies) ReadStateCookie(r *http.Request) (values *cookie.Values, readName string, err error)

ReadStateCookie reads the registration's state cookie. readName is its name, or "" when the browser carries none, or one that names another registration.

func (RegistrationStateCookies) WriteStateCookie

func (s RegistrationStateCookies) WriteStateCookie(w http.ResponseWriter, values *cookie.Values)

WriteStateCookie writes values, with the registration's name added, as the registration's state cookie.

Jump to

Keyboard shortcuts

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