public void IsExpired_True()
        {
            var issued = DateTime.Now;
            var newNow = DateTime.Now.AddSeconds(200);

            var mockClock = new MockClock()
            {
                Now = newNow
            };

            // Issued not set.
            var response = new TokenResponse();
            Assert.True(response.IsExpired(mockClock));

            // ExpiresInSeconds is not set.
            response = new TokenResponse() { Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { ExpiresInSeconds = 1, Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { ExpiresInSeconds = 100, Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { ExpiresInSeconds = 140, Issued = issued };
            Assert.True(response.IsExpired(mockClock));
        }
        public void IsExpired_True()
        {
            var issued = DateTime.Now;
            var newNow = DateTime.Now.AddSeconds(200);

            var mockClock = new MockClock()
            {
                Now = newNow
            };

            // Issued not set.
            var response = new TokenResponse();

            Assert.True(response.IsExpired(mockClock));

            // ExpiresInSeconds is not set.
            response = new TokenResponse()
            {
                Issued = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                ExpiresInSeconds = 1, Issued = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                ExpiresInSeconds = 100, Issued = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                ExpiresInSeconds = 140, Issued = issued
            };
            Assert.True(response.IsExpired(mockClock));
        }
        public void IsExpired()
        {
            var issued = DateTime.Now;
            var newNow = DateTime.Now.AddSeconds(100);

            var mockClock = new MockClock
            {
                Now = newNow
            };

            // Issued not set.
            var response = new TokenResponse();
            Assert.True(response.IsExpired(mockClock));

            // ExpiresInSeconds is not set.
            response = new TokenResponse() { Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 1, Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 100, Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 158, Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 159, Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 160, Issued = issued };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 161, Issued = issued };
            Assert.False(response.IsExpired(mockClock));

            response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 162, Issued = issued };
            Assert.False(response.IsExpired(mockClock));
        }
        public void IsExpired_False()
        {
            var issued = DateTime.Now;
            var newNow = DateTime.Now.AddSeconds(200);

            var mockClock = new MockClock()
            {
                Now = newNow
            };

            var response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 141, Issued = issued
            };

            Assert.False(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 142, Issued = issued
            };
            Assert.False(response.IsExpired(mockClock));
        }
 private bool IsValidToken(TokenResponse token)
 {
     // If the token is expired but we have a non-null RefreshToken, we can assume the token will be 
     // automatically refreshed when we query Google Blogger and is therefore valid.
     return token != null && (!token.IsExpired(SystemClock.Default) || token.RefreshToken != null);
 }
        public void IsExpired()
        {
            var issued = DateTime.UtcNow;
            var newNow = DateTime.UtcNow.AddSeconds(100);

            var mockClock = new MockClock
            {
                UtcNow = newNow
            };

            // Issued not set.
            var response = new TokenResponse();

            Assert.True(response.IsExpired(mockClock));

            // ExpiresInSeconds is not set.
            response = new TokenResponse()
            {
                IssuedUtc = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 1, IssuedUtc = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 100, IssuedUtc = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 100 + 5 * 60 - 2, IssuedUtc = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 100 + 5 * 60 - 1, IssuedUtc = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 100 + 5 * 60, IssuedUtc = issued
            };
            Assert.True(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 100 + 5 * 60 + 1, IssuedUtc = issued
            };
            Assert.False(response.IsExpired(mockClock));

            response = new TokenResponse()
            {
                AccessToken = "a", ExpiresInSeconds = 100 + 5 * 60 + 2, IssuedUtc = issued
            };
            Assert.False(response.IsExpired(mockClock));
        }
 /// <summary>
 /// Determines the need for retrieval of a new authorization code, based on the given token and the 
 /// authorization code flow.
 /// </summary>
 public bool ShouldRequestAuthorizationCode(TokenResponse token)
 {
     // TODO: This code should be shared between this class and AuthorizationCodeInstalledApp.
     // If the flow includes a parameter that requires a new token, if the stored token is null or it doesn't
     // have a refresh token and the access token is expired we need to retrieve a new authorization code.
     return Flow.ShouldForceTokenRetrieval() || token == null || (token.RefreshToken == null
         && token.IsExpired(flow.Clock));
 }
        public void IsExpired_False()
        {
            var issued = DateTime.Now;
            var newNow = DateTime.Now.AddSeconds(200);

            var mockClock = new MockClock()
            {
                Now = newNow
            };

            var response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 141, Issued = issued };
            Assert.False(response.IsExpired(mockClock));

            response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds = 142, Issued = issued };
            Assert.False(response.IsExpired(mockClock));
        }