]> git.basschouten.com Git - openhab-addons.git/blob
c4dfb47629e3a06b8de7338560b16eda56d5adbe
[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.gardena.internal.model.dto.api;
14
15 import java.time.Instant;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18 import java.time.temporal.ChronoUnit;
19
20 import com.google.gson.annotations.SerializedName;
21
22 /**
23  * Represents a Gardena object that is sent via the Gardena API.
24  *
25  * @author Gerhard Riegler - Initial contribution
26  */
27 public class PostOAuth2Response {
28     // refresh token is valid 10 days
29     private transient Instant refreshTokenValidity = Instant.now().plus(10, ChronoUnit.DAYS).minus(1,
30             ChronoUnit.MINUTES);
31     private transient Instant accessTokenValidity;
32
33     @SerializedName("access_token")
34     public String accessToken;
35
36     // The scope of the token (what you are allowed to do)
37     public String scope;
38
39     // The expire time in seconds for the access token
40     @SerializedName("expires_in")
41     public Integer expiresIn;
42
43     @SerializedName("refresh_token")
44     public String refreshToken;
45
46     public String provider;
47
48     @SerializedName("user_id")
49     public String userId;
50
51     @SerializedName("token_type")
52     public String tokenType;
53
54     public void postProcess() {
55         accessTokenValidity = Instant.now().plus(expiresIn - 10, ChronoUnit.SECONDS);
56     }
57
58     public boolean isAccessTokenExpired() {
59         return Instant.now().isAfter(accessTokenValidity);
60     }
61
62     public boolean isRefreshTokenExpired() {
63         return Instant.now().isAfter(refreshTokenValidity);
64     }
65
66     @Override
67     public String toString() {
68         return "Token expiration: accessToken: " + ZonedDateTime.ofInstant(accessTokenValidity, ZoneId.systemDefault())
69                 + ", refreshToken: " + ZonedDateTime.ofInstant(refreshTokenValidity, ZoneId.systemDefault());
70     }
71 }