Ejemplo n.º 1
0
        public async Task UpsertTokens(string stravaId, StravaOauthResponse stravaOauthResponse)
        {
            var existing = await GetCredentials(stravaId);

            if (existing == null)
            {
                DbContext.Credentials.Add(new StravaCredential(stravaId, stravaOauthResponse.AccessToken, stravaOauthResponse.RefreshToken));
            }
            else
            {
                existing.UpdateWithNewTokens(stravaOauthResponse);
                DbContext.Credentials.Update(existing);
            }
            await DbContext.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public async Task CreateWithCredentials(LeaderboardParticipant participant, StravaOauthResponse oauthResponse)
        {
            _dbContext.Participants.Add(participant);

            var credentials = _dbContext.Credentials.FirstOrDefault(x => x.StravaId == participant.StravaId);

            if (credentials == null)
            {
                _dbContext.Credentials.Add(new StravaCredential(participant.StravaId, oauthResponse.AccessToken, oauthResponse.RefreshToken));
            }
            else
            {
                credentials.UpdateWithNewTokens(oauthResponse);
                _dbContext.Credentials.Update(credentials);
            }
            await _dbContext.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        private void UpsertCredentialWithoutSaving(string stravaId, StravaOauthResponse oauthResponse)
        {
            var credential = _dbContext.Credentials.FirstOrDefault(x => x.StravaId == stravaId);

            _logger.LogInformation($"Credentials found - {credential != null}");
            if (credential == null)
            {
                _logger.LogInformation("Credentials not found, adding");
                _dbContext.Credentials.Add(new StravaCredential(stravaId, oauthResponse.AccessToken,
                                                                oauthResponse.RefreshToken));
                return;
            }

            _logger.LogInformation("Credentials found, updating");
            _logger.LogDebug($"Access token changed - {credential.AccessToken != oauthResponse.AccessToken}");
            _logger.LogDebug($"Refresh token changed - {credential.RefreshToken != oauthResponse.RefreshToken}");

            credential.UpdateWithNewTokens(oauthResponse);
            _dbContext.Credentials.Update(credential);
        }
Ejemplo n.º 4
0
        public async Task <StravaCredential> RefreshCredentialsForParticipant(LeaderboardParticipant participant)
        {
            StravaOauthResponse authResponse = null;
            StravaCredential    credential;

            try
            {
                credential = await GetCredentialsForParticipant(participant);

                authResponse = await _stravaApiService.RefreshAccessTokenAsync(credential.RefreshToken)
                               .ConfigureAwait(false);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Failed to refresh access token for {participant.DiscordUserId}");
                throw new StravaException(StravaException.StravaErrorType.RefreshFailed);
            }

            credential.UpdateWithNewTokens(authResponse);
            _dbContext.Credentials.Update(credential);
            await _dbContext.SaveChangesAsync();

            return(credential);
        }
 public void UpdateWithNewTokens(StravaOauthResponse result)
 {
     AccessToken  = result.AccessToken;
     RefreshToken = result.RefreshToken;
 }