function setup_azure_sso_v1
Initializes and configures an Azure SSO (Single Sign-On) instance by loading credentials and settings from a configuration module.
/tf/active/vicechatdev/docchat/auth/azure_auth.py
121 - 166
moderate
Purpose
This function serves as a factory method to create an AzureSSO instance with proper configuration validation. It retrieves Azure AD authentication parameters (client ID, tenant ID, client secret, redirect URI, and scope) from a configuration module, validates that all required parameters are present, and returns a configured AzureSSO object. If any required configuration is missing or an error occurs, it logs appropriate warnings/errors and returns None. This is typically used during application initialization to set up Azure authentication capabilities.
Source Code
def setup_azure_sso(config_module=None):
"""
Set up Azure SSO with configuration from config module.
Args:
config_module: Configuration module (defaults to docchat config)
Returns:
AzureSSO: Configured AzureSSO instance or None if configuration is missing
"""
try:
if config_module is None:
import config as config_module
client_id = getattr(config_module, 'AZURE_CLIENT_ID', None)
tenant_id = getattr(config_module, 'AZURE_TENANT_ID', None)
client_secret = getattr(config_module, 'AZURE_CLIENT_SECRET', None)
redirect_uri = getattr(config_module, 'AZURE_REDIRECT_URI', None)
scope = getattr(config_module, 'AZURE_SCOPE', 'openid profile email User.Read')
logger.debug(f"Azure SSO configuration: Client ID exists: {client_id is not None}, "
f"Tenant ID exists: {tenant_id is not None}, "
f"Client Secret exists: {client_secret is not None}, "
f"Redirect URI: {redirect_uri}")
if not all([client_id, tenant_id, client_secret, redirect_uri]):
logger.warning("Azure SSO configuration is incomplete. "
f"Client ID exists: {client_id is not None}, "
f"Tenant ID exists: {tenant_id is not None}, "
f"Client Secret exists: {client_secret is not None}, "
f"Redirect URI exists: {redirect_uri is not None}")
return None
return AzureSSO(
client_id=client_id,
tenant_id=tenant_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=scope
)
except ImportError as ie:
logger.error(f"Could not import config module: {ie}")
return None
except Exception as e:
logger.error(f"Error setting up Azure SSO: {e}")
return None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
config_module |
- | None | positional_or_keyword |
Parameter Details
config_module: Optional configuration module object containing Azure SSO settings. If None (default), the function will attempt to import a module named 'config'. The module should have attributes: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, AZURE_REDIRECT_URI, and optionally AZURE_SCOPE. Expected to be a Python module object with these configuration attributes.
Return Value
Returns an AzureSSO instance if all required configuration parameters (client_id, tenant_id, client_secret, redirect_uri) are present and valid. Returns None if: (1) any required configuration parameter is missing, (2) the config module cannot be imported, or (3) any exception occurs during setup. The AzureSSO instance, when returned, is fully configured and ready to perform Azure AD authentication operations.
Dependencies
msallogging
Required Imports
import logging
from azure_sso_module import AzureSSO
Conditional/Optional Imports
These imports are only needed under specific conditions:
import config as config_module
Condition: only when config_module parameter is None (default behavior)
Required (conditional)Usage Example
# Example 1: Using default config module
from azure_sso_setup import setup_azure_sso
import logging
# Setup logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
# Setup Azure SSO with default config
azure_sso = setup_azure_sso()
if azure_sso:
print("Azure SSO configured successfully")
else:
print("Azure SSO configuration failed")
# Example 2: Using custom config module
import types
# Create custom config
custom_config = types.ModuleType('custom_config')
custom_config.AZURE_CLIENT_ID = 'your-client-id'
custom_config.AZURE_TENANT_ID = 'your-tenant-id'
custom_config.AZURE_CLIENT_SECRET = 'your-client-secret'
custom_config.AZURE_REDIRECT_URI = 'https://yourapp.com/callback'
custom_config.AZURE_SCOPE = 'openid profile email'
azure_sso = setup_azure_sso(config_module=custom_config)
if azure_sso:
# Use azure_sso for authentication
pass
Best Practices
- Always check if the returned value is None before using the AzureSSO instance
- Ensure all required Azure AD configuration parameters are set in your config module before calling this function
- Store Azure credentials securely (use environment variables or secure vaults, not hardcoded values)
- Configure appropriate logging level to capture debug/warning messages for troubleshooting
- The function gracefully handles missing configuration by returning None rather than raising exceptions
- Use custom config_module parameter for testing or when managing multiple Azure AD configurations
- The AZURE_SCOPE parameter is optional and defaults to 'openid profile email User.Read' if not specified
- Ensure the AzureSSO class is properly imported and available in the module scope before calling this function
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function setup_azure_sso 81.4% similar
-
class AzureSSO 58.7% similar
-
class AzureSSO_v1 58.3% similar
-
class AzureSSO_v2 57.9% similar
-
function auth_callback 55.8% similar