From cac9e726f96eb37a183735946f8e709f15dafa8b Mon Sep 17 00:00:00 2001 From: rleidner Date: Fri, 4 Jul 2025 12:24:04 +0200 Subject: [PATCH] soc Modul vwid: maintain libvwid.py - update http strings --- packages/modules/vehicles/vwid/README.txt | 8 ++-- packages/modules/vehicles/vwid/libvwid.py | 42 ++++++++++++++++-- .../modules/vehicles/vwid/prepare_libvwid.sh | 43 ------------------- 3 files changed, 44 insertions(+), 49 deletions(-) delete mode 100755 packages/modules/vehicles/vwid/prepare_libvwid.sh diff --git a/packages/modules/vehicles/vwid/README.txt b/packages/modules/vehicles/vwid/README.txt index 87c734609c..d450ddcc2c 100644 --- a/packages/modules/vehicles/vwid/README.txt +++ b/packages/modules/vehicles/vwid/README.txt @@ -1,8 +1,10 @@ -Die python library libvwid.py dient als Basis und wird hier gepflegt: +Die python library libvwid.py diente ursprünglich als Basis und wurde hier gepflegt: https://github.com/skagmo/ha_vwid/blob/main/custom_components/vwid/libvwid.py +Der Autor der libvwid.py wird diese nicht mehr weiterpflegen. +Daher wird die libvwid.py im Rahmen des vwid SoC-Moduls weitergepflegt. + +Als weitere Quelle dient https://github.com/TA2k/ioBroker.vw-connect Folgende python Komponenten werden zusätzlich benötigt, diese werden in requirements.txt eingetragen: lxml, aiohttp, pyjwt -The script prepare_libvwid.py downloads libvwid.py from github and applies fixes for flake8 issues, -resulting in a flake8-clean version of libvwid.py diff --git a/packages/modules/vehicles/vwid/libvwid.py b/packages/modules/vehicles/vwid/libvwid.py index 9084ad58d9..8ea17c86fd 100755 --- a/packages/modules/vehicles/vwid/libvwid.py +++ b/packages/modules/vehicles/vwid/libvwid.py @@ -7,6 +7,9 @@ import secrets import logging import json +import uuid +import base64 +import hashlib from helpermodules.utils.error_handling import ImportErrorContext with ImportErrorContext(): @@ -16,6 +19,20 @@ LOGIN_BASE = "https://emea.bff.cariad.digital/user-login/v1" LOGIN_HANDLER_BASE = "https://identity.vwgroup.io" API_BASE = "https://emea.bff.cariad.digital/vehicle/v1" +CLIENT_ID = "a24fba63-34b3-4d43-b181-942111e6bda8@apps_vw-dilab_com" +SCOPE = "openid profile badge cars dealers birthdate vin" +REDIRECT_URI = "weconnect://authenticated" +RESPONSE_TYPE = "code id_token token" +CODE_CHALLENGE_METHOD = 'S256' +REGION = "emea" + +# XREQUEST = "com.volkswagen.weconnect" +# XCLIENT_ID = "" +# TYPE = "VW" +# COUNTRY = "DE" +# XAPPVERSION = "" +# XAPPNAME = "" +# XBRAND = "volkswagen" class vwid: @@ -83,15 +100,34 @@ def set_credentials(self, username, password): def set_jobs(self, jobs): self.jobs_string = ','.join(jobs) + def get_code_challenge(self): + code_verifier = secrets.token_urlsafe(64).replace('+', '-').replace('/', '_').replace('=', '') + code_challenge = base64.b64encode(hashlib.sha256(code_verifier.encode('utf-8')).digest()) + code_challenge = code_challenge.decode('utf-8').replace('+', '-').replace('/', '_').replace('=', '') + return (code_verifier, code_challenge) + async def connect(self, username, password): self.set_credentials(username, password) return (await self.reconnect()) async def reconnect(self): + # Get code challenge and verifier + code_verifier, code_challenge = self.get_code_challenge() + # Get authorize page + # payload = { + # 'nonce': secrets.token_urlsafe(12), + # 'redirect_uri': 'weconnect://authenticated' + # } payload = { + 'client_id': CLIENT_ID, + 'scope': SCOPE, + 'response_type': RESPONSE_TYPE, 'nonce': secrets.token_urlsafe(12), - 'redirect_uri': 'weconnect://authenticated' + 'redirect_uri': REDIRECT_URI, + 'state': str(uuid.uuid4()), + 'code_challenge': code_challenge, + 'code_challenge_method': CODE_CHALLENGE_METHOD } response = await self.session.get(LOGIN_BASE + '/authorize', params=payload) @@ -156,8 +192,8 @@ async def reconnect(self): payload = { 'state': query['state'], 'id_token': query['id_token'], - 'redirect_uri': "weconnect://authenticated", - 'region': "emea", + 'redirect_uri': REDIRECT_URI, + 'region': REGION, 'access_token': query["access_token"], 'authorizationCode': query["code"] } diff --git a/packages/modules/vehicles/vwid/prepare_libvwid.sh b/packages/modules/vehicles/vwid/prepare_libvwid.sh deleted file mode 100755 index 5bde746a25..0000000000 --- a/packages/modules/vehicles/vwid/prepare_libvwid.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash -echo "downloading libvwid.py from github to libvwid.org" -curl -sS -o libvwid.org https://raw.githubusercontent.com/skagmo/ha_vwid/main/custom_components/vwid/libvwid.py - -echo "apply known flake8 fixes to libvwid.org, direct logging to soc.log" -sed ' -s/\t/ /g -/^import aiohttp/s/import/# import/ -/^import asyncio/s/import/# import/ -/^class vwid:/i -/action = /s/ % /\\\n % / -s/^ *$// -s/ $// -/query = /s/ : /: / -/query = /s/) ]/)]/ -/self\.log = /s/(.*)/("soc."+__name__)/ -' < libvwid.org > libvwid.mod -# /get_status(self):/s/get_status(self):/get_status(self):\n url = API_BASE + "\/vehicles\/" + self.vin + "\/selectivestatus?jobs=all"/ -# /get(API_BASE/s/(.*,/(url,/ - -echo "checking libvwid.mod for flake8 issues" -flake8 libvwid.mod > libvwid.flake8 -l=$(wc -l libvwid.flake8 | awk '{print $1}') -if [ $l -eq 0 ] -then - echo "libvwid is flake8 clean" - echo "diff to previous libvwid.py" - diff libvwid.py libvwid.mod - echo "replace libvwid.py by libvwid.mod?(Y)" - read a - if [ "$a" == "Y" ] - then - mv libvwid.mod libvwid.py - chmod +x libvwid.py - else - echo "libvwid.py is not replaced" - fi -else - echo "found flake8 issues:" - cat libvwid.flake8 -fi - -echo "preparation if libvwid.py completed"