]> git.basschouten.com Git - openhab-addons.git/blob
dda5655e2a36a06a4802cd3686cffc850f74bdd6
[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.binding.bmwconnecteddrive.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.bmwconnecteddrive.internal.utils.Constants;
17
18 /**
19  * The {@link Token} BMW ConnectedDrive Token storage
20  *
21  * @author Bernd Weymann - Initial contribution
22  */
23 @NonNullByDefault
24 public class Token {
25     private String token = Constants.EMPTY;
26     private String tokenType = Constants.EMPTY;
27     private long expiration = 0;
28     private boolean myBmwApiUsage = false;
29
30     public boolean isMyBmwApiUsage() {
31         return myBmwApiUsage;
32     }
33
34     public void setMyBmwApiUsage(boolean myBmwAppUsage) {
35         this.myBmwApiUsage = myBmwAppUsage;
36     }
37
38     public String getBearerToken() {
39         return new StringBuilder(tokenType).append(Constants.SPACE).append(token).toString();
40     }
41
42     public void setToken(String token) {
43         this.token = token;
44     }
45
46     public void setExpiration(int expiration) {
47         this.expiration = System.currentTimeMillis() / 1000 + expiration;
48     }
49
50     public void setType(String type) {
51         tokenType = type;
52     }
53
54     public boolean isValid() {
55         return (!token.equals(Constants.EMPTY) && !tokenType.equals(Constants.EMPTY)
56                 && (this.expiration - System.currentTimeMillis() / 1000) > 1);
57     }
58
59     @Override
60     public String toString() {
61         return tokenType + token;
62     }
63 }