← All Go packages

github.com/anulum/director-ai/gateway/internal/auth

package auth // import "github.com/anulum/director-ai/gateway/internal/auth"

Package auth validates API keys in exactly the same shape as the Python
middleware (“Authorization: Bearer“ or “X-API-Key“) and produces audit
fingerprints that match “audit_salt.get_audit_salt“ on the Python side. Any
fingerprint persisted by the Go gateway is interchangeable with one persisted by
the Python server.

CONSTANTS

const (
	// KeyFingerprint is the context key under which a successfully
	// authenticated request carries its key fingerprint. Downstream
	// middleware (rate limit, audit) reads it with
	// ``r.Context().Value(KeyFingerprint)``.
	KeyFingerprint ctxKey = iota
	// KeyAuthenticated flags whether the request cleared auth. Always
	// true when set; absence means unauthenticated.
	KeyAuthenticated
)

FUNCTIONS

func FingerprintFromContext(r *http.Request) string
    FingerprintFromContext returns the audit fingerprint attached by a prior
    Handler run, or the empty string if no authentication occurred on this
    request.


TYPES

type Middleware struct {
	// Has unexported fields.
}
    Middleware validates keys against “keys“. When “keys“ is empty the
    middleware is a no-op (dev mode) — the caller is responsible for logging
    that choice.

func New(keys []string, auditSalt []byte) *Middleware
    New constructs a middleware from the decoded keys and audit salt. “keys“ may
    be empty (no-auth mode); passing “nil“ is equivalent.

func (m *Middleware) Fingerprint(key string) string
    Fingerprint returns the truncated salted SHA-512 HMAC of “key“, matching
    “director_ai.middleware.api_key._hash_key“ on the Python side (16 hex
    chars).

func (m *Middleware) Handler(next http.Handler) http.Handler
    Handler wraps “next“ with API-key validation. Exempt paths are forwarded
    unchanged. Invalid or missing keys return 401 JSON.

func (m *Middleware) SetKeys(keys []string)
    SetKeys replaces the valid key list atomically. Safe for concurrent readers
    running through Handler.