コード例 #1
0
        /// <summary>
        /// Gets the UI for this authenticator.
        /// </summary>
        /// <returns>
        /// The UI that needs to be presented.
        /// </returns>
        protected override AuthenticateUIType GetPlatformUI()
        {
            AuthenticateUIType ui = null;

            if (this.IsUsingNativeUI == true)
            {
                Uri uri = GetInitialUrlAsync().Result;
                IDictionary <string, string> query_parts = WebEx.FormDecode(uri.Query);
                if (query_parts.ContainsKey("redirect_uri"))
                {
                    Uri    redirect_uri = new Uri(query_parts["redirect_uri"]);
                    string scheme       = redirect_uri.Scheme;
                    if (scheme.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("WARNING");
                        sb.AppendLine($"Scheme = {scheme}");
                        sb.AppendLine($"Native UI used with http[s] schema!");
                        sb.AppendLine($"Redirect URL will be loaded in Native UI!");
                        sb.AppendLine($"OAuth Data parsing might fail!");

                        ShowErrorForNativeUI(sb.ToString());
                    }
                }
                ui = GetPlatformUINative();
            }
            else
            {
                ui = GetPlatformUIEmbeddedBrowser();
            }

            return(ui);
        }
コード例 #2
0
        /// <summary>
        /// Asynchronously makes a request to the access token URL with the given parameters.
        /// </summary>
        /// <param name="queryValues">The parameters to make the request with.</param>
        /// <returns>The data provided in the response to the access token request.</returns>
        protected async Task <IDictionary <string, string> > RequestAccessTokenAsync(IDictionary <string, string> queryValues)
        {
            var query = queryValues.FormEncode();

            HttpClient client = new HttpClient();
            //var response = await client.PostAsync(accessTokenUrl.AbsoluteUri, new StringContent(query, Encoding.UTF8, "application/x-www-form-urlencoded"));
            //var text = await response.Content.ReadAsStringAsync();

            //// Parse the response
            //var data = text.Contains("{") ? WebEx.JsonDecode(text) : WebEx.FormDecode(text);

            //if (data.ContainsKey("error"))
            //{
            //    throw new AuthException("Error authenticating: " + data["error"]);
            //}
            //else if (data.ContainsKey("access_token"))
            //{
            //    return data;
            //}
            //else
            //{
            //    throw new AuthException("Expected access_token in access token response, but did not receive one.");
            //}

            //var query = queryValues.FormEncode();

            //var req = WebRequest.Create(accessTokenUrl);
            //req.Method = "POST";
            //var body = Encoding.UTF8.GetBytes(query);
            //req.ContentLength = body.Length;
            //req.ContentType = "application/x-www-form-urlencoded";
            //using (var s = req.GetRequestStream())
            //{
            //    s.Write(body, 0, body.Length);
            //}
            var taskPost = client.PostAsync(accessTokenUrl.AbsoluteUri, new StringContent(query, Encoding.UTF8, "application/x-www-form-urlencoded"));

            taskPost.Wait();
            var taskReadResponse = taskPost.Result.Content.ReadAsStringAsync();

            taskReadResponse.Wait();

            var text = taskReadResponse.Result;

            // Parse the response
            var data = text.Contains("{") ? WebEx.JsonDecode(text) : WebEx.FormDecode(text);

            if (data.ContainsKey("error"))
            {
                throw new AuthException("Error authenticating: " + data["error"]);
            }
            else if (data.ContainsKey("access_token"))
            {
                return(data);
            }
            else
            {
                throw new AuthException("Expected access_token in access token response, but did not receive one.");
            }
        }
コード例 #3
0
        /// <summary>
        /// Method that returns the initial URL to be displayed in the web browser.
        /// </summary>
        /// <returns>
        /// A task that will return the initial URL.
        /// </returns>
        public override Task <Uri> GetInitialUrlAsync()
        {
            var req = OAuth1.CreateRequest(
                "GET",
                requestTokenUrl,
                new Dictionary <string, string>()
            {
                { "oauth_callback", callbackUrl.AbsoluteUri },
            },
                consumerKey,
                consumerSecret,
                "");

            return(req.GetResponseAsync().ContinueWith(respTask => {
                var content = respTask.Result.GetResponseText();

                var r = WebEx.FormDecode(content);

                token = r["oauth_token"];
                tokenSecret = r["oauth_token_secret"];

                var url = authorizeUrl.AbsoluteUri + "?oauth_token=" + Uri.EscapeDataString(token);

                return new Uri(url);
            }));
        }
