auth

package
v0.176.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func ConstantTimeEqual(a, b string) bool

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

func RandomToken(nBytes int) (string, error)

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

func SignSession(secret []byte, now time.Time, ttl time.Duration) string

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

type Session struct {
	IssuedAt time.Time
	Expiry   time.Time
}

Session is the decoded, verified payload carried by a session cookie.

func VerifySession

func VerifySession(secret []byte, value string, now time.Time) (Session, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL