Index: Src/GoogleApis.Auth/OAuth2/Responses/TokenResponse.cs =================================================================== new file mode 100644 --- /dev/null +++ b/Src/GoogleApis.Auth/OAuth2/Responses/TokenResponse.cs @@ -0,0 +1,93 @@ +/* +Copyright 2013 Google Inc + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +using System; + +using Google.Apis.Util; + +namespace Google.Apis.Auth.OAuth2.Responses +{ + /// + /// OAuth 2.0 model for a successful access token response as specified in + /// http://tools.ietf.org/html/rfc6749#section-5.1. + /// + public class TokenResponse + { + /// Gets or sets the access token issued by the authorization server. + [Newtonsoft.Json.JsonPropertyAttribute("access_token")] + public string AccessToken { get; set; } + + /// + /// Gets or sets the token type as specified in http://tools.ietf.org/html/rfc6749#section-7.1. + /// + [Newtonsoft.Json.JsonPropertyAttribute("token_type")] + public string TokenType { get; set; } + + /// Gets or sets the lifetime in seconds of the access token. + [Newtonsoft.Json.JsonPropertyAttribute("expires_in")] + public Nullable ExpiresInSeconds { get; set; } + + /// + /// Gets or sets the refresh token which can be used to obtain a new access token. + /// For example, the value "3600" denotes that the access token will expire in one hour from the time the + /// response was generated. + /// + [Newtonsoft.Json.JsonPropertyAttribute("refresh_token")] + public string RefreshToken { get; set; } + + /// + /// Gets or sets the scope of the access token as specified in http://tools.ietf.org/html/rfc6749#section-3.3. + /// + [Newtonsoft.Json.JsonPropertyAttribute("scope")] + public string Scope { get; set; } + + /// The date and time that this token was issued. + /// It should be set by the CLIENT after the token was received from the server. + /// + /// + public DateTime Issued { get; set; } + + /// + /// Returns true if the token is expired or it's going to be expired in the next minute. + /// + public bool IsExpired(IClock clock) + { + //TODO(peleyal): consider move it to an extension method + if (!ExpiresInSeconds.HasValue) + { + return true; + } + + return Issued.AddSeconds(ExpiresInSeconds.Value + 60) <= clock.Now; + } + + /// + /// Copies all properties from the other token, except the other's refresh token property in case it's null. + /// + internal void CopyFrom(TokenResponse other) + { + AccessToken = other.AccessToken; + TokenType = other.TokenType; + ExpiresInSeconds = other.ExpiresInSeconds; + if (other.RefreshToken != null) + { + RefreshToken = other.RefreshToken; + } + Scope = other.Scope; + Issued = other.Issued; + } + } +} \ No newline at end of file