Skip to content

Commit 4bea865

Browse files
committed
Allow passing user and enterprise_id to JWTAuth
The initial commit that added the `user` parameter to `JWTAuth` only allowed it to be passed when `enterprise_id=None` was passed. After playing around with this a little bit, I determined that this is unnecessarily inflexible for developers. So this constraint is now relaxed. They can both be passed, with `user` taking precedence. Fixup for Pull Request #178.
1 parent 12fb081 commit 4bea865

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

boxsdk/auth/jwt_auth.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ def __init__(
4040
):
4141
"""Extends baseclass method.
4242
43-
At most one of `enterprise_id` and `user` may be non-`None`. If they
44-
are both `None`, then the caller is required to manually call either
45-
`authenticate_user()` or `authenticate_instance()` with the desired
46-
`sub` claim in order to begin using this auth object.
43+
If both `enterprise_id` and `user` are non-`None`, the `user` takes
44+
precedence when `refresh()` is called. This can be overruled with a
45+
call to `authenticate_instance()`.
4746
4847
:param client_id:
4948
Box API key used for identifying the application the user is authenticating with.
@@ -59,7 +58,9 @@ def __init__(
5958
May be `None`, if the caller knows that it will not be
6059
authenticating as an enterprise instance / service account.
6160
62-
Must be `None` if `user` is passed.
61+
If `user` is passed, this value is not used, unless
62+
`authenticate_instance()` is called to clear the user and
63+
authenticate as the enterprise instance.
6364
:type enterprise_id:
6465
`unicode` or `None`
6566
:param jwt_key_id:
@@ -82,7 +83,10 @@ def __init__(
8283
will be auto-authenticated at the time of the first API call or
8384
when calling `authenticate_user()` without any arguments.
8485
85-
Must be `None` if `enterprise_id` is passed.
86+
Should be `None` if the intention is to authenticate as the
87+
enterprise instance / service account. If both `enterprise_id` and
88+
`user` are non-`None`, the `user` takes precedense when `refresh()`
89+
is called.
8690
8791
May be one of this application's created App User. Depending on the
8892
configured User Access Level, may also be any other App User or
@@ -117,8 +121,6 @@ def __init__(
117121
:type jwt_algorithm:
118122
`unicode`
119123
"""
120-
if enterprise_id and user:
121-
raise TypeError("Cannot pass both 'enterprise_id' and 'user'.")
122124
user_id = self._normalize_user_id(user)
123125
super(JWTAuth, self).__init__(
124126
client_id,

test/unit/auth/test_jwt_auth.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,10 @@ def _jwt_auth_init_mocks(**kwargs):
111111
return _jwt_auth_init_mocks
112112

113113

114-
def test_jwt_auth_constructor_raises_type_error_if_enterprise_id_and_user_both_passed(jwt_auth_init_mocks):
115-
enterprise_id = 'fake_enterprise_id'
114+
def test_refresh_authenticates_with_user_if_enterprise_id_and_user_both_passed_to_constructor(jwt_auth_init_and_auth_mocks):
116115
user = 'fake_user_id'
117-
118-
# Make sure that constructing the `JWTAuth` object works when `user` is not
119-
# passed.
120-
with jwt_auth_init_mocks(enterprise_id=enterprise_id, assert_authed=False):
121-
pass
122-
123-
with pytest.raises(TypeError):
124-
with jwt_auth_init_mocks(enterprise_id=enterprise_id, user=user):
125-
assert False
116+
with jwt_auth_init_and_auth_mocks(sub=user, sub_type='user', enterprise_id='fake_enterprise_id', user=user) as oauth:
117+
oauth.refresh(None)
126118

127119

128120
@pytest.mark.parametrize('jwt_auth_method_name', ['authenticate_user', 'authenticate_instance'])

0 commit comments

Comments
 (0)