Skip to content

Add JWT authentication support#133

Draft
jameswnl wants to merge 2 commits intomainfrom
feat/jwt-authentication
Draft

Add JWT authentication support#133
jameswnl wants to merge 2 commits intomainfrom
feat/jwt-authentication

Conversation

@jameswnl
Copy link
Copy Markdown
Contributor

@jameswnl jameswnl commented Feb 13, 2026

Summary

This PR implements JWT authentication support for the AAP MCP Server, matching the functionality of the Python ansible-mcp-tools implementation.

Changes

New Features

  • JWT Authentication: Primary authentication method using X-DAB-JW-TOKEN header
  • Dual Authentication: Automatic fallback to Bearer token if JWT is not provided
  • Public Key Caching: RSA public key cached for 10 minutes for performance
  • Full JWT Validation: RS256 signature verification, claims validation (audience, issuer, expiration)

Files Added

  • src/jwt-validator.ts - Core JWT validation module (188 lines)
  • src/__tests__/jwt-validator.test.ts - Comprehensive unit tests
  • JWT_AUTHENTICATION.md - Complete documentation and usage guide
  • IMPLEMENTATION_SUMMARY.md - Implementation overview and summary

Files Modified

  • src/index.ts - Integrated JWT authentication with new authenticateRequest() function
  • package.json - Added JWT dependencies
  • package-lock.json - Updated with new dependencies

Dependencies Added

  • jsonwebtoken (^9.0.2) - JWT token validation
  • node-cache (^5.1.2) - Public key caching
  • @types/jsonwebtoken (^9.0.5) - TypeScript type definitions

Authentication Flow

Client Request
    ↓
Try JWT Authentication (X-DAB-JW-TOKEN header)
    ├─ Fetch public key from AAP Gateway (cached)
    ├─ Verify JWT signature (RS256)
    ├─ Validate claims (aud, iss, exp)
    └─ Extract username from user_data
    ↓
Success? → Use JWT token
    ↓
No JWT or Failed?
    ↓
Fallback to Bearer Token (Authorization header)
    └─ Validate against /api/gateway/v1/me/

Testing

  • ✅ All 223 tests passing
  • ✅ New unit tests for JWT validation
  • ✅ Header extraction tests
  • ✅ Cache management tests
  • ✅ Integration test framework ready (requires AAP Gateway)

Configuration

The JWT validator respects existing configuration:

  • ignore-certificate-errors: Controls SSL verification
  • Works with existing environment variables
  • No breaking changes to current authentication

Security

  • ✅ JWT signature verification using RSA public key
  • ✅ Claims validation (audience: ansible-services, issuer: ansible-issuer)
  • ✅ Token expiration automatically checked
  • ✅ Public key fetched over HTTPS
  • ✅ Backward compatible with existing Bearer token auth

Documentation

Complete documentation provided:

  • Usage examples for both JWT and Bearer token
  • Configuration guide
  • Troubleshooting section
  • Cache management utilities

Compatibility

  • ✅ Matches Python ansible-mcp-tools implementation
  • ✅ Same header name (X-DAB-JW-TOKEN)
  • ✅ Same JWT validation parameters
  • ✅ Same caching strategy
  • ✅ Backward compatible with existing authentication

🤖 Generated with Claude Code


Note

Medium Risk
Touches authentication/session initialization and adds new token verification logic plus network key-fetching and caching, which could impact login reliability or security if misconfigured.

Overview
Adds JWT authentication as the primary session-init mechanism, validating the X-DAB-JW-TOKEN header by fetching the AAP Gateway RSA public key (cached) and verifying RS256 claims, then falling back to the existing Authorization: Bearer ... flow when JWT auth is unavailable or fails.

Introduces a new src/jwt-validator.ts module plus unit tests, and wires the new authenticateRequest() flow into src/index.ts session initialization; adds jsonwebtoken and node-cache dependencies and ships accompanying documentation (JWT_AUTHENTICATION.md, IMPLEMENTATION_SUMMARY.md).

Written by Cursor Bugbot for commit 155cca3. This will update automatically on new commits. Configure here.

@jameswnl jameswnl requested review from goneri and mabashian February 13, 2026 22:41
@jameswnl jameswnl marked this pull request as draft February 13, 2026 22:41
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Comment thread src/index.ts
headers,
CONFIG.BASE_URL,
!localConfig["ignore-certificate-errors"],
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JWT TLS override uses wrong config source

Medium Severity

authenticateRequest() passes !localConfig["ignore-certificate-errors"] to validateJWT() instead of the resolved ignoreCertificateErrors value. This bypasses environment overrides from IGNORE_CERTIFICATE_ERRORS, so JWT key retrieval can run with a different TLS verification policy than the rest of the server.

Fix in Cursor Fix in Web

@jameswnl
Copy link
Copy Markdown
Contributor Author

@ldjebran @TamiTakamiya @goneri @mabashian this is a draft to make this MCP a drop-in replacement for the python version for ALIA.

No need to merge this PR, this is just a PoC.

@jameswnl jameswnl force-pushed the feat/jwt-authentication branch from 9dda07a to 1918bec Compare February 13, 2026 23:11
Implements JWT token validation to match the Python ansible-mcp-tools
implementation. The server now supports dual authentication:
- Primary: JWT authentication via X-DAB-JW-TOKEN header
- Fallback: Bearer token authentication via Authorization header

Key features:
- JWT signature validation using RS256 algorithm
- Public key caching (10 minute TTL) for performance
- Claims validation (audience, issuer, expiration)
- Username extraction from user_data claim
- Seamless fallback to existing Bearer token auth

New files:
- src/jwt-validator.ts: Core JWT validation module
- src/__tests__/jwt-validator.test.ts: Unit tests
- JWT_AUTHENTICATION.md: Comprehensive documentation
- IMPLEMENTATION_SUMMARY.md: Implementation overview

Dependencies added:
- jsonwebtoken: JWT validation library
- node-cache: Public key caching
- @types/jsonwebtoken: TypeScript types

All tests passing (223/223).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jameswnl jameswnl force-pushed the feat/jwt-authentication branch from 1918bec to a40f111 Compare February 14, 2026 04:49
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 14, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 52.71% 1047 / 1986
🔵 Statements 52.71% 1047 / 1986
🟢 Functions 91.91% (🎯 70%) 91 / 99
🟢 Branches 86.84% (🎯 70%) 231 / 266
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/index.ts 0% 100% 100% 0% 3-887
src/jwt-validator.ts 61.32% 61.11% 80% 61.32% 58-59, 70, 76-83, 86-118, 157-164, 170-171
Generated in workflow #363 for commit 145e635 by the Vitest Coverage Report Action

Copy link
Copy Markdown

@ldjebran ldjebran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this implementation will work, I can see only validation , but the JWT should work with target services directly not via gateway , I do not see this changes in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants