Exemplo n.º 1
0
 private static void loadUserProfile(string provider, ParsedExternalAccessToken verifiedAccessToken, JObject accessTokenResponse)
 {
     if (provider == LinkedIn)
     {
         accessTokenResponse.Add("userProfile", JToken.FromObject(verifiedAccessToken.userProfile));
     }
     if (provider == GitHub)
     {
         accessTokenResponse.Add("githubUserProfile", JToken.FromObject(verifiedAccessToken.githubUserProfile));
     }
     if (provider == StackExchange)
     {
         accessTokenResponse.Add("stackexchangeUserProfile", JToken.FromObject(verifiedAccessToken.stackexchangeUserProfile));
     }
     if (provider == Twitter)
     {
         accessTokenResponse.Add("twitterUserProfile", JToken.FromObject(verifiedAccessToken.twitterUserProfile));
     }
 }
Exemplo n.º 2
0
        private async Task<ParsedExternalAccessToken> VerifyExternalAccessToken(string provider, string accessToken)
        {
            ParsedExternalAccessToken parsedToken = null;

            var verifyTokenEndPoint = "";

            switch (provider)
            {
                case LinkedIn:
                   // verifyTokenEndPoint = string.Format("https://api.linkedin.com/v1/companies/universal-name=victor:(id,name,ticker,description)?oauth2_access_token={0}", accessToken);
                    verifyTokenEndPoint = string.Format("https://api.linkedin.com/v1/people/~:(id,first-name,last-name,formatted-name,email-address,positions,headline,location,public-profile-url)?oauth2_access_token={0}", accessToken);
                    break;
                case GitHub:
                    verifyTokenEndPoint = string.Format("https://api.github.com/user?access_token={0}", accessToken);
                    break;
                case Twitter:
                    verifyTokenEndPoint = string.Format("https://api.twitter.com/1.1/users/show.json?access_token={0}", accessToken);
                    break;
                case StackExchange:
                    verifyTokenEndPoint = string.Format("https://api.stackexchange.com/2.2/me?order=desc&sort=reputation&site=stackoverflow&access_token={0}&key={1}", accessToken, Startup.stackexchangeAuthOptions.Key);
                    break;
                case Facebook:
                    //You can get it from here: https://developers.facebook.com/tools/accesstoken/
                    //More about debug_tokn here: http://stackoverflow.com/questions/16641083/how-does-one-get-the-app-access-token-for-debug-token-inspection-on-facebook
                    var appToken = "xxxxx";
                    verifyTokenEndPoint = string.Format("https://graph.facebook.com/debug_token?input_token={0}&access_token={1}", accessToken, appToken);
                    break;
                case GooglePlus:
                    verifyTokenEndPoint = string.Format("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={0}", accessToken);
                    break;
                case Stripe:
                    const string UserInfoEndpoint = "https://api.stripe.com/v1/account";
                    HttpRequestMessage userRequest;
                    var httpClient = getWebClient(accessToken, UserInfoEndpoint, out userRequest);

                    HttpResponseMessage graphResponse = await httpClient.SendAsync(userRequest);
                    graphResponse.EnsureSuccessStatusCode();
                    var content = await graphResponse.Content.ReadAsStringAsync();
                    dynamic jObj = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(content);
                    parsedToken = new ParsedExternalAccessToken();

                    parsedToken.user_id = jObj["id"];
                    //parsedToken.email = jObj["email"];

                    return parsedToken;
                default:
                    break;
            }

            var client = new HttpClient();

            if (provider == StackExchange)
            {
                var handler = new HttpClientHandler();
                if (handler.SupportsAutomaticDecompression)
                    handler.AutomaticDecompression = DecompressionMethods.GZip |
                                                     DecompressionMethods.Deflate;
                client = new HttpClient(handler);
            }

            var uri = new Uri(verifyTokenEndPoint);

            if (provider.Equals(LinkedIn, StringComparison.InvariantCultureIgnoreCase))
                client.DefaultRequestHeaders.Add("x-li-format", "json");
            if (provider.Equals(GitHub, StringComparison.InvariantCultureIgnoreCase))
                client.DefaultRequestHeaders.Add("User-Agent", ClientName);
            
            var response = await client.GetAsync(uri);

            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();

               

                dynamic jObj = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(content);
                JObject profile = jObj as JObject;
                parsedToken = new ParsedExternalAccessToken();

                switch (provider)
                {
                    case LinkedIn:
                        parsedToken.user_id = jObj["id"];
                        parsedToken.userProfile = profile.ToObject<UserProfile>();
                        parsedToken.email = parsedToken.userProfile.emailAddress;
                    //if (!string.Equals(Startup.linkedinAuthOptions.ClientId, parsedToken.app_id, StringComparison.OrdinalIgnoreCase))
                    //{
                    //    return null;
                    //}
                        break;
                    case GitHub:
                        parsedToken.user_id = jObj["id"];
                        parsedToken.githubUserProfile = profile.ToObject<GitHubUserProfile>();
                        parsedToken.email = parsedToken.githubUserProfile.email;
                        break;
                    case Twitter:
                        parsedToken.user_id = jObj["id"];
                        parsedToken.twitterUserProfile = profile.ToObject<TwitterUserProfile>();
                        parsedToken.email = parsedToken.twitterUserProfile.email;
                        break;
                    case StackExchange:
                        parsedToken.stackexchangeUserProfile = profile.ToObject<Angjobs.Models.StackExchange.RootObject>().items[0];
                        parsedToken.user_id = parsedToken.stackexchangeUserProfile.user_id.ToString();
                        break;
                    case "Facebook":
                        parsedToken.user_id = jObj["data"]["user_id"];
                        parsedToken.app_id = jObj["data"]["app_id"];

                        if (!string.Equals(Startup.facebookAuthOptions.AppId, parsedToken.app_id, StringComparison.OrdinalIgnoreCase))
                        {
                            return null;
                        }
                        break;
                    case "GooglePlus":
                        parsedToken.user_id = jObj["user_id"];
                        parsedToken.app_id = jObj["audience"];
                        //parsedToken.email = jObj["email"];
                        if (!string.Equals(Startup.googleAuthOptions.ClientId, parsedToken.app_id, StringComparison.OrdinalIgnoreCase))
                        {
                            return null;
                        }
                        break;
                    default:
                        break;
                }
            }

            return parsedToken;
        }