public void RefreshTokenWithEmptyRefreshTokenThrowsArgumentException() { // Arrange var accessTokenClient = new AccessTokenClient(ServerConfiguration); var emptyRefreshToken = string.Empty; // Act // Assert Assert.Throws <ArgumentException>(() => accessTokenClient.RefreshToken(emptyRefreshToken)); }
public void RefreshTokenCancellationTokenOverloadWithNullRefreshTokenThrowsArgumentNullException() { // Arrange var accessTokenClient = new AccessTokenClient(ServerConfiguration); string nullRefreshToken = null; // Act // Assert Assert.Throws <ArgumentNullException>(() => accessTokenClient.RefreshToken(nullRefreshToken, CancellationToken.None)); }
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); }
public void RefreshTokenCancellationTokenOverloadWithEmptyRefreshTokenThrowsArgumentException() { // Arrange var accessTokenClient = new AccessTokenClient(ServerConfiguration); var emptyRefreshToken = string.Empty; // Act // Assert Assert.Throws<ArgumentException>(() => accessTokenClient.RefreshToken(emptyRefreshToken, CancellationToken.None)); }
public void RefreshTokenCancellationTokenOverloadWithNullRefreshTokenThrowsArgumentNullException() { // Arrange var accessTokenClient = new AccessTokenClient(ServerConfiguration); string nullRefreshToken = null; // Act // Assert Assert.Throws<ArgumentNullException>(() => accessTokenClient.RefreshToken(nullRefreshToken, CancellationToken.None)); }
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 } }); } } }