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

[mybmw] Improve authentication #18235

Merged
merged 5 commits into from
Feb 12, 2025
Merged

[mybmw] Improve authentication #18235

merged 5 commits into from
Feb 12, 2025

Conversation

mherwege
Copy link
Contributor

@mherwege mherwege commented Feb 6, 2025

The current version of the binding requires a Captcha Token parameter to be set in the account bridge thing on initial configuration and for every restart of the binding or openHAB. The Captcha Token is created from a bimmerconnect webpage.

This PR makes the following changes:

  • Use the core OAuth2 client to retrieve and store the token and refresh token. This core service then automatically takes care of the token refresh. The token is persisted and will also survive restarts.
  • At initial bridge start, if no token is available yet, a page will be created for the Captcha (link in the bridge status details), and the respons will set the configuration parameter and reinitialize. Therefore, if working in the UI, it is no longer necessary to retrieve the Captcha Token from an external website.
  • Give clearer messages in the thing status when quota have been reached, and block further requests until quota are available again.
  • Adapt the tests to these changes and make sure they all pass. I have removed some integration tests that were effectively double tests with different code now.

I have tested this for the ROW region. Code is also adapted for the NA region, as it only has a few differences.

Support for China has been removed as it is not functional anyway at the moment. There also is no way to test without a Chinese user supporting it.

@martingrassl I would appreciate your testing and feedback.

@mherwege mherwege self-assigned this Feb 6, 2025
@mherwege mherwege requested a review from ntruchsess as a code owner February 6, 2025 15:19
@jlaur jlaur added the enhancement An enhancement or new feature for an existing add-on label Feb 6, 2025
@jlaur
Copy link
Contributor

jlaur commented Feb 6, 2025

@kaikreuzer - can you add @martingrassl to the contributors team since he is one of the maintainers of this binding?

@martingrassl
Copy link
Contributor

@mherwege thanks for your PR I'll review it at the weekend

Signed-off-by: Mark Herwege <[email protected]>
Copy link
Contributor

@martingrassl martingrassl left a comment

Choose a reason for hiding this comment

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

@mherwege thanks a lot for this PR. I could not test it, but it would be a big step forward in the stability of the system. Just added some minor comments

}
}
super.updateConfiguration(config);
localBridgeConfiguration = getConfigAs(MyBMWBridgeConfiguration.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe you can add a comment here - does this mean you retrieve the configuration in line 114, update it in OH central config and then get an updated version here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes indeed.

@@ -60,6 +61,22 @@ public interface MyBMWConstants {

static final int DEFAULT_REFRESH_INTERVAL_MINUTES = 60;

static final String BASE_PATH = "/" + BINDING_ID + "/";

static final String HTML_SOURCE = "captcha/";
Copy link
Contributor

Choose a reason for hiding this comment

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

here maybe CAPTCHA_URL_ROOT could be more self explaining

logger.warn(
"initial Authentication failed, maybe request a new captcha token, see https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html!");
"initial Authentication failed, maybe request a new captcha token, see https://bimmer-connected.readthedocs.io/en/stable/captcha.html!");
Copy link
Contributor

Choose a reason for hiding this comment

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

so your servlet will not replace the initial bimmer-connected hcaptcha?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes and no. If working through the UI, it will have the link to the servlet. But if you do the setup in thing files, you could still get the captcha from the original bimmer-connected link and set it in the thing files. This will still work. And that's why the log refers to that instead of the servlet. The servlet will only be available while waiting for a captcha.
Note that as soon as there is a token, the captcha from a configuration file will be ignored as long as the token can be refreshed.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok yes I'm always forgetting about the manual configuration since the UI configuration improved so much over the last releases

logger.warn("Authorization failed!");
bridgeHandler.setHCaptchaToken(Constants.EMPTY);
} else if (!waitingForInitialToken && tokenResponse.isExpired(Instant.now(), 5)) {
if (REGION_CHINA.equals(bridgeConfiguration.getRegion())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

imho we can remove China completely - the binding can't work right now in China and nobody complained so far :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do so.


if (bridgeConfiguration.getHCaptchaToken().isBlank()) {
logger.warn(
"initial Authentication failed, request a new captcha token, see https://bimmer-connected.readthedocs.io/en/stable/captcha.html!");
Copy link
Contributor

Choose a reason for hiding this comment

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

same here there should be a reference to the local servlet, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See above.

Signed-off-by: Mark Herwege <[email protected]>
Signed-off-by: Mark Herwege <[email protected]>
Signed-off-by: Mark Herwege <[email protected]>
Copy link
Contributor

@martingrassl martingrassl left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@lsiepel lsiepel left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM

bundles/org.openhab.binding.mybmw/README.md Outdated Show resolved Hide resolved
@lsiepel lsiepel merged commit 3b820ed into openhab:main Feb 12, 2025
2 checks passed
@lsiepel lsiepel added this to the 5.0 milestone Feb 12, 2025
@lsiepel lsiepel changed the title [mybmw] improve authentication [mybmw] Improve authentication Feb 12, 2025
@mherwege mherwege deleted the bmw branch February 12, 2025 22:01
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.

4 participants