public void VerifyTokenBody(GoogleTokenResponse body) { if (string.IsNullOrWhiteSpace(body.IdToken) || string.IsNullOrEmpty(body.AccessToken)) { throw new CallerException("An Id Token and Access Token are required"); } }
/// <summary> /// Returns the User object and profile image URL of the given authorization code /// </summary> /// <param name="code"></param> /// <returns></returns> public static async Task <Tuple <User, string, string> > GetUserProfile(string code) { client = ServerConfig.Instance.client; HttpResponseMessage response = null; string access_token = await GetUserToken(code, REDIRECT_URI); response = await client.GetAsync(GetProfileAddr(access_token)); if (response.StatusCode == HttpStatusCode.OK) { } string rawProfile = await response.Content.ReadAsStringAsync(); Console.WriteLine(rawProfile); GoogleTokenResponse foundProfile = JsonConvert.DeserializeObject <GoogleTokenResponse>(rawProfile); string firstName = foundProfile.name.givenName; string lastName = foundProfile.name.familyName; string email = foundProfile.emails[0].value; string id = foundProfile.id; // TODO update image string imageURL = foundProfile.image.url; imageURL = imageURL.Substring(0, imageURL.Length - 6); imageURL += "?sz=1000"; return(new Tuple <User, string, string>(new User( firstName, lastName, email), imageURL, foundProfile.id)); }
public async Task <GoogleSSOResponse> LoginOrRegisterGoogleUser(GoogleTokenResponse googleTokenResponse, bool isAdmin) { GoogleUserProfile googleUser = await GoogleOAuthClient.GetUserProfile(googleTokenResponse.AccessToken); int userId = await FindExistingUserOrRegister(googleUser, isAdmin); string budgeterClientId = EnvironmentRequester.GetVariable("ServiceClientId"); BudgeterToken authTokenResponse = await GetAuthTokenForUser(userId, budgeterClientId, "Google"); return(new GoogleSSOResponse(googleTokenResponse, authTokenResponse)); }
public async Task <GoogleTokenResponse> VerifyAuthCode(GoogleSSORequest ssoRequest) { string secretKey = EnvironmentRequester.GetVariable("GoogleSSOClientSecret"); string redirectUrl = EnvironmentRequester.GetVariable("BudgeterRedirectUrl"); string googleRequestedScope = EnvironmentRequester.GetVariable("GoogleRequestedScope"); GoogleTokenResponse tokenResponse = await GoogleOAuthClient.VerifyCode(ssoRequest, secretKey, redirectUrl, googleRequestedScope); VerifyTokenBody(tokenResponse); return(tokenResponse); }
public async Task <GoogleSSOResponse> Login([FromBody] GoogleSSORequest ssoRequest) { CheckNullBody(ssoRequest); if (string.IsNullOrEmpty(ssoRequest.ClientId) || string.IsNullOrEmpty(ssoRequest.AuthorizationCode)) { throw new CallerException("Google Client Id and Authorization Code are required."); } GoogleSSOLogic ssoLogic = new GoogleSSOLogic(GoogleOAuthClient, EnvironmentRequester, SettingRequester, Cache, UserContext, AuthContext, BudgeterLock); GoogleTokenResponse tokenResponse = await ssoLogic.VerifyAuthCode(ssoRequest); return(await ssoLogic.LoginOrRegisterGoogleUser(tokenResponse, IsAdmin)); }
/* * Makes an api call and returns a user access token */ private static async Task <string> GetUserToken(string code, string redirect_uri) { client = ServerConfig.Instance.client; HttpContent content = new StringContent(""); HttpResponseMessage response = null; response = await client.PostAsync(GetTokenAddr(code, redirect_uri), content); string rawToken = await response.Content.ReadAsStringAsync(); GoogleTokenResponse tokenData = JsonConvert.DeserializeObject <GoogleTokenResponse>(rawToken); return(tokenData.access_token); }
public async Task <ActionResult> OAuthCallback(string code) { GoogleOAuthParameters googleOAuthParameters = new GoogleOAuthParameters(); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://oauth2.googleapis.com/token"); List <KeyValuePair <string, string> > formValues = new List <KeyValuePair <string, string> >(); formValues.Add(new KeyValuePair <string, string>("code", code)); formValues.Add(new KeyValuePair <string, string>("client_id", googleOAuthParameters.client_id)); formValues.Add(new KeyValuePair <string, string>("client_secret", googleOAuthParameters.client_secret)); formValues.Add(new KeyValuePair <string, string>("redirect_uri", googleOAuthParameters.redirect_uri)); formValues.Add(new KeyValuePair <string, string>("grant_type", "authorization_code")); request.Content = new FormUrlEncodedContent(formValues); HttpResponseMessage response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); GoogleTokenResponse token = JsonConvert.DeserializeObject <GoogleTokenResponse>(responseBody); HttpRequestMessage userProfileRequest = new HttpRequestMessage(HttpMethod.Get, "https://www.googleapis.com/oauth2/v2/userinfo"); userProfileRequest.Headers.Add("Authorization", "Bearer " + token.access_token); HttpResponseMessage userProfileResponse = await client.SendAsync(userProfileRequest); if (userProfileResponse.IsSuccessStatusCode) { string userProfileJson = await userProfileResponse.Content.ReadAsStringAsync(); GoogleUserProfile userProfile = JsonConvert.DeserializeObject <GoogleUserProfile>(userProfileJson); return(RedirectToAction("Index", "Home")); } else { return(RedirectToAction("Error", "Home")); } } else { return(RedirectToAction("Error", "Home")); } }
/// <summary> /// Returns the User object and profile image URL of the given authorization code /// </summary> /// <param name="code"></param> /// <returns></returns> public static async Task <string> GetUserId(string code) { client = ServerConfig.Instance.client; HttpResponseMessage response = null; string access_token = await GetUserToken(code, REDIRECT_URI_CHANGE_LOGIN); response = await client.GetAsync(GetProfileAddr(access_token)); if (response.StatusCode == HttpStatusCode.OK) { } string rawProfile = await response.Content.ReadAsStringAsync(); Console.WriteLine(rawProfile); GoogleTokenResponse foundProfile = JsonConvert.DeserializeObject <GoogleTokenResponse>(rawProfile); return(foundProfile.id); }
/// <summary> /// GetTokenByCode /// </summary> /// <param name="url"></param> /// <param name="postData"></param> /// <returns></returns> private async Task <GoogleTokenResponse> GetTokenByCode(string url, GoogleTokenRequest postData) { var result = new GoogleTokenResponse(); var keyValues = new List <KeyValuePair <string, string> >(); keyValues.Add(new KeyValuePair <string, string>("code", postData.Code)); keyValues.Add(new KeyValuePair <string, string>("client_id", postData.ClientId)); keyValues.Add(new KeyValuePair <string, string>("client_secret", postData.ClientSecret)); keyValues.Add(new KeyValuePair <string, string>("redirect_uri", postData.RedirectUri)); keyValues.Add(new KeyValuePair <string, string>("grant_type", "authorization_code")); HttpContent cont = new FormUrlEncodedContent(keyValues); using (HttpClient client = new HttpClient()) { using (HttpResponseMessage response = await client.PostAsync(url, cont)) { using (HttpContent content = response.Content) { string data = await content.ReadAsStringAsync(); if (!string.IsNullOrWhiteSpace(data)) { if (data.Contains("error")) { result.ErrorMessage = data; } else { result = JsonToObject.FromJson <GoogleTokenResponse>(data); } } } } } return(result); }
public GoogleSSOResponse(GoogleTokenResponse googleToken, BudgeterToken budgeterToken) { GoogleToken = googleToken; BudgeterToken = budgeterToken; }