private async Task <RefreshTokenStatus> RefreshTokens(CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(_tokenStorage.RefreshToken) || string.IsNullOrEmpty(_tokenStorage.Uid))
            {
                return(RefreshTokenStatus.Unauthorized);
            }

            try
            {
                ApiResponseResult <RefreshTokenResponse> response =
                    await _tokenClient.RefreshTokenAsync(cancellationToken);

                if (response.Success)
                {
                    _tokenStorage.AccessToken  = response.Value.AccessToken;
                    _tokenStorage.RefreshToken = response.Value.RefreshToken;

                    return(RefreshTokenStatus.Success);
                }
            }
            catch (ArgumentNullException e)
            {
                _logger.Error($"An error occurred when refreshing the auth token: {e.ParamName}");
            }
            catch (HttpRequestException)
            {
                return(RefreshTokenStatus.Fail);
            }

            return(RefreshTokenStatus.Unauthorized);
        }
Exemple #2
0
        private async Task <HttpRequestMessage> CreateRequestAsync(HttpMethod httpMethod, string address, Pop pop,
                                                                   HttpContent content = null, string token = null)
        {
            var uri     = new Uri(address);
            var request = new HttpRequestMessage(httpMethod, uri);

            if (content != null)
            {
                request.Content = content;
            }

            if (IsNullOrEmpty(token))
            {
                token = await _tokenClient.RefreshTokenAsync(false, pop.Audience, pop.Region).ConfigureAwait(false);
            }

            request.Headers.Authorization = new AuthenticationHeaderValue(Bearer, token);
            request.Headers.TryAddWithoutValidation(XEnvId, _environmentId);
            return(request);
        }
        public void TestInitialize()
        {
            _tokenClient = Substitute.For <ITokenClient>();
            _tokenClient.RefreshTokenAsync(Arg.Any <CancellationToken>())
            .Returns(ApiResponseResult <RefreshTokenResponse> .Ok(new RefreshTokenResponse()));

            _tokenStorage = Substitute.For <ITokenStorage>();
            _tokenStorage.AccessToken.Returns("Access token");
            _tokenStorage.RefreshToken.Returns("Refresh token");
            _tokenStorage.Uid.Returns(string.Empty);

            _innerHandler = new MockHttpMessageHandler();
        }
        public void TestInitialize()
        {
            _tokenClient = Substitute.For <ITokenClient>();
            _tokenClient.RefreshTokenAsync(Arg.Any <CancellationToken>())
            .Returns(ApiResponseResult <RefreshTokenResponse> .Ok(new RefreshTokenResponse()));

            _tokenStorage = Substitute.For <ITokenStorage>();
            _tokenStorage.AccessToken.Returns("Access token");
            _tokenStorage.RefreshToken.Returns("Refresh token");
            _tokenStorage.Uid.Returns("User ID");

            _userStorage = Substitute.For <IUserStorage>();
            _userStorage.User().Returns(new Core.Models.User {
                Username = "******"
            });

            _logger = Substitute.For <ILogger>();

            _innerHandler = new MockHttpMessageHandler();
        }
        private async Task <bool> RefreshTokens(CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(_tokenStorage.RefreshToken))
            {
                return(false);
            }

            try
            {
                var response = await _tokenClient.RefreshTokenAsync(cancellationToken);

                if (response.Success)
                {
                    _tokenStorage.AccessToken  = response.Value.AccessToken;
                    _tokenStorage.RefreshToken = response.Value.RefreshToken;

                    return(true);
                }
            }
            catch (HttpRequestException)
            { }

            return(false);
        }
            public async Task <ApiResponseResult <RefreshTokenResponse> > RefreshTokenAsync(CancellationToken token)
            {
                await Breakpoint.Hit().WaitForContinue();

                return(await _origin.RefreshTokenAsync(token));
            }