コード例 #4
0
        protected async Task <IDictionary <string, string> > RequestAccessTokenAsync(CancellationToken cancellationToken)
        {
            var query = FieldsToFormUrlEncodedContent();

            using (var client = new HttpClient(new NativeMessageHandler()))
            {
                var response = await client.PostAsync(_accessTokenUrl, query, cancellationToken);

                var contentStream = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    if (response.Content.Headers.ContentType.MediaType.Contains("html"))
                    {
                        throw new AuthException($"{response.StatusCode}: {response.ReasonPhrase}");
                    }
                }

                var data = contentStream.Contains("{")
                        ? WebEx.JsonDecode(contentStream)
                        : WebEx.FormDecode(contentStream);

                if (data.ContainsKey("access_token") || data.ContainsKey("error"))
                {
                    return(data);
                }

                throw new AuthException("Expected an access_token or error header in the response message.");
            }
        }
コード例 #5
0
 //--------------------------------------------------------------------------
 public string GetHashOfContens()
 {    //HTTPレスポンスのハッシュ値を返す
     try
     {
         HashAlgorithm HashFunc = SHA256Managed.Create();
         WebRequest    req      = WebRequest.Create(this.url);
         WebResponse   rsp      = req.GetResponse();
         Stream        stm      = rsp.GetResponseStream();
         byte[]        hash     = null;
         if (stm != null)
         {
             hash = HashFunc.ComputeHash(stm);
             BinaryReader reader = new BinaryReader(stm);
             reader.Close();
             stm.Close();
         }
         rsp.Close();
         StringBuilder sb = new StringBuilder();
         if (hash != null)
         {
             for (int i = 0; i < hash.Length; i++)
             {
                 sb.Append(hash[i].ToString("X2"));
             }
         }
         return(sb.ToString());
     }
     catch (WebException WebEx)
     {
         WebEx.ToString();
         return("");
     }
 }
コード例 #6
0
        public override Task <Uri> GetInitialUrlAsync()
        {
            var req = OAuth1.CreateRequest(
                "GET",
                requestTokenUrl,
                new Dictionary <string, string> (),
                consumerKey,
                consumerSecret,
                ""
                );

            return(req.GetResponseAsync().ContinueWith(respTask => {
                var content = respTask.Result.GetResponseText();

                var r = WebEx.FormDecode(content);

                token = r["oauth_token"];
                tokenSecret = r["oauth_token_secret"];

                string paramType = authorizeUrl.AbsoluteUri.IndexOf("?") >= 0 ? "&" : "?";

                var url = String.Format("{0}{1}oauth_token={2}&oauth_callback={3}",
                                        authorizeUrl.AbsoluteUri,
                                        paramType,
                                        Uri.EscapeDataString(token),
                                        Uri.EscapeDataString(callbackUrl.AbsoluteUri));

                return new Uri(url);
            }));
        }
        /// <summary>
        /// Asynchronously makes a request to the access token URL with the given parameters.
        /// </summary>
        /// <param name="queryValues">The parameters to make the request with.</param>
        /// <returns>The data provided in the response to the access token request.</returns>
        protected Task <IDictionary <string, string> > RequestAccessTokenAsync(IDictionary <string, string> queryValues)
        {
            var query = queryValues.FormEncode();

            var req = WebRequest.Create(accessTokenUrl);

            req.Method = "POST";
            var body = Encoding.UTF8.GetBytes(query);

            req.ContentLength = body.Length;
            req.ContentType   = "application/x-www-form-urlencoded";
            using (var s = req.GetRequestStream())
            {
                s.Write(body, 0, body.Length);
            }
            return(req.GetResponseAsync().ContinueWith(task => {
                var text = task.Result.GetResponseText();

                // Parse the response
                var data = text.Contains("{") ? WebEx.JsonDecode(text) : WebEx.FormDecode(text);

                if (data.ContainsKey("error"))
                {
                    throw new AuthException("Error authenticating: " + data["error"]);
                }
                else if (data.ContainsKey("access_token"))
                {
                    return data;
                }
                else
                {
                    throw new AuthException("Expected access_token in access token response, but did not receive one.");
                }
            }));
        }
コード例 #8
0
        /// <summary>
        /// Event handler called when a page has completed loading.
        /// </summary>
        /// <param name='url'>
        /// The URL of the page.
        /// </param>
        public override void OnPageLoaded(Uri url)
        {
            var query    = WebEx.FormDecode(url.Query);
            var fragment = WebEx.FormDecode(url.Fragment);

            OnPageEncountered(url, query, fragment);
        }
