Skip to content

Sessions and redirects

Greg Bowler edited this page Jul 16, 2020 · 1 revision

The client needs storage for the exchange and a way to redirect the browser. Supply per-request session data or use a framework adapter.

Starting a native session

Runtime integrations use an opaque random 256-bit session ID in an HTTP-only, SameSite=Lax cookie. Cookies are Secure by default. Set InsecureCookie: true only for local HTTP. The browser must retain its cookie while visiting the provider, and all application instances must share a persistent store.

type SessionStore interface {
    Load(context.Context, string) (map[string]any, error)
    Save(context.Context, string, map[string]any, time.Time) error
}

Implement expiration using the supplied time.Time. NewMemorySessionStore() provides a concurrent development store which loses data on restart. Configure CookieName, TTL and InsecureCookie through HTTPOptions.

Store only JSON-compatible records and enforce expiration. Session records include token secrets; keep them on the server. Use store-level concurrency control to serialize mutations to the same session when multiple requests overlap.

Supplying a container

session, err := authwave.NewGlobalSessionContainer(requestSession)
if err != nil { return nil, err }
auth, err := authwave.NewAuthenticator(authwave.Options{
    ClientKey: clientKey, CurrentURIPath: requestPath, Session: session,
})
if err != nil { return nil, err }

The container supports Get, Set, Contains, Remove with string keys. It does not start a session or discover a process-global visitor. Missing storage returns ErrSessionNotStarted.

Authentication uses SessionStoreKey, whose value is Authwave\SessionData. JSON storage is rehydrated into token and response objects. Preserve unrelated application session values.

Redirect behaviour

DefaultRedirectHandler(uri, code) returns *RedirectError. The framework adapter catches this control flow, saves the session and sends the redirect. This can happen during construction as well as login and logout.

Custom RedirectHandler functions return an error. Return redirect errors to the HTTP adapter immediately rather than continuing to protected content.

Adapters persist changes even when callbacks fail, set Cache-Control: no-store and buffer results until persistence succeeds. Store failures prevent successful redirects. Streaming responses are outside the buffered adapter contract.

Building a login URI directly

GetLoginURI(token) builds a URI without storing the token or redirecting. Prefer the normal login method, which saves the matching token for the provider's return request.


Next, see Development and testing.

Clone this wiki locally