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:
parent
7f1db871bc
commit
d3c5367492
80 changed files with 5605 additions and 4565 deletions
90
authz_test.go
Normal file
90
authz_test.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
package authkit
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestPredicateLeavesAndCombinators(t *testing.T) {
|
||||
p := &Principal{
|
||||
UserID: uuid.New(),
|
||||
Roles: []string{"admin", "manager"},
|
||||
Permissions: []string{"posts:read", "posts:write"},
|
||||
}
|
||||
|
||||
if !HasRole("admin").Match(p) {
|
||||
t.Fatalf("HasRole admin should match")
|
||||
}
|
||||
if HasRole("nope").Match(p) {
|
||||
t.Fatalf("HasRole nope should not match")
|
||||
}
|
||||
if !HasPermission("posts:write").Match(p) {
|
||||
t.Fatalf("HasPermission posts:write should match")
|
||||
}
|
||||
|
||||
// AnyLogin: short-circuit on first match.
|
||||
any1 := AnyLogin(HasRole("nope"), HasRole("admin"))
|
||||
if !any1.Match(p) {
|
||||
t.Fatalf("AnyLogin with one match should match")
|
||||
}
|
||||
any2 := AnyLogin(HasRole("nope"), HasRole("missing"))
|
||||
if any2.Match(p) {
|
||||
t.Fatalf("AnyLogin with no matches should not match")
|
||||
}
|
||||
// AnyLogin with no children: vacuously false.
|
||||
if AnyLogin().Match(p) {
|
||||
t.Fatalf("AnyLogin() should be false (no candidates can satisfy)")
|
||||
}
|
||||
|
||||
// AllLogin: every child must match.
|
||||
all1 := AllLogin(HasRole("admin"), HasRole("manager"))
|
||||
if !all1.Match(p) {
|
||||
t.Fatalf("AllLogin with all matches should match")
|
||||
}
|
||||
all2 := AllLogin(HasRole("admin"), HasRole("missing"))
|
||||
if all2.Match(p) {
|
||||
t.Fatalf("AllLogin with one missing should not match")
|
||||
}
|
||||
if !AllLogin().Match(p) {
|
||||
t.Fatalf("AllLogin() should be true (vacuous truth)")
|
||||
}
|
||||
|
||||
// Nested: Admin OR (Manager AND AdsManager). Without ads_manager, the
|
||||
// AND-arm fails but the Admin-arm succeeds.
|
||||
expr := AnyLogin(
|
||||
HasRole("admin"),
|
||||
AllLogin(HasRole("manager"), HasRole("ads_manager")),
|
||||
)
|
||||
if !expr.Match(p) {
|
||||
t.Fatalf("Admin OR (Manager AND AdsManager) should match: admin alone qualifies")
|
||||
}
|
||||
|
||||
// Same expression against a non-admin manager who lacks ads_manager:
|
||||
pNonAdmin := &Principal{Roles: []string{"manager"}}
|
||||
if expr.Match(pNonAdmin) {
|
||||
t.Fatalf("manager without ads_manager should not match the compound")
|
||||
}
|
||||
pBoth := &Principal{Roles: []string{"manager", "ads_manager"}}
|
||||
if !expr.Match(pBoth) {
|
||||
t.Fatalf("manager+ads_manager should match the AND-arm")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceKeyPredicates(t *testing.T) {
|
||||
k := &ServiceKey{Abilities: []string{"events:write", "events:read"}}
|
||||
|
||||
if !HasAbility("events:write").Match(k) {
|
||||
t.Fatalf("HasAbility events:write should match")
|
||||
}
|
||||
if HasAbility("admin:nuke").Match(k) {
|
||||
t.Fatalf("HasAbility admin:nuke should not match")
|
||||
}
|
||||
|
||||
if !AllServiceKey(HasAbility("events:write"), HasAbility("events:read")).Match(k) {
|
||||
t.Fatalf("AllServiceKey should match when key carries both")
|
||||
}
|
||||
if AnyServiceKey(HasAbility("admin:nuke"), HasAbility("missing")).Match(k) {
|
||||
t.Fatalf("AnyServiceKey should not match with no candidates")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue