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.