Skip to content

Commit 9204111

Browse files
authored
Add support for app authentication (#305)
1 parent b21934d commit 9204111

File tree

16 files changed

+535
-248
lines changed

16 files changed

+535
-248
lines changed

dropbox/account.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
248248
SetProfilePhotoArg_validator,
249249
SetProfilePhotoResult_validator,
250250
SetProfilePhotoError_validator,
251-
{'host': u'api',
251+
{'auth': u'user',
252+
'host': u'api',
252253
'style': u'rpc'},
253254
)
254255

dropbox/auth.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
679679
TokenFromOAuth1Arg_validator,
680680
TokenFromOAuth1Result_validator,
681681
TokenFromOAuth1Error_validator,
682-
{'host': u'api',
682+
{'auth': u'app',
683+
'host': u'api',
683684
'style': u'rpc'},
684685
)
685686
token_revoke = bb.Route(
@@ -689,7 +690,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
689690
bv.Void(),
690691
bv.Void(),
691692
bv.Void(),
692-
{'host': u'api',
693+
{'auth': u'user',
694+
'host': u'api',
693695
'style': u'rpc'},
694696
)
695697

dropbox/check.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
8080
EchoArg_validator,
8181
EchoResult_validator,
8282
bv.Void(),
83-
{'host': u'api',
83+
{'auth': u'app',
84+
'host': u'api',
8485
'style': u'rpc'},
8586
)
8687
user = bb.Route(
@@ -90,7 +91,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
9091
EchoArg_validator,
9192
EchoResult_validator,
9293
bv.Void(),
93-
{'host': u'api',
94+
{'auth': u'user',
95+
'host': u'api',
9496
'style': u'rpc'},
9597
)
9698

dropbox/contacts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
115115
bv.Void(),
116116
bv.Void(),
117117
bv.Void(),
118-
{'host': u'api',
118+
{'auth': u'user',
119+
'host': u'api',
119120
'style': u'rpc'},
120121
)
121122
delete_manual_contacts_batch = bb.Route(
@@ -125,7 +126,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
125126
DeleteManualContactsArg_validator,
126127
bv.Void(),
127128
DeleteManualContactsError_validator,
128-
{'host': u'api',
129+
{'auth': u'user',
130+
'host': u'api',
129131
'style': u'rpc'},
130132
)
131133

dropbox/dropbox_client.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# before release.
99
__version__ = '0.0.0'
1010

11+
import base64
1112
import contextlib
1213
import json
1314
import logging
@@ -59,6 +60,11 @@
5960

6061
SELECT_USER_HEADER = 'Dropbox-API-Select-User'
6162

63+
USER_AUTH = 'user'
64+
TEAM_AUTH = 'team'
65+
APP_AUTH = 'app'
66+
NO_AUTH = 'noauth'
67+
6268
class RouteResult(object):
6369
"""The successful result of a call to a route."""
6470

@@ -293,6 +299,7 @@ def request(self,
293299
self.check_and_refresh_access_token()
294300

295301
host = route.attrs['host'] or 'api'
302+
auth_type = route.attrs['auth']
296303
route_name = namespace + '/' + route.name
297304
if route.version > 1:
298305
route_name += '_v{}'.format(route.version)
@@ -315,6 +322,7 @@ def request(self,
315322
route_name,
316323
route_style,
317324
serialized_arg,
325+
auth_type,
318326
request_binary,
319327
timeout=timeout)
320328
decoded_obj_result = json.loads(res.obj_result)
@@ -407,6 +415,7 @@ def request_json_object(self,
407415
route_name,
408416
route_style,
409417
request_arg,
418+
auth_type,
410419
request_binary,
411420
timeout=None):
412421
"""
@@ -418,6 +427,7 @@ def request_json_object(self,
418427
:param route_style: The style of the route.
419428
:param str request_arg: A JSON-serializable Python object representing
420429
the argument for the route.
430+
:param auth_type str
421431
:param Optional[bytes] request_binary: Bytes representing the binary
422432
payload. Use None if there is no binary payload.
423433
:param Optional[float] timeout: Maximum duration in seconds
@@ -432,6 +442,7 @@ def request_json_object(self,
432442
route_name,
433443
route_style,
434444
serialized_arg,
445+
auth_type,
435446
request_binary,
436447
timeout=timeout)
437448
# This can throw a ValueError if the result is not deserializable,
@@ -447,6 +458,7 @@ def request_json_string_with_retry(self,
447458
route_name,
448459
route_style,
449460
request_json_arg,
461+
auth_type,
450462
request_binary,
451463
timeout=None):
452464
"""
@@ -465,6 +477,7 @@ def request_json_string_with_retry(self,
465477
route_name,
466478
route_style,
467479
request_json_arg,
480+
auth_type,
468481
request_binary,
469482
timeout=timeout)
470483
except AuthError as e:
@@ -507,6 +520,7 @@ def request_json_string(self,
507520
func_name,
508521
route_style,
509522
request_json_arg,
523+
auth_type,
510524
request_binary,
511525
timeout=None):
512526
"""
@@ -529,10 +543,24 @@ def request_json_string(self,
529543
url = self._get_route_url(fq_hostname, func_name)
530544

531545
headers = {'User-Agent': self._user_agent}
532-
if host != HOST_NOTIFY:
546+
if auth_type == USER_AUTH or auth_type == TEAM_AUTH:
533547
headers['Authorization'] = 'Bearer %s' % self._oauth2_access_token
534548
if self._headers:
535549
headers.update(self._headers)
550+
elif auth_type == APP_AUTH:
551+
if self._app_key is None or self._app_secret is None:
552+
raise BadInputException(
553+
'Client id and client secret are required for routes with app auth')
554+
auth_header = base64.b64encode(
555+
"{}:{}".format(self._app_key, self._app_secret).encode("utf-8")
556+
)
557+
headers['Authorization'] = 'Basic {}'.format(auth_header.decode("utf-8"))
558+
if self._headers:
559+
headers.update(self._headers)
560+
elif auth_type == NO_AUTH:
561+
pass
562+
else:
563+
raise BadInputException('Unhandled auth type: {}'.format(auth_type))
536564

537565
# The contents of the body of the HTTP request
538566
body = None

dropbox/file_properties.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21062106
AddPropertiesArg_validator,
21072107
bv.Void(),
21082108
AddPropertiesError_validator,
2109-
{'host': u'api',
2109+
{'auth': u'user',
2110+
'host': u'api',
21102111
'style': u'rpc'},
21112112
)
21122113
properties_overwrite = bb.Route(
@@ -2116,7 +2117,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21162117
OverwritePropertyGroupArg_validator,
21172118
bv.Void(),
21182119
InvalidPropertyGroupError_validator,
2119-
{'host': u'api',
2120+
{'auth': u'user',
2121+
'host': u'api',
21202122
'style': u'rpc'},
21212123
)
21222124
properties_remove = bb.Route(
@@ -2126,7 +2128,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21262128
RemovePropertiesArg_validator,
21272129
bv.Void(),
21282130
RemovePropertiesError_validator,
2129-
{'host': u'api',
2131+
{'auth': u'user',
2132+
'host': u'api',
21302133
'style': u'rpc'},
21312134
)
21322135
properties_search = bb.Route(
@@ -2136,7 +2139,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21362139
PropertiesSearchArg_validator,
21372140
PropertiesSearchResult_validator,
21382141
PropertiesSearchError_validator,
2139-
{'host': u'api',
2142+
{'auth': u'user',
2143+
'host': u'api',
21402144
'style': u'rpc'},
21412145
)
21422146
properties_search_continue = bb.Route(
@@ -2146,7 +2150,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21462150
PropertiesSearchContinueArg_validator,
21472151
PropertiesSearchResult_validator,
21482152
PropertiesSearchContinueError_validator,
2149-
{'host': u'api',
2153+
{'auth': u'user',
2154+
'host': u'api',
21502155
'style': u'rpc'},
21512156
)
21522157
properties_update = bb.Route(
@@ -2156,7 +2161,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21562161
UpdatePropertiesArg_validator,
21572162
bv.Void(),
21582163
UpdatePropertiesError_validator,
2159-
{'host': u'api',
2164+
{'auth': u'user',
2165+
'host': u'api',
21602166
'style': u'rpc'},
21612167
)
21622168
templates_add_for_team = bb.Route(
@@ -2166,7 +2172,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21662172
AddTemplateArg_validator,
21672173
AddTemplateResult_validator,
21682174
ModifyTemplateError_validator,
2169-
{'host': u'api',
2175+
{'auth': u'team',
2176+
'host': u'api',
21702177
'style': u'rpc'},
21712178
)
21722179
templates_add_for_user = bb.Route(
@@ -2176,7 +2183,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21762183
AddTemplateArg_validator,
21772184
AddTemplateResult_validator,
21782185
ModifyTemplateError_validator,
2179-
{'host': u'api',
2186+
{'auth': u'user',
2187+
'host': u'api',
21802188
'style': u'rpc'},
21812189
)
21822190
templates_get_for_team = bb.Route(
@@ -2186,7 +2194,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21862194
GetTemplateArg_validator,
21872195
GetTemplateResult_validator,
21882196
TemplateError_validator,
2189-
{'host': u'api',
2197+
{'auth': u'team',
2198+
'host': u'api',
21902199
'style': u'rpc'},
21912200
)
21922201
templates_get_for_user = bb.Route(
@@ -2196,7 +2205,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
21962205
GetTemplateArg_validator,
21972206
GetTemplateResult_validator,
21982207
TemplateError_validator,
2199-
{'host': u'api',
2208+
{'auth': u'user',
2209+
'host': u'api',
22002210
'style': u'rpc'},
22012211
)
22022212
templates_list_for_team = bb.Route(
@@ -2206,7 +2216,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
22062216
bv.Void(),
22072217
ListTemplateResult_validator,
22082218
TemplateError_validator,
2209-
{'host': u'api',
2219+
{'auth': u'team',
2220+
'host': u'api',
22102221
'style': u'rpc'},
22112222
)
22122223
templates_list_for_user = bb.Route(
@@ -2216,7 +2227,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
22162227
bv.Void(),
22172228
ListTemplateResult_validator,
22182229
TemplateError_validator,
2219-
{'host': u'api',
2230+
{'auth': u'user',
2231+
'host': u'api',
22202232
'style': u'rpc'},
22212233
)
22222234
templates_remove_for_team = bb.Route(
@@ -2226,7 +2238,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
22262238
RemoveTemplateArg_validator,
22272239
bv.Void(),
22282240
TemplateError_validator,
2229-
{'host': u'api',
2241+
{'auth': u'team',
2242+
'host': u'api',
22302243
'style': u'rpc'},
22312244
)
22322245
templates_remove_for_user = bb.Route(
@@ -2236,7 +2249,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
22362249
RemoveTemplateArg_validator,
22372250
bv.Void(),
22382251
TemplateError_validator,
2239-
{'host': u'api',
2252+
{'auth': u'user',
2253+
'host': u'api',
22402254
'style': u'rpc'},
22412255
)
22422256
templates_update_for_team = bb.Route(
@@ -2246,7 +2260,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
22462260
UpdateTemplateArg_validator,
22472261
UpdateTemplateResult_validator,
22482262
ModifyTemplateError_validator,
2249-
{'host': u'api',
2263+
{'auth': u'team',
2264+
'host': u'api',
22502265
'style': u'rpc'},
22512266
)
22522267
templates_update_for_user = bb.Route(
@@ -2256,7 +2271,8 @@ def _process_custom_annotations(self, annotation_type, field_path, processor):
22562271
UpdateTemplateArg_validator,
22572272
UpdateTemplateResult_validator,
22582273
ModifyTemplateError_validator,
2259-
{'host': u'api',
2274+
{'auth': u'user',
2275+
'host': u'api',
22602276
'style': u'rpc'},
22612277
)
22622278

0 commit comments

Comments
 (0)