ccproxy.auth¶
ccproxy.auth
¶
Authentication module for centralized auth handling.
BearerTokenAuthManager
¶
Bases: BaseAuthManager
Authentication manager for static bearer tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
Bearer token string |
required |
Source code in ccproxy/auth/bearer.py
get_access_token
async
¶
Get the bearer token.
Returns:
Type | Description |
---|---|
str
|
Bearer token string |
Raises:
Type | Description |
---|---|
AuthenticationError
|
If token is invalid |
Source code in ccproxy/auth/bearer.py
get_credentials
async
¶
Get credentials (not supported for bearer tokens).
Raises:
Type | Description |
---|---|
AuthenticationError
|
Bearer tokens don't support full credentials |
Source code in ccproxy/auth/bearer.py
is_authenticated
async
¶
Check if bearer token is available.
Returns:
Type | Description |
---|---|
bool
|
True if token is available, False otherwise |
get_user_profile
async
¶
Get user profile (not supported for bearer tokens).
Returns:
Type | Description |
---|---|
UserProfile | None
|
None - bearer tokens don't support user profiles |
CredentialsAuthManager
¶
Bases: BaseAuthManager
Adapter to make CredentialsManager compatible with AuthManager interface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
credentials_manager
|
CredentialsManager | None
|
CredentialsManager instance, creates new if None |
None
|
Source code in ccproxy/auth/credentials_adapter.py
get_access_token
async
¶
Get valid access token from credentials manager.
Returns:
Type | Description |
---|---|
str
|
Access token string |
Raises:
Type | Description |
---|---|
AuthenticationError
|
If authentication fails |
Source code in ccproxy/auth/credentials_adapter.py
get_credentials
async
¶
Get valid credentials from credentials manager.
Returns:
Type | Description |
---|---|
ClaudeCredentials
|
Valid credentials |
Raises:
Type | Description |
---|---|
AuthenticationError
|
If authentication fails |
Source code in ccproxy/auth/credentials_adapter.py
is_authenticated
async
¶
Check if current authentication is valid.
Returns:
Type | Description |
---|---|
bool
|
True if authenticated, False otherwise |
Source code in ccproxy/auth/credentials_adapter.py
get_user_profile
async
¶
Get user profile information.
Returns:
Type | Description |
---|---|
UserProfile | None
|
UserProfile if available, None otherwise |
Source code in ccproxy/auth/credentials_adapter.py
AuthenticationRequiredError
¶
Bases: AuthenticationError
Authentication is required but not provided.
CredentialsError
¶
Bases: AuthenticationError
Base credentials error.
CredentialsExpiredError
¶
Bases: CredentialsError
Credentials expired error.
CredentialsInvalidError
¶
Bases: CredentialsError
Credentials are invalid or malformed.
CredentialsNotFoundError
¶
Bases: CredentialsError
Credentials not found error.
CredentialsStorageError
¶
Bases: CredentialsError
Error occurred during credentials storage operations.
InsufficientPermissionsError
¶
Bases: AuthenticationError
Insufficient permissions for the requested operation.
InvalidTokenError
¶
Bases: AuthenticationError
Invalid or expired token.
OAuthCallbackError
¶
Bases: OAuthError
OAuth callback failed.
OAuthError
¶
Bases: AuthenticationError
Base OAuth error.
OAuthLoginError
¶
Bases: OAuthError
OAuth login failed.
OAuthTokenRefreshError
¶
Bases: OAuthError
OAuth token refresh failed.
AuthManager
¶
Bases: Protocol
Protocol for authentication managers.
get_access_token
async
¶
Get valid access token.
Returns:
Type | Description |
---|---|
str
|
Access token string |
Raises:
Type | Description |
---|---|
AuthenticationError
|
If authentication fails |
get_credentials
async
¶
Get valid credentials.
Returns:
Type | Description |
---|---|
ClaudeCredentials
|
Valid credentials |
Raises:
Type | Description |
---|---|
AuthenticationError
|
If authentication fails |
is_authenticated
async
¶
Check if current authentication is valid.
Returns:
Type | Description |
---|---|
bool
|
True if authenticated, False otherwise |
get_user_profile
async
¶
Get user profile information.
Returns:
Type | Description |
---|---|
UserProfile | None
|
UserProfile if available, None otherwise |
BaseAuthManager
¶
Bases: ABC
Base class for authentication managers.
get_access_token
abstractmethod
async
¶
Get valid access token.
Returns:
Type | Description |
---|---|
str
|
Access token string |
Raises:
Type | Description |
---|---|
AuthenticationError
|
If authentication fails |
get_credentials
abstractmethod
async
¶
Get valid credentials.
Returns:
Type | Description |
---|---|
ClaudeCredentials
|
Valid credentials |
Raises:
Type | Description |
---|---|
AuthenticationError
|
If authentication fails |
is_authenticated
abstractmethod
async
¶
Check if current authentication is valid.
Returns:
Type | Description |
---|---|
bool
|
True if authenticated, False otherwise |
get_user_profile
async
¶
Get user profile information.
Returns:
Type | Description |
---|---|
UserProfile | None
|
UserProfile if available, None otherwise |
JsonFileTokenStorage
¶
Bases: TokenStorage
JSON file storage implementation for Claude credentials with keyring fallback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
Path
|
Path to the JSON credentials file |
required |
Source code in ccproxy/auth/storage/json_file.py
load
async
¶
Load credentials from JSON file .
Returns:
Type | Description |
---|---|
ClaudeCredentials | None
|
Parsed credentials if found and valid, None otherwise |
Raises:
Type | Description |
---|---|
CredentialsInvalidError
|
If the JSON file is invalid |
CredentialsStorageError
|
If there's an error reading the file |
Source code in ccproxy/auth/storage/json_file.py
save
async
¶
Save credentials to both keyring and JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
credentials
|
ClaudeCredentials
|
Credentials to save |
required |
Returns:
Type | Description |
---|---|
bool
|
True if saved successfully, False otherwise |
Raises:
Type | Description |
---|---|
CredentialsStorageError
|
If there's an error writing the file |
Source code in ccproxy/auth/storage/json_file.py
exists
async
¶
Check if credentials file exists.
Returns:
Type | Description |
---|---|
bool
|
True if file exists, False otherwise |
delete
async
¶
Delete credentials from both keyring and file.
Returns:
Type | Description |
---|---|
bool
|
True if deleted successfully, False otherwise |
Raises:
Type | Description |
---|---|
CredentialsStorageError
|
If there's an error deleting the file |
Source code in ccproxy/auth/storage/json_file.py
KeyringTokenStorage
¶
Bases: TokenStorage
OS keyring storage implementation for Claude credentials.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service_name
|
str
|
Name of the service in the keyring |
'claude-code-proxy'
|
username
|
str
|
Username to associate with the stored credentials |
'default'
|
Source code in ccproxy/auth/storage/keyring.py
load
async
¶
Load credentials from the OS keyring.
Returns:
Type | Description |
---|---|
ClaudeCredentials | None
|
Parsed credentials if found and valid, None otherwise |
Raises:
Type | Description |
---|---|
CredentialsStorageError
|
If the stored data is invalid |
CredentialsStorageError
|
If there's an error reading from keyring |
Source code in ccproxy/auth/storage/keyring.py
save
async
¶
Save credentials to the OS keyring.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
credentials
|
ClaudeCredentials
|
Credentials to save |
required |
Returns:
Type | Description |
---|---|
bool
|
True if saved successfully, False otherwise |
Raises:
Type | Description |
---|---|
CredentialsStorageError
|
If there's an error writing to keyring |
Source code in ccproxy/auth/storage/keyring.py
exists
async
¶
Check if credentials exist in the keyring.
Returns:
Type | Description |
---|---|
bool
|
True if credentials exist, False otherwise |
Source code in ccproxy/auth/storage/keyring.py
delete
async
¶
Delete credentials from the keyring.
Returns:
Type | Description |
---|---|
bool
|
True if deleted successfully, False otherwise |
Raises:
Type | Description |
---|---|
CredentialsStorageError
|
If there's an error deleting from keyring |
Source code in ccproxy/auth/storage/keyring.py
TokenStorage
¶
Bases: ABC
Abstract interface for token storage operations.
load
abstractmethod
async
¶
Load credentials from storage.
Returns:
Type | Description |
---|---|
ClaudeCredentials | None
|
Parsed credentials if found and valid, None otherwise |
save
abstractmethod
async
¶
Save credentials to storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
credentials
|
ClaudeCredentials
|
Credentials to save |
required |
Returns:
Type | Description |
---|---|
bool
|
True if saved successfully, False otherwise |
exists
abstractmethod
async
¶
Check if credentials exist in storage.
Returns:
Type | Description |
---|---|
bool
|
True if credentials exist, False otherwise |
delete
abstractmethod
async
¶
Delete credentials from storage.
Returns:
Type | Description |
---|---|
bool
|
True if deleted successfully, False otherwise |
CredentialsManager
¶
Manager for Claude credentials with storage and OAuth support.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
AuthSettings | None
|
Credentials configuration (uses defaults if not provided) |
None
|
storage
|
TokenStorage | None
|
Storage backend (uses JSON file storage if not provided) |
None
|
oauth_client
|
OAuthClient | None
|
OAuth client (creates one if not provided) |
None
|
http_client
|
AsyncClient | None
|
HTTP client for OAuth operations |
None
|
Source code in ccproxy/services/credentials/manager.py
find_credentials_file
async
¶
Find existing credentials file in configured paths.
Returns:
Type | Description |
---|---|
Path | None
|
Path to credentials file if found, None otherwise |
Source code in ccproxy/services/credentials/manager.py
load
async
¶
Load credentials from storage.
Returns:
Type | Description |
---|---|
ClaudeCredentials | None
|
Credentials if found and valid, None otherwise |
Source code in ccproxy/services/credentials/manager.py
save
async
¶
Save credentials to storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
credentials
|
ClaudeCredentials
|
Credentials to save |
required |
Returns:
Type | Description |
---|---|
bool
|
True if saved successfully, False otherwise |
Source code in ccproxy/services/credentials/manager.py
login
async
¶
Perform OAuth login and save credentials.
Returns:
Type | Description |
---|---|
ClaudeCredentials
|
New credentials from login |
Raises:
Type | Description |
---|---|
OAuthLoginError
|
If login fails |
Source code in ccproxy/services/credentials/manager.py
get_valid_credentials
async
¶
Get valid credentials, refreshing if necessary.
Returns:
Type | Description |
---|---|
ClaudeCredentials
|
Valid credentials |
Raises:
Type | Description |
---|---|
CredentialsNotFoundError
|
If no credentials found |
CredentialsExpiredError
|
If credentials expired and refresh fails |
Source code in ccproxy/services/credentials/manager.py
get_access_token
async
¶
Get valid access token, refreshing if necessary.
Returns:
Type | Description |
---|---|
str
|
Access token string |
Raises:
Type | Description |
---|---|
CredentialsNotFoundError
|
If no credentials found |
CredentialsExpiredError
|
If credentials expired and refresh fails |
Source code in ccproxy/services/credentials/manager.py
refresh_token
async
¶
Refresh the access token without checking expiration.
This method directly refreshes the token regardless of whether it's expired. Useful for force-refreshing tokens or testing.
Returns:
Type | Description |
---|---|
ClaudeCredentials
|
Updated credentials with new token |
Raises:
Type | Description |
---|---|
CredentialsNotFoundError
|
If no credentials found |
RuntimeError
|
If OAuth client not initialized |
ValueError
|
If no refresh token available |
Exception
|
If token refresh fails |
Source code in ccproxy/services/credentials/manager.py
fetch_user_profile
async
¶
Fetch user profile information.
Returns:
Type | Description |
---|---|
UserProfile | None
|
UserProfile if successful, None otherwise |
Source code in ccproxy/services/credentials/manager.py
get_account_profile
async
¶
Get saved account profile information.
Returns:
Type | Description |
---|---|
UserProfile | None
|
UserProfile if available, None otherwise |
validate
async
¶
Validate current credentials.
Returns:
Type | Description |
---|---|
ValidationResult
|
ValidationResult with credentials status and details |
Source code in ccproxy/services/credentials/manager.py
logout
async
¶
Delete stored credentials.
Returns:
Type | Description |
---|---|
bool
|
True if deleted successfully, False otherwise |
Source code in ccproxy/services/credentials/manager.py
get_access_token
async
¶
Get access token from authenticated manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth_manager
|
Annotated[AuthManager, Depends(require_auth)]
|
Authentication manager |
required |
Returns:
Type | Description |
---|---|
str
|
Access token string |
Raises:
Type | Description |
---|---|
HTTPException
|
If token retrieval fails |
Source code in ccproxy/auth/dependencies.py
get_auth_manager
async
¶
Get authentication manager with fallback strategy.
Try bearer token first, then fall back to credentials.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
credentials
|
Annotated[HTTPAuthorizationCredentials | None, Depends(bearer_scheme)]
|
HTTP authorization credentials |
None
|
Returns:
Type | Description |
---|---|
AuthManager
|
AuthManager instance |
Raises:
Type | Description |
---|---|
HTTPException
|
If no valid authentication available |
Source code in ccproxy/auth/dependencies.py
get_bearer_auth_manager
async
¶
Get bearer token authentication manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
credentials
|
Annotated[HTTPAuthorizationCredentials | None, Depends(bearer_scheme)]
|
HTTP authorization credentials |
required |
Returns:
Type | Description |
---|---|
AuthManager
|
BearerTokenAuthManager instance |
Raises:
Type | Description |
---|---|
HTTPException
|
If no valid bearer token provided |
Source code in ccproxy/auth/dependencies.py
get_credentials_auth_manager
async
¶
Get credentials-based authentication manager.
Returns:
Type | Description |
---|---|
AuthManager
|
CredentialsAuthManager instance |
require_auth
async
¶
Require authentication for endpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth_manager
|
Annotated[AuthManager, Depends(get_auth_manager)]
|
Authentication manager |
required |
Returns:
Type | Description |
---|---|
AuthManager
|
AuthManager instance |
Raises:
Type | Description |
---|---|
HTTPException
|
If authentication fails |