コード例 #9
0
        /// <summary>
        /// Event handler called when a new page is being loaded in the web browser.
        /// </summary>
        /// <param name='url'>
        /// The URL of the page.
        /// </param>
        /// <param name="formParams">
        /// The parsed form parameters from the HTTP message body.
        /// </param>
        public override void OnPageLoading(Uri url, IDictionary <string, string> formParams)
        {
            var query    = WebEx.FormDecode(url.Query);
            var fragment = WebEx.FormDecode(url.Fragment);

            OnPageEncountered(url, query, fragment, formParams);
        }
コード例 #10
0
        /// <summary>
        /// Asynchronously makes a request to the access token URL with the given parameters.
        /// </summary>
        /// <param name="queryValues">The parameters to make the request with.</param>
        /// <returns>The data provided in the response to the access token request.</returns>
        protected async Task <IDictionary <string, string> > RequestAccessTokenAsync(IDictionary <string, string> queryValues)
        {
            var content = new FormUrlEncodedContent(queryValues);


            HttpClient          client   = new HttpClient();
            HttpResponseMessage response = await client.PostAsync(accessTokenUrl, content).ConfigureAwait(false);

            string text = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            // Parse the response
            var data = text.Contains("{") ? WebEx.JsonDecode(text) : WebEx.FormDecode(text);

            if (data.ContainsKey("error"))
            {
                throw new AuthException("Error authenticating: " + data ["error"]);
            }
            else if (data.ContainsKey("access_token"))
            {
                return(data);
            }
            else
            {
                throw new AuthException("Expected access_token in access token response, but did not receive one.");
            }
        }
コード例 #11
0
        public void JsonDecode(string json, string arg, string value)
        {
            var    dict = WebEx.JsonDecode(json);
            string v;

            Assert.That(dict.TryGetValue(arg, out v), Is.True, "Dictionary did not contain argument '" + arg + "'");
            Assert.That(v, Is.EqualTo(value));
        }
            public override void DecidePolicy
            (
                WKWebView webView,
                WKNavigationAction navigationAction,
                Action <WKNavigationActionPolicy> decisionHandler
            )
            {
                #if DEBUG
                StringBuilder sb = new StringBuilder();
                sb.AppendLine($"WKWebViewNavigationDelegate.DecidePolicy ");
                sb.AppendLine($"        webView.Url.AbsoluteString = {webView.Url.AbsoluteString}");
                sb.AppendLine($"        navigationAction?.Request?.Url = {navigationAction?.Request?.Url}");
                sb.AppendLine($"        navigationAction?.NavigationType = {navigationAction?.NavigationType}");
                System.Diagnostics.Debug.WriteLine(sb.ToString());
                #endif

                Uri    uri      = new Uri(webView.Url.AbsoluteString);
                string fragment = uri.Fragment;

                if
                (
                    fragment.Contains("access_token")
                    ||
                    fragment.Contains("state")
                    ||
                    fragment.Contains("expires_in")
                    ||
                    fragment.Contains("error")
                )
                {
                    IDictionary <string, string> fragments = WebEx.FormDecode(uri.Fragment);

                    Account account = new Account
                                      (
                        "",
                        new Dictionary <string, string>(fragments)
                                      );
                    controller.authenticator.OnSucceeded(account);
                }
                else if
                (
                    fragment.Contains("code")
                )
                {
                    throw new NotImplementedException("code - Explicit/Server");
                }

                if (navigationAction.NavigationType == WKNavigationType.LinkActivated)
                {
                    UIApplication.SharedApplication.OpenUrl(navigationAction.Request.Url);
                    decisionHandler(WKNavigationActionPolicy.Cancel);
                    return;
                }

                // Navigation Allowed?
                // page will not load without this one!
                decisionHandler(WKNavigationActionPolicy.Allow);
            }
コード例 #13
0
        /// <summary>
        /// Event handler called when a new page is being loaded in the web browser.
        /// </summary>
        /// <param name='url'>
        /// The URL of the page.
        /// </param>
        public override bool OnPageLoading(Uri url)
        {
            var query    = WebEx.FormDecode(url.Query);
            var fragment = WebEx.FormDecode(url.Fragment);

            OnPageEncountered(url, query, fragment);

            return(true);
        }
