Beispiel #1
0
        public async Task RefreshTokens(CharacterTokens tokens)
        {
            HttpRequestMessage CreateRefreshTokenRequest() =>
            CreateTokenRequest(
                () => new[]
            {
                new KeyValuePair <string, string>("grant_type", "refresh_token"),
                new KeyValuePair <string, string>("refresh_token", tokens.RefreshToken)
            });

            void PrepareResult(Task <string> task)
            {
                var response = JsonConvert.DeserializeObject <SsoAuthorizationResponse>(task.Result);

                tokens.AccessToken          = response.AccessToken;
                tokens.RefreshToken         = response.RefreshToken;
                tokens.AccessTokenValidTill = _clock.UtcNow.AddSeconds(response.ExpirationTimeInSeconds);
            }

            _logger.LogInformation("Refreshing tokens for character using SSO.");

            await _httpService.CallAsync(
                CreateRefreshTokenRequest,
                response => response.Content.ReadAsStringAsync().ContinueWith(PrepareResult));
        }
        protected async Task <string> GetActualAccessTokenForCharacterAsync(CharacterTokens tokens)
        {
            if (tokens.AccessTokenValidTill < _clock.UtcNow)
            {
                await _authenticator.RefreshTokens(tokens);
            }

            return(tokens.AccessToken);
        }