コード例 #1
0
        public void RefreshTokenWithEmptyRefreshTokenThrowsArgumentException()
        {
            // Arrange
            var accessTokenClient = new AccessTokenClient(ServerConfiguration);
            var emptyRefreshToken = string.Empty;

            // Act

            // Assert
            Assert.Throws <ArgumentException>(() => accessTokenClient.RefreshToken(emptyRefreshToken));
        }
コード例 #2
0
        public void RefreshTokenCancellationTokenOverloadWithNullRefreshTokenThrowsArgumentNullException()
        {
            // Arrange
            var    accessTokenClient = new AccessTokenClient(ServerConfiguration);
            string nullRefreshToken  = null;

            // Act

            // Assert
            Assert.Throws <ArgumentNullException>(() => accessTokenClient.RefreshToken(nullRefreshToken, CancellationToken.None));
        }
コード例 #3
0
        public void RefreshTokenCancellationTokenOverloadReturnsNewAccessToken()
        {
            // Arrange
            var accessTokenClient = new AccessTokenClient(ServerConfiguration);

            // Act
            var task = accessTokenClient.GetUserAccessToken(Username, Password, UserScope, CancellationToken.None);

            task.Wait();

            var refreshTokenTask = accessTokenClient.RefreshToken(task.Result.RefreshToken, CancellationToken.None);

            refreshTokenTask.Wait();

            // Assert
            Assert.NotNull(refreshTokenTask.Result);
        }
コード例 #4
0
        public void RefreshTokenCancellationTokenOverloadWithEmptyRefreshTokenThrowsArgumentException()
        {
            // Arrange
            var accessTokenClient = new AccessTokenClient(ServerConfiguration);
            var emptyRefreshToken = string.Empty;

            // Act

            // Assert
            Assert.Throws<ArgumentException>(() => accessTokenClient.RefreshToken(emptyRefreshToken, CancellationToken.None));
        }
コード例 #5
0
        public void RefreshTokenCancellationTokenOverloadWithNullRefreshTokenThrowsArgumentNullException()
        {
            // Arrange
            var accessTokenClient = new AccessTokenClient(ServerConfiguration);
            string nullRefreshToken = null;

            // Act

            // Assert
            Assert.Throws<ArgumentNullException>(() => accessTokenClient.RefreshToken(nullRefreshToken, CancellationToken.None));
        }
コード例 #6
0
        public async Task <GetAccessTokenResponse> RefreshToken(CancellationToken cancellationToken)
        {
            var tokenResponse = await _accessTokenStore.GetClientAccessToken(_appSettings.Identity.ClientId,
                                                                             _appSettings.ServiceId,
                                                                             cancellationToken).ConfigureAwait(false);

            if (!tokenResponse.HasAccessToken)
            {
                return(new GetAccessTokenResponse {
                    AccessToken = null, Error = tokenResponse.Error
                });
            }

            var token = tokenResponse.AccessToken;

            if (!token.ShouldBeRefreshed(TimeSpan.FromMinutes(2)))
            {
                return new GetAccessTokenResponse {
                           AccessToken = token, Error = null
                }
            }
            ;

            var accessTokenClient = new AccessTokenClient(new OAuthServerConfiguration(
                                                              _appSettings.Identity.Host,
                                                              "/auth/token",
                                                              _appSettings.Identity.ClientId, _appSettings.Identity.ClientSecret));

            try
            {
                var newTokenResponse = await accessTokenClient
                                       .RefreshToken(token.RefreshToken, cancellationToken).ConfigureAwait(false);

                if (newTokenResponse.IsSuccess)
                {
                    var newToken = newTokenResponse.AccessToken;

                    await _accessTokenStore.SaveClientAccessToken(_appSettings.Identity.ClientId,
                                                                  _appSettings.ServiceId,
                                                                  newToken, cancellationToken).ConfigureAwait(false);

                    return(new GetAccessTokenResponse {
                        AccessToken = newToken, Error = null
                    });
                }
                return(new GetAccessTokenResponse
                {
                    AccessToken = null,
                    Error = newTokenResponse.Error
                });
            }
            catch (Exception exception)
            {
                return(new GetAccessTokenResponse
                {
                    AccessToken = null,
                    Error = new ErrorResult
                    {
                        Error = "genericerror",
                        ErrorDescription = exception.Message
                    }
                });
            }
        }
    }
コード例 #7
0
        public void RefreshTokenCancellationTokenOverloadReturnsNewAccessToken()
        {
            // Arrange
            var accessTokenClient = new AccessTokenClient(ServerConfiguration);

            // Act
            var task = accessTokenClient.GetUserAccessToken(Username, Password, UserScope, CancellationToken.None);
            task.Wait();

            var refreshTokenTask = accessTokenClient.RefreshToken(task.Result.RefreshToken, CancellationToken.None);
            refreshTokenTask.Wait();

            // Assert
            Assert.NotNull(refreshTokenTask.Result);
        }