コード例 #14
0
        /// <summary>
        /// Method that returns the initial URL to be displayed in the web browser.
        /// </summary>
        /// <returns>
        /// A task that will return the initial URL.
        /// </returns>
        public override Task <Uri> GetInitialUrlAsync()
        {
            /*
             *                  mc++
             *                  OriginalString property of the Uri object should be used instead of AbsoluteUri
             *                  otherwise trailing slash is added.
             */
            string oauth_callback_uri_absolute = callbackUrl.AbsoluteUri;
            string oauth_callback_uri_original = callbackUrl.OriginalString;

            System.Diagnostics.Debug.WriteLine("GetInitialUrlAsync callbackUrl.AbsoluteUri    = " + oauth_callback_uri_absolute);
            System.Diagnostics.Debug.WriteLine("GetInitialUrlAsync callbackUrl.OriginalString = " + oauth_callback_uri_original);

            string oauth_callback_uri = oauth_callback_uri_absolute;

            var req = OAuth1.CreateRequest
                      (
                "GET",
                requestTokenUrl,
                new Dictionary <string, string>()
            {
                { "oauth_callback", oauth_callback_uri },
            },
                consumerKey,
                consumerSecret,
                ""
                      );

            if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.WebRequest)
            {
                return(req.GetResponseAsync()
                       .ContinueWith
                       (
                           respTask =>
                {
                    var content = respTask.Result.GetResponseText();

                    var r = WebEx.FormDecode(content);

                    token = r["oauth_token"];
                    tokenSecret = r["oauth_token_secret"];

                    string paramType = authorizeUrl.AbsoluteUri.IndexOf("?") >= 0 ? "&" : "?";

                    var url = authorizeUrl.AbsoluteUri + paramType + "oauth_token=" + Uri.EscapeDataString(token);
                    return new Uri(url);
                }
                       ));
            }
            else if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.HttpClient)
            {
                throw new NotImplementedException("HttpClient implementation!");
            }

            return(null);
        }
コード例 #15
0
        Task GetAccessTokenAsync()
        {
            var requestParams = new Dictionary <string, string> {
                { "oauth_token", token }
            };

            if (verifier != null)
            {
                requestParams["oauth_verifier"] = verifier;
            }

            var req = OAuth1.CreateRequest(
                "GET",
                accessTokenUrl,
                requestParams,
                consumerKey,
                consumerSecret,
                tokenSecret);

            if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.WebRequest)
            {
                return(req.GetResponseAsync().ContinueWith(respTask =>
                {
                    var content = respTask.Result.GetResponseText();

                    var accountProperties = WebEx.FormDecode(content);
                    accountProperties["oauth_consumer_key"] = consumerKey;
                    accountProperties["oauth_consumer_secret"] = consumerSecret;

                    if (getUsernameAsync != null)
                    {
                        getUsernameAsync(accountProperties).ContinueWith(uTask =>
                        {
                            if (uTask.IsFaulted)
                            {
                                OnError(uTask.Exception);
                            }
                            else
                            {
                                OnSucceeded(uTask.Result, accountProperties);
                            }
                        });
                    }
                    else
                    {
                        OnSucceeded("", accountProperties);
                    }
                }));
            }
            else if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.HttpClient)
            {
                throw new NotImplementedException("HttpClient implementation!");
            }

            return(null);
        }
コード例 #16
0
        protected override Task <string> GetUsernameAsync(IDictionary <string, string> accountProperties)
        {
            var request = base.CreateRequest("GET",
                                             new Uri("https://api.dropbox.com/1/account/info"),
                                             new Account(string.Empty, accountProperties));

            return(request.GetResponseAsync().ContinueWith(reqTask => {
                var responseText = reqTask.Result.GetResponseText();
                return WebEx.GetValueFromJson(responseText, "display_name");
            }));
        }
コード例 #17
0
        /// <summary>
        /// Event handler that watches for the callback URL to be loaded.
        /// </summary>
        /// <param name='url'>
        /// The URL of the loaded page.
        /// </param>
        public override void OnPageLoaded(Uri url)
        {
            if (url.Host == callbackUrl.Host && url.AbsolutePath == callbackUrl.AbsolutePath)
            {
                var query = url.Query;
                var r     = WebEx.FormDecode(query);

                verifier = r["oauth_verifier"];

                GetAccessTokenAsync();
            }
        }
