function validate_azure_client_id
Validates that an Azure client ID string conforms to the standard GUID format (8-4-4-4-12 hexadecimal pattern) and is not a placeholder value.
/tf/active/vicechatdev/SPFCsync/validate_config.py
33 - 43
simple
Purpose
This function ensures that Azure AD application client IDs are properly formatted before being used in authentication workflows. It checks for placeholder values and validates the GUID structure to prevent configuration errors that would cause authentication failures. This is typically used during application initialization or configuration validation to provide early feedback on misconfigured Azure credentials.
Source Code
def validate_azure_client_id(client_id):
"""Validate Azure client ID format."""
if not client_id or client_id == "your-azure-app-client-id":
return False, "Please update AZURE_CLIENT_ID with your Azure AD app's client ID"
# GUID format: 8-4-4-4-12 characters
guid_pattern = r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
if not re.match(guid_pattern, client_id, re.IGNORECASE):
return False, "Client ID should be in GUID format (e.g., 12345678-1234-1234-1234-123456789012)"
return True, "Azure Client ID format is valid"
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
client_id |
- | - | positional_or_keyword |
Parameter Details
client_id: A string representing the Azure AD application client ID to validate. Expected to be a GUID in the format '12345678-1234-1234-1234-123456789012' (8-4-4-4-12 hexadecimal characters separated by hyphens). Can be None, empty string, or any string value. The function specifically rejects the placeholder value 'your-azure-app-client-id'.
Return Value
Returns a tuple of two elements: (bool, str). The first element is a boolean indicating validation success (True) or failure (False). The second element is a string message describing the validation result - either an error message explaining why validation failed ('Please update AZURE_CLIENT_ID...' or 'Client ID should be in GUID format...') or a success message ('Azure Client ID format is valid').
Dependencies
re
Required Imports
import re
Usage Example
import re
def validate_azure_client_id(client_id):
"""Validate Azure client ID format."""
if not client_id or client_id == "your-azure-app-client-id":
return False, "Please update AZURE_CLIENT_ID with your Azure AD app's client ID"
guid_pattern = r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
if not re.match(guid_pattern, client_id, re.IGNORECASE):
return False, "Client ID should be in GUID format (e.g., 12345678-1234-1234-1234-123456789012)"
return True, "Azure Client ID format is valid"
# Example usage
client_id = "12345678-1234-1234-1234-123456789012"
is_valid, message = validate_azure_client_id(client_id)
print(f"Valid: {is_valid}, Message: {message}")
# Test with invalid client ID
invalid_id = "invalid-client-id"
is_valid, message = validate_azure_client_id(invalid_id)
print(f"Valid: {is_valid}, Message: {message}")
# Test with placeholder
placeholder = "your-azure-app-client-id"
is_valid, message = validate_azure_client_id(placeholder)
print(f"Valid: {is_valid}, Message: {message}")
Best Practices
- Always check both the boolean and message components of the return tuple to provide appropriate user feedback
- Use this validation early in application startup or configuration loading to fail fast with clear error messages
- The function performs case-insensitive matching for the GUID pattern, accepting both uppercase and lowercase hexadecimal characters
- Consider logging the validation message for debugging purposes, but avoid logging the actual client ID in production environments for security reasons
- This function only validates format, not whether the client ID actually exists or is authorized in Azure AD
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function validate_azure_client_secret 78.7% similar
-
function test_azure_token 52.3% similar
-
function main_v19 50.2% similar
-
class AppIdPath 49.2% similar
-
class GuidCollection 47.8% similar