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
- On login, if the caller opted in,
Service.OnLoginSuccesssets a signed cookie:base64(username) : expiryUnix : HMAC(username, expiry, password-hash fragment). - On each subsequent request,
rememberme.MiddlewarecallsAutologin, which verifies the HMAC against the current password-hash fragment before trusting anything else in the cookie. - If the user’s password changes, the hash fragment changes, the HMAC no longer matches, and every outstanding remember-me cookie is silently rejected.
Middlewareonly runsAutologinwhen the request has noAuthenticationyet, so it never overrides a session/bearer/API-key authentication that ran earlier in the chain.
Setup
NewTokenBased returns:
rememberme.ErrKeyTooShortifKeyis under 32 bytesrememberme.ErrFetcherRequiredifFetcheris 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 — theWithLogin(...) 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:
examples/webapp/main.go in the repo for the complete runnable version of
this wiring, including sessionResolver.
Login request
The opt-in field defaults toremember-me on both the login handler
(LoginConfig.RememberMeField) and the service itself
(TokenBasedConfig.ParamName):
Cookie security
The cookie is:- HMAC-signed — tamper-proof
- Built from a password-hash fragment — self-invalidating on password change
HttpOnly— forced, never accessible to JavaScriptSecureby default — setInsecureCookie: trueto opt out, local dev onlySameSite=Laxby 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 viaAuthFactory:
