|
5 | 5 | 'DropboxOAuth2Flow',
|
6 | 6 | 'DropboxOAuth2FlowNoRedirect',
|
7 | 7 | 'NotApprovedException',
|
| 8 | + 'OAuth2FlowNoRedirectResult', |
| 9 | + 'OAuth2FlowResult', |
8 | 10 | 'ProviderException',
|
9 | 11 | ]
|
10 | 12 |
|
|
24 | 26 |
|
25 | 27 | OAUTH_ROUTE_VERSION = '1'
|
26 | 28 |
|
| 29 | + |
| 30 | +class OAuth2FlowNoRedirectResult(object): |
| 31 | + """ |
| 32 | + Authorization information for an OAuth2Flow performed with no redirect. |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, access_token, account_id, user_id): |
| 36 | + """ |
| 37 | + Args: |
| 38 | + access_token (str): Token to be used to authenticate later |
| 39 | + requests. |
| 40 | + account_id (str): The Dropbox user's account ID. Please use this |
| 41 | + instead of the user_id. |
| 42 | + user_id (str): For backwards compatibility with API v1, please |
| 43 | + avoid using this if possible. |
| 44 | + """ |
| 45 | + self.access_token = access_token |
| 46 | + self.account_id = account_id |
| 47 | + self.user_id = user_id |
| 48 | + |
| 49 | + def __repr__(self): |
| 50 | + return 'OAuth2FlowNoRedirectResult(%r, %r, %r)' % ( |
| 51 | + self.access_token, |
| 52 | + self.account_id, |
| 53 | + self.user_id, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +class OAuth2FlowResult(OAuth2FlowNoRedirectResult): |
| 58 | + """ |
| 59 | + Authorization information for an OAuth2Flow with redirect. |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__(self, access_token, account_id, user_id, url_state): |
| 63 | + """ |
| 64 | + Same as OAuth2FlowNoRedirectResult but with url_state. |
| 65 | +
|
| 66 | + Args: |
| 67 | + url_state (str): The url state that was set by |
| 68 | + :meth:`DropboxOAuth2Flow.start`. |
| 69 | + """ |
| 70 | + super(OAuth2FlowResult, self).__init__( |
| 71 | + access_token, account_id, user_id) |
| 72 | + self.url_state = url_state |
| 73 | + |
| 74 | + @classmethod |
| 75 | + def from_no_redirect_result(cls, result, url_state): |
| 76 | + assert isinstance(result, OAuth2FlowNoRedirectResult) |
| 77 | + return cls( |
| 78 | + result.access_token, result.account_id, result.user_id, url_state) |
| 79 | + |
| 80 | + def __repr__(self): |
| 81 | + return 'OAuth2FlowResult(%r, %r, %r, %r)' % ( |
| 82 | + self.access_token, |
| 83 | + self.account_id, |
| 84 | + self.user_id, |
| 85 | + self.url_state, |
| 86 | + ) |
| 87 | + |
| 88 | + |
27 | 89 | class DropboxOAuth2FlowBase(object):
|
28 | 90 |
|
29 | 91 | def __init__(self, consumer_key, consumer_secret, locale=None):
|
@@ -63,8 +125,12 @@ def _finish(self, code, redirect_uri):
|
63 | 125 |
|
64 | 126 | access_token = d["access_token"]
|
65 | 127 | user_id = d["uid"]
|
| 128 | + print('BASE', d) |
66 | 129 |
|
67 |
| - return access_token, user_id |
| 130 | + return OAuth2FlowNoRedirectResult( |
| 131 | + d['access_token'], |
| 132 | + d['account_id'], |
| 133 | + d['uid']) |
68 | 134 |
|
69 | 135 | def build_path(self, target, params=None):
|
70 | 136 | """Build the path component for an API URL.
|
@@ -170,9 +236,7 @@ def finish(self, code):
|
170 | 236 |
|
171 | 237 | :param str code: The authorization code shown to the user when they
|
172 | 238 | approved your app.
|
173 |
| - :return: A pair of ``(access_token, user_id)``. ``access_token`` is a |
174 |
| - string that can be passed to Dropbox. ``user_id`` is the |
175 |
| - Dropbox user ID (string) of the user that just approved your app. |
| 239 | + :rtype: OAuth2FlowNoRedirectResult |
176 | 240 | :raises: The same exceptions as :meth:`DropboxOAuth2Flow.finish()`.
|
177 | 241 | """
|
178 | 242 | return self._finish(code, None)
|
@@ -288,11 +352,7 @@ def finish(self, query_params):
|
288 | 352 |
|
289 | 353 | :param dict query_params: The query parameters on the GET request to
|
290 | 354 | your redirect URI.
|
291 |
| - :return: A tuple of ``(access_token, user_id, url_state)``. |
292 |
| - ``access_token`` can be used to construct a |
293 |
| - :class:`dropbox.dropbox.Dropbox`. ``user_id`` is the Dropbox user |
294 |
| - ID (string) of the user that just approved your app. ``url_state`` |
295 |
| - is the value you originally passed in to :meth:`start()`. |
| 355 | + :rtype: OAuth2FlowResult |
296 | 356 | :raises: :class:`BadRequestException` If the redirect URL was missing
|
297 | 357 | parameters or if the given parameters were not valid.
|
298 | 358 | :raises: :class:`BadStateException` If there's no CSRF token in the
|
@@ -366,8 +426,9 @@ def finish(self, query_params):
|
366 | 426 |
|
367 | 427 | # If everything went ok, make the network call to get an access token.
|
368 | 428 |
|
369 |
| - access_token, user_id = self._finish(code, self.redirect_uri) |
370 |
| - return access_token, user_id, url_state |
| 429 | + no_redirect_result = self._finish(code, self.redirect_uri) |
| 430 | + return OAuth2FlowResult.from_no_redirect_result( |
| 431 | + no_redirect_result, url_state) |
371 | 432 |
|
372 | 433 |
|
373 | 434 | class BadRequestException(Exception):
|
|
0 commit comments