Source code for apifrom.utils.csrf

from typing import Optional, Set, List, Dict, Any, Callable, Union

[docs] def generate_csrf_token(session_id: Optional[str] = None) -> str: """ Generate a CSRF token for the given session ID. Args: session_id: The session ID to generate the token for. Returns: str: The generated CSRF token. """ # Implementation return ""
[docs] def verify_csrf_token(token: str, session_id: Optional[str] = None) -> bool: """ Verify that the given CSRF token is valid for the session. Args: token: The CSRF token to verify. session_id: The session ID to verify the token against. Returns: bool: True if the token is valid, False otherwise. """ # Implementation return True
[docs] class CSRFProtection: """ Class that provides CSRF protection middleware and utilities. """ def __init__( self, secret_key: str, cookie_name: str = "csrf_token", header_name: str = "X-CSRF-Token", form_field_name: str = "csrf_token", exempt_methods: Optional[Set[str]] = None, exempt_routes: Optional[List[str]] = None, token_expiry: int = 3600 ): """ Initialize the CSRF protection middleware. Args: secret_key: The secret key used to sign CSRF tokens. cookie_name: The name of the cookie to store the CSRF token in. header_name: The name of the header to look for the CSRF token in. form_field_name: The name of the form field to look for the CSRF token in. exempt_methods: HTTP methods that are exempt from CSRF protection. exempt_routes: Routes that are exempt from CSRF protection. token_expiry: How long CSRF tokens are valid for, in seconds. """ self.secret_key = secret_key self.cookie_name = cookie_name self.header_name = header_name self.form_field_name = form_field_name self.exempt_methods = exempt_methods or {"GET", "HEAD", "OPTIONS"} self.exempt_routes = exempt_routes or [] self.token_expiry = token_expiry