Documentation
¶
Overview ¶
Package cookie implements all cookie handling for the session package
Index ¶
- Constants
- Variables
- func RedirectToReturnURL(w http.ResponseWriter, returnURL string, code int)
- func SanitizeReturnURL(returnURL string) string
- func StateCookieName(auth, registration string) string
- func ValidSessionID(sessionID string) (impulse.UUID, bool)
- func WriteRedirect(w http.ResponseWriter, target string, code int)
- type Client
- func (c *Client) Cookie() *cookie.Client
- func (c *Client) CreateXSRFTokenCookie(w http.ResponseWriter, sessionID impulse.UUID)
- func (c *Client) DeleteStateCookie(w http.ResponseWriter, name string)
- func (c *Client) ForRegistration(registration string) RegistrationStateCookies
- func (c *Client) HasValidXSRFToken(r *http.Request) (bool, error)
- func (c *Client) NewAuthCookie(w http.ResponseWriter, sameSiteStrict bool, sessionID impulse.UUID) *cookie.Values
- func (c *Client) ReadAuthCookie(r *http.Request) (values *cookie.Values, found bool, err error)
- func (c *Client) ReadStateCookie(r *http.Request, name string) (values *cookie.Values, readName string, err error)
- func (c *Client) RefreshXSRFTokenCookie(w http.ResponseWriter, r *http.Request, sessionID impulse.UUID) (set bool, err error)
- func (c *Client) WriteAuthCookie(w http.ResponseWriter, sameSiteStrict bool, values *cookie.Values)
- func (c *Client) WriteStateCookie(w http.ResponseWriter, name string, values *cookie.Values)
- type Handler
- type Option
- type RegistrationStateCookies
- func (s RegistrationStateCookies) DeleteStateCookie(w http.ResponseWriter, name string)
- func (s RegistrationStateCookies) Name() string
- func (s RegistrationStateCookies) ReadStateCookie(r *http.Request) (values *cookie.Values, readName string, err error)
- func (s RegistrationStateCookies) WriteStateCookie(w http.ResponseWriter, values *cookie.Values)
Constants ¶
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" )
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
NewCookieClient returns a new CookieClient
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 ¶
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 ¶
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 ¶
WriteAuthCookie writes the Auth cookie to the response
func (*Client) WriteStateCookie ¶
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 ¶
WithCookieDomain sets the domain for the session cookie.
func WithCookieName ¶
WithCookieName sets the cookie name for the session cookie.
func WithXSRFCookieName ¶
WithXSRFCookieName sets the cookie name for the XSRF cookie.
func WithXSRFHeaderName ¶
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 ¶
func (s RegistrationStateCookies) Name() string
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.