nagisa-kunhah opened a new pull request, #1600:
URL: https://github.com/apache/dubbo-admin/pull/1600

   **Please provide a description of this PR:**
   
   Add optional JWT authentication to the independent AI service, using the 
existing Admin login session to obtain short-lived access tokens.
   
   - Admin exposes a session-protected `POST /api/v1/auth/token` endpoint to 
issue RS256-signed JWTs and a public `GET /api/v1/auth/jwks` endpoint to 
publish the verification keys.
   - AI authentication middleware validates Bearer tokens against Admin's JWKS, 
including signature, issuer, audience, expiration, issued-at, and subject 
checks. Missing or invalid tokens return HTTP 401 with a `WWW-Authenticate: 
Bearer` header.
   - The frontend keeps access tokens in runtime memory, attaches them to AI 
requests, renews them before expiration, and retries once after a Bearer 
authentication challenge.
   - Admin token issuance and AI authentication are explicitly enabled through 
YAML configuration. Admin requires a persistent RSA private key when token 
issuance is enabled.
   - Existing Admin session authentication, MCP API Key authentication, and AI 
session storage behavior are preserved.
   
   This change does not introduce RBAC, user-specific AI session isolation, or 
immediate JWT revocation on logout.
   
   **AI token authentication flow**
   
   Prerequisite: the user already has a valid Admin session. Existing login 
flows are handled by Admin and are omitted here. The diagram assumes both Admin 
token issuance and AI JWT authentication are enabled.
   
   ```mermaid
   sequenceDiagram
       participant UI as Frontend
       participant API as Admin API
       participant AI as AI service
       Note over API: Holds RSA private key
       AI->>API: GET /api/v1/auth/jwks
       API-->>AI: Public keys (JWKS)
       Note over AI: Cache keys at startup and refresh when needed
       UI->>API: POST /api/v1/auth/token + Session Cookie
       API-->>UI: Short-lived RS256 access JWT
       Note over UI: Store JWT only in runtime memory
       UI->>AI: /api/v1/ai/* + Authorization: Bearer JWT
       Note over AI: Verify signature and claims locally
       AI-->>UI: Valid JWT returns API response / SSE, invalid or missing JWT 
returns 401
   ```
   
   Admin validates the existing session before issuing a token. AI uses cached 
public keys to verify tokens locally, without contacting Admin for every 
request. The frontend renews tokens before expiry and retries once after a 
Bearer 401 challenge. The private key stays on Admin.
   
   
   **Validation**
   
   - Admin: `go test ./...` — passed.
   - AI: `go test ./...` — passed.
   - Frontend: `yarn test:unit --run` — all 64 tests passed.
   - Frontend: `yarn vite build` — passed.
   - `git diff --check` — passed.
   
   **Manual verification with curl**
   
   Verified locally with Admin on port `8888`, AI on port `8880`, and JWT 
signing and verification enabled in both services. The commands below use local 
test credentials and require `jq`.
   
   1. Log in to Admin and save the session cookie:
   
   ```bash
   COOKIE_FILE=$(mktemp)
   
   curl -sS -c "$COOKIE_FILE" \
     -X POST http://localhost:8888/api/v1/auth/login \
     --data-urlencode 'user=admin' \
     --data-urlencode 'password=dubbo@2025'
   ```
   
   Observed response:
   
   ```json
   {"code":"Success","message":"success","data":true}
   ```
   
   2. Obtain an AI access token using the Admin session:
   
   ```bash
   TOKEN=$(curl -fsS -b "$COOKIE_FILE" \
     -X POST http://localhost:8888/api/v1/auth/token \
     | jq -er '.data.accessToken')
   ```
   
   Token issuance succeeded. The JWT value is omitted from this report.
   
   3. Call the AI sessions endpoint with the Bearer token:
   
   ```bash
   curl -i http://localhost:8880/api/v1/ai/sessions \
     -H "Authorization: Bearer $TOKEN"
   ```
   
   Observed: **HTTP 200 OK**, with a successful session-list response.
   
   4. Call the same endpoint without a token:
   
   ```bash
   curl -i http://localhost:8880/api/v1/ai/sessions
   ```
   
   Observed response, with unrelated headers omitted:
   
   ```http
   HTTP/1.1 401 Unauthorized
   WWW-Authenticate: Bearer
   Content-Type: application/json; charset=utf-8
   
   {"error":"invalid or missing bearer token"}
   ```
   
   Additional requests verified against the integrated version:
   
   | Request to the AI sessions endpoint | Observed result |
   | --- | --- |
   | Valid Bearer JWT | HTTP 200 |
   | No token | HTTP 401 |
   | Invalid Bearer token | HTTP 401 |
   | Admin session cookie only | HTTP 401 |
   
   These checks verify the Admin-session-to-JWT flow and AI endpoint 
authentication. They do not verify model generation, which depends on the 
configured model provider and account status.
   
   Optional cleanup:
   
   ```bash
   rm -- "$COOKIE_FILE"
   unset COOKIE_FILE TOKEN
   ```
   
   **To help us figure out who should review this PR, please put an X in all 
the areas that this PR affects.**
   
   - [ ] Docs
   - [X] Installation
   - [X] User Experience
   - [ ] Dubboctl
   - [X] Console
   - [X] Core Component
   
   **Please check any characteristics that apply to this pull request.**
   
   - [X] Adds optional authentication functionality
   - [X] Changes security-sensitive behavior
   - [X] Adds YAML configuration and schema validation
   - [X] Adds backend and frontend tests
   - [X] Preserves anonymous AI access when authentication is disabled
   - [ ] Requires a data migration
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to