mirror of
https://github.com/mudler/LocalAI.git
synced 2026-05-29 11:07:18 -04:00
* feat(usage): add Source, APIKeyID, APIKeyName columns to UsageRecord Adds three additive columns plus UsageSource* constants. The columns are auto-migrated by InitDB. APIKeyID is a nullable foreign reference to UserAPIKey.ID; APIKeyName is snapshotted on each row so revoked keys keep showing their name in history. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(usage): backfill Source on pre-feature usage rows InitDB now classifies any pre-existing usage_record with an empty source: 'legacy-api-key' user -> legacy, everything else -> web. The backfill is idempotent (only touches NULL/empty rows). Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(usage): add GetUserUsageBySource aggregator Groups by (bucket, source, api_key_id, api_key_name). Filters out legacy by default. Returns both per-bucket detail and roll-ups (by_source, by_key sorted desc and capped at 200, grand_total). The MAX(created_at) projection is iterated via Rows().Scan into a string column and parsed manually because the SQLite driver surfaces the aggregated timestamp as a string, which database/sql refuses to scan directly into time.Time. Postgres returns a real timestamp; the same string path handles its RFC3339 form too. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(usage): log Rows() errors and assert LastUsed in tests Adds rows.Err() and Rows() open-failure logging in computeSourceTotals so silent data drops surface in logs. Logs on parseLastUsedString format misses for the same reason. Strengthens the snapshot-survival test to assert LastUsed is a recent timestamp, locking the SQLite time-string parser behaviour. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(usage): add admin GetAllUsageBySource with filters and truncation Optional user_id and api_key_id filters (composed with AND). Legacy bucket is included for admin callers. truncated=true when more than 200 distinct keys would be in the by_key roll-up. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(auth): plumb auth_source and auth_apikey through Echo context tryAuthenticate now sets auth_source on every successful branch (web for session/Bearer-session, apikey for Bearer-key/x-api-key/ token-cookie, legacy for legacy env key match). For named-key branches it also stores the resolved *UserAPIKey under auth_apikey so downstream middlewares can snapshot id+name without re-validating. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(auth): expand tryAuthenticate godoc and cover Bearer-session branch Documents all three context-keys side effects (auth_source, auth_apikey, _auth_session) plus the split of responsibilities with the parent Middleware. Adds a test for the Bearer-as-session-token classification so future regressions there fail loudly. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(usage): UsageMiddleware records source + snapshots key name Reads auth_source and auth_apikey from the Echo context (set by auth.Middleware in the previous task). Snapshots UserAPIKey.ID and Name onto each row so revoked keys remain readable in history. Falls back to source=web when no auth_source is set (auth disabled or unrecognised path). Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(usage): add /api/auth/usage/sources and admin variant Self endpoint filters legacy server-side; admin endpoint includes legacy and accepts user_id + api_key_id filters. Response includes buckets, totals.{by_source, by_key, grand_total}, and a truncated flag set when the per-key roll-up was capped at 200. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * docs(routes): mark test mirror handlers as keep-in-sync with production The newTestAuthApp helper duplicates production route handlers inline because it cannot use RegisterAuthRoutes (which requires a *application.Application). Naming the source path on each mirror makes the drift contract explicit for future maintainers. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): add usageApi.getMySources/getAdminSources + i18n strings Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): add Sources tab skeleton with data fetch Adds Usage page tab that fetches /api/auth/usage/sources (or the admin variant). Renders raw totals plus a placeholder key list; real visualisations land in subsequent commits. Restructures the existing tab button block so Models and Sources are visible to non-admins (Users remains admin-only). Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): source mix ribbon + searchable/sortable sources table Replaces the SourcesTab placeholder rendering with two reusable components: SourceMixRibbon (one segmented bar per source class) and SourcesTable (search + sort + revoked-key dim). Pulls the current API key list to detect revoked keys. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): skip revoked-key detection until the key list is known existingKeyIds defaulted to an empty Set, which made every live api_key row render as (revoked) during the brief window before apiKeysApi.list() resolved, and permanently after a fetch failure. Use null as the unknown state and suppress the revoked badge until the parent provides a real Set. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): top-N stacked time chart and drill-in chip for Sources tab Top 7 sources by total tokens get distinct colours; the rest roll up into 'Other'. Clicking a row in the SourcesTable dims everything except that series in the chart; the chip is the canonical clear. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * docs(usage): document per-API-key Sources tab and endpoints Extends features/authentication.md Usage Tracking section with: - A 'Sources' tab description and source-class taxonomy - Endpoint documentation for /api/auth/usage/sources and the admin variant - Response shape example with by_source / by_key / grand_total - Migration note about pre-feature row backfill Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(usage): silence errcheck on deferred rows.Close CI errcheck flagged the bare 'defer rows.Close()' in computeSourceTotals. Wrap in a closure that discards the close error explicitly; an error here is non-actionable since we have already drained the rows and logged any iteration failure. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactor(usage): bound batcher intake and add Shutdown/FlushNow hooks The pre-existing usage batcher had no cap on its add() path; the usageMaxPending=5000 constant only guarded the re-queue path after a failed write, leaving memory growth unbounded if the DB fell behind. This commit: - Adds the cap to add() so saturation drops new records (rate-limited warn at 1/1024) instead of growing unbounded. - Raises usageMaxPending to 50000 to absorb realistic inference bursts. - Replaces the package-level batcher global with a mutex-guarded pair plus a currentBatcher() accessor so Init / Shutdown cycles are race-free. - Adds ShutdownUsageRecorder() for graceful drain on process exit (not yet wired into app shutdown, just published). - Adds FlushNow() for deterministic tests; the middleware suite no longer needs 6s sleeps per spec and now runs in ~50ms instead of 18s. - Re-queue on failed flush is now cap-aware: prepends as much of the failed batch as fits alongside concurrent arrivals, instead of dropping the whole batch when full. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(usage): drain usage batcher on graceful shutdown Registers ShutdownUsageRecorder with the existing signals.RegisterGracefulTerminationHandler so SIGINT/SIGTERM synchronously flushes any in-memory usage records before the process exits. Without this, up to one flush interval (5s) of recorded usage was lost when LocalAI restarted. Refs: #9862 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
425 lines
15 KiB
Go
425 lines
15 KiB
Go
//go:build auth
|
|
|
|
package auth_test
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/http/auth"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var _ = Describe("Auth Middleware", func() {
|
|
|
|
Context("auth disabled, no API keys", func() {
|
|
var app *echo.Echo
|
|
|
|
BeforeEach(func() {
|
|
appConfig := config.NewApplicationConfig()
|
|
app = newAuthTestApp(nil, appConfig)
|
|
})
|
|
|
|
It("passes through all requests", func() {
|
|
rec := doRequest(app, http.MethodGet, "/v1/models")
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("passes through POST requests", func() {
|
|
rec := doRequest(app, http.MethodPost, "/v1/chat/completions")
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
})
|
|
|
|
Context("auth disabled, API keys configured", func() {
|
|
var app *echo.Echo
|
|
const validKey = "sk-test-key-123"
|
|
|
|
BeforeEach(func() {
|
|
appConfig := config.NewApplicationConfig()
|
|
appConfig.ApiKeys = []string{validKey}
|
|
app = newAuthTestApp(nil, appConfig)
|
|
})
|
|
|
|
It("returns 401 for request without key", func() {
|
|
rec := doRequest(app, http.MethodGet, "/v1/models")
|
|
Expect(rec.Code).To(Equal(http.StatusUnauthorized))
|
|
})
|
|
|
|
It("passes with valid Bearer token", func() {
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withBearerToken(validKey))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("passes with valid x-api-key header", func() {
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withXApiKey(validKey))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("passes with valid token cookie", func() {
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withTokenCookie(validKey))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("returns 401 for invalid key", func() {
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withBearerToken("wrong-key"))
|
|
Expect(rec.Code).To(Equal(http.StatusUnauthorized))
|
|
})
|
|
})
|
|
|
|
Context("auth enabled with database", func() {
|
|
var (
|
|
db *gorm.DB
|
|
app *echo.Echo
|
|
appConfig *config.ApplicationConfig
|
|
user *auth.User
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
db = testDB()
|
|
appConfig = config.NewApplicationConfig()
|
|
app = newAuthTestApp(db, appConfig)
|
|
user = createTestUser(db, "user@example.com", auth.RoleUser, auth.ProviderGitHub)
|
|
})
|
|
|
|
It("allows requests with valid session cookie", func() {
|
|
sessionID := createTestSession(db, user.ID)
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("allows requests with valid session as Bearer token", func() {
|
|
sessionID := createTestSession(db, user.ID)
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withBearerToken(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("allows requests with valid user API key as Bearer token", func() {
|
|
plaintext, _, err := auth.CreateAPIKey(db, user.ID, "test", auth.RoleUser, "", nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withBearerToken(plaintext))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("allows requests with legacy API_KEY as admin bypass", func() {
|
|
appConfig.ApiKeys = []string{"legacy-key-123"}
|
|
app = newAuthTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withBearerToken("legacy-key-123"))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("returns 401 for expired session", func() {
|
|
sessionID := createTestSession(db, user.ID)
|
|
// Manually expire (session ID in DB is the hash)
|
|
hash := auth.HashAPIKey(sessionID, "")
|
|
db.Model(&auth.Session{}).Where("id = ?", hash).
|
|
Update("expires_at", "2020-01-01")
|
|
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusUnauthorized))
|
|
})
|
|
|
|
It("returns 401 for invalid session ID", func() {
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withSessionCookie("invalid-session-id"))
|
|
Expect(rec.Code).To(Equal(http.StatusUnauthorized))
|
|
})
|
|
|
|
It("returns 401 for revoked API key", func() {
|
|
plaintext, record, err := auth.CreateAPIKey(db, user.ID, "to revoke", auth.RoleUser, "", nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
err = auth.RevokeAPIKey(db, record.ID, user.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
rec := doRequest(app, http.MethodGet, "/v1/models", withBearerToken(plaintext))
|
|
Expect(rec.Code).To(Equal(http.StatusUnauthorized))
|
|
})
|
|
|
|
It("skips auth for /api/auth/* paths", func() {
|
|
rec := doRequest(app, http.MethodGet, "/api/auth/status")
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("skips auth for PathWithoutAuth paths", func() {
|
|
rec := doRequest(app, http.MethodGet, "/healthz")
|
|
// healthz is not registered in our test app, so it'll be 404/405 but NOT 401
|
|
Expect(rec.Code).ToNot(Equal(http.StatusUnauthorized))
|
|
})
|
|
|
|
It("returns 401 for unauthenticated API requests", func() {
|
|
rec := doRequest(app, http.MethodGet, "/v1/models")
|
|
Expect(rec.Code).To(Equal(http.StatusUnauthorized))
|
|
})
|
|
|
|
It("allows unauthenticated access to non-API paths when no legacy keys", func() {
|
|
rec := doRequest(app, http.MethodGet, "/app")
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
})
|
|
|
|
Describe("RequireAdmin", func() {
|
|
var (
|
|
db *gorm.DB
|
|
appConfig *config.ApplicationConfig
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
db = testDB()
|
|
appConfig = config.NewApplicationConfig()
|
|
})
|
|
|
|
It("passes for admin user", func() {
|
|
admin := createTestUser(db, "admin@example.com", auth.RoleAdmin, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, admin.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodPost, "/api/settings", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("returns 403 for user role", func() {
|
|
user := createTestUser(db, "user@example.com", auth.RoleUser, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, user.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodPost, "/api/settings", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
|
|
It("returns 401 when no user in context", func() {
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodPost, "/api/settings")
|
|
Expect(rec.Code).To(Equal(http.StatusUnauthorized))
|
|
})
|
|
|
|
It("allows admin to access model management", func() {
|
|
admin := createTestUser(db, "admin@example.com", auth.RoleAdmin, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, admin.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodPost, "/models/apply", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("blocks user from model management", func() {
|
|
user := createTestUser(db, "user@example.com", auth.RoleUser, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, user.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodPost, "/models/apply", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
|
|
It("allows user to access regular inference endpoints", func() {
|
|
user := createTestUser(db, "user@example.com", auth.RoleUser, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, user.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodPost, "/v1/chat/completions", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("allows legacy API key (admin bypass) on admin routes", func() {
|
|
appConfig.ApiKeys = []string{"admin-key"}
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodPost, "/api/settings", withBearerToken("admin-key"))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("allows admin to access trace endpoints", func() {
|
|
admin := createTestUser(db, "admin2@example.com", auth.RoleAdmin, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, admin.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodGet, "/api/traces", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
|
|
rec = doRequest(app, http.MethodGet, "/api/backend-logs", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("blocks non-admin from trace endpoints", func() {
|
|
user := createTestUser(db, "user2@example.com", auth.RoleUser, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, user.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodGet, "/api/traces", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusForbidden))
|
|
|
|
rec = doRequest(app, http.MethodGet, "/api/backend-logs", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
|
|
It("allows admin to access agent job endpoints", func() {
|
|
admin := createTestUser(db, "admin3@example.com", auth.RoleAdmin, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, admin.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodGet, "/api/agent/tasks", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
|
|
rec = doRequest(app, http.MethodGet, "/api/agent/jobs", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("blocks non-admin from agent job endpoints", func() {
|
|
user := createTestUser(db, "user3@example.com", auth.RoleUser, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, user.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
rec := doRequest(app, http.MethodGet, "/api/agent/tasks", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusForbidden))
|
|
|
|
rec = doRequest(app, http.MethodGet, "/api/agent/jobs", withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
|
|
It("blocks non-admin from system/management endpoints", func() {
|
|
user := createTestUser(db, "user4@example.com", auth.RoleUser, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, user.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
for _, path := range []string{"/api/operations", "/api/models", "/api/backends", "/api/resources", "/api/p2p/workers", "/system", "/backend/monitor"} {
|
|
rec := doRequest(app, http.MethodGet, path, withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusForbidden), "expected 403 for path: "+path)
|
|
}
|
|
})
|
|
|
|
It("allows admin to access system/management endpoints", func() {
|
|
admin := createTestUser(db, "admin4@example.com", auth.RoleAdmin, auth.ProviderGitHub)
|
|
sessionID := createTestSession(db, admin.ID)
|
|
app := newAdminTestApp(db, appConfig)
|
|
|
|
for _, path := range []string{"/api/operations", "/api/models", "/api/backends", "/api/resources", "/api/p2p/workers", "/system", "/backend/monitor"} {
|
|
rec := doRequest(app, http.MethodGet, path, withSessionCookie(sessionID))
|
|
Expect(rec.Code).To(Equal(http.StatusOK), "expected 200 for path: "+path)
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("auth context plumbing for usage source", func() {
|
|
// probeApp builds a minimal echo app with the auth middleware and a single
|
|
// "/probe" route that captures the user, source, and apikey from context.
|
|
type probe struct {
|
|
user *auth.User
|
|
source string
|
|
key *auth.UserAPIKey
|
|
}
|
|
probeApp := func(db *gorm.DB, appConfig *config.ApplicationConfig, p *probe) *echo.Echo {
|
|
e := echo.New()
|
|
e.Use(auth.Middleware(db, appConfig))
|
|
e.GET("/probe", func(c echo.Context) error {
|
|
p.user = auth.GetUser(c)
|
|
p.source = auth.GetSource(c)
|
|
p.key = auth.GetAPIKey(c)
|
|
return c.NoContent(http.StatusOK)
|
|
})
|
|
return e
|
|
}
|
|
|
|
It("session cookie sets source=web, apikey=nil", func() {
|
|
db := testDB()
|
|
appConfig := config.NewApplicationConfig()
|
|
user := createTestUser(db, "alice@example.com", auth.RoleUser, auth.ProviderLocal)
|
|
token := createTestSession(db, user.ID)
|
|
|
|
var p probe
|
|
app := probeApp(db, appConfig, &p)
|
|
rec := doRequest(app, http.MethodGet, "/probe", withSessionCookie(token))
|
|
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
Expect(p.user).ToNot(BeNil())
|
|
Expect(p.user.ID).To(Equal(user.ID))
|
|
Expect(p.source).To(Equal(auth.UsageSourceWeb))
|
|
Expect(p.key).To(BeNil())
|
|
})
|
|
|
|
It("Bearer session token sets source=web, apikey=nil", func() {
|
|
db := testDB()
|
|
appConfig := config.NewApplicationConfig()
|
|
user := createTestUser(db, "alice@example.com", auth.RoleUser, auth.ProviderLocal)
|
|
token := createTestSession(db, user.ID)
|
|
|
|
var p probe
|
|
app := probeApp(db, appConfig, &p)
|
|
rec := doRequest(app, http.MethodGet, "/probe", withBearerToken(token))
|
|
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
Expect(p.user).ToNot(BeNil())
|
|
Expect(p.user.ID).To(Equal(user.ID))
|
|
Expect(p.source).To(Equal(auth.UsageSourceWeb))
|
|
Expect(p.key).To(BeNil())
|
|
})
|
|
|
|
It("Bearer API key sets source=apikey and exposes the resolved *UserAPIKey", func() {
|
|
db := testDB()
|
|
appConfig := config.NewApplicationConfig()
|
|
user := createTestUser(db, "alice@example.com", auth.RoleUser, auth.ProviderLocal)
|
|
plaintext, key, err := auth.CreateAPIKey(db, user.ID, "ci", auth.RoleUser, appConfig.Auth.APIKeyHMACSecret, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
var p probe
|
|
app := probeApp(db, appConfig, &p)
|
|
rec := doRequest(app, http.MethodGet, "/probe", withBearerToken(plaintext))
|
|
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
Expect(p.source).To(Equal(auth.UsageSourceAPIKey))
|
|
Expect(p.key).ToNot(BeNil())
|
|
Expect(p.key.ID).To(Equal(key.ID))
|
|
})
|
|
|
|
It("x-api-key header sets source=apikey", func() {
|
|
db := testDB()
|
|
appConfig := config.NewApplicationConfig()
|
|
user := createTestUser(db, "alice@example.com", auth.RoleUser, auth.ProviderLocal)
|
|
plaintext, _, err := auth.CreateAPIKey(db, user.ID, "ci", auth.RoleUser, appConfig.Auth.APIKeyHMACSecret, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
var p probe
|
|
app := probeApp(db, appConfig, &p)
|
|
rec := doRequest(app, http.MethodGet, "/probe", withXApiKey(plaintext))
|
|
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
Expect(p.source).To(Equal(auth.UsageSourceAPIKey))
|
|
Expect(p.key).ToNot(BeNil())
|
|
})
|
|
|
|
It("token cookie sets source=apikey", func() {
|
|
db := testDB()
|
|
appConfig := config.NewApplicationConfig()
|
|
user := createTestUser(db, "alice@example.com", auth.RoleUser, auth.ProviderLocal)
|
|
plaintext, _, err := auth.CreateAPIKey(db, user.ID, "ci", auth.RoleUser, appConfig.Auth.APIKeyHMACSecret, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
var p probe
|
|
app := probeApp(db, appConfig, &p)
|
|
rec := doRequest(app, http.MethodGet, "/probe", withTokenCookie(plaintext))
|
|
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
Expect(p.source).To(Equal(auth.UsageSourceAPIKey))
|
|
Expect(p.key).ToNot(BeNil())
|
|
})
|
|
|
|
It("legacy env key sets source=legacy, apikey=nil", func() {
|
|
db := testDB()
|
|
appConfig := config.NewApplicationConfig()
|
|
appConfig.ApiKeys = []string{"legacy-secret"}
|
|
|
|
var p probe
|
|
app := probeApp(db, appConfig, &p)
|
|
rec := doRequest(app, http.MethodGet, "/probe", withBearerToken("legacy-secret"))
|
|
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
Expect(p.source).To(Equal(auth.UsageSourceLegacy))
|
|
Expect(p.key).To(BeNil())
|
|
})
|
|
})
|
|
})
|