001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *  
016 * Contributors:
017 *     Kevin Leturc
018 */
019package org.nuxeo.ecm.liveconnect.onedrive.oauth;
020
021import com.google.api.client.auth.oauth2.RefreshTokenRequest;
022import com.google.api.client.auth.oauth2.TokenResponse;
023import com.google.api.client.json.GenericJson;
024import com.google.api.client.json.JsonString;
025import com.google.api.client.util.Key;
026import com.google.api.client.util.Preconditions;
027
028/**
029 * We need this class for oauth with OneDrive as they don't return {@code expiresInSeconds} in number format. See
030 * https://github.com/google/google-oauth-java-client/issues/62. Copy of {@link TokenResponse}.
031 * 
032 * @since 8.2
033 */
034public class OneDriveTokenResponse extends GenericJson {
035
036    /** Access token issued by the authorization server. */
037    @Key("access_token")
038    private String accessToken;
039
040    /**
041     * Token type (as specified in <a href="http://tools.ietf.org/html/rfc6749#section-7.1">Access
042     * Token Types</a>).
043     */
044    @Key("token_type")
045    private String tokenType;
046
047    /**
048     * Lifetime in seconds of the access token (for example 3600 for an hour) or {@code null} for
049     * none.
050     */
051    @Key("expires_in")
052    @JsonString
053    private Long expiresInSeconds;
054
055    /**
056     * Refresh token which can be used to obtain new access tokens using {@link RefreshTokenRequest}
057     * or {@code null} for none.
058     */
059    @Key("refresh_token")
060    private String refreshToken;
061
062    /**
063     * Scope of the access token as specified in <a
064     * href="http://tools.ietf.org/html/rfc6749#section-3.3">Access Token Scope</a> or {@code null}
065     * for none.
066     */
067    @Key
068    private String scope;
069
070    /** Returns the access token issued by the authorization server. */
071    public final String getAccessToken() {
072        return accessToken;
073    }
074
075    /**
076     * Sets the access token issued by the authorization server.
077     *
078     * <p>
079     * Overriding is only supported for the purpose of calling the super implementation and changing
080     * the return type, but nothing else.
081     * </p>
082     */
083    public OneDriveTokenResponse setAccessToken(String accessToken) {
084        this.accessToken = Preconditions.checkNotNull(accessToken);
085        return this;
086    }
087
088    /**
089     * Returns the token type (as specified in <a
090     * href="http://tools.ietf.org/html/rfc6749#section-7.1">Access Token Types</a>).
091     */
092    public final String getTokenType() {
093        return tokenType;
094    }
095
096    /**
097     * Sets the token type (as specified in <a
098     * href="http://tools.ietf.org/html/rfc6749#section-7.1">Access Token Types</a>).
099     *
100     * <p>
101     * Overriding is only supported for the purpose of calling the super implementation and changing
102     * the return type, but nothing else.
103     * </p>
104     */
105    public OneDriveTokenResponse setTokenType(String tokenType) {
106        this.tokenType = Preconditions.checkNotNull(tokenType);
107        return this;
108    }
109
110    /**
111     * Returns the lifetime in seconds of the access token (for example 3600 for an hour) or
112     * {@code null} for none.
113     */
114    public final Long getExpiresInSeconds() {
115        return expiresInSeconds;
116    }
117
118    /**
119     * Sets the lifetime in seconds of the access token (for example 3600 for an hour) or {@code null}
120     * for none.
121     *
122     * <p>
123     * Overriding is only supported for the purpose of calling the super implementation and changing
124     * the return type, but nothing else.
125     * </p>
126     */
127    public OneDriveTokenResponse setExpiresInSeconds(Long expiresInSeconds) {
128        this.expiresInSeconds = expiresInSeconds;
129        return this;
130    }
131
132    /**
133     * Returns the refresh token which can be used to obtain new access tokens using the same
134     * authorization grant or {@code null} for none.
135     */
136    public final String getRefreshToken() {
137        return refreshToken;
138    }
139
140    /**
141     * Sets the refresh token which can be used to obtain new access tokens using the same
142     * authorization grant or {@code null} for none.
143     *
144     * <p>
145     * Overriding is only supported for the purpose of calling the super implementation and changing
146     * the return type, but nothing else.
147     * </p>
148     */
149    public OneDriveTokenResponse setRefreshToken(String refreshToken) {
150        this.refreshToken = refreshToken;
151        return this;
152    }
153
154    /**
155     * Returns the scope of the access token or {@code null} for none.
156     */
157    public final String getScope() {
158        return scope;
159    }
160
161    /**
162     * Sets the scope of the access token or {@code null} for none.
163     *
164     * <p>
165     * Overriding is only supported for the purpose of calling the super implementation and changing
166     * the return type, but nothing else.
167     * </p>
168     */
169    public OneDriveTokenResponse setScope(String scope) {
170        this.scope = scope;
171        return this;
172    }
173
174    @Override
175    public OneDriveTokenResponse set(String fieldName, Object value) {
176        return (OneDriveTokenResponse) super.set(fieldName, value);
177    }
178
179    @Override
180    public OneDriveTokenResponse clone() {
181        return (OneDriveTokenResponse) super.clone();
182    }
183
184    public TokenResponse toTokenResponse() {
185        TokenResponse response = new TokenResponse();
186        response.setAccessToken(getAccessToken());
187        response.setTokenType(getTokenType());
188        response.setExpiresInSeconds(getExpiresInSeconds());
189        response.setRefreshToken(getRefreshToken());
190        response.setScope(getScope());
191        return response;
192    }
193
194}