🔍 Code Extractor

function is_valid_auth_cookies

Maturity: 37

Validates whether a dictionary of cookies contains valid SharePoint/Office 365 authentication cookies (FedAuth or SPOIDCRL).

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/auth/providers/saml_token_provider.py
Lines:
32 - 39
Complexity:
simple

Purpose

This function checks if the provided cookie values dictionary contains the necessary authentication cookies for SharePoint Online or Office 365 services. It returns True if the dictionary is non-empty and contains either a 'FedAuth' cookie or a 'SPOIDCRL' cookie (or both), which are standard authentication tokens used by Microsoft's federated authentication system.

Source Code

def is_valid_auth_cookies(values):
    """
    Validates authorization cookies
    """
    return any(values) and (
        values.get("FedAuth", None) is not None
        or values.get("SPOIDCRL", None) is not None
    )

Parameters

Name Type Default Kind
values - - positional_or_keyword

Parameter Details

values: A dictionary-like object containing cookie key-value pairs. Expected to have string keys representing cookie names and values representing cookie content. The function specifically checks for 'FedAuth' and 'SPOIDCRL' keys. Can be None or empty, in which case the function returns False.

Return Value

Returns a boolean value. True if the 'values' parameter is truthy (non-empty) AND contains at least one of the required authentication cookies ('FedAuth' or 'SPOIDCRL') with a non-None value. Returns False if 'values' is empty/falsy or if neither required cookie is present with a non-None value.

Usage Example

# Example 1: Valid authentication cookies with FedAuth
cookies = {
    'FedAuth': 'some_auth_token_value',
    'other_cookie': 'value'
}
result = is_valid_auth_cookies(cookies)
print(result)  # Output: True

# Example 2: Valid authentication cookies with SPOIDCRL
cookies = {
    'SPOIDCRL': 'some_spoid_token',
    'session_id': '12345'
}
result = is_valid_auth_cookies(cookies)
print(result)  # Output: True

# Example 3: Invalid - missing required cookies
cookies = {
    'session_id': '12345',
    'other_cookie': 'value'
}
result = is_valid_auth_cookies(cookies)
print(result)  # Output: False

# Example 4: Invalid - empty dictionary
cookies = {}
result = is_valid_auth_cookies(cookies)
print(result)  # Output: False

# Example 5: Invalid - None value
result = is_valid_auth_cookies(None)
print(result)  # Output: False

Best Practices

  • Ensure the 'values' parameter is a dictionary or dictionary-like object with a .get() method
  • This function only validates the presence of authentication cookies, not their validity or expiration
  • Use this function as a preliminary check before attempting authenticated requests to SharePoint/Office 365
  • The function uses short-circuit evaluation with 'any(values)' first to avoid unnecessary dictionary lookups on empty/None values
  • Both FedAuth and SPOIDCRL cookies are checked because different authentication flows may use different cookie names
  • Consider additional validation of cookie content and expiration in production environments

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_sharepoint_token 58.6% similar

    Tests SharePoint OAuth2 authentication by acquiring an access token using client credentials flow and validates it with a SharePoint API call.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
  • function validate_sharepoint_url 58.0% similar

    Validates that a given URL string conforms to SharePoint site URL format requirements, checking for proper protocol, domain, and path structure.

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

    Orchestrates a comprehensive SharePoint connection diagnostic tool that validates Azure AD authentication and SharePoint access by running multiple tests and reporting results.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
  • function test_sharepoint_with_token 54.8% similar

    Tests SharePoint REST API connectivity and authentication by making a GET request to retrieve site information using a provided access token.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_permissions.py
  • function test_azure_token 54.4% similar

    Tests Azure AD authentication by attempting to acquire an OAuth2 access token using client credentials flow for Microsoft Graph API access.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
← Back to Browse