WeKnora/internal/middleware
wizardchen a55b09ea82 refactor(auth): unify session-context attachment and split the Auth middleware
The five near-identical "c.Set + context.WithValue" blocks (JWT branch,
tenantless branch, both API-key attach helpers, EmbedAuth) now go through
one applyAuthSession helper, so the c.Keys surface and the request-context
surface can no longer drift apart. The 140-line JWT branch inside Auth is
split into authenticateJWTUser / resolveTargetTenant, which also removes a
duplicate GetTenantByID roundtrip on the X-Tenant-ID switch path.

Fixes folded in:
- delete GetTenantIDFromContext: it read ctx.Value("tenantID") while the
  value is stored under types.TenantIDContextKey ("TenantID"), so it could
  never succeed (dead, broken, uncalled)
- isPlatformTenantOptionalAPI: match "/api/v1/system/admin" exactly or as
  a "/"-delimited prefix instead of a bare HasPrefix that would also admit
  sibling paths sharing the prefix
- 401 for a presented-but-invalid Bearer token now says "invalid or
  expired token" instead of the misleading "missing authentication"
- replace the remaining log.Printf calls in the auth path with the
  request-scoped structured logger

New tests lock the both-surfaces invariant of applyAuthSession, the
tenantless "no tenant/role keys" contract, and bearerToken parsing.
2026-07-27 11:25:23 +08:00
..
asynqdl fix: mark knowledge delete failures after retry exhaustion 2026-06-16 16:47:46 +08:00
access.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
access_test.go refactor(tenant_member): simplify tenant ID resolution in handlers 2026-05-18 17:28:58 +08:00
api_key_gate.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
api_key_gate_test.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
audit_provider.go feat(rbac): per-tenant audit log for RBAC events (PR 6, #1303) 2026-05-18 17:28:58 +08:00
auth.go refactor(auth): unify session-context attachment and split the Auth middleware 2026-07-27 11:25:23 +08:00
auth_api_principal_test.go fix: harden API principal config after review 2026-06-30 20:01:05 +08:00
auth_context.go refactor(auth): unify session-context attachment and split the Auth middleware 2026-07-27 11:25:23 +08:00
auth_context_test.go refactor(auth): unify session-context attachment and split the Auth middleware 2026-07-27 11:25:23 +08:00
auth_platform_api_key_test.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
auth_public_ratelimit.go feat(embed): add backend embed channels with auth and rate limiting (#1657) 2026-06-12 23:10:39 +08:00
auth_role_test.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
auth_tenantless_test.go refactor(auth): unify OIDC provisioning under shared default_tenant_mode policy 2026-07-13 19:14:49 +08:00
embed_auth.go refactor(auth): unify session-context attachment and split the Auth middleware 2026-07-27 11:25:23 +08:00
embed_auth_test.go refactor: remove Neo4j-based conversation memory feature 2026-07-18 23:41:48 +08:00
error_handler.go init commit 2025-08-05 15:08:07 +08:00
kb_access.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
kb_access_test.go fix(security): harden agent share listing and align full-access API key routes 2026-07-13 17:09:58 +08:00
language.go fix(i18n): use WEKNORA_LANGUAGE env for prompt language fallback instead of hardcoded zh-CN 2026-04-02 12:01:25 +08:00
logger.go fix(mcp): coordinate OAuth token refresh and authorization polling 2026-07-23 15:35:24 +08:00
logger_test.go fix(mcp): coordinate OAuth token refresh and authorization polling 2026-07-23 15:35:24 +08:00
rbac.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
rbac_api_key_shortcircuit_test.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
rbac_audit_test.go feat(rbac): per-tenant audit log for RBAC events (PR 6, #1303) 2026-05-18 17:28:58 +08:00
rbac_test.go feat(models): let system admins edit builtin model config 2026-07-14 15:56:02 +08:00
recovery.go fix: replace standard log with structured logger in main and recovery middleware 2026-02-06 17:59:15 +08:00