|
14 | 14 |
|
15 | 15 | from __future__ import annotations |
16 | 16 |
|
17 | | -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Union |
18 | | - |
19 | | -from httpx import AsyncClient |
20 | 17 | from supertokens_python.recipe.thirdparty.provider import Provider |
21 | | -from supertokens_python.recipe.thirdparty.types import ( |
22 | | - AccessTokenAPI, |
23 | | - AuthorisationRedirectAPI, |
24 | | - UserInfo, |
25 | | - UserInfoEmail, |
26 | | -) |
| 18 | +from .custom import GenericProvider, NewProvider |
| 19 | +from ..provider import Provider, ProviderInput |
27 | 20 |
|
28 | | -if TYPE_CHECKING: |
29 | | - from supertokens_python.framework.request import BaseRequest |
30 | 21 |
|
| 22 | +# TODO Implement when it's done in Node PR |
| 23 | +class BitbucketImpl(GenericProvider): |
| 24 | + pass |
31 | 25 |
|
32 | | -class Bitbucket(Provider): |
33 | | - def __init__( |
34 | | - self, |
35 | | - client_id: str, |
36 | | - client_secret: str, |
37 | | - scope: Union[None, List[str]] = None, |
38 | | - authorisation_redirect: Union[ |
39 | | - None, Dict[str, Union[str, Callable[[BaseRequest], str]]] |
40 | | - ] = None, |
41 | | - is_default: bool = False, |
42 | | - ): |
43 | | - super().__init__("bitbucket", is_default) |
44 | | - self.client_id = client_id |
45 | | - self.client_secret = client_secret |
46 | | - self.scopes = ["account", "email"] if scope is None else list(set(scope)) |
47 | | - self.access_token_api_url = "https://bitbucket.org/site/oauth2/access_token" |
48 | | - self.authorisation_redirect_url = "https://bitbucket.org/site/oauth2/authorize" |
49 | | - self.authorisation_redirect_params = {} |
50 | | - if authorisation_redirect is not None: |
51 | | - self.authorisation_redirect_params = authorisation_redirect |
52 | 26 |
|
53 | | - async def get_profile_info( |
54 | | - self, auth_code_response: Dict[str, Any], user_context: Dict[str, Any] |
55 | | - ) -> UserInfo: |
56 | | - access_token: str = auth_code_response["access_token"] |
57 | | - headers = {"Authorization": f"Bearer {access_token}"} |
58 | | - async with AsyncClient() as client: |
59 | | - response = await client.get( # type: ignore |
60 | | - url="https://api.bitbucket.org/2.0/user", |
61 | | - headers=headers, |
62 | | - ) |
63 | | - user_info = response.json() |
64 | | - user_id = user_info["uuid"] |
65 | | - email_res = await client.get( # type: ignore |
66 | | - url="https://api.bitbucket.org/2.0/user/emails", |
67 | | - headers=headers, |
68 | | - ) |
69 | | - email_data = email_res.json() |
70 | | - email = None |
71 | | - is_verified = False |
72 | | - for email_info in email_data["values"]: |
73 | | - if email_info.get("is_primary"): |
74 | | - email = email_info["email"] |
75 | | - is_verified = email_info["is_confirmed"] |
76 | | - break |
| 27 | +# TODO Finish when it's done in Node PR |
| 28 | +def Bitbucket(input: ProviderInput) -> Provider: # pylint: disable=redefined-builtin |
| 29 | + if input.config.name is None: |
| 30 | + input.config.name = "Bitbucket" |
77 | 31 |
|
78 | | - if email is None: |
79 | | - return UserInfo(user_id) |
80 | | - return UserInfo(user_id, UserInfoEmail(email, is_verified)) |
| 32 | + if input.config.authorization_endpoint is None: |
| 33 | + input.config.authorization_endpoint = ( |
| 34 | + "https://bitbucket.org/site/oauth2/authorize" |
| 35 | + ) |
81 | 36 |
|
82 | | - def get_authorisation_redirect_api_info( |
83 | | - self, user_context: Dict[str, Any] |
84 | | - ) -> AuthorisationRedirectAPI: |
85 | | - params = { |
86 | | - "scope": " ".join(self.scopes), |
87 | | - "response_type": "code", |
88 | | - "client_id": self.client_id, |
89 | | - "access_type": "offline", |
90 | | - **self.authorisation_redirect_params, |
91 | | - } |
92 | | - return AuthorisationRedirectAPI(self.authorisation_redirect_url, params) |
| 37 | + if input.config.token_endpoint is None: |
| 38 | + input.config.token_endpoint = "https://bitbucket.org/site/oauth2/access_token" |
93 | 39 |
|
94 | | - def get_access_token_api_info( |
95 | | - self, |
96 | | - redirect_uri: str, |
97 | | - auth_code_from_request: str, |
98 | | - user_context: Dict[str, Any], |
99 | | - ) -> AccessTokenAPI: |
100 | | - params = { |
101 | | - "client_id": self.client_id, |
102 | | - "client_secret": self.client_secret, |
103 | | - "grant_type": "authorization_code", |
104 | | - "code": auth_code_from_request, |
105 | | - "redirect_uri": redirect_uri, |
106 | | - } |
107 | | - return AccessTokenAPI(self.access_token_api_url, params) |
| 40 | + if input.config.user_info_endpoint is None: |
| 41 | + input.config.user_info_endpoint = "https://api.bitbucket.org/2.0/user" |
108 | 42 |
|
109 | | - def get_redirect_uri(self, user_context: Dict[str, Any]) -> Union[None, str]: |
110 | | - return None |
| 43 | + # TODO overrides and working of this |
| 44 | + # once done in Node PR |
111 | 45 |
|
112 | | - def get_client_id(self, user_context: Dict[str, Any]) -> str: |
113 | | - return self.client_id |
| 46 | + return NewProvider(input, BitbucketImpl) |
0 commit comments