Hyundai-Kia-Connect / hyundai_kia_connect_api

This is a Kia UVO and Hyundai Bluelink written in python. It is primary consumed by home assistant. If you are looking for a home assistant Kia / Hyundai implementation please look here: https://github.com/Hyundai-Kia-Connect/kia_uvo. Much of this base code came from reading bluelinky and contributions to the kia_uvo home assistant project.
MIT License
123 stars 71 forks source link

Genesis EU / Login failed #514

Open lmuser22 opened 7 months ago

lmuser22 commented 7 months ago

Login in Germany / EU with Genesis fails

cdnninja commented 7 months ago

Will need more details.

lmuser22 commented 7 months ago

image

and then image

lmuser22 commented 7 months ago

image

dmccaffery commented 5 months ago

This seems to be an issue with the /signin invocation, which causes the /integrationinfo to fail. Also, the URL for the oidc auth endpoint should not include the /auth/ prefix.

Debug Logs

The _get_authorization_code_with_redirect_url fails:

024-05-01 12:14:09.349 DEBUG (SyncWorker_12) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - Get Device ID request: https://prd-eu-ccapi.genesis.com:443/api/v1/spa/notifications/register {'ccsp-service-id': '3020afa2-30ff-412a-aa51-d28fbe901e10', 'ccsp-application-id': 'f11f2b86-e0e7-4851-90df-5600b01d8b70', 'Stamp': 'ImpZIJ2NNURLI0cNUotk8b8G05h70TJzbvXLirZ/QKkhptv+KoiKqNhaiBJUypM=', 'Content-Type': 'application/json;charset=UTF-8', 'Host': 'prd-eu-ccapi.genesis.com:443', 'Connection': 'Keep-Alive', 'Accept-Encoding': 'gzip', 'User-Agent': 'okhttp/3.12.0'} {'pushRegId': 'XXXXXX', 'pushType': 'GCM', 'uuid': 'XXXXX'}
2024-05-01 12:14:09.616 DEBUG (SyncWorker_12) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - Get Device ID response: {'retCode': 'S', 'resCode': '0000', 'resMsg': {'deviceId': 'XXXXX'}, 'msgId': 'XXXXXX'}
2024-05-01 12:14:09.617 DEBUG (SyncWorker_12) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - Get cookies request: https://prd-eu-ccapi.genesis.com:443/api/v1/user/oauth2/authorize?response_type=code&state=test&client_id=3020afa2-30ff-412a-aa51-d28fbe901e10&redirect_uri=https://prd-eu-ccapi.genesis.com:443/api/v1/user/oauth2/redirect&lang=en
2024-05-01 12:14:09.805 DEBUG (SyncWorker_12) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - Get cookies response: {'account': 'XXXXX', 'TS01f622c7': 'XXXXX'}
2024-05-01 12:14:10.175 DEBUG (SyncWorker_12) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - Sign In Response: {'errId': 'XXXXX', 'errCode': '4010', 'errMsg': 'Require authentication'}
2024-05-01 12:14:10.176 DEBUG (SyncWorker_12) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - get_authorization_code_with_redirect_url failed

The fallback _get_authorization_code_with_form integrationinfo request also fails to return any values for userId and serviceId:

2024-05-01 12:14:10.358 DEBUG (SyncWorker_12) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - IntegrationInfo Response: {'userId': '', 'serviceId': '', 'serviceName': ''}

Finally, the login form request fails (as the URL is incorrect and we get a 404):

2024-05-01 12:14:10.537 DEBUG (SyncWorker_12) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - LoginForm https://accounts-eu.genesis.com/auth/realms/eugenesisidm/protocol/openid-connect/auth?client_id=3020afa2-30ff-412a-aa51-d28fbe901e10&scope=openid%20profile%20email%20phone&response_type=code&hkid_session_reset=true&redirect_uri=https://prd-eu-ccapi.genesis.com:443/api/v1/user/integration/redirect/login&ui_locales=en&state=: - Response: {"error":"HTTP 404 Not Found"}

This ultimately ends up with the exception in config_flow:

