Commit d32f2bf
Unify authentication with Identity struct across ToolHive (#2437)
* Unify authentication with Identity struct across ToolHive
Consolidate the vMCP Identity infrastructure into pkg/auth to eliminate
code duplication and simplify authentication flow throughout ToolHive.
Changes:
- Move Identity struct from pkg/vmcp/auth to pkg/auth as the canonical
type for representing authenticated principals
- Update all authentication middleware (OIDC, local, anonymous) to
directly create and inject Identity into context
- Remove duplicate IdentityMiddleware from vMCP (now redundant)
- Update authz and audit packages to use IdentityFromContext
- Add backward-compatible GetClaimsFromContext helper
- Delete duplicate implementations: identity.go, context.go, and
associated test files from pkg/vmcp/auth
This reduces code by ~655 lines while maintaining full functionality
and test coverage. All middleware now outputs Identity directly,
eliminating the need for a separate Claims → Identity conversion layer.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Address PR feedback: Fix token exchange integration and add test coverage
This commit addresses all feedback from PR #2437 review:
Critical fixes:
- Fix token exchange middleware to use Identity from context instead of
directly accessing ClaimsContextKey, preventing silent token exchange
failures in production (pkg/auth/tokenexchange/middleware.go)
- Update all token exchange tests to use WithIdentity pattern to match
production auth flow (pkg/auth/tokenexchange/middleware_test.go)
Code quality improvements:
- Add nil safety check in GetClaimsFromContext to guard against edge
case where nil Identity pointer is explicitly stored in context
(pkg/auth/context.go)
- Add WWW-Authenticate header for claims-to-identity conversion errors
for RFC 6750 compliance (pkg/auth/token.go)
Test coverage:
- Restore test coverage from deleted pkg/vmcp/auth files:
- pkg/auth/identity_test.go: Tests for Identity struct, MarshalJSON,
String, and claimsToIdentity conversion
- pkg/auth/context_test.go: Tests for context operations including
edge cases for nil handling and backward compatibility
All tests pass. Linting clean.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Remove dead ClaimsContextKey code
ClaimsContextKey was a legacy context key that is no longer used after
the Identity unification. All authentication middleware now uses
IdentityContextKey via WithIdentity(), and GetClaimsFromContext() reads
from Identity, never from ClaimsContextKey.
Changes:
- Remove ClaimsContextKey type definition from pkg/auth/context.go
- Remove obsolete comment reference from pkg/auth/token.go
- Update test to use realistic edge case (Identity with nil Claims)
instead of testing impossible scenario with ClaimsContextKey
All tests pass. No functional change to production code.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 028cc6d commit d32f2bf
File tree
28 files changed
+784
-805
lines changed- pkg
- audit
- authz
- auth
- tokenexchange
- vmcp/auth
28 files changed
+784
-805
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
315 | 315 | | |
316 | 316 | | |
317 | 317 | | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
322 | 322 | | |
323 | 323 | | |
324 | | - | |
325 | | - | |
326 | | - | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
327 | 327 | | |
328 | | - | |
329 | | - | |
| 328 | + | |
| 329 | + | |
330 | 330 | | |
331 | 331 | | |
332 | 332 | | |
333 | | - | |
| 333 | + | |
334 | 334 | | |
335 | 335 | | |
336 | 336 | | |
337 | | - | |
| 337 | + | |
338 | 338 | | |
339 | 339 | | |
340 | 340 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| |||
353 | 352 | | |
354 | 353 | | |
355 | 354 | | |
356 | | - | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
357 | 362 | | |
358 | 363 | | |
359 | 364 | | |
| |||
372 | 377 | | |
373 | 378 | | |
374 | 379 | | |
375 | | - | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
376 | 385 | | |
377 | 386 | | |
378 | 387 | | |
| |||
389 | 398 | | |
390 | 399 | | |
391 | 400 | | |
392 | | - | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
393 | 407 | | |
394 | 408 | | |
395 | 409 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
| 11 | + | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
16 | | - | |
| 15 | + | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
| |||
31 | 30 | | |
32 | 31 | | |
33 | 32 | | |
34 | | - | |
35 | | - | |
36 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
37 | 45 | | |
38 | 46 | | |
39 | 47 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
0 commit comments