コード例 #18
0
        /// <summary>
        /// Asynchronously makes a request to the access token URL with the given parameters.
        /// </summary>
        /// <param name="queryValues">The parameters to make the request with.</param>
        /// <returns>The data provided in the response to the access token request.</returns>
        public async Task <IDictionary <string, string> > RequestAccessTokenAsync(IDictionary <string, string> queryValues)
        {
            // mc++ changed protected to public for extension methods RefreshToken (Adrian Stevens)
            var content = new FormUrlEncodedContent(queryValues);


            HttpClient client = new HttpClient();

            //If client secret is set, use HTTP BASIC auth to authenticate to the token endpoint
            if (!string.IsNullOrEmpty(this.ClientSecret))
            {
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes($"{this.ClientId}:{this.ClientSecret}")));
            }

            HttpResponseMessage response = await client.PostAsync(accessTokenUrl, content).ConfigureAwait(false);

            string text = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            // Parse the response
            var data = text.Contains("{") ? WebEx.JsonDecode(text) : WebEx.FormDecode(text);

            if (data.ContainsKey("error"))
            {
                throw new AuthException("Error authenticating: " + data["error"]);
            }
            #region
            //---------------------------------------------------------------------------------------
            /// Pull Request - manually added/fixed
            ///		OAuth2Authenticator changes to work with joind.in OAuth #91
            ///		https://github.com/xamarin/Xamarin.Auth/pull/91
            ///
            //else if (data.ContainsKey("access_token"))
            else if (data.ContainsKey(AccessTokenName))
            //---------------------------------------------------------------------------------------
            #endregion
            {
            }
            else
            {
                #region
                //---------------------------------------------------------------------------------------
                /// Pull Request - manually added/fixed
                ///		OAuth2Authenticator changes to work with joind.in OAuth #91
                ///		https://github.com/xamarin/Xamarin.Auth/pull/91
                ///
                //throw new AuthException ("Expected access_token in access token response, but did not receive one.");
                throw new AuthException("Expected " + AccessTokenName + " in access token response, but did not receive one.");
                //---------------------------------------------------------------------------------------
                #endregion
            }

            return(data);
        }
コード例 #19
0
        /// <summary>
        /// Event handler called when a page has completed loading.
        /// </summary>
        /// <param name='url'>
        /// The URL of the page.
        /// </param>
        public override void OnPageLoaded(Uri url)
        {
            IDictionary <string, string> query    = WebEx.FormDecode(url.Query);
            IDictionary <string, string> fragment = WebEx.FormDecode(url.Fragment);

            this.Query    = query;
            this.Fragment = fragment;

            OnPageEncountered(url, query, fragment);

            return;
        }
コード例 #20
0
        /// <summary>
        /// Asynchronously makes a request to the access token URL with the given parameters.
        /// </summary>
        /// <param name="queryValues">The parameters to make the request with.</param>
        /// <returns>The data provided in the response to the access token request.</returns>
        public async Task <IDictionary <string, string> > RequestAccessTokenAsync(IDictionary <string, string> queryValues)
        {
            // mc++ changed protected to public for extension methods RefreshToken (Adrian Stevens)
            var content = new FormUrlEncodedContent(queryValues);

            HttpClient         client  = new HttpClient();
            HttpRequestMessage Message = new HttpRequestMessage(HttpMethod.Post, this.accessTokenUrl);

            Message.Content = content;
            if (this.UserAgent != null)
            {
                Message.Headers.Add(@"User-Agent", this.UserAgent);
            }
            HttpResponseMessage response = await client.SendAsync(Message).ConfigureAwait(false);

            string text = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            // Parse the response
            var data = text.Contains("{") ? WebEx.JsonDecode(text) : WebEx.FormDecode(text);

            if (data.ContainsKey("error"))
            {
                throw new AuthException("Error authenticating: " + data["error"]);
            }
            #region
            //---------------------------------------------------------------------------------------
            /// Pull Request - manually added/fixed
            ///		OAuth2Authenticator changes to work with joind.in OAuth #91
            ///		https://github.com/xamarin/Xamarin.Auth/pull/91
            ///
            //else if (data.ContainsKey("access_token"))
            else if (data.ContainsKey(AccessTokenName))
            //---------------------------------------------------------------------------------------
            #endregion
            {
            }
            else
            {
                #region
                //---------------------------------------------------------------------------------------
                /// Pull Request - manually added/fixed
                ///		OAuth2Authenticator changes to work with joind.in OAuth #91
                ///		https://github.com/xamarin/Xamarin.Auth/pull/91
                ///
                //throw new AuthException ("Expected access_token in access token response, but did not receive one.");
                throw new AuthException("Expected " + AccessTokenName + " in access token response, but did not receive one.");
                //---------------------------------------------------------------------------------------
                #endregion
            }

            return(data);
        }
