-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Garot Conklin edited this page Jan 28, 2025
·
1 revision
This page documents all public APIs available in githubauthlib.
from githubauthlib import get_github_token
def get_github_token() -> Optional[str]:
"""
Retrieves the GitHub token from the system's secure storage.
Returns:
Optional[str]: The GitHub token if found and valid, None otherwise
Raises:
CredentialHelperError: If there's an error accessing the credential helper
UnsupportedPlatformError: If the operating system is not supported
GitHubAuthError: Base exception for GitHub authentication errors
"""
try:
token = get_github_token()
if token:
print("Token retrieved successfully")
except GitHubAuthError as e:
print(f"Error: {e}")
from githubauthlib.github_auth import validate_token
def validate_token(token: str) -> bool:
"""
Validate the format of a GitHub token.
Args:
token (str): The token to validate
Returns:
bool: True if the token format is valid, False otherwise
"""
token = "ghp_1234567890abcdef1234567890abcdef123456"
is_valid = validate_token(token)
class GitHubAuthError(Exception):
"""Base exception for GitHub authentication errors."""
pass
class CredentialHelperError(GitHubAuthError):
"""Raised when there's an error with the credential helper."""
pass
class UnsupportedPlatformError(GitHubAuthError):
"""Raised when the operating system is not supported."""
pass
These functions are used internally but may be useful for debugging or extending the library:
def _get_token_macos() -> Optional[str]:
"""Retrieve token from macOS keychain."""
def _get_token_windows() -> Optional[str]:
"""Retrieve token from Windows Credential Manager."""
def _get_token_linux() -> Optional[str]:
"""Retrieve token from Linux libsecret."""
The library uses Python type hints throughout:
from typing import Optional
# Common return type for token retrieval functions
Token = Optional[str]
The library doesn't expose any public constants, but internally uses:
- Regular expression for token validation
- Logging configuration
- Platform-specific command strings
The library uses Python's standard logging module:
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
The library supports the following platforms:
-
macOS (Darwin)
- Uses
git credential-osxkeychain
- Requires Keychain Access
- Uses
-
Windows
- Uses Windows Credential Manager
- Supports
manager
,manager-core
, andwincred
helpers
-
Linux
- Uses
libsecret
- Requires
secret-tool
command-line utility
- Uses
All errors inherit from GitHubAuthError
:
Exception
└── GitHubAuthError
├── CredentialHelperError
└── UnsupportedPlatformError
- Always handle exceptions:
try:
token = get_github_token()
except GitHubAuthError as e:
# Handle error
- Validate tokens before use:
token = get_github_token()
if token and validate_token(token):
# Use token
- Configure logging as needed:
logging.getLogger('githubauthlib').setLevel(logging.DEBUG)
The current version can be accessed via:
from githubauthlib import __version__
print(__version__) # e.g., '1.0.0'