Coverage for server / authentication / auth0_config.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-04 09:33 +0000

1""" 

2Auth0 Configuration Module 

3 

4Manages Auth0 settings and configuration from environment variables. 

5Provides a centralized configuration object for Auth0 authentication. 

6 

7Usage: 

8 from server.authentication.auth0_config import get_auth0_settings 

9 

10 settings = get_auth0_settings() 

11 print(settings.AUTH0_DOMAIN) 

12""" 

13 

14from pydantic_settings import BaseSettings 

15from functools import lru_cache 

16 

17 

18class Auth0Settings(BaseSettings): 

19 """ 

20 Auth0 configuration settings loaded from environment variables. 

21 

22 All settings are loaded from .env file or environment variables. 

23 Uses Pydantic for automatic validation and type conversion. 

24 

25 Attributes: 

26 AUTH0_DOMAIN: Auth0 tenant domain (e.g., 'tenant.us.auth0.com') 

27 AUTH0_CLIENT_ID: Application client ID from Auth0 dashboard 

28 AUTH0_CLIENT_SECRET: Application client secret (keep secure) 

29 AUTH0_API_IDENTIFIER: API identifier/audience for token validation 

30 AUTH0_CALLBACK_URL: OAuth callback URL for authorization code flow 

31 AUTH0_LOGOUT_URL: URL to redirect after logout 

32 AUTH0_ALGORITHMS: List of allowed JWT signing algorithms 

33 AUTH0_ISSUER: Token issuer URL (auto-generated from domain) 

34 AUTH0_ROLES_CLAIM: Custom namespace for role claims in JWT 

35 """ 

36 

37 AUTH0_DOMAIN: str 

38 AUTH0_CLIENT_ID: str 

39 AUTH0_CLIENT_SECRET: str 

40 AUTH0_API_IDENTIFIER: str 

41 AUTH0_CALLBACK_URL: str = "http://localhost:8000/v1/auth0-test/callback" 

42 AUTH0_LOGOUT_URL: str = "http://localhost:3000" 

43 AUTH0_ALGORITHMS: list[str] = ["RS256"] 

44 AUTH0_ISSUER: str = "" 

45 AUTH0_ROLES_CLAIM: str = "https://eruditiontx.com/roles" 

46 

47 class Config: 

48 """Pydantic configuration""" 

49 env_file = ".env" 

50 case_sensitive = True 

51 extra = "ignore" 

52 

53 def __init__(self, **kwargs): 

54 """ 

55 Initialize Auth0 settings and auto-generate issuer URL. 

56 

57 The issuer URL is automatically generated from the AUTH0_DOMAIN 

58 if not explicitly provided in environment variables. 

59 """ 

60 super().__init__(**kwargs) 

61 if not self.AUTH0_ISSUER: 

62 self.AUTH0_ISSUER = f"https://{self.AUTH0_DOMAIN}/" 

63 

64 

65@lru_cache() 

66def get_auth0_settings() -> Auth0Settings: 

67 """ 

68 Get cached Auth0 settings instance. 

69 

70 Uses LRU cache to ensure settings are loaded only once 

71 and reused throughout the application lifecycle. 

72 

73 Returns: 

74 Auth0Settings: Configured Auth0 settings object 

75 

76 Example: 

77 >>> settings = get_auth0_settings() 

78 >>> print(f"Domain: {settings.AUTH0_DOMAIN}") 

79 """ 

80 return Auth0Settings()