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
26
tokens.go
26
tokens.go
|
|
@ -3,7 +3,6 @@ package authkit
|
|||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"strings"
|
||||
|
|
@ -26,9 +25,7 @@ const (
|
|||
|
||||
// MintOpaqueSecret generates a fresh opaque secret with the given prefix.
|
||||
// Returns the plaintext (show once, never persist) and the SHA-256 lookup
|
||||
// hash. A nil rng falls back to crypto/rand.Reader. Exposed so consumers
|
||||
// building bespoke storage can produce secrets in the same shape authkit
|
||||
// uses internally.
|
||||
// hash. A nil rng falls back to crypto/rand.Reader.
|
||||
func MintOpaqueSecret(rng io.Reader, prefix string) (plaintext string, hash []byte, err error) {
|
||||
const op = "authkit.MintOpaqueSecret"
|
||||
if rng == nil {
|
||||
|
|
@ -53,7 +50,7 @@ func HashOpaqueSecret(plaintext string) []byte {
|
|||
}
|
||||
|
||||
// ParseOpaqueSecret validates that a plaintext begins with the expected
|
||||
// prefix and returns the lookup hash. Returns ok=false on prefix mismatch.
|
||||
// prefix and returns the lookup hash.
|
||||
func ParseOpaqueSecret(prefix, plaintext string) (hash []byte, ok bool) {
|
||||
want := prefix + "_"
|
||||
if !strings.HasPrefix(plaintext, want) {
|
||||
|
|
@ -61,22 +58,3 @@ func ParseOpaqueSecret(prefix, plaintext string) (hash []byte, ok bool) {
|
|||
}
|
||||
return HashOpaqueSecret(plaintext), true
|
||||
}
|
||||
|
||||
// mintSecret is the internal entry point; existing callers pass prefix
|
||||
// first to match call-site readability ("mint a token of kind X").
|
||||
func mintSecret(prefix string, rng io.Reader) (plaintext string, hash []byte, err error) {
|
||||
return MintOpaqueSecret(rng, prefix)
|
||||
}
|
||||
|
||||
func hashSecret(plaintext string) []byte {
|
||||
return HashOpaqueSecret(plaintext)
|
||||
}
|
||||
|
||||
func parseSecret(prefix, plaintext string) (hash []byte, ok bool) {
|
||||
return ParseOpaqueSecret(prefix, plaintext)
|
||||
}
|
||||
|
||||
// constantTimeEqual is a thin wrapper for readability at call sites.
|
||||
func constantTimeEqual(a, b []byte) bool {
|
||||
return subtle.ConstantTimeCompare(a, b) == 1
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue