]> git.basschouten.com Git - openhab-addons.git/blob
45bd6adac584f0650a3fb01f5b315e2ecd8b81d7
[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.mybmw.internal.handler.auth;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.mybmw.internal.utils.Constants;
17
18 /**
19  * The {@link Token} MyBMW 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     public void setExpirationTotal(long expiration) {
42         this.expiration = expiration;
43     }
44
45     public void setType(String type) {
46         tokenType = type;
47     }
48
49     public boolean isValid() {
50         return (!token.equals(Constants.EMPTY) && !tokenType.equals(Constants.EMPTY)
51                 && (this.expiration - System.currentTimeMillis() / 1000) > 1);
52     }
53
54     @Override
55     public String toString() {
56         return tokenType + Constants.COLON + token + Constants.COLON + isValid();
57     }
58 }