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
149
service_user_test.go
Normal file
149
service_user_test.go
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
package authkit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIntegration_CreateUserNoPasswordThenLoginFails(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
u, err := a.CreateUser(ctx, "alice@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
if u.PasswordHash != "" {
|
||||
t.Fatalf("password should be empty for fresh user")
|
||||
}
|
||||
// Login against the password-less user must fail with
|
||||
// ErrInvalidCredentials, not leak account existence.
|
||||
if _, err := a.LoginPassword(ctx, "alice@example.com", "anything"); !errors.Is(err, ErrInvalidCredentials) {
|
||||
t.Fatalf("expected ErrInvalidCredentials, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_CreateUserSetPasswordThenLogin(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
u, err := a.CreateUser(ctx, "bob@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
if err := a.SetPassword(ctx, u.ID, "hunter2hunter2"); err != nil {
|
||||
t.Fatalf("SetPassword: %v", err)
|
||||
}
|
||||
got, err := a.LoginPassword(ctx, "Bob@Example.com", "hunter2hunter2")
|
||||
if err != nil {
|
||||
t.Fatalf("LoginPassword (case-insensitive email): %v", err)
|
||||
}
|
||||
if got.ID != u.ID {
|
||||
t.Fatalf("user id mismatch")
|
||||
}
|
||||
if _, err := a.LoginPassword(ctx, "bob@example.com", "wrong"); !errors.Is(err, ErrInvalidCredentials) {
|
||||
t.Fatalf("expected ErrInvalidCredentials, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_CreateUserDuplicateEmail(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
if _, err := a.CreateUser(ctx, "dup@example.com"); err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
if _, err := a.CreateUser(ctx, "DUP@example.com"); !errors.Is(err, ErrEmailTaken) {
|
||||
t.Fatalf("expected ErrEmailTaken on case-folded duplicate, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_EmailVerificationFlow(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
u, err := a.CreateUser(ctx, "ev@e.com")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
tok, err := a.RequestEmailVerification(ctx, u.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("RequestEmailVerification: %v", err)
|
||||
}
|
||||
confirmed, err := a.ConfirmEmail(ctx, tok)
|
||||
if err != nil {
|
||||
t.Fatalf("ConfirmEmail: %v", err)
|
||||
}
|
||||
if confirmed.EmailVerifiedAt == nil {
|
||||
t.Fatalf("email_verified_at not set")
|
||||
}
|
||||
if _, err := a.ConfirmEmail(ctx, tok); !errors.Is(err, ErrTokenInvalid) {
|
||||
t.Fatalf("expected ErrTokenInvalid on token reuse, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_PasswordResetCascadesSessionInvalidation(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
u, err := a.CreateUser(ctx, "r@r.com")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
if err := a.SetPassword(ctx, u.ID, "old-password"); err != nil {
|
||||
t.Fatalf("SetPassword: %v", err)
|
||||
}
|
||||
plain, _, err := a.IssueSession(ctx, u.ID, "ua", noIP())
|
||||
if err != nil {
|
||||
t.Fatalf("IssueSession: %v", err)
|
||||
}
|
||||
tok, err := a.RequestPasswordReset(ctx, "r@r.com")
|
||||
if err != nil {
|
||||
t.Fatalf("RequestPasswordReset: %v", err)
|
||||
}
|
||||
if tok == "" {
|
||||
t.Fatalf("expected token for known email")
|
||||
}
|
||||
if err := a.ConfirmPasswordReset(ctx, tok, "new-password"); err != nil {
|
||||
t.Fatalf("ConfirmPasswordReset: %v", err)
|
||||
}
|
||||
if _, err := a.LoginPassword(ctx, "r@r.com", "old-password"); !errors.Is(err, ErrInvalidCredentials) {
|
||||
t.Fatalf("old password should fail, got %v", err)
|
||||
}
|
||||
if _, err := a.LoginPassword(ctx, "r@r.com", "new-password"); err != nil {
|
||||
t.Fatalf("new password should work: %v", err)
|
||||
}
|
||||
if _, err := a.AuthenticateSession(ctx, plain); !errors.Is(err, ErrSessionInvalid) {
|
||||
t.Fatalf("session should be invalidated by reset: got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_PasswordResetUnknownEmailIsSilent(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
tok, err := a.RequestPasswordReset(ctx, "nobody@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("expected silent success, got err %v", err)
|
||||
}
|
||||
if tok != "" {
|
||||
t.Fatalf("expected empty token for unknown email, got %q", tok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_ChangePasswordRevokesEverything(t *testing.T) {
|
||||
a := freshAuth(t)
|
||||
ctx := context.Background()
|
||||
u, err := a.CreateUser(ctx, "cp@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
if err := a.SetPassword(ctx, u.ID, "old-password"); err != nil {
|
||||
t.Fatalf("SetPassword: %v", err)
|
||||
}
|
||||
access, _, err := a.IssueJWT(ctx, u.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueJWT: %v", err)
|
||||
}
|
||||
if err := a.ChangePassword(ctx, u.ID, "old-password", "new-password"); err != nil {
|
||||
t.Fatalf("ChangePassword: %v", err)
|
||||
}
|
||||
if _, err := a.AuthenticateJWT(ctx, access); !errors.Is(err, ErrTokenInvalid) {
|
||||
t.Fatalf("JWT should be invalidated by password change, got %v", err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue