Skip to content

Commit fed51a6

Browse files
authored
Merge pull request #3144 from vuffiraa72/more-logging
SoC Cupra/Skoda: Add debug logging for reconnect/auth flow
2 parents 13648ea + d25c58d commit fed51a6

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

packages/modules/vehicles/cupra/libcupra.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

packages/modules/vehicles/skoda/libskoda.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ async def connect(self, username, password):
9797
async def reconnect(self):
9898
# Get code challenge and verifier
9999
code_verifier, code_challenge = self.get_code_challenge()
100+
self.log.debug("Starting Skoda reconnect/auth flow")
100101

101102
# Get authorize page
102103
_scope = 'address badge birthdate cars driversLicense dealers email mileage mbb nationalIdentifier'
@@ -113,6 +114,7 @@ async def reconnect(self):
113114
}
114115

115116
response = await self.session.get(LOGIN_BASE + '/authorize', params=payload)
117+
self.log.debug("Authorize request finished with status=%s", response.status)
116118
if response.status >= 400:
117119
self.log.error(f"Authorize: Non-2xx response ({response.status})")
118120
# Non 2xx response, failed
@@ -121,49 +123,67 @@ async def reconnect(self):
121123
# Fill form with email (username)
122124
(form, action) = self.form_from_response(await response.read())
123125
form['email'] = self.username
126+
self.log.debug("Submitting email form to action=%s", action)
124127
response = await self.session.post(LOGIN_HANDLER_BASE+action, data=form)
128+
self.log.debug("Email form response status=%s", response.status)
125129
if response.status >= 400:
126130
self.log.error("Email: Non-2xx response")
127131
return False
128132

129133
# Fill form with password
130134
(form, action) = self.password_form(await response.read())
135+
if not form or not action:
136+
self.log.error("Password form parsing failed")
137+
return False
131138
form['password'] = self.password
132139
url = LOGIN_HANDLER_BASE + action
140+
self.log.debug("Submitting password form to url=%s", url)
133141
response = await self.session.post(url, data=form, allow_redirects=False)
142+
self.log.debug("Password form response status=%s", response.status)
134143

135144
# Can get a 303 redirect for a "terms and conditions" page
136145
if (response.status == 303):
137146
url = response.headers['Location']
147+
self.log.debug("Received 303 redirect to %s", url)
138148
if ("terms-and-conditions" in url):
139149
# Get terms and conditions page
140150
url = LOGIN_HANDLER_BASE + url
151+
self.log.debug("Opening terms and conditions page: %s", url)
141152
response = await self.session.get(url, data=form, allow_redirects=False)
142153
(form, action) = self.form_from_response(await response.read())
143154

144155
url = LOGIN_HANDLER_BASE + action
156+
self.log.debug("Submitting terms and conditions form to %s", url)
145157
response = await self.session.post(url, data=form, allow_redirects=False)
146158

147-
self.log.warn("Agreed to terms and conditions")
159+
self.log.warning("Agreed to terms and conditions")
148160
else:
149161
self.log.error("Got unknown 303 redirect")
150162
return False
151163

152164
# Handle every single redirect and stop if the redirect
153165
# URL uses the weconnect adapter.
154166
while (True):
167+
if 'Location' not in response.headers:
168+
self.log.error("Redirect handling stopped: missing Location header (status=%s)",
169+
response.status)
170+
return False
171+
155172
url = response.headers['Location']
173+
self.log.debug("Redirect: status=%s location=%s", response.status, url)
156174
if (url.split(':')[0] == "myskoda"):
157175
if not ('code' in url):
158176
self.log.error("Missing authorization code")
159177
return False
160178
# Parse query string
161179
query_string = url.split('?')[1]
162180
query = {x[0]: x[1] for x in [x.split("=") for x in query_string.split("&")]}
181+
self.log.debug("Authorization redirect reached")
163182
break
164183

165184
if (response.status != 302):
166-
self.log.error("Not redirected, status %u" % response.status)
185+
self.log.error("Not redirected, status=%u, last_url=%s",
186+
response.status, url)
167187
return False
168188

169189
response = await self.session.get(url, data=form, allow_redirects=False)
@@ -179,6 +199,7 @@ async def reconnect(self):
179199
}
180200
response = await self.session.post(API_BASE + '/v1/authentication/exchange-authorization-code',
181201
params=params, json=payload)
202+
self.log.debug("Authorization code exchange status=%s", response.status)
182203
if response.status >= 400:
183204
self.log.error("Login: Non-2xx response")
184205
# Non 2xx response, failed
@@ -187,6 +208,7 @@ async def reconnect(self):
187208

188209
# Update header with final token
189210
self.headers['Authorization'] = 'Bearer %s' % self.tokens["accessToken"]
211+
self.log.debug("Skoda authorization completed successfully")
190212

191213
# Success
192214
return True

0 commit comments

Comments
 (0)