1
+ import time
2
+
1
3
from . import oauth2
2
4
from .exceptions import MsalServiceError
3
5
@@ -14,19 +16,41 @@ def __init__(
14
16
self .__dict__ .update (locals ())
15
17
16
18
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
+ """
17
29
# TODO Some cache stuff here
18
30
raw = self .get_token ()
19
31
if 'error' in raw :
20
32
raise MsalServiceError (** raw )
21
33
# 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
29
48
}
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 )
30
54
31
55
def get_token (self ):
32
56
raise NotImplemented ("Use proper sub-class instead" )
0 commit comments