|
| 1 | +# Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. |
| 2 | +# |
| 3 | +# This software is licensed under the Apache License, Version 2.0 (the |
| 4 | +# "License") as published by the Apache Software Foundation. |
| 5 | +# |
| 6 | +# You may not use this file except in compliance with the License. You may |
| 7 | +# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from base64 import b64encode |
| 17 | +from typing import Any, Dict, Optional |
| 18 | +from supertokens_python.recipe.thirdparty.provider import RedirectUriInfo |
| 19 | +from supertokens_python.recipe.thirdparty.providers.utils import do_post_request, DEV_OAUTH_REDIRECT_URL, \ |
| 20 | + get_actual_client_id_from_development_client_id |
| 21 | +from ..provider import ( |
| 22 | + Provider, |
| 23 | + ProviderConfigForClient, |
| 24 | + ProviderInput, |
| 25 | + UserFields, |
| 26 | + UserInfoMap, |
| 27 | +) |
| 28 | + |
| 29 | +from .custom import ( |
| 30 | + GenericProvider, |
| 31 | + NewProvider, is_using_development_client_id, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +class TwitterImpl(GenericProvider): |
| 36 | + async def get_config_for_client_type( |
| 37 | + self, client_type: Optional[str], user_context: Dict[str, Any] |
| 38 | + ) -> ProviderConfigForClient: |
| 39 | + config = await super().get_config_for_client_type(client_type, user_context) |
| 40 | + |
| 41 | + if config.scope is None: |
| 42 | + config.scope = ["users.read", "tweet.read"] |
| 43 | + |
| 44 | + if config.force_pkce is None: |
| 45 | + config.force_pkce = True |
| 46 | + |
| 47 | + return config |
| 48 | + |
| 49 | + async def exchange_auth_code_for_oauth_tokens( |
| 50 | + self, redirect_uri_info: RedirectUriInfo, user_context: Dict[str, Any] |
| 51 | + ) -> Dict[str, Any]: |
| 52 | + |
| 53 | + client_id = self.config.client_id |
| 54 | + redirect_uri = redirect_uri_info.redirect_uri_on_provider_dashboard |
| 55 | + |
| 56 | + # We need to do this because we don't call the original implementation |
| 57 | + # Transformation needed for dev keys BEGIN |
| 58 | + if is_using_development_client_id(self.config.client_id): |
| 59 | + client_id = get_actual_client_id_from_development_client_id(self.config.client_id) |
| 60 | + redirect_uri = DEV_OAUTH_REDIRECT_URL |
| 61 | + # Transformation needed for dev keys END |
| 62 | + |
| 63 | + credentials = client_id + ":" + (self.config.client_secret or "") |
| 64 | + auth_token = b64encode(credentials.encode()).decode() |
| 65 | + |
| 66 | + twitter_oauth_tokens_params: Dict[str, Any] = { |
| 67 | + "grant_type": "authorization_code", |
| 68 | + "client_id": client_id, |
| 69 | + "code_verifier": redirect_uri_info.pkce_code_verifier, |
| 70 | + "redirect_uri": redirect_uri, |
| 71 | + "code": redirect_uri_info.redirect_uri_query_params["code"], |
| 72 | + } |
| 73 | + |
| 74 | + twitter_oauth_tokens_params = { |
| 75 | + **twitter_oauth_tokens_params, |
| 76 | + **(self.config.token_endpoint_body_params or {}), |
| 77 | + } |
| 78 | + |
| 79 | + assert self.config.token_endpoint is not None |
| 80 | + |
| 81 | + return await do_post_request( |
| 82 | + self.config.token_endpoint, |
| 83 | + body_params=twitter_oauth_tokens_params, |
| 84 | + headers={"Authorization": f"Basic {auth_token}"}, |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def Twitter(input: ProviderInput) -> Provider: # pylint: disable=redefined-builtin |
| 89 | + if input.config.name is None: |
| 90 | + input.config.name = "Twitter" |
| 91 | + |
| 92 | + if input.config.authorization_endpoint is None: |
| 93 | + input.config.authorization_endpoint = "https://twitter.com/i/oauth2/authorize" |
| 94 | + |
| 95 | + if input.config.token_endpoint is None: |
| 96 | + input.config.token_endpoint = "https://api.twitter.com/2/oauth2/token" |
| 97 | + |
| 98 | + if input.config.user_info_endpoint is None: |
| 99 | + input.config.user_info_endpoint = "https://api.twitter.com/2/users/me" |
| 100 | + |
| 101 | + if input.config.require_email is None: |
| 102 | + input.config.require_email = False |
| 103 | + |
| 104 | + if input.config.user_info_map is None: |
| 105 | + input.config.user_info_map = UserInfoMap(UserFields(), UserFields()) |
| 106 | + |
| 107 | + if input.config.user_info_map.from_user_info_api is None: |
| 108 | + input.config.user_info_map.from_user_info_api = UserFields() |
| 109 | + |
| 110 | + if input.config.user_info_map.from_user_info_api.user_id is None: |
| 111 | + input.config.user_info_map.from_user_info_api.user_id = "data.id" |
| 112 | + |
| 113 | + return NewProvider(input, TwitterImpl) |
0 commit comments