]> git.basschouten.com Git - openhab-addons.git/blob
8a3cafc6db6cd8c9500e668c93ced021be69a386
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.io.homekit.internal;
14
15 import java.math.BigInteger;
16 import java.security.InvalidAlgorithmParameterException;
17 import java.util.Base64;
18 import java.util.Collection;
19 import java.util.HashSet;
20
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.storage.Storage;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import io.github.hapjava.server.HomekitAuthInfo;
27 import io.github.hapjava.server.impl.HomekitServer;
28
29 /**
30  * Provides a mechanism to store authenticated HomeKit client details inside the
31  * StorageService, by implementing HomekitAuthInfo.
32  *
33  * @author Andy Lintner - Initial contribution
34  */
35 public class HomekitAuthInfoImpl implements HomekitAuthInfo {
36     private final Logger logger = LoggerFactory.getLogger(HomekitAuthInfoImpl.class);
37     public static final String STORAGE_KEY = "homekit";
38     private static final String STORAGE_MAC = "mac";
39     private static final String STORAGE_SALT = "salt";
40     private static final String STORAGE_PRIVATE_KEY = "privateKey";
41     private static final String STORAGE_USER_PREFIX = "user_";
42
43     private final Storage<String> storage;
44     private String mac;
45     private BigInteger salt;
46     private byte[] privateKey;
47     private String pin;
48     private String setupId;
49     private boolean blockUserDeletion;
50
51     public HomekitAuthInfoImpl(Storage<String> storage, String pin, String setupId, boolean blockUserDeletion)
52             throws InvalidAlgorithmParameterException {
53         this.storage = storage;
54         this.pin = pin;
55         this.setupId = setupId;
56         this.blockUserDeletion = blockUserDeletion;
57         initializeStorage();
58     }
59
60     @Override
61     public void createUser(String username, byte[] publicKey) {
62         logger.trace("create user {}", username);
63         final String userKey = createUserKey(username);
64         final String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey);
65         storage.put(userKey, encodedPublicKey);
66         logger.trace("stored user key {} with value {}", userKey, encodedPublicKey);
67     }
68
69     @Override
70     public String getMac() {
71         return mac;
72     }
73
74     public void setMac(String mac) {
75         this.mac = mac;
76     }
77
78     @Override
79     public String getPin() {
80         return pin;
81     }
82
83     public void setPin(String pin) {
84         this.pin = pin;
85     }
86
87     @Override
88     public String getSetupId() {
89         return setupId;
90     }
91
92     public void setSetupId(String setupId) {
93         this.setupId = setupId;
94     }
95
96     @Override
97     public byte[] getPrivateKey() {
98         return privateKey;
99     }
100
101     @Override
102     public BigInteger getSalt() {
103         return salt;
104     }
105
106     @Override
107     public byte[] getUserPublicKey(String username) {
108         final String encodedKey = storage.get(createUserKey(username));
109         if (encodedKey != null) {
110             return Base64.getDecoder().decode(encodedKey);
111         } else {
112             return null;
113         }
114     }
115
116     @Override
117     public void removeUser(String username) {
118         logger.trace("remove user {}", username);
119         if (!this.blockUserDeletion) {
120             storage.remove(createUserKey(username));
121         } else {
122             logger.debug("deletion of the user was blocked by binding settings");
123         }
124     }
125
126     @Override
127     public boolean hasUser() {
128         Collection<String> keys = storage.getKeys();
129         return keys.stream().anyMatch(this::isUserKey);
130     }
131
132     public void clear() {
133         logger.trace("clear all users");
134         if (!this.blockUserDeletion) {
135             for (String key : new HashSet<>(storage.getKeys())) {
136                 if (isUserKey(key)) {
137                     storage.remove(key);
138                 }
139             }
140         } else {
141             logger.debug("deletion of users information was blocked by binding settings");
142         }
143     }
144
145     private String createUserKey(String username) {
146         return STORAGE_USER_PREFIX + username;
147     }
148
149     private boolean isUserKey(String key) {
150         return key.startsWith(STORAGE_USER_PREFIX);
151     }
152
153     private void initializeStorage() throws InvalidAlgorithmParameterException {
154         mac = storage.get(STORAGE_MAC);
155         final @Nullable Object saltConfig = storage.get(STORAGE_SALT);
156         final @Nullable Object privateKeyConfig = storage.get(STORAGE_PRIVATE_KEY);
157         if (mac == null) {
158             logger.warn(
159                     "could not find existing MAC in {}. Generating new MAC. This will require re-pairing of iOS devices.",
160                     storage.getClass().getName());
161             mac = HomekitServer.generateMac();
162             storage.put(STORAGE_MAC, mac);
163         }
164         if (saltConfig == null) {
165             salt = HomekitServer.generateSalt();
166             storage.put(STORAGE_SALT, salt.toString());
167         } else {
168             salt = new BigInteger(saltConfig.toString());
169         }
170         if (privateKeyConfig == null) {
171             privateKey = HomekitServer.generateKey();
172             storage.put(STORAGE_PRIVATE_KEY, Base64.getEncoder().encodeToString(privateKey));
173         } else {
174             privateKey = Base64.getDecoder().decode(privateKeyConfig.toString());
175         }
176     }
177 }