@@ -104,6 +104,7 @@ async def connect(self, username, password):
104104 async def reconnect (self ):
105105 # Get code challenge and verifier
106106 code_verifier , code_challenge = self .get_code_challenge ()
107+ self .log .debug ("Starting Cupra reconnect/auth flow" )
107108
108109 # Get authorize page
109110 _scope = 'openid profile nickname birthdate phone'
@@ -119,6 +120,7 @@ async def reconnect(self):
119120 }
120121
121122 response = await self .session .get (LOGIN_BASE + '/authorize' , params = payload )
123+ self .log .debug ("Authorize request finished with status=%s" , response .status )
122124 if response .status >= 400 :
123125 self .log .error (f"Authorize: Non-2xx response ({ response .status } )" )
124126 # Non 2xx response, failed
@@ -127,27 +129,37 @@ async def reconnect(self):
127129 # Fill form with email (username)
128130 (form , action ) = self .form_from_response (await response .read ())
129131 form ['email' ] = self .username
132+ self .log .debug ("Submitting email form to action=%s" , action )
130133 response = await self .session .post (LOGIN_HANDLER_BASE + action , data = form )
134+ self .log .debug ("Email form response status=%s" , response .status )
131135 if response .status >= 400 :
132136 self .log .error ("Email: Non-2xx response" )
133137 return False
134138
135139 # Fill form with password
136140 (form , action ) = self .password_form (await response .read ())
141+ if not form or not action :
142+ self .log .error ("Password form parsing failed" )
143+ return False
137144 form ['password' ] = self .password
138145 url = LOGIN_HANDLER_BASE + action
146+ self .log .debug ("Submitting password form to url=%s" , url )
139147 response = await self .session .post (url , data = form , allow_redirects = False )
148+ self .log .debug ("Password form response status=%s" , response .status )
140149
141150 # Can get a 303 redirect for a "terms and conditions" page
142151 if (response .status == 303 ):
143152 url = response .headers ['Location' ]
153+ self .log .debug ("Received 303 redirect to %s" , url )
144154 if ("terms-and-conditions" in url ):
145155 # Get terms and conditions page
146156 url = LOGIN_HANDLER_BASE + url
157+ self .log .debug ("Opening terms and conditions page: %s" , url )
147158 response = await self .session .get (url , data = form , allow_redirects = False )
148159 (form , action ) = self .form_from_response (await response .read ())
149160
150161 url = LOGIN_HANDLER_BASE + action
162+ self .log .debug ("Submitting terms and conditions form to %s" , url )
151163 response = await self .session .post (url , data = form , allow_redirects = False )
152164
153165 self .log .warning ("Agreed to terms and conditions" )
@@ -158,18 +170,25 @@ async def reconnect(self):
158170 # Handle every single redirect and stop if the redirect
159171 # URL uses the seat adapter.
160172 while (True ):
173+ if 'Location' not in response .headers :
174+ self .log .error ("Redirect handling stopped: missing Location header (status=%s)" ,
175+ response .status )
176+ return False
177+
161178 url = response .headers ['Location' ]
179+ self .log .debug ("Redirect: status=%s location=%s" , response .status , url )
162180 if (url .split (':' )[0 ] == "seat" ):
163181 if not ('code' in url ):
164182 self .log .error ("Missing authorization code" )
165183 return False
166184 # Parse query string
167185 query_string = url .split ('?' )[1 ]
168186 query = {x [0 ]: x [1 ] for x in [x .split ("=" ) for x in query_string .split ("&" )]}
187+ self .log .debug ("Authorization redirect reached" )
169188 break
170189
171190 if (response .status != 302 ):
172- self .log .error ("Not redirected, status %u" % response .status )
191+ self .log .error ("Not redirected, status=%u, last_url=%s" , response .status , url )
173192 return False
174193
175194 response = await self .session .get (url , data = form , allow_redirects = False )
@@ -191,6 +210,7 @@ async def reconnect(self):
191210
192211 response = await self .session .post (API_BASE + '/authorization/api/v1/token' ,
193212 headers = headers , data = form )
213+ self .log .debug ("Authorization code exchange status=%s" , response .status )
194214 if response .status >= 400 :
195215 self .log .error ("Login: Non-2xx response" )
196216 # Non 2xx response, failed
@@ -199,6 +219,7 @@ async def reconnect(self):
199219
200220 # Update header with final token
201221 self .headers ['Authorization' ] = 'Bearer %s' % self .tokens ["accessToken" ]
222+ self .log .debug ("Cupra authorization completed successfully" )
202223
203224 # Success
204225 return True
0 commit comments