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

[netatmo] Make OAuth2 token refresh RFC compliant #14548

Merged
merged 5 commits into from
Mar 10, 2023

Conversation

clinique
Copy link
Contributor

@clinique clinique commented Mar 7, 2023

Resolves #14546

@clinique clinique requested a review from lolodomo as a code owner March 7, 2023 14:55
@clinique clinique self-assigned this Mar 7, 2023
@clinique clinique added enhancement An enhancement or new feature for an existing add-on additional testing preferred The change works for the pull request author. A test from someone else is preferred though. labels Mar 7, 2023
@jlaur
Copy link
Contributor

jlaur commented Mar 7, 2023

Thanks, I will give it a test run. Has textual configuration been considered?

@clinique
Copy link
Contributor Author

clinique commented Mar 7, 2023

Thanks, I will give it a test run. Has textual configuration been considered?

You're correct. Textual configuration could be a real challenge because we can't override the stored configuration - so I think I should move the refreshToken to properties instead of configuration.

@jlaur
Copy link
Contributor

jlaur commented Mar 7, 2023

Thanks, I will give it a test run. Has textual configuration been considered?

You're correct. Textual configuration could be a real challenge because we can't override the stored configuration - so I think I should move the refreshToken to properties instead of configuration.

Can you always reauthenticate from clientId and clientSecret to get new access and refresh token? I'm wondering if we even need a property. I guess the advantage is that we don't need to get new token when restarting openHAB/binding, but besides that we could just keep the refresh token in memory?

@lolodomo
Copy link
Contributor

lolodomo commented Mar 7, 2023

You're correct. Textual configuration could be a real challenge because we can't override the stored configuration - so I think I should move the refreshToken to properties instead of configuration.

That would mean we have to re-authenticate each time we restart OH !
If I correctly remember, you are not using the oAuth2 client from OH core for this binding ? Do you remember what was the reason and do you think the original reason is still valid ?
Because this would be the solution as OH is then storing the necessary data in file userdata/json/StorageHandler.For.OAuthClientService.json

@lolodomo
Copy link
Contributor

lolodomo commented Mar 7, 2023

The best solution would be really to use the oAuth2 client implementation from the core framework.
In case you really can't, I would suggest that you store your token(s) in your own file somewhere in userdata. The data will then be persisted and this will work whatever the way you define your thing.

@clinique
Copy link
Contributor Author

clinique commented Mar 8, 2023

Yes, there is a valid reason why I do not use the core oAuth mechanism. Netatmo is not complying to oAuth standard.
I opened an issue a while ago that has never been adressed here.

I tried to adress it from a core perspective here and we discussed this here

@lolodomo
Copy link
Contributor

lolodomo commented Mar 8, 2023

If you can't use the OH oAuth2 client, I would then like that you store your token data in a file in folder userdata/netatmo.

@clinique
Copy link
Contributor Author

clinique commented Mar 8, 2023

If you can't use the OH oAuth2 client, I would then like that you store your token data in a file in folder userdata/netatmo.

Ok but why not in properties ?

@lolodomo
Copy link
Contributor

lolodomo commented Mar 8, 2023

Because thing properties are lost when you restart OH when your thing is defined in a things file.

If you need to persist something (a token( in a file to survive and be re-used after a OH restart or upgrade, do not consider the thing properties option.

@clinique
Copy link
Contributor Author

clinique commented Mar 8, 2023

Because thing properties are lost when you restart OH when your thing is defined in a things file.

If you need to persist something (a token( in a file to survive and be re-used after a OH restart or upgrade, do not consider the thing properties option.

Valid. Did not think to that. Thanks, you're there !

Copy link
Contributor

@lolodomo lolodomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you

@lolodomo
Copy link
Contributor

lolodomo commented Mar 8, 2023

@clinique : there is a conflict to fix before the merge.
I will then test the PR and if test is ok, we can merge.

@clinique
Copy link
Contributor Author

clinique commented Mar 8, 2023

I have resolved the conflict directly online. This conflict is weird, but git may have some mysteries.

@lolodomo
Copy link
Contributor

lolodomo commented Mar 8, 2023

I have resolved the conflict directly online. This conflict is weird, but git may have some mysteries.

Conflict still mentioned on file ApiBridgeHandler.java.

@clinique
Copy link
Contributor Author

clinique commented Mar 8, 2023

I have resolved the conflict directly online. This conflict is weird, but git may have some mysteries.

Conflict still mentioned on file ApiBridgeHandler.java.

Understood why in the meantime, it's due to the merge of CO detector, I need to rebase and force push

@jlaur
Copy link
Contributor

jlaur commented Mar 8, 2023

Understood why in the meantime, it's due to the merge of CO detector, I need to rebase and force push

If these two PR's conflict, we cannot cherry-pick into the 3.4.x branch, but will instead have to create a new branch tailored for that, i.e. at PR targeting 3.4.x.

clinique added 5 commits March 9, 2023 08:53
Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: clinique <gael@lhopital.org>
…file based configuration).

Differed activation of the grantServlet only when refresh token is needed.

Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: clinique <gael@lhopital.org>
@clinique clinique force-pushed the netatmo_oauth_14546 branch from c11b8d7 to 15479cb Compare March 9, 2023 07:54
@clinique
Copy link
Contributor Author

clinique commented Mar 9, 2023

@jlaur , @lolodomo : rebased and conflict fixed.

@lolodomo
Copy link
Contributor

@clinique: tests are OK for me.

@lolodomo lolodomo removed the additional testing preferred The change works for the pull request author. A test from someone else is preferred though. label Mar 10, 2023
@lolodomo lolodomo merged commit d130595 into openhab:main Mar 10, 2023
@lolodomo lolodomo added this to the 4.0 milestone Mar 10, 2023
@lolodomo
Copy link
Contributor

This change also simplified the process for users using config files.

I am asking myself if we should document where are stored the token files ?

@lolodomo
Copy link
Contributor

And thank you @clinique for providing that change so quickly.
The delay forced by netatmo is very short !

@clinique
Copy link
Contributor Author

You're welcome. I'll add a note in the README

@clinique clinique deleted the netatmo_oauth_14546 branch March 10, 2023 09:29
@lolodomo
Copy link
Contributor

Just to understand, does it mean that if you stop your OH server more than 3 hours, you will have to go through the authorization process when restarting OH ?

@clinique
Copy link
Contributor Author

Hopefully not - I wondered the same reading the oAuth documentation. I hope they refresh with the last provided token, or else, it is the end of this binding. It's a bit hard to predict because the new refreshing refresh token is not in place currently.

@clinique
Copy link
Contributor Author

clinique commented Mar 10, 2023

I had this fear and you reactivated it. I opened a question on the Netatmo forum. Everything lies in the way to understand this : When refreshing tokens, Access Token and Refresh Token will be automatically renewed and former tokens invalidated.

@lolodomo
Copy link
Contributor

I assume you are storing the updated refresh token in the new file.
Re-reading the sentence, I understand the old refresh token will be invalidated only when you try to refresh. So if API is not requested even during several days, there should be no invalidation.

@clinique
Copy link
Contributor Author

You assume correctly, and I read it also the way you do.

@jlaur
Copy link
Contributor

jlaur commented Mar 10, 2023

@clinique, @lolodomo - I assume we now need a separate PR targeting 3.4.x, since the merged branch seemed to be overlapping with other changes for 4.0?

@clinique
Copy link
Contributor Author

Yes, a distinct PR has to be created for 3.4.x - I will try to find time to produce it.

clinique added a commit to clinique/openhab-addons that referenced this pull request Mar 10, 2023
Signed-off-by: clinique <gael@lhopital.org>
@clinique
Copy link
Contributor Author

@jlaur : done here

@jlaur jlaur changed the title [Netatmo] Modification of the tokenRefresh handling process [netatmo] Make OAuth2 token refresh RFC compliant Mar 11, 2023
jlaur pushed a commit that referenced this pull request Mar 16, 2023
* Cherry picking PR #14548 in branch 3.4.x
* Correcting wrong syntax

Signed-off-by: clinique <gael@lhopital.org>
@kaikreuzer kaikreuzer added patch A PR that has been cherry-picked to a patch release branch and removed patch A PR that has been cherry-picked to a patch release branch labels Mar 16, 2023
renescherer pushed a commit to renescherer/openhab-addons that referenced this pull request Mar 23, 2023
…14548)

* Modification of the tokenRefresh handling process
* Storing refreshToken in userdata/netatmo

---------

Signed-off-by: clinique <gael@lhopital.org>
FordPrfkt pushed a commit to FordPrfkt/openhab-addons that referenced this pull request Apr 20, 2023
…14548)

* Modification of the tokenRefresh handling process
* Storing refreshToken in userdata/netatmo

---------

Signed-off-by: clinique <gael@lhopital.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature for an existing add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Netatmo] oAuth token refresh process evolutions
4 participants