public async Task <string> LoginAsync(AuthRequest request) { var pairs = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("client_id", request.clientId), new KeyValuePair <string, string>("redirect_uri", request.redirectUri), new KeyValuePair <string, string>("client_secret", clientSecret), new KeyValuePair <string, string>("code", request.code), new KeyValuePair <string, string>("grant_type", "authorization_code"), new KeyValuePair <string, string>("access_type", "offline") }; HttpRequestMessage tokenRequest = new HttpRequestMessage(HttpMethod.Post, new Uri("https://www.googleapis.com/oauth2/v4/token")); tokenRequest.Content = new FormUrlEncodedContent(pairs); tokenRequest.Content.Headers.TryAddWithoutValidation("ContentType ", "application/x-www-form-urlencoded"); var tokenResult = await httpClient.SendAsync(tokenRequest); if (!tokenResult.IsSuccessStatusCode) { throw AuthenticationException.InvalidCredential; } var tokenResponseString = await tokenResult.Content.ReadAsStringAsync(); var tokenResponse = JsonConvert.DeserializeObject <GoogleAccesTokenResponse>(tokenResponseString); HttpRequestMessage openIdRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("https://www.googleapis.com/plus/v1/people/me/openIdConnect")); openIdRequest.Headers.Authorization = new AuthenticationHeaderValue(tokenResponse.token_type, tokenResponse.access_token); var openIdResult = await httpClient.SendAsync(openIdRequest); if (!openIdResult.IsSuccessStatusCode) { throw AuthenticationException.InvalidCredential; } var googleUserString = await openIdResult.Content.ReadAsStringAsync(); var googleUser = JsonConvert.DeserializeObject <GoogleMeResponse>(googleUserString); var dbUser = userSecurityRepository.GetByEmail(googleUser.email); if (dbUser == null) { var userRole = roleRepository.GetByName("User"); var userSecurity = new UserSecurity( Guid.Empty, googleUser.email, AuthProvider.Google, tokenResponse.access_token, tokenResponse.refresh_token, Guid.NewGuid().ToString(), null, null, googleUser.locale == "tr" ? "tr" : "en", // TODO: check language correctly googleUser.given_name, googleUser.family_name, googleUser.picture, googleUser.gender == "male" ? Gender.Male : Gender.Female, false, true, new List <Role> { userRole } ); userSecurity.CreateToken(abiokaToken, tokenResponse.access_token); userSecurityRepository.Add(userSecurity); return(userSecurity.Token); } if (dbUser.AuthProvider != AuthProvider.Google) { throw new DenialException($"UserIsRegisteredFor{dbUser.AuthProvider}", dbUser.Email); } dbUser.UpdateProviderRefreshToken(tokenResponse.refresh_token ?? tokenResponse.access_token); dbUser.CreateToken(abiokaToken, tokenResponse.access_token); userSecurityRepository.Update(dbUser); return(dbUser.Token); }
public async Task <string> LoginAsync(AuthRequest request) { var urlQuery = new StringBuilder(); urlQuery.Append("https://graph.facebook.com/v2.8/oauth/access_token"); urlQuery.AppendFormat("?client_id={0}", clientId); urlQuery.AppendFormat("&client_secret={0}", clientSecret); urlQuery.AppendFormat("&redirect_uri={0}", request.redirectUri); urlQuery.AppendFormat("&code={0}", request.code); var tokenResult = await httpClient.GetAsync(urlQuery.ToString()); if (!tokenResult.IsSuccessStatusCode) { throw AuthenticationException.InvalidCredential; } var tokenResponseString = await tokenResult.Content.ReadAsStringAsync(); var tokenResponse = JsonConvert.DeserializeObject <FacebookAccesTokenResponse>(tokenResponseString); var meUrlQuery = new StringBuilder(); meUrlQuery.Append("https://graph.facebook.com/v2.8/me"); meUrlQuery.Append("?fields=first_name,last_name,email,picture,gender"); meUrlQuery.AppendFormat("&access_token={0}", tokenResponse.access_token); HttpRequestMessage meRequest = new HttpRequestMessage(HttpMethod.Get, new Uri(meUrlQuery.ToString())); var meResult = await httpClient.SendAsync(meRequest); if (!meResult.IsSuccessStatusCode) { throw AuthenticationException.InvalidCredential; } var facebookUserString = await meResult.Content.ReadAsStringAsync(); var facebookUser = JsonConvert.DeserializeObject <FacebookMeResponse>(facebookUserString); var dbUser = userSecurityRepository.GetByEmail(facebookUser.email); if (dbUser == null) { var userRole = roleRepository.GetByName("User"); var userSecurity = new UserSecurity( Guid.Empty, facebookUser.email, AuthProvider.Facebook, tokenResponse.access_token, tokenResponse.access_token, Guid.NewGuid().ToString(), null, null, facebookUser.locale == "tr" ? "tr" : "en", // TODO: check language correctly facebookUser.first_name, facebookUser.last_name, facebookUser.picture?.data?.url, facebookUser.gender == "male" ? Gender.Male : Gender.Female, false, true, new List <Role> { userRole } ); userSecurity.CreateToken(abiokaToken, tokenResponse.access_token); userSecurityRepository.Add(userSecurity); return(userSecurity.Token); } if (dbUser.AuthProvider != AuthProvider.Facebook) { throw new DenialException($"UserIsRegisteredFor{dbUser.AuthProvider}", dbUser.Email); } dbUser.UpdateProviderRefreshToken(tokenResponse.access_token); dbUser.CreateToken(abiokaToken, tokenResponse.access_token); userSecurityRepository.Update(dbUser); return(dbUser.Token); }