]> git.basschouten.com Git - openhab-addons.git/blob
9ea8f43df798552c07b128474240bf625cdc2f59
[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.tapocontrol.internal.helpers;
14
15 import java.security.KeyPair;
16 import java.security.KeyPairGenerator;
17 import java.security.MessageDigest;
18 import java.security.NoSuchAlgorithmException;
19 import java.security.SecureRandom;
20 import java.security.interfaces.RSAPrivateKey;
21 import java.security.interfaces.RSAPublicKey;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Handler class for TAPO Credentials
29  *
30  * @author Christian Wild - Initial contribution
31  */
32 @NonNullByDefault
33 public class TapoCredentials {
34
35     private final Logger logger = LoggerFactory.getLogger(TapoCredentials.class);
36     private MimeEncode mimeEncoder;
37     private String encodedPassword = "";
38     private String encodedEmail = "";
39     private String publicKey = "";
40     private String privateKey = "";
41     private String username = "";
42     private String password = "";
43
44     /**
45      * INIT CLASS
46      *
47      */
48     public TapoCredentials() {
49         this.mimeEncoder = new MimeEncode();
50     }
51
52     /**
53      * INIT CLASS
54      *
55      * @param eMail E-Mail-adress of Tapo Cloud
56      * @param password Password of Tapo Cloud
57      */
58     public TapoCredentials(String eMail, String password) {
59         this.mimeEncoder = new MimeEncode();
60         setCredectials(eMail, password);
61     }
62
63     /**
64      * set credentials.
65      *
66      * @param eMail username (eMail-adress) of Tapo Cloud
67      * @param password Password of Tapo Cloud
68      */
69     public void setCredectials(String eMail, String password) {
70         try {
71             this.username = eMail;
72             this.password = password;
73             encryptCredentials(eMail, password);
74             createKeyPair();
75         } catch (Exception e) {
76             logger.warn("error init credential class '{}'", e.toString());
77         }
78     }
79
80     /**
81      * encrypt credentials.
82      *
83      * @param username username (eMail-adress) of Tapo Cloud
84      * @param passowrd Password of Tapo Cloud
85      */
86     private void encryptCredentials(String username, String password) throws Exception {
87         logger.trace("encrypt credentials for '{}'", username);
88
89         /* Password Encoding */
90         byte[] byteWord = password.getBytes();
91         this.encodedPassword = mimeEncoder.encodeToString(byteWord);
92
93         /* User Encoding */
94         String encodedUser = this.shaDigestUsername(username);
95         byteWord = encodedUser.getBytes("UTF-8");
96         this.encodedEmail = mimeEncoder.encodeToString(byteWord);
97     }
98
99     /**
100      * Create Key-Pairs
101      *
102      */
103     public void createKeyPair() throws NoSuchAlgorithmException {
104         logger.trace("generating new keypair");
105         KeyPairGenerator instance = KeyPairGenerator.getInstance("RSA");
106         instance.initialize(1024, new SecureRandom());
107         KeyPair generateKeyPair = instance.generateKeyPair();
108
109         this.publicKey = new String(mimeEncoder.encode(((RSAPublicKey) generateKeyPair.getPublic()).getEncoded()));
110         this.privateKey = new String(mimeEncoder.encode(((RSAPrivateKey) generateKeyPair.getPrivate()).getEncoded()));
111         logger.trace("new privateKey: '{}'", this.privateKey);
112         logger.trace("new ublicKey: '{}'", this.publicKey);
113     }
114
115     /**
116      * shaDigest USERNAME
117      *
118      */
119     private String shaDigestUsername(String str) throws NoSuchAlgorithmException {
120         byte[] bArr = str.getBytes();
121         byte[] digest = MessageDigest.getInstance("SHA1").digest(bArr);
122
123         StringBuilder sb = new StringBuilder();
124         for (byte b : digest) {
125             String hexString = Integer.toHexString(b & 255);
126             if (hexString.length() == 1) {
127                 sb.append("0");
128                 sb.append(hexString);
129             } else {
130                 sb.append(hexString);
131             }
132         }
133         return sb.toString();
134     }
135
136     /**
137      * RETURN ENCODED PASSWORD
138      *
139      */
140     public String getEncodedPassword() {
141         return encodedPassword;
142     }
143
144     /**
145      * RETURN ENCODED E-MAIL
146      *
147      */
148     public String getEncodedEmail() {
149         return encodedEmail;
150     }
151
152     /**
153      * RETURN PASSWORD
154      *
155      */
156     public String getPassword() {
157         return password;
158     }
159
160     /**
161      * RETURN Username (E-MAIL)
162      *
163      */
164     public String getUsername() {
165         return username;
166     }
167
168     /**
169      * RETURN PRIVATE-KEY
170      * 
171      * @return String -----BEGIN PRIVATE KEY-----\n%s\n-----END PRIVATE KEY-----
172      */
173     public String getPrivateKey() {
174         return String.format("-----BEGIN PRIVATE KEY-----%n%s%n-----END PRIVATE KEY-----%n", privateKey);
175     }
176
177     /**
178      * RETURN PUBLIC KEY
179      * 
180      * @return String -----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----
181      */
182     public String getPublicKey() {
183         return String.format("-----BEGIN PUBLIC KEY-----%n%s%n-----END PUBLIC KEY-----%n", publicKey);
184     }
185
186     /**
187      * RETURN PRIVATE-KEY (BYTES)
188      * 
189      * @return UTF-8 coded byte[] with private key
190      */
191     public byte[] getPrivateKeyBytes() {
192         try {
193             return privateKey.getBytes("UTF-8");
194         } catch (Exception e) {
195             return new byte[0];
196         }
197     }
198
199     /**
200      * RETURN PUBLIC-KEY (BYTES)
201      * 
202      * @return UTF-8 coded byte[] with private key
203      */
204     public byte[] getPublicKeyBytes() {
205         try {
206             return publicKey.getBytes("UTF-8");
207         } catch (Exception e) {
208             return new byte[0];
209         }
210     }
211
212     /**
213      * CHECK IF CREDENTIALS ARE SET
214      * 
215      * @return
216      */
217     public Boolean areSet() {
218         return !(this.username.isEmpty() || this.password.isEmpty());
219     }
220 }