21
21
22
22
23
23
# The __init__.py will import this. Not the other way around.
24
- __version__ = "1.4.0 "
24
+ __version__ = "1.4.1 "
25
25
26
26
logger = logging .getLogger (__name__ )
27
27
@@ -198,9 +198,8 @@ def __init__(
198
198
authority or "https://login.microsoftonline.com/common/" ,
199
199
self .http_client , validate_authority = validate_authority )
200
200
# Here the self.authority is not the same type as authority in input
201
- self .client = None
202
201
self .token_cache = token_cache or TokenCache ()
203
- self ._client_credential = client_credential
202
+ self .client = self . _build_client ( client_credential , self . authority )
204
203
self .authority_groups = None
205
204
206
205
def _build_client (self , client_credential , authority ):
@@ -249,12 +248,6 @@ def _build_client(self, client_credential, authority):
249
248
on_removing_rt = self .token_cache .remove_rt ,
250
249
on_updating_rt = self .token_cache .update_rt )
251
250
252
- def _get_client (self ):
253
- if not self .client :
254
- self .authority .initialize ()
255
- self .client = self ._build_client (self ._client_credential , self .authority )
256
- return self .client
257
-
258
251
def get_authorization_request_url (
259
252
self ,
260
253
scopes , # type: list[str]
@@ -314,7 +307,6 @@ def get_authorization_request_url(
314
307
authority ,
315
308
self .http_client
316
309
) if authority else self .authority
317
- the_authority .initialize ()
318
310
319
311
client = Client (
320
312
{"authorization_endpoint" : the_authority .authorization_endpoint },
@@ -375,7 +367,7 @@ def acquire_token_by_authorization_code(
375
367
# really empty.
376
368
assert isinstance (scopes , list ), "Invalid parameter type"
377
369
self ._validate_ssh_cert_input_data (kwargs .get ("data" , {}))
378
- return self ._get_client () .obtain_token_by_authorization_code (
370
+ return self .client .obtain_token_by_authorization_code (
379
371
code , redirect_uri = redirect_uri ,
380
372
scope = decorate_scope (scopes , self .client_id ),
381
373
headers = {
@@ -399,7 +391,6 @@ def get_accounts(self, username=None):
399
391
Your app can choose to display those information to end user,
400
392
and allow user to choose one of his/her accounts to proceed.
401
393
"""
402
- self .authority .initialize ()
403
394
accounts = self ._find_msal_accounts (environment = self .authority .instance )
404
395
if not accounts : # Now try other aliases of this authority instance
405
396
for alias in self ._get_authority_aliases (self .authority .instance ):
@@ -552,7 +543,6 @@ def acquire_token_silent_with_error(
552
543
# authority,
553
544
# self.http_client,
554
545
# ) if authority else self.authority
555
- self .authority .initialize ()
556
546
result = self ._acquire_token_silent_from_cache_and_possibly_refresh_it (
557
547
scopes , account , self .authority , force_refresh = force_refresh ,
558
548
correlation_id = correlation_id ,
@@ -565,7 +555,6 @@ def acquire_token_silent_with_error(
565
555
"https://" + alias + "/" + self .authority .tenant ,
566
556
self .http_client ,
567
557
validate_authority = False )
568
- the_authority .initialize ()
569
558
result = self ._acquire_token_silent_from_cache_and_possibly_refresh_it (
570
559
scopes , account , the_authority , force_refresh = force_refresh ,
571
560
correlation_id = correlation_id ,
@@ -735,7 +724,7 @@ def acquire_token_by_refresh_token(self, refresh_token, scopes):
735
724
* A dict contains "error" and some other keys, when error happened.
736
725
* A dict contains no "error" key means migration was successful.
737
726
"""
738
- return self ._get_client () .obtain_token_by_refresh_token (
727
+ return self .client .obtain_token_by_refresh_token (
739
728
refresh_token ,
740
729
scope = decorate_scope (scopes , self .client_id ),
741
730
rt_getter = lambda rt : rt ,
@@ -766,7 +755,7 @@ def initiate_device_flow(self, scopes=None, **kwargs):
766
755
- an error response would contain some other readable key/value pairs.
767
756
"""
768
757
correlation_id = _get_new_correlation_id ()
769
- flow = self ._get_client () .initiate_device_flow (
758
+ flow = self .client .initiate_device_flow (
770
759
scope = decorate_scope (scopes or [], self .client_id ),
771
760
headers = {
772
761
CLIENT_REQUEST_ID : correlation_id ,
@@ -790,7 +779,7 @@ def acquire_token_by_device_flow(self, flow, **kwargs):
790
779
- A successful response would contain "access_token" key,
791
780
- an error response would contain "error" and usually "error_description".
792
781
"""
793
- return self ._get_client () .obtain_token_by_device_flow (
782
+ return self .client .obtain_token_by_device_flow (
794
783
flow ,
795
784
data = dict (kwargs .pop ("data" , {}), code = flow ["device_code" ]),
796
785
# 2018-10-4 Hack:
@@ -827,15 +816,14 @@ def acquire_token_by_username_password(
827
816
CLIENT_CURRENT_TELEMETRY : _build_current_telemetry_request_header (
828
817
self .ACQUIRE_TOKEN_BY_USERNAME_PASSWORD_ID ),
829
818
}
830
- self .authority .initialize ()
831
819
if not self .authority .is_adfs :
832
820
user_realm_result = self .authority .user_realm_discovery (
833
821
username , correlation_id = headers [CLIENT_REQUEST_ID ])
834
822
if user_realm_result .get ("account_type" ) == "Federated" :
835
823
return self ._acquire_token_by_username_password_federated (
836
824
user_realm_result , username , password , scopes = scopes ,
837
825
headers = headers , ** kwargs )
838
- return self ._get_client () .obtain_token_by_username_password (
826
+ return self .client .obtain_token_by_username_password (
839
827
username , password , scope = scopes ,
840
828
headers = headers ,
841
829
** kwargs )
@@ -864,16 +852,16 @@ def _acquire_token_by_username_password_federated(
864
852
GRANT_TYPE_SAML1_1 = 'urn:ietf:params:oauth:grant-type:saml1_1-bearer'
865
853
grant_type = {
866
854
SAML_TOKEN_TYPE_V1 : GRANT_TYPE_SAML1_1 ,
867
- SAML_TOKEN_TYPE_V2 : Client .GRANT_TYPE_SAML2 ,
855
+ SAML_TOKEN_TYPE_V2 : self . client .GRANT_TYPE_SAML2 ,
868
856
WSS_SAML_TOKEN_PROFILE_V1_1 : GRANT_TYPE_SAML1_1 ,
869
- WSS_SAML_TOKEN_PROFILE_V2 : Client .GRANT_TYPE_SAML2
857
+ WSS_SAML_TOKEN_PROFILE_V2 : self . client .GRANT_TYPE_SAML2
870
858
}.get (wstrust_result .get ("type" ))
871
859
if not grant_type :
872
860
raise RuntimeError (
873
861
"RSTR returned unknown token type: %s" , wstrust_result .get ("type" ))
874
- Client .grant_assertion_encoders .setdefault ( # Register a non-standard type
875
- grant_type , Client .encode_saml_assertion )
876
- return self ._get_client () .obtain_token_by_assertion (
862
+ self . client .grant_assertion_encoders .setdefault ( # Register a non-standard type
863
+ grant_type , self . client .encode_saml_assertion )
864
+ return self .client .obtain_token_by_assertion (
877
865
wstrust_result ["token" ], grant_type , scope = scopes , ** kwargs )
878
866
879
867
@@ -891,7 +879,7 @@ def acquire_token_for_client(self, scopes, **kwargs):
891
879
- an error response would contain "error" and usually "error_description".
892
880
"""
893
881
# TBD: force_refresh behavior
894
- return self ._get_client () .obtain_token_for_client (
882
+ return self .client .obtain_token_for_client (
895
883
scope = scopes , # This grant flow requires no scope decoration
896
884
headers = {
897
885
CLIENT_REQUEST_ID : _get_new_correlation_id (),
@@ -923,9 +911,9 @@ def acquire_token_on_behalf_of(self, user_assertion, scopes, **kwargs):
923
911
"""
924
912
# The implementation is NOT based on Token Exchange
925
913
# https://tools.ietf.org/html/draft-ietf-oauth-token-exchange-16
926
- return self ._get_client () .obtain_token_by_assertion ( # bases on assertion RFC 7521
914
+ return self .client .obtain_token_by_assertion ( # bases on assertion RFC 7521
927
915
user_assertion ,
928
- Client .GRANT_TYPE_JWT , # IDTs and AAD ATs are all JWTs
916
+ self . client .GRANT_TYPE_JWT , # IDTs and AAD ATs are all JWTs
929
917
scope = decorate_scope (scopes , self .client_id ), # Decoration is used for:
930
918
# 1. Explicitly requesting an RT, without relying on AAD default
931
919
# behavior, even though it currently still issues an RT.
0 commit comments