public bool Equals(OAuthTokenCredential other) { return(other != null && string.Equals(DisplayId, other.DisplayId) && string.Equals(AccessToken, other.AccessToken) && AccessTokenExpiration.Equals(other.AccessTokenExpiration) && string.Equals(RefreshToken, other.RefreshToken) && string.Equals(MailAddress, other.MailAddress)); }
/// <summary> /// Handle the RefreshToken response /// </summary> /// <param name="response">The HttpResponseMessage from the TokenRefresh request</param> private static async Task ParseRefreshTokenResponse(HttpResponseMessage response) { if (response.StatusCode == HttpStatusCode.OK) { dynamic responseObject = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); _accessToken = responseObject.access_token; AccessTokenExpiration = AccessTokenExpiration.AddSeconds((double)responseObject.expires_in); RefreshToken = responseObject.refresh_token; } }
public override int GetHashCode() { unchecked { var hashCode = (DisplayId != null ? DisplayId.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (AccessToken != null ? AccessToken.GetHashCode() : 0); hashCode = (hashCode * 397) ^ AccessTokenExpiration.GetHashCode(); hashCode = (hashCode * 397) ^ (RefreshToken != null ? RefreshToken.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (MailAddress != null ? MailAddress.GetHashCode() : 0); return(hashCode); } }
/// <summary> /// Refreshes the live authentication access token if it has expired /// </summary> public static async Task <bool> RefreshAuthTokenIfNeeded() { var refreshed = false; if (AccessTokenExpiration.CompareTo(DateTimeOffset.UtcNow) <= 0) { await AttemptAccessTokenRefresh(); refreshed = true; } return(refreshed); }
/// <summary> /// return OAuth 2.0 Credentials. If the token is expired, it will first attempt to refresh the token. /// </summary> /// <param name="uri">Ignored.</param> /// <param name="authType">Ignored.</param> /// <exception cref="WebException">May occour when refreshing the token</exception> public NetworkCredential GetCredential(Uri uri, string authType) { if (AccessTokenExpiration != null && RefreshToken != null && AccessTokenExpiration.Subtract(DateTime.Now).TotalSeconds < RefreshThreshold) { OAuthCredential newCreds = _client.RefreshAccessToken(RefreshToken); AccessToken = newCreds.AccessToken; // a new refresh token may be issued, per 5.2 if (newCreds.RefreshToken != null) { RefreshToken = newCreds.RefreshToken; AccessTokenExpiration = newCreds.AccessTokenExpiration; ExpiresIn = newCreds.ExpiresIn; } else { // otherwise, calculate the next refresh time AccessTokenExpiration = DateTime.Now.AddSeconds(ExpiresIn); } } return(new NetworkCredential("OAuth", AccessToken)); }
public bool AccessTokenIsValid() { var timeValid = AccessTokenExpiration.CompareTo(DateTimeOffset.Now) > 0; return(!string.IsNullOrEmpty(AccessToken) && timeValid); }