]> git.basschouten.com Git - openhab-addons.git/blob
49aaaa91a73041f12295072bbf825459c086d9b6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.loxone.internal.security;
14
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;
21
22 /**
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.
26  *
27  * @author Pawel Pieczul - initial contribution
28  *
29  */
30 class LxWsSecurityHash extends LxWsSecurity {
31
32     private static final String CMD_GET_KEY = "jdev/sys/getkey";
33     private static final String CMD_AUTHENTICATE = "authenticate/";
34
35     private final Logger logger = LoggerFactory.getLogger(LxWsSecurityHash.class);
36
37     /**
38      * Create a hash-based authentication instance.
39      *
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
45      */
46     LxWsSecurityHash(int debugId, LxServerHandlerApi thingHandler, LxWebSocket socket, String user, String password) {
47         super(debugId, thingHandler, socket, user, password);
48     }
49
50     @Override
51     boolean execute() {
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.");
55         }
56         LxResponse resp = socket.sendCmdWithResp(CMD_GET_KEY, true, false);
57         if (!checkResponse(resp)) {
58             return false;
59         }
60         String hash = hashString(user + ":" + password, resp.getValueAsString(), false);
61         if (hash == null) {
62             return setError(LxErrorCode.INTERNAL_ERROR, "Error hashing credentials.");
63         }
64         String cmd = CMD_AUTHENTICATE + hash;
65         if (!checkResponse(socket.sendCmdWithResp(cmd, true, false))) {
66             return false;
67         }
68         logger.debug("[{}] Authenticated - hash based authentication.", debugId);
69         return true;
70     }
71 }