]> git.basschouten.com Git - openhab-addons.git/blob
b4896cbefa589ec4aaa6b2469c0d7d401f5b0564
[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.netatmo.internal.api.dto;
14
15 import java.util.List;
16 import java.util.StringJoiner;
17
18 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Scope;
19 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
20
21 import com.google.gson.annotations.SerializedName;
22
23 /**
24  * This is the Access Token Response, a simple value-object holding the result of an Access Token Request, as
25  * provided by Netatmo API.
26  * 
27  * This is different from {@link AccessTokenResponse} because it violates RFC 6749 by having {@link #scope}
28  * defined as an array of strings.
29  *
30  * @author GaĆ«l L'hopital - Initial contribution
31  */
32 public final class NetatmoAccessTokenResponse {
33
34     /**
35      * The access token issued by the authorization server. It is used
36      * by the client to gain access to a resource.
37      */
38     @SerializedName("access_token")
39     private String accessToken;
40
41     @SerializedName("token_type")
42     private String tokenType;
43
44     /**
45      * Number of seconds that this OAuthToken is valid for since the time it was created.
46      */
47     @SerializedName("expires_in")
48     private long expiresIn;
49
50     /**
51      * Refresh token is a string representing the authorization granted to
52      * the client by the resource owner. Unlike access tokens, refresh tokens are
53      * intended for use only with authorization servers and are never sent
54      * to resource servers.
55      *
56      */
57     @SerializedName("refresh_token")
58     private String refreshToken;
59
60     /**
61      * A list of scopes. This is not compliant with RFC 6749 which defines scope
62      * as a list of space-delimited case-sensitive strings.
63      *
64      * @see <a href="https://tools.ietf.org/html/rfc6749#section-3.3">rfc6749 section-3.3</a>
65      */
66     private List<Scope> scope;
67
68     /**
69      * State from prior access token request (if present).
70      */
71     private String state;
72
73     public String getAccessToken() {
74         return accessToken;
75     }
76
77     public long getExpiresIn() {
78         return expiresIn;
79     }
80
81     public String getRefreshToken() {
82         return refreshToken;
83     }
84
85     public List<Scope> getScope() {
86         return scope;
87     }
88
89     @Override
90     public String toString() {
91         return "AccessTokenResponse [accessToken=" + accessToken + ", tokenType=" + tokenType + ", expiresIn="
92                 + expiresIn + ", refreshToken=" + refreshToken + ", scope=" + scope + ", state=" + state + "]";
93     }
94
95     /**
96      * Convert Netatmo-specific DTO to standard DTO in core resembling RFC 6749.
97      * 
98      * @return response converted into {@link AccessTokenResponse}
99      */
100     public AccessTokenResponse toStandard() {
101         var standardResponse = new AccessTokenResponse();
102
103         standardResponse.setAccessToken(accessToken);
104         standardResponse.setTokenType(tokenType);
105         standardResponse.setExpiresIn(expiresIn);
106         standardResponse.setRefreshToken(refreshToken);
107
108         StringJoiner stringJoiner = new StringJoiner(" ");
109         scope.forEach(s -> stringJoiner.add(s.name().toLowerCase()));
110         standardResponse.setScope(stringJoiner.toString());
111         standardResponse.setState(state);
112
113         return standardResponse;
114     }
115 }