-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christian Kittel <ckittel@gmx.de>
- Loading branch information
Showing
9 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ic/src/main/java/org/openhab/binding/homematic/internal/common/AuthenticationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
Check failure on line 2 in bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/common/AuthenticationHandler.java GitHub Actions / Build (Java 17, ubuntu-22.04)
|
||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.homematic.internal.common; | ||
|
||
import java.net.URI; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jetty.client.HttpClient; | ||
import org.eclipse.jetty.client.api.Authentication; | ||
import org.eclipse.jetty.client.api.AuthenticationStore; | ||
import org.eclipse.jetty.client.util.BasicAuthentication; | ||
|
||
/** | ||
* Handles the authentication to Homematic server. | ||
* | ||
* @author Christian Kittel | ||
*/ | ||
@NonNullByDefault | ||
public class AuthenticationHandler { | ||
|
||
private final HomematicConfig config; | ||
private final HttpClient httpClient; | ||
|
||
public AuthenticationHandler(HomematicConfig config, HttpClient httpClient) { | ||
this.config = config; | ||
this.httpClient = httpClient; | ||
} | ||
|
||
/** | ||
* Add or remove the basic auth credetials to the AuthenticationStore if needed. | ||
*/ | ||
public synchronized void updateAuthenticationInformation(final URI uri) { | ||
final AuthenticationStore authStore = httpClient.getAuthenticationStore(); | ||
|
||
Authentication findAuthentication = authStore.findAuthentication("Basic", uri, Authentication.ANY_REALM); | ||
|
||
if (config.getUseAuthentication()) { | ||
if (findAuthentication == null) { | ||
if (config.getPassword() == null || config.getUserName() == null) { | ||
throw new IllegalStateException("Username or password missing"); | ||
} | ||
authStore.addAuthentication(new BasicAuthentication(uri, Authentication.ANY_REALM, config.getUserName(), | ||
config.getPassword())); | ||
} | ||
} else if (findAuthentication != null) { | ||
authStore.removeAuthentication(findAuthentication); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters