🔍 Code Extractor

function validate_azure_client_id

Maturity: 42

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.

File:
/tf/active/vicechatdev/SPFCsync/validate_config.py
Lines:
33 - 43
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function validate_azure_client_secret 78.3% similar

    Validates an Azure client secret by checking for placeholder values, minimum length requirements, and common invalid patterns.

    From: /tf/active/vicechatdev/SPFCsync/validate_config.py
  • function validate_config 58.4% similar

    Validates the presence of required Microsoft 365 configuration settings and raises an error if any are missing.

    From: /tf/active/vicechatdev/email-forwarder/src/config/settings.py
  • function test_configuration_v1 58.3% similar

    Validates that all required configuration variables (Azure AD credentials, OpenAI API key, and domain) are properly set and not using placeholder values.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
  • function check_configuration_v1 56.9% similar

    Validates the presence of a .env configuration file and checks that all required environment variables (TENANT_ID, CLIENT_ID, CLIENT_SECRET, FROM_EMAIL) are defined.

    From: /tf/active/vicechatdev/email-forwarder/run_service.py
  • function test_authentication 52.3% similar

    Tests Azure AD authentication for a VendorEmailExtractor instance by attempting to authenticate and providing detailed troubleshooting guidance on failure.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
← Back to Browse