Authentication Module
Overview
The authentication module provides OAuth2-based authentication functionality for the Strata Cloud Manager (SCM) API. It handles token management, including acquisition, validation, refresh, and JWT token operations, with robust error handling and retry mechanisms.
Core Components
OAuth2Client
The main class that handles OAuth2 authentication with Palo Alto Networks' services.
def __init__(self, auth_request: AuthRequestModel)
Configuration Constants
MAX_RETRIES
: Maximum number of retry attempts (default: 3)RETRY_BACKOFF
: Backoff factor for retries (default: 0.3)TOKEN_EXPIRY_BUFFER
: Buffer time before token expiry in seconds (default: 300)
Attributes
auth_request
: Authentication parameters containersession
: Authenticated OAuth2 sessionsigning_key
: Key used for JWT token verification
Authentication Flow
Session Creation
The client establishes an authenticated session using the following steps:
- Creates a BackendApplicationClient with the provided client ID
- Configures retry strategy for resilient connections
- Fetches initial OAuth2 token
- Sets up HTTP adapters with retry logic
AuthRequestModel(
client_id="your_client_id",
client_secret="your_client_secret",
token_url="https://auth.example.com/token",
scope="your_scope",
tsg_id="your_tsg_id"
)
)
Retry Strategy
The client implements a robust retry mechanism for handling transient failures:
- Retries on status codes: 408, 429, 500, 502, 503, 504
- Supports retries on both POST and GET methods
- Uses exponential backoff
- Configurable maximum retry attempts
total=3,
backoff_factor=0.3,
status_forcelist=[408, 429, 500, 502, 503, 504],
allowed_methods=["POST", "GET"]
)
Token Management
Token Validation
The client provides several methods for token validation and management:
Token Expiration Check
auth_client.refresh_token()
# Check if token has already expiredif auth_client.is_expired:
auth_client.refresh_token()
Token Decoding
payload = auth_client.decode_token()
except ExpiredSignatureError:
# Handle expired token
auth_client.refresh_token()
Token Refresh
The client handles automatic token refresh with the following features:
- Creates new session for refresh requests to avoid stale state
- Implements retry logic for resilient token refresh
- Updates signing key after successful refresh
- Comprehensive error handling
auth_client.refresh_token()
except APIError as e:
# Handle refresh failure
logger.error(f"Token refresh failed: {e}")
Error Handling
The module implements comprehensive error handling for various scenarios:
Network Errors
- Connection errors
- Timeout errors
- Request exceptions
Authentication Errors
- Token expiration
- Invalid credentials
- JWT decode errors
- Signing key retrieval failures
HTTP Errors
- Server errors (5xx)
- Client errors (4xx)
- Gateway timeouts
Example error handling:
auth_client = OAuth2Client(auth_request)
except APIError as e:
if "Network error" in str(e):
# Handle network-related errors
logger.error(f"Network error: {e}")
elif "HTTP error" in str(e):
# Handle HTTP-related errors
logger.error(f"HTTP error: {e}")
else:
# Handle other API errors
logger.error(f"Authentication error: {e}")
Security Features
The client implements several security best practices:
-
JWT Token Verification
- Uses RS256 algorithm for token verification
- Validates token audience
- Verifies token signature using JWK
-
SSL/TLS Security
- Enforces SSL verification
- Uses secure token endpoints
-
Token Expiration Management
- Implements buffer time before expiration
- Automatic token refresh
- Validates token expiration
Logging
The module includes comprehensive logging for troubleshooting:
- Debug-level logs for token operations
- Error-level logs for failures
- Detailed error messages for exceptional cases
# Logs will include:# - Token fetch operations# - Refresh attempts# - Error conditions# - Network issues
Dependencies
The module requires the following external libraries:
jwt
: For JWT token operationsoauthlib
: For OAuth2 implementationrequests
: For HTTP operationsrequests_oauthlib
: For OAuth2 session management
Best Practices
-
Token Management
- Always check token expiration before making API calls
- Implement proper error handling for token refresh
- Use the token expiry buffer to prevent last-minute expiration
-
Error Handling
- Catch specific exceptions for different error scenarios
- Implement proper logging for troubleshooting
- Use retry mechanisms for transient failures
-
Security
- Store credentials securely
- Use environment variables for sensitive information
- Implement proper token validation