|
| 1 | +"""Factory to build authentication strategies based on server and credentials.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import ssl |
| 6 | + |
| 7 | +from aiohttp import ClientSession |
| 8 | + |
| 9 | +from pyoverkiz.auth.credentials import ( |
| 10 | + Credentials, |
| 11 | + LocalTokenCredentials, |
| 12 | + RexelOAuthCodeCredentials, |
| 13 | + TokenCredentials, |
| 14 | + UsernamePasswordCredentials, |
| 15 | +) |
| 16 | +from pyoverkiz.auth.strategies import ( |
| 17 | + AuthStrategy, |
| 18 | + BearerTokenAuthStrategy, |
| 19 | + CozytouchAuthStrategy, |
| 20 | + LocalTokenAuthStrategy, |
| 21 | + NexityAuthStrategy, |
| 22 | + RexelAuthStrategy, |
| 23 | + SessionLoginStrategy, |
| 24 | + SomfyAuthStrategy, |
| 25 | +) |
| 26 | +from pyoverkiz.enums import APIType, Server |
| 27 | +from pyoverkiz.models import ServerConfig |
| 28 | + |
| 29 | + |
| 30 | +def build_auth_strategy( |
| 31 | + *, |
| 32 | + server_config: ServerConfig, |
| 33 | + credentials: Credentials, |
| 34 | + session: ClientSession, |
| 35 | + ssl_context: ssl.SSLContext | bool, |
| 36 | +) -> AuthStrategy: |
| 37 | + """Build the correct auth strategy for the given server and credentials.""" |
| 38 | + server: Server | None = server_config.server |
| 39 | + |
| 40 | + if server == Server.SOMFY_EUROPE: |
| 41 | + return SomfyAuthStrategy( |
| 42 | + _ensure_username_password(credentials), |
| 43 | + session, |
| 44 | + server_config, |
| 45 | + ssl_context, |
| 46 | + server_config.type, |
| 47 | + ) |
| 48 | + |
| 49 | + if server in { |
| 50 | + Server.ATLANTIC_COZYTOUCH, |
| 51 | + Server.THERMOR_COZYTOUCH, |
| 52 | + Server.SAUTER_COZYTOUCH, |
| 53 | + }: |
| 54 | + return CozytouchAuthStrategy( |
| 55 | + _ensure_username_password(credentials), |
| 56 | + session, |
| 57 | + server_config, |
| 58 | + ssl_context, |
| 59 | + server_config.type, |
| 60 | + ) |
| 61 | + |
| 62 | + if server == Server.NEXITY: |
| 63 | + return NexityAuthStrategy( |
| 64 | + _ensure_username_password(credentials), |
| 65 | + session, |
| 66 | + server_config, |
| 67 | + ssl_context, |
| 68 | + server_config.type, |
| 69 | + ) |
| 70 | + |
| 71 | + if server == Server.REXEL: |
| 72 | + return RexelAuthStrategy( |
| 73 | + _ensure_rexel(credentials), |
| 74 | + session, |
| 75 | + server_config, |
| 76 | + ssl_context, |
| 77 | + server_config.type, |
| 78 | + ) |
| 79 | + |
| 80 | + if server_config.type == APIType.LOCAL: |
| 81 | + if isinstance(credentials, LocalTokenCredentials): |
| 82 | + return LocalTokenAuthStrategy( |
| 83 | + credentials, session, server_config, ssl_context, server_config.type |
| 84 | + ) |
| 85 | + return BearerTokenAuthStrategy( |
| 86 | + _ensure_token(credentials), |
| 87 | + session, |
| 88 | + server_config, |
| 89 | + ssl_context, |
| 90 | + server_config.type, |
| 91 | + ) |
| 92 | + |
| 93 | + if isinstance(credentials, TokenCredentials) and not isinstance( |
| 94 | + credentials, LocalTokenCredentials |
| 95 | + ): |
| 96 | + return BearerTokenAuthStrategy( |
| 97 | + credentials, session, server_config, ssl_context, server_config.type |
| 98 | + ) |
| 99 | + |
| 100 | + return SessionLoginStrategy( |
| 101 | + _ensure_username_password(credentials), |
| 102 | + session, |
| 103 | + server_config, |
| 104 | + ssl_context, |
| 105 | + server_config.type, |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +def _ensure_username_password(credentials: Credentials) -> UsernamePasswordCredentials: |
| 110 | + """Validate that credentials are username/password based.""" |
| 111 | + if not isinstance(credentials, UsernamePasswordCredentials): |
| 112 | + raise TypeError("UsernamePasswordCredentials are required for this server.") |
| 113 | + return credentials |
| 114 | + |
| 115 | + |
| 116 | +def _ensure_token(credentials: Credentials) -> TokenCredentials: |
| 117 | + """Validate that credentials carry a bearer token.""" |
| 118 | + if not isinstance(credentials, TokenCredentials): |
| 119 | + raise TypeError("TokenCredentials are required for this server.") |
| 120 | + return credentials |
| 121 | + |
| 122 | + |
| 123 | +def _ensure_rexel(credentials: Credentials) -> RexelOAuthCodeCredentials: |
| 124 | + """Validate that credentials are of Rexel OAuth code type.""" |
| 125 | + if not isinstance(credentials, RexelOAuthCodeCredentials): |
| 126 | + raise TypeError("RexelOAuthCodeCredentials are required for this server.") |
| 127 | + return credentials |
0 commit comments