forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[homematic] Add Authentication (openhab#16196)
* Add Authentication --------- Signed-off-by: Christian Kittel <ckittel@gmx.de> Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
- Loading branch information
Showing
10 changed files
with
186 additions
and
12 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
53 changes: 53 additions & 0 deletions
53
...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,53 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* 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.util.Base64; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.eclipse.jetty.client.api.Request; | ||
import org.eclipse.jetty.http.HttpHeader; | ||
import org.openhab.core.i18n.ConfigurationException; | ||
|
||
/** | ||
* Handles the authentication to Homematic server. | ||
* | ||
* @author Christian Kittel | ||
*/ | ||
@NonNullByDefault | ||
public class AuthenticationHandler { | ||
|
||
private Boolean useAuthentication; | ||
private @Nullable String authValue; | ||
|
||
public AuthenticationHandler(HomematicConfig config) throws ConfigurationException { | ||
this.useAuthentication = config.getUseAuthentication(); | ||
if (!useAuthentication) { | ||
return; | ||
} | ||
|
||
if (config.getPassword() == null || config.getUserName() == null) { | ||
throw new ConfigurationException("Username or password missing"); | ||
} | ||
this.authValue = "Basic " | ||
+ Base64.getEncoder().encodeToString((config.getUserName() + ":" + config.getPassword()).getBytes()); | ||
} | ||
|
||
/** | ||
* Add or remove the basic auth credentials th the request if needed. | ||
*/ | ||
public Request updateAuthenticationInformation(final Request request) { | ||
return useAuthentication ? request.header(HttpHeader.AUTHORIZATION, authValue) : request; | ||
} | ||
} |
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
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