Skip to content

Commit 5687d97

Browse files
committed
Start the work on Request middle layer
1 parent 323d04d commit 5687d97

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

msal/application.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from . import oauth2
2-
from .exceptions import MsalServiceError
1+
from . import request
32

43

54
class ClientApplication(object):
65
DEFAULT_AUTHORITY = "https://login.microsoftonline.com/common/"
7-
TOKEN_ENDPOINT_PATH = 'oauth2/v2.0/token'
86

97
def __init__(
108
self, client_id,
@@ -37,12 +35,7 @@ def __init__(self, client_id, client_credential, user_token_cache, **kwargs):
3735
self.app_token_cache = None # TODO
3836

3937
def acquire_token_for_client(self, scope, policy=''):
40-
result = oauth2.ClientCredentialGrant(
41-
self.client_id,
42-
token_endpoint="%s%s?policy=%s" % (
43-
self.authority, self.TOKEN_ENDPOINT_PATH, policy),
44-
).get_token(scope=scope, client_secret=self.client_credential)
45-
if 'error' in result:
46-
raise MsalServiceError(**result)
47-
return result
38+
return request.ClientCredentialRequest(
39+
client_id=self.client_id, client_credential=self.client_credential,
40+
scope=scope, policy=policy, authority=self.authority).run()
4841

msal/request.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from . import oauth2
2+
from .exceptions import MsalServiceError
3+
4+
5+
class BaseRequest(object):
6+
TOKEN_ENDPOINT_PATH = 'oauth2/v2.0/token'
7+
8+
def __init__(
9+
self, authority=None, token_cache=None, scope=None, policy="",
10+
client_id=None, client_credential=None, authenticator=None,
11+
support_adfs=False, restrict_to_single_user=False):
12+
if not scope:
13+
raise ValueError("scope cannot be empty")
14+
self.__dict__.update(locals())
15+
16+
def run(self):
17+
# TODO Some cache stuff here
18+
raw = self.get_token()
19+
if 'error' in raw:
20+
raise MsalServiceError(**raw)
21+
# TODO: Deal with refresh_token
22+
return { # i.e. the AuthenticationResult
23+
"token": raw.get('access_token'),
24+
"expires_on": raw.get('expires_in'), # TODO: Change into EPOCH
25+
"tenant_id": None, # TODO
26+
"user": None, # TODO
27+
"id_token": None, # TODO
28+
"scope": set([]), # TODO
29+
}
30+
31+
def get_token(self):
32+
raise NotImplemented("Use proper sub-class instead")
33+
34+
35+
class ClientCredentialRequest(BaseRequest):
36+
def get_token(self):
37+
return oauth2.ClientCredentialGrant(
38+
self.client_id,
39+
token_endpoint="%s%s?policy=%s" % (
40+
self.authority, self.TOKEN_ENDPOINT_PATH, self.policy),
41+
).get_token(scope=self.scope, client_secret=self.client_credential)
42+

0 commit comments

Comments
 (0)