-
Notifications
You must be signed in to change notification settings - Fork 14.5k
feat(a2a): Add API key authentication provider #19548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
packages/core/src/agents/auth-provider/api-key-provider.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, afterEach, vi } from 'vitest'; | ||
| import { ApiKeyAuthProvider } from './api-key-provider.js'; | ||
|
|
||
| describe('ApiKeyAuthProvider', () => { | ||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| describe('initialization', () => { | ||
| it('should initialize with literal API key', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: 'my-api-key', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const headers = await provider.headers(); | ||
| expect(headers).toEqual({ 'X-API-Key': 'my-api-key' }); | ||
| }); | ||
|
|
||
| it('should resolve API key from environment variable', async () => { | ||
| vi.stubEnv('TEST_API_KEY', 'env-api-key'); | ||
|
|
||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: '$TEST_API_KEY', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const headers = await provider.headers(); | ||
| expect(headers).toEqual({ 'X-API-Key': 'env-api-key' }); | ||
| }); | ||
|
|
||
| it('should throw if environment variable is not set', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: '$MISSING_KEY_12345', | ||
| }); | ||
|
|
||
| await expect(provider.initialize()).rejects.toThrow( | ||
| "Environment variable 'MISSING_KEY_12345' is not set", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('headers', () => { | ||
| it('should throw if not initialized', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: 'test-key', | ||
| }); | ||
|
|
||
| await expect(provider.headers()).rejects.toThrow('not initialized'); | ||
| }); | ||
|
|
||
| it('should use custom header name', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: 'my-key', | ||
| name: 'X-Custom-Auth', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const headers = await provider.headers(); | ||
| expect(headers).toEqual({ 'X-Custom-Auth': 'my-key' }); | ||
| }); | ||
|
|
||
| it('should use default header name X-API-Key', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: 'my-key', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const headers = await provider.headers(); | ||
| expect(headers).toEqual({ 'X-API-Key': 'my-key' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shouldRetryWithHeaders', () => { | ||
| it('should return undefined for non-auth errors', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: 'test-key', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const result = await provider.shouldRetryWithHeaders( | ||
| {}, | ||
| new Response(null, { status: 500 }), | ||
| ); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined for literal keys on 401 (same headers would fail again)', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: 'test-key', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const result = await provider.shouldRetryWithHeaders( | ||
| {}, | ||
| new Response(null, { status: 401 }), | ||
| ); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined for env-var keys on 403', async () => { | ||
| vi.stubEnv('RETRY_TEST_KEY', 'some-key'); | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: '$RETRY_TEST_KEY', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const result = await provider.shouldRetryWithHeaders( | ||
| {}, | ||
| new Response(null, { status: 403 }), | ||
| ); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should re-resolve and return headers for command keys on 401', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: '!echo refreshed-key', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const result = await provider.shouldRetryWithHeaders( | ||
| {}, | ||
| new Response(null, { status: 401 }), | ||
| ); | ||
| expect(result).toEqual({ 'X-API-Key': 'refreshed-key' }); | ||
| }); | ||
|
|
||
| it('should stop retrying after MAX_AUTH_RETRIES', async () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: '!echo rotating-key', | ||
| }); | ||
| await provider.initialize(); | ||
|
|
||
| const r1 = await provider.shouldRetryWithHeaders( | ||
| {}, | ||
| new Response(null, { status: 401 }), | ||
| ); | ||
| expect(r1).toBeDefined(); | ||
|
|
||
| const r2 = await provider.shouldRetryWithHeaders( | ||
| {}, | ||
| new Response(null, { status: 401 }), | ||
| ); | ||
| expect(r2).toBeDefined(); | ||
|
|
||
| const r3 = await provider.shouldRetryWithHeaders( | ||
| {}, | ||
| new Response(null, { status: 401 }), | ||
| ); | ||
| expect(r3).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('type property', () => { | ||
| it('should have type apiKey', () => { | ||
| const provider = new ApiKeyAuthProvider({ | ||
| type: 'apiKey', | ||
| key: 'test', | ||
| }); | ||
| expect(provider.type).toBe('apiKey'); | ||
| }); | ||
| }); | ||
| }); |
85 changes: 85 additions & 0 deletions
85
packages/core/src/agents/auth-provider/api-key-provider.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { HttpHeaders } from '@a2a-js/sdk/client'; | ||
| import { BaseA2AAuthProvider } from './base-provider.js'; | ||
| import type { ApiKeyAuthConfig } from './types.js'; | ||
| import { resolveAuthValue, needsResolution } from './value-resolver.js'; | ||
| import { debugLogger } from '../../utils/debugLogger.js'; | ||
|
|
||
| const DEFAULT_HEADER_NAME = 'X-API-Key'; | ||
|
|
||
| /** | ||
| * Authentication provider for API Key authentication. | ||
| * Sends the API key as an HTTP header. | ||
| * | ||
| * The API key value can be: | ||
| * - A literal string | ||
| * - An environment variable reference ($ENV_VAR) | ||
| * - A shell command (!command) | ||
| */ | ||
| export class ApiKeyAuthProvider extends BaseA2AAuthProvider { | ||
| readonly type = 'apiKey' as const; | ||
|
|
||
| private resolvedKey: string | undefined; | ||
| private readonly headerName: string; | ||
|
|
||
| constructor(private readonly config: ApiKeyAuthConfig) { | ||
| super(); | ||
| this.headerName = config.name ?? DEFAULT_HEADER_NAME; | ||
| } | ||
|
|
||
| override async initialize(): Promise<void> { | ||
| if (needsResolution(this.config.key)) { | ||
| this.resolvedKey = await resolveAuthValue(this.config.key); | ||
| debugLogger.debug( | ||
| `[ApiKeyAuthProvider] Resolved API key from: ${this.config.key.startsWith('$') ? 'env var' : 'command'}`, | ||
| ); | ||
| } else { | ||
| this.resolvedKey = this.config.key; | ||
| } | ||
| } | ||
|
|
||
| async headers(): Promise<HttpHeaders> { | ||
| if (!this.resolvedKey) { | ||
| throw new Error( | ||
| 'ApiKeyAuthProvider not initialized. Call initialize() first.', | ||
| ); | ||
| } | ||
| return { [this.headerName]: this.resolvedKey }; | ||
| } | ||
|
|
||
| /** | ||
| * Re-resolve command-based API keys on auth failure. | ||
| */ | ||
| override async shouldRetryWithHeaders( | ||
| _req: RequestInit, | ||
| res: Response, | ||
| ): Promise<HttpHeaders | undefined> { | ||
| if (res.status !== 401 && res.status !== 403) { | ||
| this.authRetryCount = 0; | ||
| return undefined; | ||
| } | ||
|
|
||
| // Only retry for command-based keys that may resolve to a new value. | ||
| // Literal and env-var keys would just resend the same failing headers. | ||
| if (!this.config.key.startsWith('!') || this.config.key.startsWith('!!')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (this.authRetryCount >= BaseA2AAuthProvider.MAX_AUTH_RETRIES) { | ||
| return undefined; | ||
| } | ||
| this.authRetryCount++; | ||
|
|
||
| debugLogger.debug( | ||
| '[ApiKeyAuthProvider] Re-resolving API key after auth failure', | ||
| ); | ||
| this.resolvedKey = await resolveAuthValue(this.config.key); | ||
|
adamfweidman marked this conversation as resolved.
|
||
|
|
||
| return this.headers(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.