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 in Azure authentication scenarios. This is particularly useful for validating environment variables or configuration files before attempting Azure AD authentication.
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.
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 or a success message confirming the 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
valid_id = "12345678-1234-1234-1234-123456789012"
is_valid, message = validate_azure_client_id(valid_id)
print(f"Valid: {is_valid}, Message: {message}")
invalid_id = "invalid-client-id"
is_valid, message = validate_azure_client_id(invalid_id)
print(f"Valid: {is_valid}, Message: {message}")
placeholder_id = "your-azure-app-client-id"
is_valid, message = validate_azure_client_id(placeholder_id)
print(f"Valid: {is_valid}, Message: {message}")
Best Practices
- Always check the boolean return value before proceeding with Azure authentication to avoid runtime errors
- Use the returned message string to provide user-friendly feedback when validation fails
- This function only validates format, not whether the client ID actually exists in Azure AD
- The function is case-insensitive for the hexadecimal characters in the GUID
- Consider calling this function during application startup or configuration loading to fail fast on invalid configuration
- The function explicitly checks for the placeholder value 'your-azure-app-client-id' to catch common configuration mistakes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function validate_azure_client_secret 78.3% similar
-
function validate_config 58.4% similar
-
function test_configuration_v1 58.3% similar
-
function check_configuration_v1 56.9% similar
-
function test_authentication 52.3% similar