2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.gardena.internal.model.dto.api;
15 import java.time.Instant;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18 import java.time.temporal.ChronoUnit;
20 import com.google.gson.annotations.SerializedName;
23 * Represents a Gardena object that is sent via the Gardena API.
25 * @author Gerhard Riegler - Initial contribution
27 public class PostOAuth2Response {
28 // refresh token is valid 10 days
29 private transient Instant refreshTokenValidity = Instant.now().plus(10, ChronoUnit.DAYS).minus(1,
31 private transient Instant accessTokenValidity;
33 @SerializedName("access_token")
34 public String accessToken;
36 // The scope of the token (what you are allowed to do)
39 // The expire time in seconds for the access token
40 @SerializedName("expires_in")
41 public Integer expiresIn;
43 @SerializedName("refresh_token")
44 public String refreshToken;
46 public String provider;
48 @SerializedName("user_id")
51 @SerializedName("token_type")
52 public String tokenType;
54 public void postProcess() {
55 accessTokenValidity = Instant.now().plus(expiresIn - 10, ChronoUnit.SECONDS);
58 public boolean isAccessTokenExpired() {
59 return Instant.now().isAfter(accessTokenValidity);
62 public boolean isRefreshTokenExpired() {
63 return Instant.now().isAfter(refreshTokenValidity);
67 public String toString() {
68 return "Token expiration: accessToken: " + ZonedDateTime.ofInstant(accessTokenValidity, ZoneId.systemDefault())
69 + ", refreshToken: " + ZonedDateTime.ofInstant(refreshTokenValidity, ZoneId.systemDefault());