Skip to content

Commit f365c17

Browse files
committed
Merge pull request #97 from box/dev_token_auth
Add developer token auth class.
2 parents a3965f3 + 1f1784a commit f365c17

File tree

9 files changed

+90
-9
lines changed

9 files changed

+90
-9
lines changed

boxsdk/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
from __future__ import unicode_literals
44

5-
try:
6-
from .auth.jwt_auth import JWTAuth
7-
except ImportError:
8-
JWTAuth = None # If extras are not installed, JWTAuth won't be available.
9-
from .auth.oauth2 import OAuth2
10-
from .client import Client
5+
from .auth import JWTAuth, OAuth2
6+
from .client import Client, DeveloperTokenClient
117
from .object import * # pylint:disable=wildcard-import,redefined-builtin

boxsdk/auth/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# coding: utf-8
22

33
from __future__ import unicode_literals
4+
5+
from .cooperatively_managed_oauth2 import CooperativelyManagedOAuth2
6+
from .developer_token_auth import DeveloperTokenAuth
7+
try:
8+
from .jwt_auth import JWTAuth
9+
except ImportError:
10+
JWTAuth = None # If extras are not installed, JWTAuth won't be available.
11+
from .oauth2 import OAuth2
12+
from .redis_managed_oauth2 import RedisManagedJWTAuth, RedisManagedOAuth2
13+
from .remote_managed_oauth2 import RemoteOAuth2

boxsdk/auth/cooperatively_managed_oauth2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding: utf-8
22

33
from __future__ import unicode_literals
4-
from boxsdk import OAuth2
4+
from .oauth2 import OAuth2
55

66

77
class CooperativelyManagedOAuth2Mixin(OAuth2):
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# coding: utf-8
2+
3+
from __future__ import unicode_literals, absolute_import
4+
5+
from .oauth2 import OAuth2
6+
7+
8+
class DeveloperTokenAuth(OAuth2):
9+
ENTER_TOKEN_PROMPT = 'Enter developer token: '
10+
11+
def __init__(self, get_new_token_callback=None, **kwargs):
12+
self._get_new_token = get_new_token_callback
13+
super(DeveloperTokenAuth, self).__init__(
14+
client_id=None,
15+
client_secret=None,
16+
access_token=self._refresh_developer_token(),
17+
**kwargs
18+
)
19+
20+
def _refresh_developer_token(self):
21+
if self._get_new_token is not None:
22+
return self._get_new_token()
23+
else:
24+
return raw_input(self.ENTER_TOKEN_PROMPT)
25+
26+
def _refresh(self, access_token):
27+
"""
28+
Base class override.
29+
Ask for a new developer token.
30+
"""
31+
self._access_token = self._refresh_developer_token()
32+
return self._access_token, None

boxsdk/auth/redis_managed_oauth2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from redis import StrictRedis
88
from redis.lock import Lock
99

10-
from boxsdk import JWTAuth, OAuth2
10+
from .jwt_auth import JWTAuth
11+
from .oauth2 import OAuth2
1112

1213

1314
class RedisManagedOAuth2Mixin(OAuth2):

boxsdk/auth/remote_managed_oauth2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding: utf-8
22

33
from __future__ import unicode_literals
4-
from boxsdk import OAuth2
4+
from .oauth2 import OAuth2
55

66

77
class RemoteOAuth2Mixin(OAuth2):

boxsdk/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import unicode_literals
44
import json
55

6+
from .auth import DeveloperTokenAuth
67
from .config import API
78
from .session.box_session import BoxSession
89
from .network.default_network import DefaultNetwork
@@ -329,3 +330,8 @@ def with_shared_link(self, shared_link, shared_link_password):
329330
self._network,
330331
self._session.with_shared_link(shared_link, shared_link_password),
331332
)
333+
334+
335+
class DeveloperTokenClient(Client):
336+
def __init__(self, oauth=None, network_layer=None, session=None):
337+
super(DeveloperTokenClient, self).__init__(oauth or DeveloperTokenAuth(), network_layer, session)

docs/source/boxsdk.auth.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ boxsdk.auth.cooperatively_managed_oauth2 module
1212
:undoc-members:
1313
:show-inheritance:
1414

15+
boxsdk.auth.developer_token_auth module
16+
---------------------------------------
17+
18+
.. automodule:: boxsdk.auth.developer_token_auth
19+
:members:
20+
:undoc-members:
21+
:show-inheritance:
22+
1523
boxsdk.auth.jwt_auth module
1624
---------------------------
1725

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# coding: utf-8
2+
3+
from __future__ import unicode_literals, absolute_import
4+
5+
from mock import Mock, patch
6+
from boxsdk.auth import developer_token_auth
7+
8+
9+
def test_developer_token_auth_calls_callback_during_init_and_refresh(access_token):
10+
get_new_token_callback = Mock()
11+
get_new_token_callback.return_value = access_token
12+
auth = developer_token_auth.DeveloperTokenAuth(
13+
get_new_token_callback=get_new_token_callback,
14+
)
15+
assert auth.access_token == access_token
16+
get_new_token_callback.assert_called_once_with()
17+
assert auth.refresh(access_token) == (access_token, None)
18+
assert len(get_new_token_callback.mock_calls) == 2
19+
20+
21+
def test_developer_token_auth_uses_raw_input_by_default(access_token):
22+
with patch('boxsdk.auth.developer_token_auth.raw_input', create=True) as mock_raw_input:
23+
mock_raw_input.return_value = access_token
24+
auth = developer_token_auth.DeveloperTokenAuth()
25+
mock_raw_input.assert_called_once_with(auth.ENTER_TOKEN_PROMPT)
26+
assert auth.access_token == access_token
27+
assert auth.refresh(access_token) == (access_token, None)
28+
assert len(mock_raw_input.mock_calls) == 2

0 commit comments

Comments
 (0)