| 130 | ) |
| 131 | |
| 132 | def __init__(self): |
| 133 | # Selected provider name (set during authenticate()). |
| 134 | # Initializing avoids AttributeError in edge cases/tests. |
| 135 | self.oauth2_current_client = None |
| 136 | |
| 137 | for oauth2_config in config.OAUTH2_CONFIG: |
| 138 | |
| 139 | provider_name = oauth2_config.get('OAUTH2_NAME', '<unknown>') |
| 140 | |
| 141 | client_auth_method = oauth2_config.get( |
| 142 | 'OAUTH2_CLIENT_AUTH_METHOD', 'client_secret' |
| 143 | ) |
| 144 | if not isinstance(client_auth_method, str): |
| 145 | client_auth_method = 'client_secret' |
| 146 | client_auth_method = client_auth_method.strip().lower() |
| 147 | is_workload_identity = (client_auth_method == 'workload_identity') |
| 148 | |
| 149 | OAuth2Authentication.oauth2_config[ |
| 150 | oauth2_config['OAUTH2_NAME']] = oauth2_config |
| 151 | |
| 152 | # Build client_kwargs with defaults |
| 153 | client_kwargs = { |
| 154 | 'scope': oauth2_config.get( |
| 155 | 'OAUTH2_SCOPE', 'email profile'), |
| 156 | 'verify': oauth2_config.get( |
| 157 | 'OAUTH2_SSL_CERT_VERIFICATION', True) |
| 158 | } |
| 159 | |
| 160 | pkce_method = oauth2_config.get('OAUTH2_CHALLENGE_METHOD') |
| 161 | pkce_response_type = oauth2_config.get('OAUTH2_RESPONSE_TYPE') |
| 162 | pkce_is_configured = any( |
| 163 | [ |
| 164 | pkce_method is not None, |
| 165 | pkce_response_type is not None |
| 166 | ] |
| 167 | ) |
| 168 | |
| 169 | raw_client_secret = oauth2_config.get('OAUTH2_CLIENT_SECRET') |
| 170 | client_secret_is_empty = ( |
| 171 | raw_client_secret is None or |
| 172 | (isinstance(raw_client_secret, str) and |
| 173 | raw_client_secret.strip() == '') |
| 174 | ) |
| 175 | |
| 176 | if is_workload_identity: |
| 177 | if pkce_is_configured: |
| 178 | raise ValueError( |
| 179 | f'OAuth2 provider "{provider_name}" is configured ' |
| 180 | 'with OAUTH2_CLIENT_AUTH_METHOD="workload_identity" ' |
| 181 | 'and PKCE settings. Workload identity must not use ' |
| 182 | 'PKCE; remove OAUTH2_CHALLENGE_METHOD and ' |
| 183 | 'OAUTH2_RESPONSE_TYPE.' |
| 184 | ) |
| 185 | |
| 186 | if not client_secret_is_empty: |
| 187 | raise ValueError( |
| 188 | f'OAuth2 provider "{provider_name}" is configured ' |
| 189 | 'with OAUTH2_CLIENT_AUTH_METHOD="workload_identity" ' |