Skip to content

Commit eb38e8b

Browse files
committed
Implement an AuthenticationResult equivalent
1 parent 5687d97 commit eb38e8b

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

msal/request.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import time
2+
13
from . import oauth2
24
from .exceptions import MsalServiceError
35

@@ -14,19 +16,41 @@ def __init__(
1416
self.__dict__.update(locals())
1517

1618
def run(self):
19+
"""Returns a dictionary, which typically contains following keys:
20+
21+
* token: A string containing an access token (or id token)
22+
* expires_on: A timestamp, in seconds. So compare it with time.time().
23+
* user: TBD
24+
* and some other keys from the wire, such as "scope", "id_token", etc.,
25+
which may or may not appear in every different grant flow.
26+
So you should NOT assume their existence,
27+
instead you would need to access them safely by dict.get('...').
28+
"""
1729
# TODO Some cache stuff here
1830
raw = self.get_token()
1931
if 'error' in raw:
2032
raise MsalServiceError(**raw)
2133
# 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
34+
35+
# Keep (most) contents in raw token response, extend it, and return it
36+
raw['token'] = raw.get('access_token') or raw.get('id_token')
37+
raw['expires_on'] = self.__timestamp(
38+
# A timestamp is chosen because it is more lighweight than Datetime,
39+
# and then the entire return value can be serialized as JSON string,
40+
# should the developers choose to do so.
41+
# This is the same timestamp format used in JWT's "iat", by the way.
42+
raw.get('expires_in') or raw.get('id_token_expires_in'))
43+
if 'scope' in raw:
44+
raw['scope'] = set(raw['scope'].split()) # Using SPACE as delimiter
45+
raw['user'] = { # Contents derived from raw['id_token']
46+
# TODO: Follow https://github.com/AzureAD/microsoft-authentication-library-for-android/blob/dev/msal/src/internal/java/com/microsoft/identity/client/IdToken.java
47+
# https://github.com/AzureAD/microsoft-authentication-library-for-android/blob/dev/msal/src/internal/java/com/microsoft/identity/client/User.java
2948
}
49+
return raw # equivalent to AuthenticationResult in other MSAL SDKs
50+
51+
def __timestamp(self, seconds_from_now=None): # Returns timestamp IN SECOND
52+
return time.time() + (
53+
seconds_from_now if seconds_from_now is not None else 3600)
3054

3155
def get_token(self):
3256
raise NotImplemented("Use proper sub-class instead")

0 commit comments

Comments
 (0)