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.netatmo.internal.api.dto;
15 import java.util.List;
16 import java.util.StringJoiner;
18 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Scope;
19 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
21 import com.google.gson.annotations.SerializedName;
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.
27 * This is different from {@link AccessTokenResponse} because it violates RFC 6749 by having {@link #scope}
28 * defined as an array of strings.
30 * @author Gaƫl L'hopital - Initial contribution
32 public final class NetatmoAccessTokenResponse {
35 * The access token issued by the authorization server. It is used
36 * by the client to gain access to a resource.
38 @SerializedName("access_token")
39 private String accessToken;
41 @SerializedName("token_type")
42 private String tokenType;
45 * Number of seconds that this OAuthToken is valid for since the time it was created.
47 @SerializedName("expires_in")
48 private long expiresIn;
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.
57 @SerializedName("refresh_token")
58 private String refreshToken;
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.
64 * @see <a href="https://tools.ietf.org/html/rfc6749#section-3.3">rfc6749 section-3.3</a>
66 private List<Scope> scope;
69 * State from prior access token request (if present).
73 public String getAccessToken() {
77 public long getExpiresIn() {
81 public String getRefreshToken() {
85 public List<Scope> getScope() {
90 public String toString() {
91 return "AccessTokenResponse [accessToken=" + accessToken + ", tokenType=" + tokenType + ", expiresIn="
92 + expiresIn + ", refreshToken=" + refreshToken + ", scope=" + scope + ", state=" + state + "]";
96 * Convert Netatmo-specific DTO to standard DTO in core resembling RFC 6749.
98 * @return response converted into {@link AccessTokenResponse}
100 public AccessTokenResponse toStandard() {
101 var standardResponse = new AccessTokenResponse();
103 standardResponse.setAccessToken(accessToken);
104 standardResponse.setTokenType(tokenType);
105 standardResponse.setExpiresIn(expiresIn);
106 standardResponse.setRefreshToken(refreshToken);
108 StringJoiner stringJoiner = new StringJoiner(" ");
109 scope.forEach(s -> stringJoiner.add(s.name().toLowerCase()));
110 standardResponse.setScope(stringJoiner.toString());
111 standardResponse.setState(state);
113 return standardResponse;