Generates a response containing the auth uri for user to sign in. Returns: An AuthCredential object containing the auth URI and state. Raises: ValueError: If the authorization endpoint is not configured in the auth scheme.
(
self,
)
| 150 | ) |
| 151 | |
| 152 | def generate_auth_uri( |
| 153 | self, |
| 154 | ) -> AuthCredential: |
| 155 | """Generates a response containing the auth uri for user to sign in. |
| 156 | |
| 157 | Returns: |
| 158 | An AuthCredential object containing the auth URI and state. |
| 159 | |
| 160 | Raises: |
| 161 | ValueError: If the authorization endpoint is not configured in the auth |
| 162 | scheme. |
| 163 | """ |
| 164 | if not AUTHLIB_AVAILABLE: |
| 165 | return ( |
| 166 | self.auth_config.raw_auth_credential.model_copy(deep=True) |
| 167 | if self.auth_config.raw_auth_credential |
| 168 | else None |
| 169 | ) |
| 170 | |
| 171 | auth_scheme = self.auth_config.auth_scheme |
| 172 | auth_credential = self.auth_config.raw_auth_credential |
| 173 | if not auth_credential or not auth_credential.oauth2: |
| 174 | raise ValueError("raw_auth_credential or oauth2 is empty") |
| 175 | |
| 176 | if isinstance(auth_scheme, OpenIdConnectWithConfig): |
| 177 | authorization_endpoint = auth_scheme.authorization_endpoint |
| 178 | scopes = _normalize_oauth_scopes(auth_scheme.scopes) |
| 179 | else: |
| 180 | authorization_endpoint = ( |
| 181 | auth_scheme.flows.implicit |
| 182 | and auth_scheme.flows.implicit.authorizationUrl |
| 183 | or auth_scheme.flows.authorizationCode |
| 184 | and auth_scheme.flows.authorizationCode.authorizationUrl |
| 185 | or auth_scheme.flows.clientCredentials |
| 186 | and auth_scheme.flows.clientCredentials.tokenUrl |
| 187 | or auth_scheme.flows.password |
| 188 | and auth_scheme.flows.password.tokenUrl |
| 189 | ) |
| 190 | if auth_scheme.flows.implicit: |
| 191 | scopes = _normalize_oauth_scopes(auth_scheme.flows.implicit.scopes) |
| 192 | elif auth_scheme.flows.authorizationCode: |
| 193 | scopes = _normalize_oauth_scopes( |
| 194 | auth_scheme.flows.authorizationCode.scopes |
| 195 | ) |
| 196 | elif auth_scheme.flows.clientCredentials: |
| 197 | scopes = _normalize_oauth_scopes( |
| 198 | auth_scheme.flows.clientCredentials.scopes |
| 199 | ) |
| 200 | elif auth_scheme.flows.password: |
| 201 | scopes = _normalize_oauth_scopes(auth_scheme.flows.password.scopes) |
| 202 | else: |
| 203 | scopes = [] |
| 204 | |
| 205 | client = OAuth2Session( |
| 206 | auth_credential.oauth2.client_id, |
| 207 | auth_credential.oauth2.client_secret, |
| 208 | scope=" ".join(scopes), |
| 209 | redirect_uri=auth_credential.oauth2.redirect_uri, |