|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Optional, Type |
| 4 | + |
| 5 | +from bunq import ApiEnvironmentType |
| 6 | +from bunq.sdk.context.bunq_context import BunqContext |
| 7 | +from bunq.sdk.exception.bunq_exception import BunqException |
| 8 | +from bunq.sdk.http.anonymous_api_client import AnonymousApiClient |
| 9 | +from bunq.sdk.http.bunq_response import BunqResponse |
| 10 | +from bunq.sdk.http.bunq_response_raw import BunqResponseRaw |
| 11 | +from bunq.sdk.http.http_util import HttpUtil |
| 12 | +from bunq.sdk.json import converter |
| 13 | +from bunq.sdk.model.core.bunq_model import BunqModel |
| 14 | +from bunq.sdk.model.core.oauth_grant_type import OauthGrantType |
| 15 | +from bunq.sdk.model.generated.endpoint import OauthClient |
| 16 | +from bunq.sdk.util.type_alias import T |
| 17 | + |
| 18 | + |
| 19 | +class OauthAccessToken(BunqModel): |
| 20 | + # Field constants. |
| 21 | + FIELD_GRANT_TYPE = "grant_type" |
| 22 | + FIELD_CODE = "code" |
| 23 | + FIELD_REDIRECT_URI = "redirect_uri" |
| 24 | + FIELD_CLIENT_ID = "client_id" |
| 25 | + FIELD_CLIENT_SECRET = "client_secret" |
| 26 | + |
| 27 | + # Token constants. |
| 28 | + TOKEN_URI_FORMAT_SANDBOX = "https://api-oauth.sandbox.bunq.com/v1/token?%s" |
| 29 | + TOKEN_URI_FORMAT_PRODUCTION = "https://api.oauth.bunq.com/v1/token?%s" |
| 30 | + |
| 31 | + # Error constants. |
| 32 | + ERROR_ENVIRONMENT_TYPE_NOT_SUPPORTED = "You are trying to use an unsupported environment type." |
| 33 | + |
| 34 | + def __init__(self, token: str, token_type: str, state: str = None) -> None: |
| 35 | + self._token = token |
| 36 | + self._token_type = token_type |
| 37 | + self._state = state |
| 38 | + |
| 39 | + @property |
| 40 | + def token(self) -> str: |
| 41 | + return self._token |
| 42 | + |
| 43 | + @property |
| 44 | + def token_type(self) -> str: |
| 45 | + return self._token_type |
| 46 | + |
| 47 | + @property |
| 48 | + def state(self) -> Optional[str]: |
| 49 | + return self._state |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def create(cls, |
| 53 | + grant_type: OauthGrantType, |
| 54 | + oauth_code: str, |
| 55 | + redirect_uri: str, |
| 56 | + client: OauthClient) -> OauthAccessToken: |
| 57 | + api_client = AnonymousApiClient(BunqContext.api_context()) |
| 58 | + response_raw = api_client.post( |
| 59 | + cls.create_token_uri(grant_type.value, oauth_code, redirect_uri, client), |
| 60 | + bytearray(), |
| 61 | + {} |
| 62 | + ) |
| 63 | + |
| 64 | + return cls.from_json(OauthAccessToken, response_raw).value |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def create_token_uri(cls, grant_type: str, auth_code: str, redirect_uri: str, client: OauthClient) -> str: |
| 68 | + all_token_parameter = { |
| 69 | + cls.FIELD_GRANT_TYPE: grant_type, |
| 70 | + cls.FIELD_CODE: auth_code, |
| 71 | + cls.FIELD_REDIRECT_URI: redirect_uri, |
| 72 | + cls.FIELD_CLIENT_ID: client.id_, |
| 73 | + cls.FIELD_CLIENT_SECRET: client.secret, |
| 74 | + } |
| 75 | + |
| 76 | + return cls.determine_auth_uri_format().format(HttpUtil.create_query_string(all_token_parameter)) |
| 77 | + |
| 78 | + def is_all_field_none(self) -> bool: |
| 79 | + if self._token is not None: |
| 80 | + return False |
| 81 | + elif self._token_type is not None: |
| 82 | + return False |
| 83 | + elif self._state is not None: |
| 84 | + return False |
| 85 | + |
| 86 | + return True |
| 87 | + |
| 88 | + @classmethod |
| 89 | + def from_json(cls, class_of_object: Type[T], response_raw: BunqResponseRaw): |
| 90 | + response_item_object = converter.deserialize(class_of_object, response_raw) |
| 91 | + response_value = converter.json_to_class(class_of_object, response_item_object) |
| 92 | + |
| 93 | + return BunqResponse(response_value, response_raw.headers) |
| 94 | + |
| 95 | + @classmethod |
| 96 | + def determine_auth_uri_format(cls) -> str: |
| 97 | + environment_type = BunqContext.api_context().environment_type |
| 98 | + |
| 99 | + if ApiEnvironmentType.PRODUCTION == environment_type: |
| 100 | + return cls.TOKEN_URI_FORMAT_PRODUCTION |
| 101 | + |
| 102 | + if ApiEnvironmentType.SANDBOX == environment_type: |
| 103 | + return cls.TOKEN_URI_FORMAT_SANDBOX |
| 104 | + |
| 105 | + raise BunqException(cls.ERROR_ENVIRONMENT_TYPE_NOT_SUPPORTED) |
0 commit comments