Documentation
¶
Overview ¶
Package theauth provides session-based authentication for Go applications.
TheAuth ships magic-link email auth, opaque session tokens with revocation, and chi-friendly middleware. Storage backends include in-memory and Postgres (pgx + sqlc). OAuth providers, TOTP, WebAuthn, and MCP OAuth 2.1 land in future versions — see the README roadmap.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidToken = errors.New("theauth: invalid token") ErrSessionExpired = errors.New("theauth: session expired") ErrUserNotFound = errors.New("theauth: user not found") ErrMagicLinkExpired = errors.New("theauth: magic link expired") ErrMagicLinkUsed = errors.New("theauth: magic link already used") ErrEmailNotVerified = errors.New("theauth: email not verified") // ErrStorageNotFound is the canonical "row missing" sentinel that storage // adapters return on lookup misses. Lives in the root package so service // code can errors.Is-check without importing the storage package // (which would create an import cycle). ErrStorageNotFound = errors.New("theauth: storage row not found") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Storage Storage
EmailSender email.Sender
BaseURL string
SigningKey ed25519.PrivateKey
SessionTTL time.Duration
MagicLinkTTL time.Duration
CookieName string
SecureCookie bool
}
Config holds the wiring for a TheAuth instance.
Storage and BaseURL are required. Everything else has sensible defaults applied by New: SessionTTL=24h, MagicLinkTTL=15m, CookieName="theauth_session", EmailSender=email.Noop{}. SigningKey is reserved for future JWT signing (v0.2+); v0.1 uses opaque tokens and leaves the field nil.
type Session ¶
type Session struct {
ID ULID `json:"id"`
UserID ULID `json:"userId"`
TokenHash []byte `json:"-"` // never serialize raw hash
UserAgent string `json:"userAgent"`
IP string `json:"ip"`
CreatedAt time.Time `json:"createdAt"`
ExpiresAt time.Time `json:"expiresAt"`
RevokedAt *time.Time `json:"revokedAt,omitempty"`
}
func SessionFromContext ¶
SessionFromContext returns the Session attached by Authn middleware, if any. Returns false when the request is anonymous.
type Storage ¶
type Storage interface {
// Users
CreateUser(ctx context.Context, u User) (User, error)
UserByEmail(ctx context.Context, email string) (*User, error)
UserByID(ctx context.Context, id ULID) (*User, error)
MarkEmailVerified(ctx context.Context, userID ULID) error
// Sessions
CreateSession(ctx context.Context, s Session) (Session, error)
SessionByTokenHash(ctx context.Context, hash []byte) (*Session, error)
RevokeSession(ctx context.Context, id ULID) error
RevokeUserSessions(ctx context.Context, userID ULID) error
// Magic links
CreateMagicLink(ctx context.Context, ml MagicLink) error
ConsumeMagicLink(ctx context.Context, tokenHash []byte) (*MagicLink, error)
}
Storage is the persistence contract TheAuth depends on. Adapters live in sub-packages (storage/memory, storage/postgres). Defined here so that service code in this package can reference it without importing the storage sub-package (which would create an import cycle, because storage imports this package for the model types).
The storage package re-exports this as storage.Storage so consumers can keep importing it from the conventional location.
type TheAuth ¶
type TheAuth struct {
// contains filtered or unexported fields
}
TheAuth is the public entry point — constructed once at app start and shared across handlers.
func (*TheAuth) Authn ¶
Authn looks for a session cookie, validates it, and adds the user + session to the request context. Does NOT reject anonymous requests — pair with RequireAuth.
func (*TheAuth) Mount ¶
Mount wires TheAuth's HTTP routes onto the supplied chi router under /auth. Routes:
POST /auth/magic-link request a magic link GET /auth/magic-link/verify consume a magic link, set session cookie GET /auth/me return the authenticated user (RequireAuth) DELETE /auth/sessions/current revoke the current session (RequireAuth)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
mcpresource
module
|
|
|
postgres
Package postgres provides a Postgres-backed storage.Storage implementation built on top of sqlc-generated queries and pgx/v5.
|
Package postgres provides a Postgres-backed storage.Storage implementation built on top of sqlc-generated queries and pgx/v5. |