2024-05-01 12:14:10.542 ERROR (MainThread) [custom_components.kia_uvo.config_flow] Unexpected exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 238, in login
    authorization_code = self._get_authorization_code_with_redirect_url(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 1226, in _get_authorization_code_with_redirect_url
    parsed_url = urlparse(response["redirectUrl"])
                          ~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'redirectUrl'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/config/custom_components/kia_uvo/config_flow.py", line 159, in async_step_user
    await validate_input(self.hass, user_input)
  File "/config/custom_components/kia_uvo/config_flow.py", line 63, in validate_input
    token: Token = await hass.async_add_executor_job(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 243, in login
    authorization_code = self._get_authorization_code_with_form(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 1250, in _get_authorization_code_with_form
    login_form_action_url = soup.find("form")["action"].replace("&", "&")
                            ~~~~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

LoginForm URL

The login form URL used includes an /auth/ segment in the URL:

https://github.com/Hyundai-Kia-Connect/hyundai_kia_connect_api/blob/6e449388b47629c4634ba690edfd6d893ee8a6c9/hyundai_kia_connect_api/KiaUvoApiEU.py#L222

This should be:

     + "/realms/eugenesisidm/protocol/openid-connect/auth?client_id="

404 Error

curl -o /dev/null -s -w "\n%{http_code}\n" https://accounts-eu.genesis.com/auth/realms/eugenesisidm/protocol/openid-connect/auth

404

400 Error (I didn't submit client_id, etc)

curl -o /dev/null -s -w "\n%{http_code}\n" https://accounts-eu.genesis.com/realms/eugenesisidm/protocol/openid-connect/auth 

400
cdnninja commented 5 months ago

Could you create a PR to solve it?

dmccaffery commented 5 months ago

Could you create a PR to solve it?

I am working through it, but even with the updated url I am now getting a Bad Request on the login form. I believe the issue may be that the https://prd-eu-ccapi.genesis.com:443/api/v1/user/integrationinfo endpoint is returning empty values for userId, serviceId, and serviceName. Is this API endpoint documented anywhere? I could not find anything on https://developers.genesis.com/web/v1/genesis/specification/account.

cdnninja commented 5 months ago

No. Reverse engineering the native app is the only way.

pasqualito79 commented 5 months ago

any updates on this issue? This is also not working: https://prd-eu-ccapi.genesis.com/api/v1/user/oauth2/authorize?response_type=code&state=test&client_id=3020afa2-30ff-412a-aa51-d28fbe901e10&redirect_uri=https://prd-eu-ccapi.genesis.com/api/v1/user/oauth2/redirect

{"errId":"fe69c315-9b0f-49d7-8c16-778b9b33ea87","errCode":"4002","errMsg":"Invalid parameters"}

pasqualito79 commented 5 months ago

Ok I found one issue...the redirect_uri must be https://accounts-eu.genesis.com/realms/eugenesisidm/ga-api/redirect/api/v1/user/oauth2/redirect:

https://prd-eu-ccapi.genesis.com/api/v1/user/oauth2/authorize?response_type=code&state=test&client_id=3020afa2-30ff-412a-aa51-d28fbe901e10&redirect_uri=https://accounts-eu.genesis.com/realms/eugenesisidm/ga-api/redirect/api/v1/user/oauth2/redirect

this results in:

Found.

after that, I also receive the parameters for this one:

https://prd-eu-ccapi.genesis.com/api/v1/user/integrationinfo

{"userId":"***","serviceId":"3020afa2-30ff-412a-aa51-d28fbe901e10","serviceName":"Genesis Connected Services"}

pasqualito79 commented 5 months ago

I'm nearly there...Setting Language and Login works (Language of push requests were changed after that):

https://prd-eu-ccapi.genesis.com/api/v1/user/language
 {"lang":"de"}
https://prd-eu-ccapi.genesis.com/api/v1/user/signin
{"email":"***","password":"***"}
--
{"step":4,"upgrade":false}

after that, this is also wrong:

https://prd-eu-ccapi.genesis.com/api/v1/user/oauth2/token

code=&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fprd-eu-ccapi.genesis.com%2Fapi%2Fv1%2Fuser%2Foauth2%2Fredirect
--
{"errId":"a0dc915f-bfe8-4b7e-afad-45a385cb3b15","errCode":"4002","errMsg":"Invalid parameters"}

should be:

https://prd-eu-ccapi.genesis.com/api/v1/user/oauth2/token

client_id=3020afa2-30ff-412a-aa51-d28fbe901e10&code=***&grant_type=authorization_code&redirect_uri=https%3A//accounts-eu.genesis.com/realms/eugenesisidm/ga-api/redirect2

hope this helps!

cdnninja commented 5 months ago

Could you create a PR for this?

pasqualito79 commented 5 months ago

Sorry, I'm not really a developer, I just tried to read your code because I'm interested in getting this API running. I've a Genesis car and also know how to use a proxy to be able to catch all the requests and responses from the Genesis iOS app. So if you need any info or testing of the Genesis app / API I'm here to help you.