|
11 | 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
12 | 12 | # License for the specific language governing permissions and limitations
|
13 | 13 | # under the License.
|
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import base64 |
| 17 | +from typing import Any, Dict, Union, Optional, List |
| 18 | + |
| 19 | +from ..interfaces import ( |
| 20 | + ActiveTokenResponse, |
| 21 | + CreateOAuth2ClientInput, |
| 22 | + CreateOAuth2ClientOkResult, |
| 23 | + DeleteOAuth2ClientOkResult, |
| 24 | + ErrorOAuth2Response, |
| 25 | + GetOAuth2ClientOkResult, |
| 26 | + GetOAuth2ClientsOkResult, |
| 27 | + InactiveTokenResponse, |
| 28 | + OAuth2TokenValidationRequirements, |
| 29 | + RevokeTokenUsingAuthorizationHeader, |
| 30 | + RevokeTokenUsingClientIDAndClientSecret, |
| 31 | + TokenInfo, |
| 32 | + UpdateOAuth2ClientInput, |
| 33 | + UpdateOAuth2ClientOkResult, |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +async def get_oauth2_client( |
| 38 | + client_id: str, user_context: Optional[Dict[str, Any]] = None |
| 39 | +) -> Union[GetOAuth2ClientOkResult, ErrorOAuth2Response]: |
| 40 | + if user_context is None: |
| 41 | + user_context = {} |
| 42 | + from ..recipe import OAuth2ProviderRecipe |
| 43 | + |
| 44 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.get_oauth2_client( |
| 45 | + client_id=client_id, user_context=user_context |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +async def get_oauth2_clients( |
| 50 | + page_size: Optional[int] = None, |
| 51 | + pagination_token: Optional[str] = None, |
| 52 | + client_name: Optional[str] = None, |
| 53 | + user_context: Optional[Dict[str, Any]] = None, |
| 54 | +) -> Union[GetOAuth2ClientsOkResult, ErrorOAuth2Response]: |
| 55 | + if user_context is None: |
| 56 | + user_context = {} |
| 57 | + from ..recipe import OAuth2ProviderRecipe |
| 58 | + |
| 59 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.get_oauth2_clients( |
| 60 | + page_size=page_size, |
| 61 | + pagination_token=pagination_token, |
| 62 | + client_name=client_name, |
| 63 | + user_context=user_context, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +async def create_oauth2_client( |
| 68 | + params: CreateOAuth2ClientInput, |
| 69 | + user_context: Optional[Dict[str, Any]] = None, |
| 70 | +) -> Union[CreateOAuth2ClientOkResult, ErrorOAuth2Response]: |
| 71 | + if user_context is None: |
| 72 | + user_context = {} |
| 73 | + from ..recipe import OAuth2ProviderRecipe |
| 74 | + |
| 75 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.create_oauth2_client( |
| 76 | + params=params, |
| 77 | + user_context=user_context, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +async def update_oauth2_client( |
| 82 | + params: UpdateOAuth2ClientInput, |
| 83 | + user_context: Optional[Dict[str, Any]] = None, |
| 84 | +) -> Union[UpdateOAuth2ClientOkResult, ErrorOAuth2Response]: |
| 85 | + if user_context is None: |
| 86 | + user_context = {} |
| 87 | + from ..recipe import OAuth2ProviderRecipe |
| 88 | + |
| 89 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.update_oauth2_client( |
| 90 | + params=params, |
| 91 | + user_context=user_context, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +async def delete_oauth2_client( |
| 96 | + client_id: str, user_context: Optional[Dict[str, Any]] = None |
| 97 | +) -> Union[DeleteOAuth2ClientOkResult, ErrorOAuth2Response]: |
| 98 | + if user_context is None: |
| 99 | + user_context = {} |
| 100 | + from ..recipe import OAuth2ProviderRecipe |
| 101 | + |
| 102 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.delete_oauth2_client( |
| 103 | + client_id=client_id, user_context=user_context |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +async def validate_oauth2_access_token( |
| 108 | + token: str, |
| 109 | + requirements: Optional[OAuth2TokenValidationRequirements] = None, |
| 110 | + check_database: Optional[bool] = None, |
| 111 | + user_context: Optional[Dict[str, Any]] = None, |
| 112 | +) -> Dict[str, Any]: |
| 113 | + if user_context is None: |
| 114 | + user_context = {} |
| 115 | + from ..recipe import OAuth2ProviderRecipe |
| 116 | + |
| 117 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.validate_oauth2_access_token( |
| 118 | + token=token, |
| 119 | + requirements=requirements, |
| 120 | + check_database=check_database, |
| 121 | + user_context=user_context, |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +async def create_token_for_client_credentials( |
| 126 | + client_id: str, |
| 127 | + client_secret: str, |
| 128 | + scope: Optional[List[str]] = None, |
| 129 | + audience: Optional[str] = None, |
| 130 | + user_context: Optional[Dict[str, Any]] = None, |
| 131 | +) -> Union[TokenInfo, ErrorOAuth2Response]: |
| 132 | + if user_context is None: |
| 133 | + user_context = {} |
| 134 | + from ..recipe import OAuth2ProviderRecipe |
| 135 | + |
| 136 | + return ( |
| 137 | + await OAuth2ProviderRecipe.get_instance().recipe_implementation.token_exchange( |
| 138 | + authorization_header=None, |
| 139 | + body={ |
| 140 | + "grant_type": "client_credentials", |
| 141 | + "client_id": client_id, |
| 142 | + "client_secret": client_secret, |
| 143 | + "scope": " ".join(scope) if scope else None, |
| 144 | + "audience": audience, |
| 145 | + }, |
| 146 | + user_context=user_context, |
| 147 | + ) |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +async def revoke_token( |
| 152 | + token: str, |
| 153 | + client_id: str, |
| 154 | + client_secret: Optional[str] = None, |
| 155 | + user_context: Optional[Dict[str, Any]] = None, |
| 156 | +) -> Optional[ErrorOAuth2Response]: |
| 157 | + if user_context is None: |
| 158 | + user_context = {} |
| 159 | + from ..recipe import OAuth2ProviderRecipe |
| 160 | + |
| 161 | + recipe = OAuth2ProviderRecipe.get_instance() |
| 162 | + |
| 163 | + client_info = await recipe.recipe_implementation.get_oauth2_client( |
| 164 | + client_id=client_id, user_context=user_context |
| 165 | + ) |
| 166 | + |
| 167 | + if isinstance(client_info, ErrorOAuth2Response): |
| 168 | + raise Exception( |
| 169 | + f"Failed to get OAuth2 client with id {client_id}: {client_info.error}" |
| 170 | + ) |
| 171 | + |
| 172 | + token_endpoint_auth_method = client_info.client.token_endpoint_auth_method |
| 173 | + |
| 174 | + if token_endpoint_auth_method == "none": |
| 175 | + auth_header = "Basic " + base64.b64encode(f"{client_id}:".encode()).decode() |
| 176 | + return await recipe.recipe_implementation.revoke_token( |
| 177 | + RevokeTokenUsingAuthorizationHeader( |
| 178 | + token=token, |
| 179 | + authorization_header=auth_header, |
| 180 | + ), |
| 181 | + user_context=user_context, |
| 182 | + ) |
| 183 | + elif token_endpoint_auth_method == "client_secret_basic" and client_secret: |
| 184 | + auth_header = ( |
| 185 | + "Basic " |
| 186 | + + base64.b64encode(f"{client_id}:{client_secret}".encode()).decode() |
| 187 | + ) |
| 188 | + return await recipe.recipe_implementation.revoke_token( |
| 189 | + RevokeTokenUsingAuthorizationHeader( |
| 190 | + token=token, |
| 191 | + authorization_header=auth_header, |
| 192 | + ), |
| 193 | + user_context=user_context, |
| 194 | + ) |
| 195 | + |
| 196 | + return await recipe.recipe_implementation.revoke_token( |
| 197 | + RevokeTokenUsingClientIDAndClientSecret( |
| 198 | + token=token, |
| 199 | + client_id=client_id, |
| 200 | + client_secret=client_secret, |
| 201 | + ), |
| 202 | + user_context=user_context, |
| 203 | + ) |
| 204 | + |
| 205 | + |
| 206 | +async def revoke_tokens_by_client_id( |
| 207 | + client_id: str, user_context: Optional[Dict[str, Any]] = None |
| 208 | +) -> None: |
| 209 | + if user_context is None: |
| 210 | + user_context = {} |
| 211 | + from ..recipe import OAuth2ProviderRecipe |
| 212 | + |
| 213 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.revoke_tokens_by_client_id( |
| 214 | + client_id=client_id, user_context=user_context |
| 215 | + ) |
| 216 | + |
| 217 | + |
| 218 | +async def revoke_tokens_by_session_handle( |
| 219 | + session_handle: str, user_context: Optional[Dict[str, Any]] = None |
| 220 | +) -> None: |
| 221 | + if user_context is None: |
| 222 | + user_context = {} |
| 223 | + from ..recipe import OAuth2ProviderRecipe |
| 224 | + |
| 225 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.revoke_tokens_by_session_handle( |
| 226 | + session_handle=session_handle, user_context=user_context |
| 227 | + ) |
| 228 | + |
| 229 | + |
| 230 | +async def validate_oauth2_refresh_token( |
| 231 | + token: str, |
| 232 | + scopes: Optional[List[str]] = None, |
| 233 | + user_context: Optional[Dict[str, Any]] = None, |
| 234 | +) -> Union[ActiveTokenResponse, InactiveTokenResponse]: |
| 235 | + if user_context is None: |
| 236 | + user_context = {} |
| 237 | + from ..recipe import OAuth2ProviderRecipe |
| 238 | + |
| 239 | + return await OAuth2ProviderRecipe.get_instance().recipe_implementation.introspect_token( |
| 240 | + token=token, scopes=scopes, user_context=user_context |
| 241 | + ) |
0 commit comments