File: _oauth_service.py

package info (click to toggle)
python-stripe 12.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,864 kB
  • sloc: python: 157,573; makefile: 13; sh: 9
file content (99 lines) | stat: -rw-r--r-- 3,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from stripe._stripe_service import StripeService
from stripe._error import AuthenticationError
from stripe._encode import _api_encode
from stripe._oauth import OAuth
from urllib.parse import urlencode
from stripe._request_options import RequestOptions
from stripe._client_options import _ClientOptions

from typing import cast, Optional
from typing_extensions import NotRequired, TypedDict


class OAuthService(StripeService):
    _options: Optional[_ClientOptions]

    def __init__(self, client, options=None):
        super(OAuthService, self).__init__(client)
        self._options = options

    class OAuthAuthorizeUrlOptions(TypedDict):
        express: NotRequired[bool]
        """
        Express only
        Boolean to indicate that the user should be sent to the express onboarding flow instead of the standard onboarding flow.
        """

    def _set_client_id(self, params):
        if "client_id" in params:
            return

        client_id = self._options and self._options.client_id

        if client_id:
            params["client_id"] = client_id
            return

        raise AuthenticationError(
            "No client_id provided. (HINT: set your client_id when configuring "
            'your StripeClient: "stripe.StripeClient(..., client_id=<CLIENT_ID>)"). '
            "You can find your client_ids in your Stripe dashboard at "
            "https://dashboard.stripe.com/account/applications/settings, "
            "after registering your account as a platform. See "
            "https://stripe.com/docs/connect/standalone-accounts for details, "
            "or email support@stripe.com if you have any questions."
        )

    def authorize_url(
        self,
        params: OAuth.OAuthAuthorizeUrlParams = {},
        options: OAuthAuthorizeUrlOptions = {},
    ) -> str:
        if options.get("express"):
            path = "/express/oauth/authorize"
        else:
            path = "/oauth/authorize"

        self._set_client_id(params)
        if "response_type" not in params:
            params["response_type"] = "code"
        query = urlencode(list(_api_encode(params, "V1")))

        # connect_api_base will be always set to stripe.DEFAULT_CONNECT_API_BASE
        # if it is not overridden on the client explicitly.
        connect_api_base = self._requestor.base_addresses.get("connect")
        assert connect_api_base is not None

        url = connect_api_base + path + "?" + query
        return url

    def token(
        self, params: OAuth.OAuthTokenParams, options: RequestOptions = {}
    ) -> OAuth.OAuthToken:
        return cast(
            OAuth.OAuthToken,
            self._requestor.request(
                "post",
                "/oauth/token",
                params=params,
                options=options,
                base_address="connect",
            ),
        )

    def deauthorize(
        self,
        params: OAuth.OAuthDeauthorizeParams,
        options: RequestOptions = {},
    ) -> OAuth.OAuthDeauthorization:
        self._set_client_id(params)
        return cast(
            OAuth.OAuthDeauthorization,
            self._requestor.request(
                "post",
                "/oauth/deauthorize",
                params=params,
                options=options,
                base_address="connect",
            ),
        )