2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.homematic.internal.common;
15 import java.util.Base64;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.api.Request;
20 import org.eclipse.jetty.http.HttpHeader;
21 import org.openhab.core.i18n.ConfigurationException;
24 * Handles the authentication to Homematic server.
26 * @author Christian Kittel
29 public class AuthenticationHandler {
31 private Boolean useAuthentication;
32 private @Nullable String authValue;
34 public AuthenticationHandler(HomematicConfig config) throws ConfigurationException {
35 this.useAuthentication = config.getUseAuthentication();
36 if (!useAuthentication) {
40 if (config.getPassword() == null || config.getUserName() == null) {
41 throw new ConfigurationException("Username or password missing");
43 this.authValue = "Basic "
44 + Base64.getEncoder().encodeToString((config.getUserName() + ":" + config.getPassword()).getBytes());
48 * Add or remove the basic auth credentials th the request if needed.
50 public Request updateAuthenticationInformation(final Request request) {
51 return useAuthentication ? request.header(HttpHeader.AUTHORIZATION, authValue) : request;