@@ -62,17 +62,13 @@ def _parse_login_page(html: str, page_url: str) -> dict[str, str]:
6262 # Extract CSRF token from var SETTINGS = {..., "csrf":"...", ...}
6363 csrf_match = re .search (r'"csrf"\s*:\s*"([^"]+)"' , html )
6464 if not csrf_match :
65- raise AuthenticationError (
66- "Could not find CSRF token in B2C login page"
67- )
65+ raise AuthenticationError ("Could not find CSRF token in B2C login page" )
6866 csrf = csrf_match .group (1 )
6967
7068 # Extract transId from SETTINGS
7169 tx_match = re .search (r'"transId"\s*:\s*"([^"]+)"' , html )
7270 if not tx_match :
73- raise AuthenticationError (
74- "Could not find transId in B2C login page"
75- )
71+ raise AuthenticationError ("Could not find transId in B2C login page" )
7672 tx = tx_match .group (1 )
7773
7874 p = _B2C_POLICY
@@ -84,10 +80,7 @@ def _parse_login_page(html: str, page_url: str) -> dict[str, str]:
8480 qs = f"tx={ tx } &p={ p } "
8581
8682 post_url = f"{ origin } { base } SelfAsserted?{ qs } "
87- confirmed_url = (
88- f"{ origin } { base } "
89- f"api/CombinedSigninAndSignup/confirmed"
90- )
83+ confirmed_url = f"{ origin } { base } api/CombinedSigninAndSignup/confirmed"
9184
9285 return {
9386 "csrf" : csrf ,
@@ -109,10 +102,8 @@ def _build_cookie_header(
109102 browsers send them). This function formats cookies in the plain
110103 ``name=value; name2=value2`` style that B2C requires.
111104 """
112- filtered = cookie_jar .filter_cookies (url )
113- return "; " .join (
114- f"{ m .key } ={ m .value } " for m in filtered .values ()
115- )
105+ filtered = cookie_jar .filter_cookies (yarl .URL (url ))
106+ return "; " .join (f"{ m .key } ={ m .value } " for m in filtered .values ())
116107
117108
118109def _log_request (
@@ -130,10 +121,7 @@ def _log_request(
130121 if headers :
131122 _LOGGER .debug (">>> headers: %s" , headers )
132123 if data :
133- safe = {
134- k : ("***" if k == "password" else v )
135- for k , v in data .items ()
136- }
124+ safe = {k : ("***" if k == "password" else v ) for k , v in data .items ()}
137125 _LOGGER .debug (">>> body: %s" , safe )
138126
139127
@@ -143,7 +131,9 @@ def _log_response(
143131) -> None :
144132 """Log an incoming HTTP response at DEBUG level."""
145133 _LOGGER .debug (
146- "<<< %s %s" , resp .status , resp .url ,
134+ "<<< %s %s" ,
135+ resp .status ,
136+ resp .url ,
147137 )
148138 _LOGGER .debug ("<<< headers: %s" , dict (resp .headers ))
149139 if body is not None :
@@ -153,9 +143,7 @@ def _log_response(
153143 _LOGGER .debug ("<<< body: %s" , preview )
154144
155145
156- async def b2c_login_with_credentials (
157- auth_uri : str , email : str , password : str
158- ) -> str :
146+ async def b2c_login_with_credentials (auth_uri : str , email : str , password : str ) -> str :
159147 """Submit credentials directly to Azure AD B2C and return the redirect URL.
160148
161149 Performs the same HTTP flow a browser would:
@@ -216,12 +204,8 @@ async def b2c_login_with_credentials(
216204 "X-Requested-With" : "XMLHttpRequest" ,
217205 "Referer" : auth_uri ,
218206 "Origin" : origin ,
219- "Accept" : (
220- "application/json, text/javascript, */*; q=0.01"
221- ),
222- "Content-Type" : (
223- "application/x-www-form-urlencoded; charset=UTF-8"
224- ),
207+ "Accept" : ("application/json, text/javascript, */*; q=0.01" ),
208+ "Content-Type" : ("application/x-www-form-urlencoded; charset=UTF-8" ),
225209 }
226210
227211 # Build an unquoted Cookie header — aiohttp's cookie jar
@@ -255,17 +239,11 @@ async def b2c_login_with_credentials(
255239 _log_response (resp , body )
256240 if resp .status != 200 :
257241 raise AuthenticationError (
258- f"Credential submission returned HTTP"
259- f" { resp .status } "
242+ f"Credential submission returned HTTP { resp .status } "
260243 )
261244 # Check for error in the JSON-like response
262- if (
263- '"status":"400"' in body
264- or '"status": "400"' in body
265- ):
266- raise AuthenticationError (
267- "Invalid email or password"
268- )
245+ if '"status":"400"' in body or '"status": "400"' in body :
246+ raise AuthenticationError ("Invalid email or password" )
269247 # Merge cookies set by the POST response (e.g.
270248 # updated x-ms-cpim-cache and x-ms-cpim-trans)
271249 # into the cookie header for the confirmed GET.
@@ -276,16 +254,12 @@ async def b2c_login_with_credentials(
276254 if "=" in part :
277255 n , v = part .split ("=" , 1 )
278256 cookies [n ] = v
279- for raw_sc in resp .headers .getall (
280- "Set-Cookie" , []
281- ):
257+ for raw_sc in resp .headers .getall ("Set-Cookie" , []):
282258 sc_pair = raw_sc .split (";" , 1 )[0 ]
283259 if "=" in sc_pair :
284260 n , v = sc_pair .split ("=" , 1 )
285261 cookies [n ] = v
286- cookie_header = "; " .join (
287- f"{ n } ={ v } " for n , v in cookies .items ()
288- )
262+ cookie_header = "; " .join (f"{ n } ={ v } " for n , v in cookies .items ())
289263
290264 # Step 4: GET the confirmed endpoint — follows redirects
291265 # until we hit the custom-scheme redirect
@@ -300,9 +274,7 @@ async def b2c_login_with_credentials(
300274 )
301275
302276 # Follow redirects manually to catch custom-scheme one
303- next_url : str = (
304- fields ["confirmed_url" ] + "?" + confirmed_qs
305- )
277+ next_url : str = fields ["confirmed_url" ] + "?" + confirmed_qs
306278 confirmed_headers = {
307279 "Cookie" : cookie_header ,
308280 }
@@ -316,53 +288,39 @@ async def b2c_login_with_credentials(
316288 resp_body = await resp .text ()
317289 _log_response (resp , resp_body )
318290 if resp .status in (301 , 302 , 303 , 307 , 308 ):
319- location = resp .headers .get (
320- "Location" , ""
321- )
291+ location = resp .headers .get ("Location" , "" )
322292 if not location :
323293 raise AuthenticationError (
324- "Redirect without Location"
325- " header"
294+ "Redirect without Location header"
326295 )
327296 # Custom-scheme redirect (msal{id}://auth)
328- if (
329- location .startswith ("msal" )
330- and "://auth" in location
331- ):
297+ if location .startswith ("msal" ) and "://auth" in location :
332298 _LOGGER .debug (
333- "Captured custom-scheme"
334- " redirect: %s" ,
299+ "Captured custom-scheme redirect: %s" ,
335300 location [:120 ] + "..." ,
336301 )
337302 return location
338303 # Resolve relative URLs
339304 if not location .startswith ("http" ):
340- location = urljoin (
341- next_url , location
342- )
305+ location = urljoin (next_url , location )
343306 next_url = location
344307 continue
345308 if resp .status == 200 :
346309 redirect_match = re .search (
347- r' (msal[a-f0-9-]+://auth'
310+ r" (msal[a-f0-9-]+://auth"
348311 r'\?[^\s"\'<]+)' ,
349312 resp_body ,
350313 )
351314 if redirect_match :
352315 return redirect_match .group (1 )
353316 raise AuthenticationError (
354- "Reached 200 response without"
355- " finding redirect URL"
317+ "Reached 200 response without finding redirect URL"
356318 )
357319 raise AuthenticationError (
358- "Unexpected HTTP"
359- f" { resp .status } during"
360- " redirect chain"
320+ f"Unexpected HTTP { resp .status } during redirect chain"
361321 )
362322
363- raise AuthenticationError (
364- "Too many redirects during B2C login"
365- )
323+ raise AuthenticationError ("Too many redirects during B2C login" )
366324 except AuthenticationError :
367325 raise
368326 except aiohttp .ClientError as exc :
0 commit comments