]> git.basschouten.com Git - openhab-addons.git/blob
22e42170410d06fde34dcc3823e89539c8a83a5b
[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.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
29     public String getBearerToken() {
30         return new StringBuilder(tokenType).append(Constants.SPACE).append(token).toString();
31     }
32
33     public void setToken(String token) {
34         this.token = token;
35     }
36
37     public void setExpiration(int expiration) {
38         this.expiration = System.currentTimeMillis() / 1000 + expiration;
39     }
40
41     /**
42      * @return true if Token expires in less than 1 second
43      */
44     public boolean isExpired() {
45         return (expiration - System.currentTimeMillis() / 1000) < 1;
46     }
47
48     public void setType(String type) {
49         tokenType = type;
50     }
51
52     public boolean isValid() {
53         return (!token.equals(Constants.EMPTY) && !tokenType.equals(Constants.EMPTY) && expiration > 0);
54     }
55 }