Exemplo n.º 1
0
        public async void CallService()
        {
            string results = "";

            if (authResponse == null)
            {
                results = "_authResponse == null";
                OnAuth?.Invoke(results);
                return;
            }

            HttpResponseMessage response;

            //var baseAddress = new Uri(Constants.GetHost);
            using (var handler = new HttpClientHandler {
                UseCookies = false
            })
                using (var client = new HttpClient(handler)
                {
                    BaseAddress = ConnectionSettings.RedirectUri
                })
                {
                    var message = new HttpRequestMessage(HttpMethod.Get, ConnectionSettings.ServicePath);
                    client.SetBearerToken(authResponse.AccessToken);
                    try
                    {
                        response = await client.SendAsync(message);

                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            var json = await response.Content.ReadAsStringAsync();

                            //resultsText.Text = JArray.Parse(json).ToString();
                            results = json;
                        }
                        else
                        {
                            results = response.StatusCode.ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        results = ex.ToString();
                    }
                }
            OnAuth?.Invoke(results);
        }
Exemplo n.º 2
0
        void AuthPage_OnLogin(string url)
        {
            // parse response
            authResponse = new AuthorizeResponse(url);

            // CSRF check
            if (authResponse.Values.ContainsKey("state"))
            {
                var state = authResponse.Values["state"];
                if (state.Equals(currentCSRFToken))
                {
                    OnAuth?.Invoke(authResponse.AccessToken);
                    return;
                }
            }
            OnAuth?.Invoke("CSRF!");
        }
        public override void HandleGetRequest(HttpProcessor p)
        {
            p.WriteSuccess();
            if (p.HttpUrl == "/favicon.ico")
            {
                return;
            }

            Thread t;

            if (_type == AuthType.Authorization)
            {
                string url = p.HttpUrl;
                url = url.Substring(2, url.Length - 2);
                NameValueCollection col = HttpUtility.ParseQueryString(url);
                if (col.Keys.Get(0) != "code")
                {
                    p.OutputStream.WriteLine("<html><body><h1>Spotify Auth canceled!</h1></body></html>");
                    t = new Thread(o =>
                    {
                        OnAuth?.Invoke(new AuthEventArgs()
                        {
                            State = col.Get(1),
                            Error = col.Get(0),
                        });
                    });
                }
                else
                {
                    p.OutputStream.WriteLine("<html><body><h1>Spotify Auth successful!</h1><script>window.close();</script></body></html>");
                    t = new Thread(o =>
                    {
                        OnAuth?.Invoke(new AuthEventArgs()
                        {
                            Code  = col.Get(0),
                            State = col.Get(1)
                        });
                    });
                }
            }
            else
            {
                if (p.HttpUrl == "/")
                {
                    p.OutputStream.WriteLine("<html><body>" +
                                             "<script>" +
                                             "" +
                                             "var hashes = window.location.hash;" +
                                             "hashes = hashes.replace('#','&');" +
                                             "window.location = hashes" +
                                             "</script>" +
                                             "<h1>Spotify Auth successful!<br>Please copy the URL and paste it into the application</h1></body></html>");
                    p.OutputStream.Flush();
                    p.OutputStream.Close();
                    return;
                }
                string url = p.HttpUrl;
                url = url.Substring(2, url.Length - 2);
                NameValueCollection col = HttpUtility.ParseQueryString(url);
                if (col.Keys.Get(0) != "access_token")
                {
                    p.OutputStream.WriteLine("<html><body><h1>Spotify Auth canceled!</h1></body></html>");
                    t = new Thread(o =>
                    {
                        OnAuth?.Invoke(new AuthEventArgs()
                        {
                            Error = col.Get(0),
                            State = col.Get(1)
                        });
                    });
                }
                else
                {
                    p.OutputStream.WriteLine("<html><body><h1>Spotify Auth successful!</h1><script>window.close();</script></body></html>");
                    t = new Thread(o =>
                    {
                        OnAuth?.Invoke(new AuthEventArgs()
                        {
                            Code      = col.Get(0),
                            TokenType = col.Get(1),
                            ExpiresIn = Convert.ToInt32(col.Get(2)),
                            State     = col.Get(3)
                        });
                    });
                    p.OutputStream.Flush();
                    p.OutputStream.Close();
                }
            }


            t.Start();
        }