evcc icon indicating copy to clipboard operation
evcc copied to clipboard

OAuth2 Login Fails Due to Missing code Parameter in Polestar API - Requires Additional Consent Handling

Open rostbeule opened this issue 2 years ago • 17 comments

Describe the bug

The OAuth2 login process with the Polestar API is failing because the code parameter is not returned after the initial authentication request. This issue occurs due to a required user interaction step, specifically the rejection of cookies, which prevents the automatic retrieval of the code parameter. It should be safe to automate this step, as it simply involves rejecting cookies.

Steps to Reproduce

  1. Start the OAuth2 login process with the Polestar API.
  2. After entering credentials, note that the code parameter is not returned, as user consent for terms and conditions is still pending.
  3. Only a uid parameter is available at this stage.

Expected Behavior

The OAuth2 login process should automatically handle any additional consent requirements from the Polestar API, enabling the retrieval of the code parameter without extra user interaction.

Proposed Solution

  1. Implement a check to detect when the code parameter is missing but a uid parameter is present.
  2. In such cases, send a POST request to submit the user's consent for rejecting cookies.
  3. After successful rejection, the process should retrieve the code parameter as expected.

Additional Context

This adjustment will allow the OAuth2 login to handle user consent programmatically within the Polestar API, streamlining the process by removing any manual acceptance step.

Acceptance Criteria

  • [x] The OAuth2 login process with the Polestar API successfully retrieves the code parameter after automatically submitting user consent.
  • [x] No additional user interactions are required post initial login.

Configuration details

vehicles:
  - name: my_car
    type: template
    template: polestar
    title: "Polestar 2"
    user: [email protected]
    password: ********
    vin: LPSVS************
    capacity: 78

Log details

[main] ERROR 2024/11/11 08:20:58 - Vehicle creation failed for 'my_car':
  - Cannot create vehicle type 'template':
      - Cannot create vehicle type 'polestar':
          - Login failed: code not found

What type of operating system are you running?

Docker container

Nightly build

  • [X] I have verified that the issue is reproducible with the latest nightly build

Version

No response

rostbeule avatar Nov 11 '24 07:11 rostbeule

see also: https://github.com/evcc-io/evcc/issues/17118

rostbeule avatar Nov 11 '24 07:11 rostbeule

Giving consent is up to the user, not up to evcc.

andig avatar Nov 11 '24 12:11 andig

Consent in the WEB-UI of Polestar already given. How to do this via evcc?! Really necessary?

BTW: works in my Home-Assistant implementation like a charm, but not in evcc

kptkip avatar Nov 11 '24 14:11 kptkip

This is exactly the problem @kptkip: it is not possible, the website consent has no impact on the API access. If the EVCC Polestar implementation stays as is, it cannot work. But you also never know what Polestar is changing tomorrow 😉

The HA Integration team implemented the consent via API already, this is why it is working again.

loebse avatar Nov 11 '24 14:11 loebse

the website consent has no impact on the API access

Since the API is reverse-engineered: who's the regular consumer? The Polestar app? Does that allow giving consent?

andig avatar Nov 11 '24 14:11 andig

No, the app does not allow to give consent. I am assuming it is using another API.

The API we are using here is used by the https://www.polestar.com/ website. Here you can see the SOC etc. as well and you are forced to give consent before you can see the data:

image

The HA integration is making the consent via an API call to make the integration work again.

This is all valid for today. Polestar is known for making sudden changes without any communication. Which is understandable as this is not a public / documented API.

loebse avatar Nov 11 '24 15:11 loebse

The HA integration is making the consent via an API call to make the integration work again.

Why not using the same approach like the HA integration?

kptkip avatar Nov 11 '24 16:11 kptkip

Okay, long story short: I took a look at what actually needs to be accepted. It's just cookies, and nothing is stopping us from rejecting them.

So you can also send:

var confirmData = new Dictionary<string, string>
{
    { "pf.submit", "false" },
    { "subject", uid }
};
var content = new FormUrlEncodedContent(confirmData);
var response = await client.PostAsync($"{OAuthURI}/as/{resumePath}/resume/as/authorization.ping", content);

this should do the trick for now.

[Edit] Pardon me, as said, I'm more into c#

data := url.Values{
    "pf.submit": []string{"false"},
    "subject":   []string{uid},
}

rostbeule avatar Nov 11 '24 17:11 rostbeule

@andig, I understand your concern. I hope my proposed solution is acceptable. I've adjusted the issue text accordingly. Unfortunately, I am unable to provide further assistance at this time and would appreciate it if either you or someone else could implement it. Personally, I lack the necessary Go skills, and it would take me quite a while to implement it myself.

rostbeule avatar Nov 11 '24 20:11 rostbeule

evcc v0.131.5 as home assistant add-on doesn't solve the issue for me neither. Guess the suggested approach from rostbeule is needed. I get the same error [main ] ERROR 2024/11/12 01:31:34 creating vehicle Polestar_2 failed: cannot create vehicle type 'template': cannot create vehicle type 'polestar': login failed: code not found

Pypolestar in home assistant does display the SOC for me, though. I hoped if it the consent was already given by pypolestar inside home assistant, it would work without consent then for evcc. But looks like this is handled independently and evcc has to do this credentials hack itself. Oh my god Polestar... What have you done...

jahoooo avatar Nov 12 '24 00:11 jahoooo

The API we are using here is used by the https://www.polestar.com/ website

Apparently there is a difference regarding consent. Anyway, happy to take a PR.

andig avatar Nov 12 '24 09:11 andig

Not sure the failure message i get when I try to configure my polestar interface results from this problem but at least in the last days I have login problems too: cannot create vehicle type 'template': cannot create vehicle type 'polestar': login failed: code not found

noottzz avatar Nov 12 '24 13:11 noottzz

@andig here you go https://github.com/evcc-io/evcc/pull/17252

rostbeule avatar Nov 14 '24 00:11 rostbeule

Leider gibts jetzt 2 PRs mit unterschiedlichen Kommentaren, einer ist nicht hier her verlinkt. Solange nicht klar ist, welcher funktioniert wird es leider nicht weiter gehen.

andig avatar Nov 16 '24 12:11 andig

@andig wir brauchen https://github.com/evcc-io/evcc/pull/17252 nicht mehr, der ist obsolet. Wir brauchen lediglich https://github.com/evcc-io/evcc/pull/17276

loebse avatar Nov 16 '24 14:11 loebse

Danke- bitte schau Dir die restlichen Kommentare noch an

andig avatar Nov 16 '24 14:11 andig

Danke Dir, ich habe mir Deine Kommentare angeschaut und bearbeitet.

loebse avatar Nov 16 '24 15:11 loebse