Skip to content

Commit 13868ed

Browse files
committed
OnBehalfOf implementation
1 parent 8c5a1e2 commit 13868ed

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

msal/application.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,17 @@ def acquire_token_for_client(self, scopes, **kwargs):
639639
scope=scopes, # This grant flow requires no scope decoration
640640
**kwargs)
641641

642-
def acquire_token_on_behalf_of(self, user_assertion, scopes, authority=None):
643-
raise NotImplementedError()
642+
def acquire_token_on_behalf_of(
643+
self, user_assertion, scope, authority=None, policy=''):
644+
the_authority = Authority(authority) if authority else self.authority
645+
return oauth2.Client(
646+
self.client_id, token_endpoint=the_authority.token_endpoint,
647+
default_body=self._build_auth_parameters(
648+
self.client_credential, the_authority.token_endpoint,
649+
self.client_id)
650+
)._get_token( # TODO: Avoid using internal methods
651+
"urn:ietf:params:oauth:grant-type:jwt-bearer",
652+
assertion=user_assertion, requested_token_use='on_behalf_of',
653+
scope=scope, # This grant flow requires no scope decoration???
654+
query={'p': policy} if policy else None)
644655

tests/test_application.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,12 @@ def test_acquire_token_silent(self):
194194
self.assertNotEqual(None, at)
195195
self.assertEqual(self.access_token, at.get('access_token'))
196196

197+
def test_acquire_token_obo(self):
198+
token = self.app.acquire_token_on_behalf_of(
199+
self.token['access_token'], self.scope2)
200+
error_description = token.get('error_description', "")
201+
if 'grant is not supported by this API version' in error_description:
202+
raise unittest.SkipTest(
203+
"OBO is not yet supported by service: %s" % error_description)
204+
self.assertEqual(error_description, "")
205+

0 commit comments

Comments
 (0)