Rebuild for v1.0.0: postgres-only, slug-keyed authz, predicate API

Drops the Dialect/Queries abstraction in favor of a single PostgreSQL 16+
implementation collapsed into the root authkit package, removes the
public store interfaces, and reshapes the authorization model around
seeded slugs (roles, permissions, abilities) with optional labels.

Schema is now squashed into one migrations/0001_init.sql and applied
automatically on authkit.New (opt-out via Config.SkipAutoMigrate). A
schema verifier checks tables/columns/types/nullability on startup,
tolerates extra columns, and falls back to default table names when a
configured override is missing.

Auth API: CreateUser + SetPassword replace Register; password is
nullable. Email OTP (RequestEmailOTP/ConsumeEmailOTP) joins magic links
and password reset, all with anti-enumeration silent-success defaults
and a Config.RevealUnknownEmail opt-in. Service tokens drop owner
columns and validate ability slugs against authkit_abilities at issue.
Direct user permissions live alongside role-derived ones; queries
return their UNION.

Predicate API: HasRole/HasPermission/HasAbility leaves with
AnyLogin/AllLogin/AnyServiceKey/AllServiceKey combinators. Validate
runs at middleware construction, panicking on unknown slugs.

Middleware collapses to RequireLogin (cookie + JWT), RequireGuest
(configurable OnAuthenticated), and RequireServiceKey. UserIDFromCtx /
UserFromCtx (lazy) / RefreshUserInCtx provide request-lifetime user
caching. Cookie defaults flip to Secure=true and HttpOnly=true via
*bool with BoolPtr opt-out.

CLIs ship under cmd/perms, cmd/roles, cmd/abilities for seeding the
authorization vocabulary; the library never seeds rows itself.

Tests cover unit-level (slug validation + fuzz, opaque secrets, email
normalization, extractors, predicates, OTP generator) and integration
flows gated on AUTHKIT_TEST_DATABASE_URL (every Auth method, schema
drift detection, migration idempotency, lazy user cache, all middleware
paths).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
juancwu 2026-04-26 23:27:30 +00:00
commit d3c5367492
80 changed files with 5605 additions and 4565 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/google/uuid"
)
// AuthMethod tags how a Principal was authenticated.
type AuthMethod string
const (
@ -13,10 +14,10 @@ const (
AuthMethodJWT AuthMethod = "jwt"
)
// Principal represents an authenticated user. It is produced only by
// user-bound auth methods (session, JWT) and carries identity plus
// RBAC-resolved roles/permissions. Service-token auth produces a
// *ServiceKey instead — those credentials carry abilities, not identity.
// Principal represents an authenticated user. Produced only by user-bound
// auth methods (session, JWT) and carries identity plus RBAC-resolved roles
// and permissions. Service-token auth produces a *ServiceKey instead — those
// credentials carry abilities, not identity.
type Principal struct {
UserID uuid.UUID
Method AuthMethod
@ -27,27 +28,32 @@ type Principal struct {
ExpiresAt time.Time
}
func (p *Principal) HasRole(name string) bool {
// HasRole reports whether the principal holds the named role slug.
func (p *Principal) HasRole(slug string) bool {
for _, r := range p.Roles {
if r == name {
if r == slug {
return true
}
}
return false
}
func (p *Principal) HasAnyRole(names ...string) bool {
for _, n := range names {
if p.HasRole(n) {
// HasAnyRole reports whether the principal holds at least one of the named
// role slugs.
func (p *Principal) HasAnyRole(slugs ...string) bool {
for _, s := range slugs {
if p.HasRole(s) {
return true
}
}
return false
}
func (p *Principal) HasPermission(name string) bool {
// HasPermission reports whether the principal holds the named permission
// slug, resolved through any combination of roles and direct grants.
func (p *Principal) HasPermission(slug string) bool {
for _, perm := range p.Permissions {
if perm == name {
if perm == slug {
return true
}
}