コード例 #21
0
        public override Task ShareItemAsync(Item item, Account account, CancellationToken cancellationToken)
        {
            var req = CreateRequest(
                "POST",
                new Uri("http://www.flickr.com/services/upload/"),
                account);

            //
            // Add the image
            //
            item.Images.First().AddToRequest(req, "photo");

            //
            // Make the description include links
            //
            var sb = new StringBuilder();

            sb.Append(item.Text);
            if (item.Links.Count > 0)
            {
                sb.AppendLine();
                sb.AppendLine();
                foreach (var l in item.Links)
                {
                    sb.AppendFormat("<a href=\"{0}\">{0}</a>", WebEx.HtmlEncode(l.AbsoluteUri));
                    sb.AppendLine();
                }
            }
            req.AddMultipartData("description", sb.ToString());

            return(req.GetResponseAsync(cancellationToken).ContinueWith(reqTask => {
                var content = reqTask.Result.GetResponseText();

                var doc = new XmlDocument();
                doc.LoadXml(content);
                var stat = doc.DocumentElement.GetAttribute("stat");

                if (stat == "fail")
                {
                    var err = doc.DocumentElement.GetElementsByTagName("err").OfType <XmlElement> ().FirstOrDefault();
                    if (err != null)
                    {
                        throw new ApplicationException(err.GetAttribute("msg"));
                    }
                    else
                    {
                        throw new ApplicationException("Flickr returned an unknown error.");
                    }
                }
            }, cancellationToken));
        }
コード例 #22
0
        /// <summary>
        /// Asynchronously makes a request to the access token URL with the given parameters.
        /// </summary>
        /// <param name="queryValues">The parameters to make the request with.</param>
        /// <returns>The data provided in the response to the access token request.</returns>
        public async Task <IDictionary <string, string> > CustomRequestAccessTokenAsync(IDictionary <string, string> queryValues)
        {
            // mc++ changed protected to public for extension methods RefreshToken (Adrian Stevens)
            var content = new FormUrlEncodedContent(queryValues);


            HttpClient          client   = new HttpClient();
            HttpResponseMessage response = await client.PostAsync(_accessTokenUrl, content).ConfigureAwait(false);

            string text = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            try
            {
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    LogUtils.LogMessage(LogSeverity.WARNING, "CustomOAuth2Authenticator failed to refresh token.", "Error from service: " + text);
                    System.Diagnostics.Debug.Print("Error from service: " + text);
                }
            }
            catch { }

            // Parse the response
            var data = text.Contains("{") ? WebEx.JsonDecode(text) : WebEx.FormDecode(text);

            if (data.ContainsKey("error"))
            {
                throw new AuthException("Error authenticating: " + data["error"]);
            }
            //---------------------------------------------------------------------------------------
            /// Pull Request - manually added/fixed
            ///		OAuth2Authenticator changes to work with joind.in OAuth #91
            ///		https://github.com/xamarin/Xamarin.Auth/pull/91
            ///
            else if (data.ContainsKey(AccessTokenName))
            //---------------------------------------------------------------------------------------
            {
            }
            else
            {
                //---------------------------------------------------------------------------------------
                /// Pull Request - manually added/fixed
                ///		OAuth2Authenticator changes to work with joind.in OAuth #91
                ///		https://github.com/xamarin/Xamarin.Auth/pull/91
                ///
                //throw new AuthException ("Expected access_token in access token response, but did not receive one.");
                throw new AuthException("Expected " + AccessTokenName + " in access token response, but did not receive one.");
                //---------------------------------------------------------------------------------------
            }

            return(data);
        }
コード例 #23
0
        /// <summary>
        /// Event handler that watches for the callback URL to be loaded.
        /// </summary>
        /// <param name='url'>
        /// The URL of the loaded page.
        /// </param>
        public override void OnPageLoaded(Uri url)
        {
            if (
                url.Authority == callbackUrl.Authority
                //url.Host == callbackUrl.Host
                &&
                url.AbsolutePath == callbackUrl.AbsolutePath
                )
            {
                var query = url.Query;
                var r     = WebEx.FormDecode(query);

                r.TryGetValue("oauth_verifier", out verifier);

                #if DEBUG
                StringBuilder sb = new StringBuilder();
                sb.AppendLine($"OAuth1Authenticator.OnPageLoaded ");
                sb.AppendLine($"        url = {url.AbsoluteUri}");
                sb.AppendLine($"        oauth_verifier = {verifier}");
                System.Diagnostics.Debug.WriteLine(sb.ToString());
                #endif

                GetAccessTokenAsync().ContinueWith(getTokenTask =>
                {
                    if (getTokenTask.IsCanceled)
                    {
                        OnCancelled();
                    }
                    else if (getTokenTask.IsFaulted)
                    {
                        OnError(getTokenTask.Exception);
                    }
                }, TaskContinuationOptions.NotOnRanToCompletion);
            }
            else
            {
                // http[s]://www.xamarin.com != http[s]://xamarin.com
                #if DEBUG
                StringBuilder sb = new StringBuilder();
                sb.AppendLine($"OAuth1Authenticator.OnPageLoaded ");
                sb.AppendLine($"        mc++ fix");
                sb.AppendLine($"        url         = {url.AbsoluteUri}");
                sb.AppendLine($"        callbackUrl = {callbackUrl.AbsoluteUri}");
                sb.AppendLine($"        oauth_verifier = {verifier}");
                System.Diagnostics.Debug.WriteLine(sb.ToString());
                #endif
            }

            return;
        }
