public async Task <ActionResult <string> > GetToken([FromQuery] string accessKey) { // TODO Probably create a named Patreon HttpClient var redirectUri = $"{_appSettings.ApiUrl}/subscriptions/patreon/token"; var url = $"/api/oauth2/token"; var formBody = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "authorization_code"), new KeyValuePair <string, string>("code", accessKey), new KeyValuePair <string, string>("client_id", _patreonSettings.ClientId), new KeyValuePair <string, string>("client_secret", _patreonSettings.ClientSecret), new KeyValuePair <string, string>("redirect_uri", $"{_appSettings.SiteUrl}/auth/redir/patreon") }); var client = _httpClientFactory.CreateClient("patreon"); client.DefaultRequestHeaders.Add( HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded" ); var response = await client.PostAsync(url, formBody); if (response.IsSuccessStatusCode) { var contents = await response.Content.ReadAsStringAsync(); var received = JsonSerializer.Deserialize <PatreonAuthTokenResponseModel>(contents); var existingToken = await _repository.GetAll() .FirstOrDefaultAsync(r => r.AppUserId == _applicationUser.Id); if (existingToken == null) { existingToken = new PatreonToken { AppUserId = _applicationUser.Id }; } existingToken.AccessToken = received.AccessToken; existingToken.ExpiresIn = received.ExpiresIn; existingToken.TokenType = received.TokenType; existingToken.FullName = received.FullName; existingToken.RefreshToken = received.RefreshToken; existingToken.Version = received.Version; existingToken.AppUserId = _applicationUser.Id; _repository.AddOrUpdate(existingToken); await _unitOfWork.CompleteAsync(); return(Content("You have successfully connected your Patreon account", "text/plain", Encoding.UTF8)); } _logger.LogError($"Unable to connect to Patreon: {response.ReasonPhrase}"); return(BadRequest("Unable to connect your Patreon account at this time")); }
public async Task <IdentityResult> UpdateUserToken(PatreonToken token, ClassicGuildBankUser user) { user.PatreonAccessToken = token.AccessToken; user.PatreonExpiration = token.ExpiresIn; user.PatreonRefreshToken = token.RefreshToken; user.LastUpdated = DateTime.Now; if (String.IsNullOrEmpty(user.Patreon_Id)) { var patreonId = await GetPatreonId(user); user.Patreon_Id = patreonId; } return(await _userManager.UpdateAsync(user)); }