]> git.basschouten.com Git - openhab-addons.git/blob
00b90f79280db98e62f55a387b61a81f07e363f3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.tesla.internal.protocol;
14
15 import java.security.GeneralSecurityException;
16 import java.security.InvalidKeyException;
17 import java.security.NoSuchAlgorithmException;
18
19 import javax.crypto.BadPaddingException;
20 import javax.crypto.Cipher;
21 import javax.crypto.IllegalBlockSizeException;
22 import javax.crypto.NoSuchPaddingException;
23 import javax.crypto.ShortBufferException;
24 import javax.crypto.spec.SecretKeySpec;
25
26 import org.openhab.binding.tesla.internal.TeslaBindingConstants;
27
28 /**
29  * The {@link TokenRequest} is a datastructure to capture
30  * authentication/credentials required to log into the
31  * Tesla Remote Service
32  *
33  * @author Karel Goderis - Initial contribution
34  * @author Nicolai Grødum - Adding token based auth
35  */
36 @SuppressWarnings("unused")
37 public abstract class TokenRequest {
38     private String client_id;
39     private String client_secret;
40
41     TokenRequest() throws GeneralSecurityException {
42         byte[] ci = { 115, -51, 67, -104, -107, 16, -116, -114, -11, -120, 41, 84, -106, -15, -67, 78, -10, -24, -47,
43                 124, 35, 73, 10, 43, -9, 123, 127, 126, -114, 58, 23, 3, 115, -70, -115, 46, 17, 87, -115, 31, -67, -90,
44                 -107, -100, 59, 18, -19, 91, 95, -52, 82, 91, -37, -83, -74, 39, 12, 59, 14, -81, 3, 95, -111, 72 };
45
46         byte[] cs = { -28, 97, -94, 108, 69, -40, 111, 53, 88, -57, 82, 111, 57, 98, 116, -63, -75, -37, 16, 95, 2,
47                 -113, -46, -112, 32, 73, -43, 23, -114, 38, -110, -85, -42, 41, 98, 118, 30, -2, -11, 93, 22, 89, 56,
48                 105, -128, 20, -24, -108, 76, 31, -19, 60, 69, -98, -122, 54, 67, 19, 72, -37, 106, 62, -120, -52 };
49
50         SecretKeySpec key = new SecretKeySpec(TeslaBindingConstants.API_NAME.getBytes(), "AES");
51         Cipher cipher;
52         try {
53             cipher = Cipher.getInstance("AES/ECB/NoPadding");
54             byte[] plainText = new byte[ci.length];
55             cipher.init(Cipher.DECRYPT_MODE, key);
56             int ptLength = cipher.update(ci, 0, ci.length, plainText, 0);
57             cipher.doFinal(plainText, ptLength);
58             this.client_id = new String(plainText);
59
60             cipher.init(Cipher.DECRYPT_MODE, key);
61             ptLength = cipher.update(cs, 0, cs.length, plainText, 0);
62             cipher.doFinal(plainText, ptLength);
63             this.client_secret = new String(plainText);
64         } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | ShortBufferException
65                 | IllegalBlockSizeException | BadPaddingException e) {
66             throw e;
67         }
68     }
69 }