コード例 #24
0
        protected override Task <string> GetUsernameAsync(IDictionary <string, string> accountProperties)
        {
            var request = base.CreateRequest("GET",
                                             new Uri("https://www.googleapis.com/plus/v1/people/me"),
                                             new Dictionary <string, string> {
                { "fields", "url,id" }
            },
                                             new Account(string.Empty, accountProperties));

            return(request.GetResponseAsync().ContinueWith(reqTask => {
                var responseText = reqTask.Result.GetResponseText();
                return WebEx.GetValueFromJson(responseText, "id");
            }));
        }
コード例 #25
0
        public static async Task <string> GetRefreshTokenAsync()
        {
            byte[] buffer = new byte[32];
            Randomizer.NextBytes(buffer);
            var codeVerifier = Convert.ToBase64String(buffer).Replace('+', '-').Replace('/', '_').Replace("=", "");

            byte[] hash          = HashProvider.ComputeHash(Encoding.UTF8.GetBytes(codeVerifier));
            var    codeChallenge = Convert.ToBase64String(hash).Replace('+', '-').Replace('/', '_').Replace("=", "");

            var redirectUri = new Uri(ProdRedirectUrl);

            string scope    = $"{OfflineAccessScope} {WNSScope} {CCSScope} {UserNotificationsScope} {UserActivitiesScope} {DdsScope}";
            var    startUri = new Uri($"{ProdAuthorizeUrl}?client_id={Secrets.MSA_CLIENT_ID}&response_type=code&code_challenge_method=S256&code_challenge={codeChallenge}&redirect_uri={ProdRedirectUrl}&scope={scope}");

            var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
                WebAuthenticationOptions.None,
                startUri,
                redirectUri);

            if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
            {
                var codeResponseUri = new Uri(webAuthenticationResult.ResponseData);
                IDictionary <string, string> queryParams = WebEx.FormDecode(codeResponseUri.Query);
                if (!queryParams.ContainsKey("code"))
                {
                    return(string.Empty);
                }

                string authCode = queryParams["code"];
                Dictionary <string, string> refreshTokenQuery = new Dictionary <string, string>
                {
                    { "client_id", Secrets.MSA_CLIENT_ID },
                    { "redirect_uri", redirectUri.AbsoluteUri },
                    { "grant_type", "authorization_code" },
                    { "code", authCode },
                    { "code_verifier", codeVerifier },
                    { "scope", WNSScope }
                };

                IDictionary <string, string> refreshTokenResponse = await RequestAccessTokenAsync(ProdAccessTokenUrl, refreshTokenQuery);

                if (refreshTokenResponse.ContainsKey("refresh_token"))
                {
                    return(refreshTokenResponse["refresh_token"]);
                }
            }

            return(string.Empty);
        }
コード例 #26
0
        /// <summary>
        /// Reads all the response data and interprets it as a string.
        /// </summary>
        /// <returns>
        /// The response text.
        /// </returns>
        public virtual string GetResponseText()
        {
            var encoding = Encoding.UTF8;

            if (Headers.ContainsKey("Content-Type"))
            {
                encoding = WebEx.GetEncodingFromContentType(Headers ["Content-Type"]);
            }

            using (var s = GetResponseStream()) {
                using (var r = new StreamReader(s, encoding)) {
                    return(r.ReadToEnd());
                }
            }
        }
コード例 #27
0
        /// <summary>
        /// Asynchronously makes a request to the access token URL with the given parameters.
        /// </summary>
        /// <param name="queryValues">The parameters to make the request with.</param>
        /// <returns>The data provided in the response to the access token request.</returns>
        public async Task <IDictionary <string, string> > RequestAccessTokenAsync(IDictionary <string, string> queryValues)
        {
            // mc++ changed protected to public for extension methods RefreshToken (Adrian Stevens)
            var content = new FormUrlEncodedContent(queryValues);


            HttpClient          client   = new HttpClient();
            HttpResponseMessage response = await client.PostAsync(accessTokenUrl, content).ConfigureAwait(false);

            string text = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            // Parse the response
            var data = text.Contains("{") ? WebEx.JsonDecode(text) : WebEx.FormDecode(text);

            if (data.ContainsKey("error"))
            {
                System.Diagnostics.Debug.WriteLine($"RequestAccessTokenAsync exception {text}");//log actual response from server
                throw new AuthException("Error authenticating: " + data["error"]);
            }
            #region
            //---------------------------------------------------------------------------------------
            /// Pull Request - manually added/fixed
            ///		OAuth2Authenticator changes to work with joind.in OAuth #91
            ///		https://github.com/xamarin/Xamarin.Auth/pull/91
            ///
            //else if (data.ContainsKey("access_token"))
            else if (data.ContainsKey(AccessTokenName))
            //---------------------------------------------------------------------------------------
            #endregion
            {
            }
            else
            {
                #region
                //---------------------------------------------------------------------------------------
                /// Pull Request - manually added/fixed
                ///		OAuth2Authenticator changes to work with joind.in OAuth #91
                ///		https://github.com/xamarin/Xamarin.Auth/pull/91
                ///
                //throw new AuthException ("Expected access_token in access token response, but did not receive one.");
                throw new AuthException("Expected " + AccessTokenName + " in access token response, but did not receive one.");
                //---------------------------------------------------------------------------------------
                #endregion
            }

            return(data);
        }
コード例 #28
0
        Task GetAccessTokenAsync()
        {
            var requestparams = new Dictionary <string, string> {
                { "oauth_token", token },
            };

            if (verifier != null)
            {
                requestparams["oauth_verifier"] = verifier;
            }

            var req = OAuth1.CreateRequest(
                "GET",
                accessTokenUrl,
                requestparams,
                consumerKey,
                consumerSecret,
                tokenSecret);

            return(req.GetResponseAsync().ContinueWith(respTask => {
                var content = respTask.Result.GetResponseText();

                var accountProperties = WebEx.FormDecode(content);
                accountProperties["oauth_consumer_key"] = consumerKey;
                accountProperties["oauth_consumer_secret"] = consumerSecret;

                if (getUsernameAsync != null)
                {
                    getUsernameAsync(accountProperties).ContinueWith(uTask => {
                        if (uTask.IsFaulted)
                        {
                            OnError(uTask.Exception);
                        }
                        else
                        {
                            OnSucceeded(uTask.Result, accountProperties);
                        }
                    });
                }
                else
                {
                    OnSucceeded("", accountProperties);
                }
            }));
        }
コード例 #29
0
        /// <summary>
        /// Implements: http://tools.ietf.org/html/rfc6749#section-4.1
        /// </summary>
        /// <returns>
        /// The access token async.
        /// </returns>
        /// <param name='code'>
        /// Code.
        /// </param>
        Task <Dictionary <string, string> > RequestAccessTokenAsync(string code)
        {
            var queryValues = new Dictionary <string, string> {
                { "grant_type", "authorization_code" },
                { "code", code },
                { "redirect_uri", redirectUrl.AbsoluteUri },
                { "client_id", clientId },
            };

            if (!string.IsNullOrEmpty(clientSecret))
            {
                queryValues ["client_secret"] = clientSecret;
            }
            var query = queryValues.FormEncode();

            var req = WebRequest.Create(accessTokenUrl);

            req.Method = "POST";
            req.Proxy  = WebAuthenticator.Proxy;
            var body = Encoding.UTF8.GetBytes(query);

            req.ContentLength = body.Length;
            req.ContentType   = "application/x-www-form-urlencoded";
            using (var s = req.GetRequestStream()) {
                s.Write(body, 0, body.Length);
            }
            return(req.GetResponseAsync().ContinueWith(task => {
                var text = task.Result.GetResponseText();
                var data = WebEx.FormDecode(text);
                if (data.ContainsKey("error"))
                {
                    throw new AuthException("Error authenticating: " + data ["error"]);
                }
                else if (data.ContainsKey("access_token"))
                {
                    return data;
                }
                else
                {
                    throw new AuthException("Expected access_token in access token response, but did not receive one.");
                }
            }));
        }
コード例 #30
0
        public virtual async void OnSsoLoginLogoutRedirectCompleted(Uri url)
        {
            Dictionary <string, string> query = (Dictionary <string, string>)WebEx.FormDecode(url.Fragment);

            if (CurrentAction == "Logout")
            {
                CurrentLogoutTaskCompletionSource.SetResult(null);
            }
            else
            {
                Token token = query;

                Account account = Token.FromTokenToAccount(token);

                await _accountStore.SaveAsync(account, _clientAppProfile.AppName).ConfigureAwait(false);

                CurrentLoginTaskCompletionSource.SetResult(query);
            }
        }