2 * Copyright (c) 2010-2023 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.loxone.internal.security;
15 import org.openhab.binding.loxone.internal.LxServerHandlerApi;
16 import org.openhab.binding.loxone.internal.LxWebSocket;
17 import org.openhab.binding.loxone.internal.types.LxErrorCode;
18 import org.openhab.binding.loxone.internal.types.LxResponse;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * A hash-based authentication algorithm. No encryption and decryption supported.
24 * The algorithm computes a HMAC-SHA1 hash from the user name and password, using a key received from the Miniserver.
25 * This hash is sent to the Miniserver to authorize the user.
27 * @author Pawel Pieczul - initial contribution
30 class LxWsSecurityHash extends LxWsSecurity {
32 private static final String CMD_GET_KEY = "jdev/sys/getkey";
33 private static final String CMD_AUTHENTICATE = "authenticate/";
35 private final Logger logger = LoggerFactory.getLogger(LxWsSecurityHash.class);
38 * Create a hash-based authentication instance.
40 * @param debugId instance of the client used for debugging purposes only
41 * @param thingHandler API to the thing handler
42 * @param socket websocket to perform communication with Miniserver
43 * @param user user to authenticate
44 * @param password password to authenticate
46 LxWsSecurityHash(int debugId, LxServerHandlerApi thingHandler, LxWebSocket socket, String user, String password) {
47 super(debugId, thingHandler, socket, user, password);
52 logger.debug("[{}] Starting hash-based authentication.", debugId);
53 if (password == null || password.isEmpty()) {
54 return setError(LxErrorCode.USER_UNAUTHORIZED, "Enter password for hash-based authentication.");
56 LxResponse resp = socket.sendCmdWithResp(CMD_GET_KEY, true, false);
57 if (!checkResponse(resp)) {
60 String hash = hashString(user + ":" + password, resp.getValueAsString(), false);
62 return setError(LxErrorCode.INTERNAL_ERROR, "Error hashing credentials.");
64 String cmd = CMD_AUTHENTICATE + hash;
65 if (!checkResponse(socket.sendCmdWithResp(cmd, true, false))) {
68 logger.debug("[{}] Authenticated - hash based authentication.", debugId);