The web/rememberme package implements persistent “remember me” authentication using HMAC-signed cookies that are invalidated automatically when the user changes their password. securityhttp has no WithRememberMe option; wire the service and its middleware in yourself, as shown below.

Import

How it works

  1. On login, if the caller opted in, Service.OnLoginSuccess sets a signed cookie: base64(username) : expiryUnix : HMAC(username, expiry, password-hash fragment).
  2. On each subsequent request, rememberme.Middleware calls Autologin, which verifies the HMAC against the current password-hash fragment before trusting anything else in the cookie.
  3. If the user’s password changes, the hash fragment changes, the HMAC no longer matches, and every outstanding remember-me cookie is silently rejected.
  4. Middleware only runs Autologin when the request has no Authentication yet, so it never overrides a session/bearer/API-key authentication that ran earlier in the chain.

Setup

NewTokenBased returns:
  • rememberme.ErrKeyTooShort if Key is under 32 bytes
  • rememberme.ErrFetcherRequired if Fetcher is nil — there’s no safe default password-hash source, so it must be supplied
TokenBasedConfig fields:

Wiring it into the chain

Mount your own login/logout handlers with the service attached — the WithLogin(...) option doesn’t thread a RememberMe service through — and add rememberme.Middleware to the chain after session resolution, so it only fires when the session cookie didn’t already authenticate the request:
See examples/webapp/main.go in the repo for the complete runnable version of this wiring, including sessionResolver.

Login request

The opt-in field defaults to remember-me on both the login handler (LoginConfig.RememberMeField) and the service itself (TokenBasedConfig.ParamName):
The cookie is:
  • HMAC-signed — tamper-proof
  • Built from a password-hash fragment — self-invalidating on password change
  • HttpOnly — forced, never accessible to JavaScript
  • Secure by default — set InsecureCookie: true to opt out, local dev only
  • SameSite=Lax by default

Force re-authentication for sensitive actions

There’s no built-in way to tell a remembered login apart from a fresh one — tag it yourself via AuthFactory: