Skip to content

Commit

Permalink
Add support for app authentication (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiang-he authored Dec 30, 2020
1 parent b21934d commit 9204111
Show file tree
Hide file tree
Showing 16 changed files with 535 additions and 248 deletions.
3 changes: 2 additions & 1 deletion dropbox/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
SetProfilePhotoArg_validator,
SetProfilePhotoResult_validator,
SetProfilePhotoError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)

Expand Down
6 changes: 4 additions & 2 deletions dropbox/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
TokenFromOAuth1Arg_validator,
TokenFromOAuth1Result_validator,
TokenFromOAuth1Error_validator,
{'host': u'api',
{'auth': u'app',
'host': u'api',
'style': u'rpc'},
)
token_revoke = bb.Route(
Expand All @@ -689,7 +690,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
bv.Void(),
bv.Void(),
bv.Void(),
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)

Expand Down
6 changes: 4 additions & 2 deletions dropbox/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
EchoArg_validator,
EchoResult_validator,
bv.Void(),
{'host': u'api',
{'auth': u'app',
'host': u'api',
'style': u'rpc'},
)
user = bb.Route(
Expand All @@ -90,7 +91,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
EchoArg_validator,
EchoResult_validator,
bv.Void(),
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)

Expand Down
6 changes: 4 additions & 2 deletions dropbox/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
bv.Void(),
bv.Void(),
bv.Void(),
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
delete_manual_contacts_batch = bb.Route(
Expand All @@ -125,7 +126,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
DeleteManualContactsArg_validator,
bv.Void(),
DeleteManualContactsError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)

Expand Down
30 changes: 29 additions & 1 deletion dropbox/dropbox_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# before release.
__version__ = '0.0.0'

import base64
import contextlib
import json
import logging
Expand Down Expand Up @@ -59,6 +60,11 @@

SELECT_USER_HEADER = 'Dropbox-API-Select-User'

USER_AUTH = 'user'
TEAM_AUTH = 'team'
APP_AUTH = 'app'
NO_AUTH = 'noauth'

class RouteResult(object):
"""The successful result of a call to a route."""

Expand Down Expand Up @@ -293,6 +299,7 @@ def request(self,
self.check_and_refresh_access_token()

host = route.attrs['host'] or 'api'
auth_type = route.attrs['auth']
route_name = namespace + '/' + route.name
if route.version > 1:
route_name += '_v{}'.format(route.version)
Expand All @@ -315,6 +322,7 @@ def request(self,
route_name,
route_style,
serialized_arg,
auth_type,
request_binary,
timeout=timeout)
decoded_obj_result = json.loads(res.obj_result)
Expand Down Expand Up @@ -407,6 +415,7 @@ def request_json_object(self,
route_name,
route_style,
request_arg,
auth_type,
request_binary,
timeout=None):
"""
Expand All @@ -418,6 +427,7 @@ def request_json_object(self,
:param route_style: The style of the route.
:param str request_arg: A JSON-serializable Python object representing
the argument for the route.
:param auth_type str
:param Optional[bytes] request_binary: Bytes representing the binary
payload. Use None if there is no binary payload.
:param Optional[float] timeout: Maximum duration in seconds
Expand All @@ -432,6 +442,7 @@ def request_json_object(self,
route_name,
route_style,
serialized_arg,
auth_type,
request_binary,
timeout=timeout)
# This can throw a ValueError if the result is not deserializable,
Expand All @@ -447,6 +458,7 @@ def request_json_string_with_retry(self,
route_name,
route_style,
request_json_arg,
auth_type,
request_binary,
timeout=None):
"""
Expand All @@ -465,6 +477,7 @@ def request_json_string_with_retry(self,
route_name,
route_style,
request_json_arg,
auth_type,
request_binary,
timeout=timeout)
except AuthError as e:
Expand Down Expand Up @@ -507,6 +520,7 @@ def request_json_string(self,
func_name,
route_style,
request_json_arg,
auth_type,
request_binary,
timeout=None):
"""
Expand All @@ -529,10 +543,24 @@ def request_json_string(self,
url = self._get_route_url(fq_hostname, func_name)

headers = {'User-Agent': self._user_agent}
if host != HOST_NOTIFY:
if auth_type == USER_AUTH or auth_type == TEAM_AUTH:
headers['Authorization'] = 'Bearer %s' % self._oauth2_access_token
if self._headers:
headers.update(self._headers)
elif auth_type == APP_AUTH:
if self._app_key is None or self._app_secret is None:
raise BadInputException(
'Client id and client secret are required for routes with app auth')
auth_header = base64.b64encode(
"{}:{}".format(self._app_key, self._app_secret).encode("utf-8")
)
headers['Authorization'] = 'Basic {}'.format(auth_header.decode("utf-8"))
if self._headers:
headers.update(self._headers)
elif auth_type == NO_AUTH:
pass
else:
raise BadInputException('Unhandled auth type: {}'.format(auth_type))

# The contents of the body of the HTTP request
body = None
Expand Down
48 changes: 32 additions & 16 deletions dropbox/file_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
AddPropertiesArg_validator,
bv.Void(),
AddPropertiesError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
properties_overwrite = bb.Route(
Expand All @@ -2116,7 +2117,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
OverwritePropertyGroupArg_validator,
bv.Void(),
InvalidPropertyGroupError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
properties_remove = bb.Route(
Expand All @@ -2126,7 +2128,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
RemovePropertiesArg_validator,
bv.Void(),
RemovePropertiesError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
properties_search = bb.Route(
Expand All @@ -2136,7 +2139,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
PropertiesSearchArg_validator,
PropertiesSearchResult_validator,
PropertiesSearchError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
properties_search_continue = bb.Route(
Expand All @@ -2146,7 +2150,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
PropertiesSearchContinueArg_validator,
PropertiesSearchResult_validator,
PropertiesSearchContinueError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
properties_update = bb.Route(
Expand All @@ -2156,7 +2161,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
UpdatePropertiesArg_validator,
bv.Void(),
UpdatePropertiesError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
templates_add_for_team = bb.Route(
Expand All @@ -2166,7 +2172,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
AddTemplateArg_validator,
AddTemplateResult_validator,
ModifyTemplateError_validator,
{'host': u'api',
{'auth': u'team',
'host': u'api',
'style': u'rpc'},
)
templates_add_for_user = bb.Route(
Expand All @@ -2176,7 +2183,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
AddTemplateArg_validator,
AddTemplateResult_validator,
ModifyTemplateError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
templates_get_for_team = bb.Route(
Expand All @@ -2186,7 +2194,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
GetTemplateArg_validator,
GetTemplateResult_validator,
TemplateError_validator,
{'host': u'api',
{'auth': u'team',
'host': u'api',
'style': u'rpc'},
)
templates_get_for_user = bb.Route(
Expand All @@ -2196,7 +2205,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
GetTemplateArg_validator,
GetTemplateResult_validator,
TemplateError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
templates_list_for_team = bb.Route(
Expand All @@ -2206,7 +2216,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
bv.Void(),
ListTemplateResult_validator,
TemplateError_validator,
{'host': u'api',
{'auth': u'team',
'host': u'api',
'style': u'rpc'},
)
templates_list_for_user = bb.Route(
Expand All @@ -2216,7 +2227,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
bv.Void(),
ListTemplateResult_validator,
TemplateError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
templates_remove_for_team = bb.Route(
Expand All @@ -2226,7 +2238,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
RemoveTemplateArg_validator,
bv.Void(),
TemplateError_validator,
{'host': u'api',
{'auth': u'team',
'host': u'api',
'style': u'rpc'},
)
templates_remove_for_user = bb.Route(
Expand All @@ -2236,7 +2249,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
RemoveTemplateArg_validator,
bv.Void(),
TemplateError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)
templates_update_for_team = bb.Route(
Expand All @@ -2246,7 +2260,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
UpdateTemplateArg_validator,
UpdateTemplateResult_validator,
ModifyTemplateError_validator,
{'host': u'api',
{'auth': u'team',
'host': u'api',
'style': u'rpc'},
)
templates_update_for_user = bb.Route(
Expand All @@ -2256,7 +2271,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
UpdateTemplateArg_validator,
UpdateTemplateResult_validator,
ModifyTemplateError_validator,
{'host': u'api',
{'auth': u'user',
'host': u'api',
'style': u'rpc'},
)

Expand Down
Loading

0 comments on commit 9204111

Please sign in to comment.