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
81
service_session_test.go
Normal file
81
service_session_test.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package authkit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestIntegration_SessionLifecycle(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
u, err := a.CreateUser(ctx, "s@s.com")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
plain, sess, err := a.IssueSession(ctx, u.ID, "ua", noIP())
|
||||
if err != nil {
|
||||
t.Fatalf("IssueSession: %v", err)
|
||||
}
|
||||
if sess.ExpiresAt.Before(time.Now()) {
|
||||
t.Fatalf("session already expired at issue")
|
||||
}
|
||||
p, err := a.AuthenticateSession(ctx, plain)
|
||||
if err != nil {
|
||||
t.Fatalf("AuthenticateSession: %v", err)
|
||||
}
|
||||
if p.UserID != u.ID {
|
||||
t.Fatalf("principal user id mismatch")
|
||||
}
|
||||
if p.Method != AuthMethodSession {
|
||||
t.Fatalf("method = %s, want session", p.Method)
|
||||
}
|
||||
if err := a.RevokeSession(ctx, plain); err != nil {
|
||||
t.Fatalf("RevokeSession: %v", err)
|
||||
}
|
||||
if _, err := a.AuthenticateSession(ctx, plain); !errors.Is(err, ErrSessionInvalid) {
|
||||
t.Fatalf("expected ErrSessionInvalid post-revoke, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_SessionCookieDefaultsSecure(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
c := a.SessionCookie("plaintext", time.Now().Add(time.Hour))
|
||||
if !c.Secure {
|
||||
t.Fatalf("Secure should default to true")
|
||||
}
|
||||
if !c.HttpOnly {
|
||||
t.Fatalf("HttpOnly should default to true")
|
||||
}
|
||||
clear := a.ClearSessionCookie()
|
||||
if clear.MaxAge != -1 || clear.Value != "" {
|
||||
t.Fatalf("ClearSessionCookie should be MaxAge=-1 and Value=\"\"")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_RevokeAllSessions(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
u, err := a.CreateUser(ctx, "ra@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
plain, _, err := a.IssueSession(ctx, u.ID, "ua", noIP())
|
||||
if err != nil {
|
||||
t.Fatalf("IssueSession: %v", err)
|
||||
}
|
||||
access, _, err := a.IssueJWT(ctx, u.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueJWT: %v", err)
|
||||
}
|
||||
if err := a.RevokeAllUserSessions(ctx, u.ID); err != nil {
|
||||
t.Fatalf("RevokeAllUserSessions: %v", err)
|
||||
}
|
||||
if _, err := a.AuthenticateSession(ctx, plain); !errors.Is(err, ErrSessionInvalid) {
|
||||
t.Fatalf("session should be revoked, got %v", err)
|
||||
}
|
||||
if _, err := a.AuthenticateJWT(ctx, access); !errors.Is(err, ErrTokenInvalid) {
|
||||
t.Fatalf("JWT should be invalidated by session_version bump, got %v", err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue