-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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] Consolidate OAuth2 handling #14755
Comments
It was the aim of this PR, but I failed finalizing it on Netatmo binding side because I failed to make core identify and load my deserialization class (see this comment and following), and then end up developping it in the binding itself. I like that you opened this. Three years laters, things never get lost ! 👍 |
I like the idea too but I know that @clinique made already a lot of efforts to achieve that. |
I think the smallest improvement we could make would be using importAccessTokenResponse to inject the token information. This way we would still have a custom implementation of the OAuth2 flow, but at least we would use the standard storage for storing the tokens. WDYT? Since I will probably hit the same wall as you did with the custom deserialization, @clinique, I guess we should revert openhab/openhab-core#1891 since it has no usages and doesn't work as intended? I'm wondering if we instead could make it possible to inject a prepared |
Don't you think it would be better to try to make it work ? This is a field where my Java knowledge is limited. |
I also don't know how to make it work. But is it the best approach? If a pre-configured |
I can confirm the mentioned deserialization issue. package org.openhab.binding.netatmo.internal.deserialization;
import java.lang.reflect.Type;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
@NonNullByDefault
public class AccessTokenResponseDeserializer implements JsonDeserializer<AccessTokenResponse> {
private final Gson gson;
public AccessTokenResponseDeserializer() {
gson = new GsonBuilder().create();
}
@Override
public @Nullable AccessTokenResponse deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2)
throws JsonParseException {
return gson.fromJson(element, org.openhab.binding.netatmo.internal.api.dto.AccessTokenResponse.class)
.toStandard();
}
} and in public org.openhab.core.auth.client.oauth2.AccessTokenResponse toStandard() {
var standardResponse = new org.openhab.core.auth.client.oauth2.AccessTokenResponse();
standardResponse.setAccessToken(accessToken);
standardResponse.setExpiresIn(expiresIn);
standardResponse.setRefreshToken(refreshToken);
StringJoiner stringJoiner = new StringJoiner(" ");
scope.forEach(s -> stringJoiner.add(s.name().toLowerCase()));
standardResponse.setScope(stringJoiner.toString());
return standardResponse;
} then:
with result:
|
I tried a few other things to at least use the common store before jumping on the core classes:
For reference:
The advantage of at least using the store rather than the current custom file approach is that it would ensure forward-compatibility, so users wouldn't have to reauthorize when we'll succeed in using the core OAuth2 implementation. |
Small update: I jumped into core to try out my proposal to inject I just now managed to authorize from a clean slate using the existing servlet. And after this I was able to restart openHAB and reauthorize automatically as well. I'm now pretty confident that it will somehow work out, even if my current implementation will change. I'll refactor and clean up a bit, and then prepare some draft pull requests. StorageHandler.For.OAuthClientService.json {
"INDEX_HANDLES": {
"class": "java.lang.String",
"value": "[\n \"netatmo:account:a6fae04bab\"\n]"
},
"netatmo:account:a6fae04bab.AccessTokenResponse": {
"class": "java.lang.String",
"value": "{\n \"accessToken\": \"xxx\",\n \"expiresIn\": 10800,\n \"refreshToken\": \"yyy\",\n \"scope\": \"read_homecoach read_station read_thermostat write_thermostat read_presence access_presence read_carbonmonoxidedetector write_presence read_smokedetector access_doorbell read_camera write_doorbell write_camera read_doorbell access_camera\",\n \"createdOn\": \"2023-04-10T18:39:15.153302900Z\"\n}"
},
"netatmo:account:a6fae04bab.LastUsed": {
"class": "java.lang.String",
"value": "\"2023-04-10T18:45:25.273590400Z\""
},
"netatmo:account:a6fae04bab.ServiceConfiguration": {
"class": "java.lang.String",
"value": "{\n \"handle\": \"netatmo:account:a6fae04bab\",\n \"tokenUrl\": \"https://api.netatmo.com/oauth2/token\",\n \"authorizationUrl\": \"https://api.netatmo.com/oauth2/authorize\",\n \"clientId\": \"zzz\",\n \"clientSecret\": \"qqq\",\n \"scope\": \"read_homecoach read_station write_thermostat read_thermostat access_camera write_presence read_carbonmonoxidedetector read_presence read_smokedetector write_doorbell read_camera write_camera access_presence read_doorbell access_doorbell\",\n \"supportsBasicAuth\": false,\n \"tokenExpiresInSeconds\": 10\n}"
}
} |
Fixes openhab#14755 Signed-off-by: Jacob Laursen <[email protected]>
Fixes openhab#14755 Signed-off-by: Jacob Laursen <[email protected]>
Thanks for taking care of this. It will be far more clean when Netatmo binding will use core functions. |
Fixes openhab#14755 Signed-off-by: Jacob Laursen <[email protected]>
Fixes openhab#14755 Signed-off-by: Jacob Laursen <[email protected]>
…#14780) * Consolidate OAuth2 by using core implementation and storage Fixes #14755 --------- Signed-off-by: Jacob Laursen <[email protected]>
Recently Netatmo announced changes in their OAuth2 implementation to make it RFC compliant: #14546
On our side #14548 aligned the openHAB implementation with those changes, but with a custom OAuth2 implementation.
Since I have recently had a look at OAuth2 in context of #14745, I think it would be worth streamlining the OAuth2 implementation by using the available openHAB core classes.
According to the Netatmo documentation, the API should be fully RFC 6749 compliant, thus it should work.
In case any obstacles are found, this should be possible to solve, even it core changes would be needed. RFC violations should preferably be handled directly in the binding, but the core classes should make it possible to extend the functionality to make this possible.
I'm aware of #14548 (comment), so this issue also serves as a placeholder for documenting those potential problems, and coming up with solutions.
If I understand the problem correctly, the grant response looks like this:
which is expected to be:
Netatmo reply March 13rd 2023: "It's not something planned for the moment, but teams took the point and will think about it"
It seems like openhab/openhab-core#1891 should make it possible to work around.
Perhaps importAccessTokenResponse could be a second option.
@clinique, @lolodomo - FYI.
The text was updated successfully, but these errors were encountered: