@@ -19,17 +19,21 @@ def resolve_base_url(url):
19
19
return parts [0 ] + '://' + host_name
20
20
21
21
22
+ def xml_escape (s_val ):
23
+ s_val = s_val .replace ("&" , "&" )
24
+ s_val = s_val .replace ("<" , "<" )
25
+ s_val = s_val .replace (">" , ">" )
26
+ s_val = s_val .replace ("\" " , """ )
27
+ return s_val
28
+
29
+
22
30
class SamlTokenProvider (BaseTokenProvider , office365 .logger .LoggerContext ):
23
31
24
- def __init__ (self , url , username , password ):
32
+ def __init__ (self , url ):
25
33
"""SAML Security Token Service provider
26
34
27
- :type password: str
28
- :type username: str
29
35
:type url: str
30
36
"""
31
- self .__username = username
32
- self .__password = password
33
37
# Security Token Service info
34
38
self .__sts_profile = STSProfile (resolve_base_url (url ))
35
39
# Last occurred error
@@ -62,11 +66,13 @@ def acquire_token(self, **kwargs):
62
66
63
67
try :
64
68
logger .debug ("Acquiring Access Token.." )
65
- user_realm = self ._get_user_realm (self .__username )
69
+ username = kwargs .get ("username" )
70
+ password = xml_escape (kwargs .get ("password" ))
71
+ user_realm = self ._get_user_realm (username )
66
72
if user_realm .IsFederated :
67
- token = self .acquire_service_token_from_adfs (user_realm .STSAuthUrl , self . __username , self . __password )
73
+ token = self .acquire_service_token_from_adfs (user_realm .STSAuthUrl , username , password )
68
74
else :
69
- token = self ._acquire_service_token (self . __username , self . __password )
75
+ token = self ._acquire_service_token (username , password )
70
76
return self ._acquire_authentication_cookie (token , user_realm .IsFederated )
71
77
except requests .exceptions .RequestException as e :
72
78
self .error = "Error: {}" .format (e )
@@ -118,7 +124,7 @@ def acquire_service_token_from_adfs(self, adfs_url, username, password):
118
124
'{0}Body/{1}RequestSecurityTokenResponse/{1}RequestedSecurityToken/{2}Assertion' .format (
119
125
self .__ns_prefixes ['s' ], self .__ns_prefixes ['wst' ], self .__ns_prefixes ['saml' ]))
120
126
if assertion_node is None :
121
- self .error = 'Cannot get security assertion for user {0} from {1}' .format (self . __username , adfs_url )
127
+ self .error = 'Cannot get security assertion for user {0} from {1}' .format (username , adfs_url )
122
128
logger .error (self .error )
123
129
return None
124
130
# 2. prepare & submit token request
0 commit comments