Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Auth Token #83

Open
reox opened this issue Mar 26, 2023 · 12 comments
Open

Cache Auth Token #83

reox opened this issue Mar 26, 2023 · 12 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@reox
Copy link
Collaborator

reox commented Mar 26, 2023

Right now, the API is logged in every time it is triggered. However, I think the token can be used for much longer time than 15min.
Is it possible to store it and re-use for longer time?

@reox reox added enhancement New feature or request help wanted Extra attention is needed labels Mar 26, 2023
@DarwinsBuddy
Copy link
Owner

I am not aware of any secure caching functionality right now in HomeAssistant. Although I guess the Wiener Netze API offers to renew a particular token by using the refresh token along side the access token, we cannot really persist them in HA I fear.

@reox
Copy link
Collaborator Author

reox commented Mar 29, 2023

Okay, but also username and password is stored? Can we not just use the same approach?

@DarwinsBuddy
Copy link
Owner

DarwinsBuddy commented Mar 29, 2023 via email

@reox
Copy link
Collaborator Author

reox commented Apr 8, 2023

Maybe it is a good idea to switch to a proper OpenID-Connect library, which can handle all these things.

I also checked the token response, they are valid for 300 seconds, and the refresh token for 1800s (30min).
Thus, with the change to request only every 60minutes, we would have to login any ways or have another job running in the background that does the refreshing.

I guess the website sets a cookie with a longer lifespan?

@DarwinsBuddy
Copy link
Owner

regarding OpenID: that would be for sure the more stable solution, though we are assuming that log.wien correctly implemented it according to the standard.
We may have to reverse engineer that part, but imho it's worth a try.

regarding the token: Home assistant does not provide afaik the possibility to launch separate scheduled tasks then the update lifecycle. What we could do is reduce the update cycle to happen < 30min and first use refresh token and if that fails as a fallback the credentials login and update the sensors merely every 2nd-3rd time.
If the refresh token endpoint is computationally more efficient (as it should not require a persistence lookup) we could save them some load and give back some performance.

What do you think @reox?

@reox
Copy link
Collaborator Author

reox commented Apr 8, 2023

regarding OpenID: that would be for sure the more stable solution, though we are assuming that log.wien correctly implemented it according to the standard.

The question is also if we are acting according to any standard :D

What we could do is reduce the update cycle to happen < 30min and first use refresh token and if that fails as a fallback the credentials login and update the sensors merely every 2nd-3rd time.

Or even reduce it to 5min and just update the API every half an hour. I implemented something similar with the statistics, to only query after 24h after the last imported stat, eve if the update function is called more often.

@DarwinsBuddy
Copy link
Owner

regarding OpenID: that would be for sure the more stable solution, though we are assuming that log.wien correctly implemented it according to the standard.

The question is also if we are acting according to any standard :D

For sure not. But as of my experience openId is often not implemented according to the standard. Just to keep that in mind, when integrating into an external system.

What we could do is reduce the update cycle to happen < 30min and first use refresh token and if that fails as a fallback the credentials login and update the sensors merely every 2nd-3rd time.

Or even reduce it to 5min and just update the API every half an hour. I implemented something similar with the statistics, to only query after 24h after the last imported stat, eve if the update function is called more often.
calling every 5 minutes is a bit too much. Most frequently 15 minutes is I guess already enough.
Regarding your grace timeout logic: I agree, we could probably think of update if next day after updatets at 00:00 < now

@reox
Copy link
Collaborator Author

reox commented Apr 8, 2023

I had a look on what is taking so long during login. It is actually the fetching of the API keys from the javascript files.
The file that has to be downloaded is 1.8MB (okay, it is gziped to 400kB..)
Maybe a good addition would be to cache these keys. For starters, one could take the proposed cache value from the http response.

@DarwinsBuddy
Copy link
Owner

DarwinsBuddy commented Apr 9, 2023

there you have a point.
You've got a good idea there:

We could download and cache the keys (by downloading the html-file -> js-files -> api keys) if

  • the cached key is None or
  • the login with the cached key fails due to a wrong key, as a fallback

and use the cached one as long as it doesn't fail to login due to invalid keys
What do you think @reox ?

@reox
Copy link
Collaborator Author

reox commented Apr 14, 2023

I thought about that too. Probably would have to check in the _call_api function then if the call fails and if so, request new keys.

@esackbauer
Copy link

Since like 2 or 3 days I get every now and then mails from log.wien regarding unexpected logins.
This is really annoyin.

Image

@reox
Copy link
Collaborator Author

reox commented Jan 17, 2025

yes, see #289 (comment)

seems like they are doing many things with the login right now. Blocking it, etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants