Documentation
¶
Overview ¶
Package auth provides the cryptographic primitives that lock down this service's two distinct surfaces:
- The human web UI is gated by a signed session cookie. A cookie value is an HMAC-SHA256-signed payload of the form "issuedAt|expiry"; the signature is keyed by a server-held secret so a client cannot forge or tamper with it.
- The machine hook endpoint is gated by a bearer token compared in constant time (see ConstantTimeEqual).
Tokens and the session secret are random, URL-safe, and generated with crypto/rand (see RandomToken).
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMalformed indicates the cookie value is not a well-formed signed token // (wrong number of fields, non-numeric timestamps, bad base64, …). ErrMalformed = errors.New("auth: malformed session token") // ErrBadSignature indicates the HMAC did not verify: the payload was tampered // with or signed under a different secret. ErrBadSignature = errors.New("auth: invalid session signature") // ErrExpired indicates the session's expiry timestamp is in the past. ErrExpired = errors.New("auth: session expired") )
Errors returned by VerifySession. They are intentionally coarse: a caller should treat any of them as "no valid session" and never surface the reason to an unauthenticated client.
Functions ¶
func ConstantTimeEqual ¶
ConstantTimeEqual reports whether a and b are equal using a constant-time comparison, so a timing side-channel cannot be used to recover a secret token byte by byte. Empty strings never match (a missing token must not authorize).
func RandomToken ¶
RandomToken returns a cryptographically-random, URL-safe token with nBytes of entropy (base64-raw-url encoded, so the string is longer than nBytes). It is used to mint the human auth token, the hook token, and the session secret.
func SignSession ¶
SignSession returns the cookie value for a session that was issued at now and expires after ttl. The value is "<issuedAtUnix>|<expiryUnix>|<hexSignature>" where the signature is HMAC-SHA256 over "<issuedAtUnix>|<expiryUnix>".
Types ¶
type Session ¶
Session is the decoded, verified payload carried by a session cookie.
func VerifySession ¶
VerifySession parses and validates a cookie value produced by SignSession. It checks the HMAC (in constant time) before checking expiry, and returns the decoded Session on success. now is the reference time for the expiry check.