| 89 | return state.get(credential_key, None) |
| 90 | |
| 91 | def generate_auth_request(self) -> AuthConfig: |
| 92 | if not isinstance( |
| 93 | self.auth_config.auth_scheme, SecurityBase |
| 94 | ) or self.auth_config.auth_scheme.type_ not in ( |
| 95 | AuthSchemeType.oauth2, |
| 96 | AuthSchemeType.openIdConnect, |
| 97 | ): |
| 98 | return self.auth_config.model_copy(deep=True) |
| 99 | |
| 100 | # auth_uri already in exchanged credential |
| 101 | if ( |
| 102 | self.auth_config.exchanged_auth_credential |
| 103 | and self.auth_config.exchanged_auth_credential.oauth2 |
| 104 | and self.auth_config.exchanged_auth_credential.oauth2.auth_uri |
| 105 | ): |
| 106 | return self.auth_config.model_copy(deep=True) |
| 107 | |
| 108 | # Check if raw_auth_credential exists |
| 109 | if not self.auth_config.raw_auth_credential: |
| 110 | raise ValueError( |
| 111 | f"Auth Scheme {self.auth_config.auth_scheme.type_} requires" |
| 112 | " auth_credential." |
| 113 | ) |
| 114 | |
| 115 | # Check if oauth2 exists in raw_auth_credential |
| 116 | if not self.auth_config.raw_auth_credential.oauth2: |
| 117 | raise ValueError( |
| 118 | f"Auth Scheme {self.auth_config.auth_scheme.type_} requires oauth2 in" |
| 119 | " auth_credential." |
| 120 | ) |
| 121 | |
| 122 | # auth_uri in raw credential |
| 123 | if self.auth_config.raw_auth_credential.oauth2.auth_uri: |
| 124 | return AuthConfig( |
| 125 | auth_scheme=self.auth_config.auth_scheme, |
| 126 | raw_auth_credential=self.auth_config.raw_auth_credential, |
| 127 | exchanged_auth_credential=self.auth_config.raw_auth_credential.model_copy( |
| 128 | deep=True |
| 129 | ), |
| 130 | credential_key=self.auth_config.credential_key, |
| 131 | ) |
| 132 | |
| 133 | # Check for client_id and client_secret |
| 134 | if ( |
| 135 | not self.auth_config.raw_auth_credential.oauth2.client_id |
| 136 | or not self.auth_config.raw_auth_credential.oauth2.client_secret |
| 137 | ): |
| 138 | raise ValueError( |
| 139 | f"Auth Scheme {self.auth_config.auth_scheme.type_} requires both" |
| 140 | " client_id and client_secret in auth_credential.oauth2." |
| 141 | ) |
| 142 | |
| 143 | # Generate new auth URI |
| 144 | exchanged_credential = self.generate_auth_uri() |
| 145 | return AuthConfig( |
| 146 | auth_scheme=self.auth_config.auth_scheme, |
| 147 | raw_auth_credential=self.auth_config.raw_auth_credential, |
| 148 | exchanged_auth_credential=exchanged_credential, |