public async Task GetUserProfileAsync(TokenModel rawTokenModel, string sessionId) { FacebookTokensModel tokensModel = (FacebookTokensModel)rawTokenModel; if (tokensModel == null) { return; } UserProfile profile = await GetProfileFromAccessTokenAsync(tokensModel); if (profile == null) { return; } bool isSuccess = _memoryCache.TryGetValue(sessionId, out CacheModel model); if (isSuccess && model != null) { model.UserProfile = profile; } }
private async Task <UserProfile> GetProfileFromAccessTokenAsync(FacebookTokensModel tokensModel) { string fields = ReformScopeToFields.Transform(tokensModel.Scope); string url = facebookProperties.GetProfileLink + tokensModel.AccessToken + "&fields=first_name,last_name,name,gender,location,picture" + fields; var response = await _httpClient.GetAsync(url); try { response.EnsureSuccessStatusCode(); } catch (HttpRequestException) { return(null); } string facebookInfoJson = await response.Content.ReadAsStringAsync(); FacebookInfoModel facebookInfo = JsonSerializer.Deserialize <FacebookInfoModel>(facebookInfoJson); return(FacebookUserProfileInfoProvider.Provider(facebookInfo)); }
/// <summary> /// Method that exchange user authorization code for tokens /// </summary> /// <param name="appId">Application Id</param> /// <param name="authorizationCodeModel"> /// Authorization model that has code, scope and state /// </param> /// <returns>Tokens model</returns> public async Task <TokenModel> ExchangeAuthorizationCodeForTokens(int appId, AuthorizationCodeModel authorizationCodeModel) { bool isSuccess = _memoryCache.TryGetValue(authorizationCodeModel.SessionId, out CacheModel sessionInformation); if (isSuccess == false) { _logger.LogError("Unable to find session id in memory cache." + "Authorization timeout has expired"); var errorExplanation = new ErrorModel { Error = "Authorization timeout has expired.", ErrorDescription = "Try again later" }; throw new AuthorizationCodeExchangeException(errorExplanation); } string code = authorizationCodeModel.AuthorizationCode; string redirectUri = sessionInformation.RedirectUri; string clientId = _context.Settings.FirstOrDefault(s => s.AppId == appId && s.SocialId == sessionInformation.SocialId)?.ClientId; string clientSecret = _context.Settings.FirstOrDefault(s => s.AppId == appId && s.SocialId == sessionInformation.SocialId)?.SecretKey; if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret)) { _logger.LogError("Client id or client secret is null or empty"); var errorExplanation = new ErrorModel { Error = "Trying to use unregistered social network", ErrorDescription = "There is no client id or client secret key registered in our widget." }; throw new AuthorizationCodeExchangeException(errorExplanation); } Dictionary <string, string> queryParams = new Dictionary <string, string> { { "code", code }, { "redirect_uri", redirectUri }, { "client_id", clientId }, { "client_secret", clientSecret }, { "scope", string.Empty } }; string tokenUri = _context.Socials.FirstOrDefault(s => s.Id == sessionInformation.SocialId)?.TokenUrl; string responseBody = await exchangeCodeForTokenService.ExchangeCodeForTokens(tokenUri, queryParams, SocialServiceName); if (string.IsNullOrWhiteSpace(responseBody)) { return(null); } try { FacebookTokensModel tokens = JsonSerializer.Deserialize <FacebookTokensModel>(responseBody); tokens.Scope = authorizationCodeModel.Scope; return(tokens); } catch (Exception exception) { if (exception is ArgumentNullException || exception is JsonException) { _logger.LogError($"Unable to deserialize response body. Received response body:\n{responseBody}"); return(null); } throw; } }