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
101
store_scan.go
Normal file
101
store_scan.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package authkit
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// rowScanner is satisfied by both *sql.Row and *sql.Rows so scan helpers
|
||||
// serve QueryRow and Query loops uniformly.
|
||||
type rowScanner interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
|
||||
func nullableTime(t *time.Time) any {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
return *t
|
||||
}
|
||||
|
||||
func nullableString(s string) any {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func nullableLabel(s string) any {
|
||||
// Labels are user-facing display strings — empty string means "no label",
|
||||
// which we store NULL for clarity.
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func nullableAddrString(a netip.Addr) any {
|
||||
if !a.IsValid() {
|
||||
return nil
|
||||
}
|
||||
return a.String()
|
||||
}
|
||||
|
||||
func scanAddr(s *string) (netip.Addr, error) {
|
||||
if s == nil || *s == "" {
|
||||
return netip.Addr{}, nil
|
||||
}
|
||||
return netip.ParseAddr(*s)
|
||||
}
|
||||
|
||||
func uuidArg(id uuid.UUID) any { return id.String() }
|
||||
|
||||
func scanUUID(s string) (uuid.UUID, error) { return uuid.Parse(s) }
|
||||
|
||||
func chainArg(c *string) any {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return *c
|
||||
}
|
||||
|
||||
func nullableInt(n *int) any {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
return *n
|
||||
}
|
||||
|
||||
func scanNullStringPtr(ns sql.NullString) *string {
|
||||
if !ns.Valid {
|
||||
return nil
|
||||
}
|
||||
v := ns.String
|
||||
return &v
|
||||
}
|
||||
|
||||
func scanNullString(ns sql.NullString) string {
|
||||
if !ns.Valid {
|
||||
return ""
|
||||
}
|
||||
return ns.String
|
||||
}
|
||||
|
||||
func scanNullTimePtr(nt sql.NullTime) *time.Time {
|
||||
if !nt.Valid {
|
||||
return nil
|
||||
}
|
||||
t := nt.Time
|
||||
return &t
|
||||
}
|
||||
|
||||
func scanNullIntPtr(ni sql.NullInt32) *int {
|
||||
if !ni.Valid {
|
||||
return nil
|
||||
}
|
||||
v := int(ni.Int32)
|
||||
return &v
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue