Coverage for /Users/antonigmitruk/golf/src/golf/auth/api_key.py: 0%
12 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-08-16 18:46 +0200
« prev ^ index » next coverage.py v7.6.12, created at 2025-08-16 18:46 +0200
1"""API Key authentication support for Golf MCP servers.
3This module provides a simple API key pass-through mechanism for Golf servers,
4allowing tools to access API keys from request headers and forward them to
5upstream services.
6"""
8from pydantic import BaseModel, Field
11class ApiKeyConfig(BaseModel):
12 """Configuration for API key authentication."""
14 header_name: str = Field("X-API-Key", description="Name of the header containing the API key")
15 header_prefix: str = Field(
16 "",
17 description="Optional prefix to strip from the header value (e.g., 'Bearer ')",
18 )
19 required: bool = Field(True, description="Whether API key is required for all requests")
22# Global configuration storage
23_api_key_config: ApiKeyConfig | None = None
26def configure_api_key(header_name: str = "X-API-Key", header_prefix: str = "", required: bool = True) -> None:
27 """Configure API key extraction from request headers.
29 This function should be called in auth.py to set up API key handling.
31 Args:
32 header_name: Name of the header containing the API key (default: "X-API-Key")
33 header_prefix: Optional prefix to strip from the header value (e.g., "Bearer ")
34 required: Whether API key is required for all requests (default: True)
36 Example:
37 # In auth.py
38 from golf.auth.api_key import configure_api_key
40 # Require API key for all requests
41 configure_api_key(
42 header_name="Authorization",
43 header_prefix="Bearer ",
44 required=True
45 )
47 # Or make API key optional (pass-through mode)
48 configure_api_key(
49 header_name="Authorization",
50 header_prefix="Bearer ",
51 required=False
52 )
53 """
54 global _api_key_config
55 _api_key_config = ApiKeyConfig(header_name=header_name, header_prefix=header_prefix, required=required)
58def get_api_key_config() -> ApiKeyConfig | None:
59 """Get the current API key configuration.
61 Returns:
62 The API key configuration if set, None otherwise
63 """
64 return _api_key_config
67def is_api_key_configured() -> bool:
68 """Check if API key authentication is configured.
70 Returns:
71 True if API key authentication is configured, False otherwise
72 """
73 return _api_key_config is not None