-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase.py
More file actions
219 lines (182 loc) · 8.02 KB
/
base.py
File metadata and controls
219 lines (182 loc) · 8.02 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from abc import ABC, abstractmethod
from typing import Any, Iterable, Optional
from requests import Response
from requests_oauthlib import OAuth2Session
from pardner.services.utils import scope_as_set, scope_as_string
from pardner.verticals import Vertical
class InsufficientScopeException(Exception):
def __init__(
self, unsupported_verticals: Iterable[Vertical], service_name: str
) -> None:
combined_verticals = ' '.join(unsupported_verticals)
super().__init__(
f'Cannot add {combined_verticals} to {service_name} with current scope.'
)
class UnsupportedVerticalException(Exception):
def __init__(
self, unsupported_verticals: Iterable[Vertical], service_name: str
) -> None:
combined_verticals = ' '.join(unsupported_verticals)
super().__init__(
f'Cannot add {combined_verticals} to {service_name} because they are not supported.'
)
class UnsupportedRequestException(Exception):
def __init__(self, service_name: str, message: str):
super().__init__(f'Cannot fetch data from {service_name}: {message}')
class BaseTransferService(ABC):
"""
A base class to be extended by service-specific classes that implement logic for
OAuth 2.0 and data transfers.
"""
_authorization_url: str
_base_url: str
_client_secret: str
_oAuth2Session: OAuth2Session
_service_name: str
_supported_verticals: set[Vertical] = set()
_token_url: str
_verticals: set[Vertical] = set()
def __init__(
self,
service_name: str,
client_id: str,
client_secret: str,
redirect_uri: str,
supported_verticals: set[Vertical],
state: Optional[str] = None,
verticals: set[Vertical] = set(),
) -> None:
"""
Initializes an instance of BaseTransferService, which shouldn't be done unless
by classes that extend it.
:param service_name: Name of the service for which the transfer is being built.
:param client_id: Client identifier given by the OAuth provider upon registration.
:param client_secret: The `client_secret` paired to the `client_id`.
:param redirect_uri: The registered callback URI.
:param supported_verticals: The `Vertical`s that can be fetched on the service.
:param state: State string used to prevent CSRF and identify flow.
:param verticals: The `Vertical`s for which the transfer service has
appropriate scope to fetch.
"""
self._client_secret = client_secret
self._supported_verticals = supported_verticals
self._service_name = service_name
self._verticals = verticals
self._oAuth2Session = OAuth2Session(
client_id=client_id, redirect_uri=redirect_uri, state=state
)
self.scope = self.scope_for_verticals(verticals)
@property
def name(self) -> str:
return self._service_name
@property
def scope(self) -> set[str]:
return (
scope_as_set(self._oAuth2Session.scope)
if self._oAuth2Session.scope
else set()
)
@scope.setter
def scope(self, new_scope: Iterable[str]) -> None:
"""
Sets the scope of the transfer service flow.
Some services have specific requirements for the format of the scope
string (e.g., scopes have to be comma separated, or `+` separated).
:param new_scope: The new scopes that should be set for the transfer
service.
"""
self._oAuth2Session.scope = scope_as_string(new_scope)
@property
def verticals(self) -> set[Vertical]:
return self._verticals
@verticals.setter
def verticals(self, verticals: Iterable[Vertical]) -> None:
"""
:raises: :class:`UnsupportedVerticalException`: if one or more of the
`verticals` are not supported by the service.
"""
unsupported_verticals = [
vertical
for vertical in verticals
if vertical not in self._supported_verticals
]
if len(unsupported_verticals) > 0:
raise UnsupportedVerticalException(unsupported_verticals, self.name)
self._verticals = set(verticals)
def _get_resource(self, uri: str, params: dict[str, Any] = {}) -> Response:
"""
Sends a GET request to `uri` using `OAuth2Session`.
:param uri: the destination of the request (a URI).
:param params: the extra parameters to be send with the request, optionally.
:returns: The `requests.Response` object obtained from making the request.
"""
dashboard_response = self._oAuth2Session.get(uri, params=params)
if not dashboard_response.ok:
dashboard_response.raise_for_status()
return dashboard_response
def add_verticals(
self, verticals: Iterable[Vertical], should_reauth: bool = False
) -> bool:
"""
Adds to the verticals being requested.
:param verticals: `Vertical`s that should be added to service.
:param should_reauth: Whether or not the service should unauthorize itself to
start a new session with added scopes corresponding to `verticals`.
:returns: Whether add was successful without reauthorization (`True`) or not
(`False`).
:raises: :class:`UnsupportedVerticalException`: if `should_reauth` is not
passed and the current scopes are insufficient, this exception is raised.
"""
new_verticals = set(verticals) - self.verticals
new_scopes = self.scope_for_verticals(new_verticals)
if not new_scopes.issubset(self.scope) and not should_reauth:
raise InsufficientScopeException(verticals, self.name)
elif not new_scopes.issubset(self.scope):
self.verticals = new_verticals | self.verticals
del self._oAuth2Session.access_token
self.scope = self.scope | new_scopes
return False
self.verticals = new_verticals | self.verticals
return True
def fetch_token(
self,
code: Optional[str] = None,
authorization_response: Optional[str] = None,
include_client_id: bool = False,
) -> dict[str, Any]:
"""
Once the end-user authorizes the application to access their data, the
resource server sends a request to `redirect_uri` with the authorization code as
a parameter. Using this authorization code, this method makes a request to the
resource server to obtain the access token.
One of either `code` or `authorization_response` must not be None.
:param code: the code obtained from parsing the callback URL which the end-user's
browser redirected to.
:param authorization_response: the URL (with parameters) the end-user's browser
redirected to after authorization.
:param include_client_id: whether or not to send the client ID with the token request
:returns: the authorization URL and state, respectively.
"""
return self._oAuth2Session.fetch_token(
token_url=self._token_url,
code=code,
authorization_response=authorization_response,
include_client_id=include_client_id,
client_secret=self._client_secret,
)
def authorization_url(self) -> tuple[str, str]:
"""
Builds the authorization URL and state. Once the end-user (i.e., resource owner)
navigates to the authorization URL they can begin the authorization flow.
:returns: the authorization URL and state, respectively.
"""
return self._oAuth2Session.authorization_url(self._authorization_url)
@abstractmethod
def scope_for_verticals(self, verticals: Iterable[Vertical]) -> set[str]:
"""
Given a vertical, returns the scopes necessary for making API requests for
fetching data in that vertical.
:param verticals: The verticals for which scopes are being requested.
:returns: Scope names corresponding to `verticals`.
"""
pass