void BeginLoadingInitialUrl()
        {
            authenticator.GetInitialUrlAsync().ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    keepTryingAfterError = false;
                    authenticator.OnError(t.Exception);
                }
                else
                {
                    // Delete cookies so we can work with multiple accounts
                    if (this.authenticator.ClearCookiesBeforeLogin)
                    {
                        WebAuthenticator.ClearCookies();
                    }

                    //
                    // Begin displaying the page
                    //
                    Uri uri = t.Result;
                    LoadInitialUrl(uri);
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            authenticator = (OAuth2Authenticator)e.Parameter;

            url_initial = this.authenticator.GetInitialUrlAsync().Result;
            this.browser.Navigate(url_initial);

            System.Diagnostics.Debug.WriteLine("OnNavigatedTo authenticator = " + authenticator.Title);

            authenticator.Completed += auth_Completed;
            authenticator.Error     += auth_Error;

            url_initial = await authenticator.GetInitialUrlAsync();

            base.OnNavigatedTo(e);

            return;
        }
Esempio n. 3
0
        public WebAuthenticatorController(WebAuthenticator authenticator)
        {
            this.authenticator = authenticator;

            authenticator.Error             += HandleError;
            authenticator.BrowsingCompleted += HandleBrowsingCompleted;

            //
            // Create the UI
            //
            Title = authenticator.Title;

            if (authenticator.AllowCancel)
            {
                NavigationItem.LeftBarButtonItem = new UIBarButtonItem(
                    UIBarButtonSystemItem.Cancel,
                    delegate {
                    Cancel();
                });
            }

            var activityStyle = UIActivityIndicatorViewStyle.White;

            if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0))
            {
                activityStyle = UIActivityIndicatorViewStyle.Gray;
            }

            activity = new UIActivityIndicatorView(activityStyle);
            NavigationItem.RightBarButtonItem = new UIBarButtonItem(activity);

            webView = new UIWebView(View.Bounds)
            {
                Delegate         = new WebViewDelegate(this),
                AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight,
            };
            View.AddSubview(webView);
            View.BackgroundColor = UIColor.Black;

            //
            // Locate our initial URL
            //
            BeginLoadingInitialUrl();
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            //
            // Load the state either from a configuration change or from the intent.
            //
            state = LastNonConfigurationInstance as State;
            if (state == null && Intent.HasExtra("StateKey"))
            {
                var stateKey = Intent.GetStringExtra("StateKey");
                state = StateRepo.Remove(stateKey);
            }
            if (state == null)
            {
                Finish();
                return;
            }

            Title = state.Authenticator.Title;

            //
            // Watch for completion
            //
            state.Authenticator.Completed +=
                (s, e) =>
            {
                SetResult(e.IsAuthenticated ? Result.Ok : Result.Canceled);

                #region
                ///-------------------------------------------------------------------------------------------------
                /// Pull Request - manually added/fixed
                ///		Added IsAuthenticated check #88
                ///		https://github.com/xamarin/Xamarin.Auth/pull/88
                if (e.IsAuthenticated)
                {
                    if (state.Authenticator.GetAccountResult != null)
                    {
                        var accountResult = state.Authenticator.GetAccountResult(e.Account);

                        Bundle result = new Bundle();
                        result.PutString(global::Android.Accounts.AccountManager.KeyAccountType, accountResult.AccountType);
                        result.PutString(global::Android.Accounts.AccountManager.KeyAccountName, accountResult.Name);
                        result.PutString(global::Android.Accounts.AccountManager.KeyAuthtoken, accountResult.Token);
                        result.PutString(global::Android.Accounts.AccountManager.KeyAccountAuthenticatorResponse, e.Account.Serialize());

                        SetAccountAuthenticatorResult(result);
                    }
                }
                ///-------------------------------------------------------------------------------------------------
                #endregion

                Finish();
            };

            state.Authenticator.Error +=
                (s, e) =>
            {
                if (!state.Authenticator.ShowErrors)
                {
                    return;
                }

                if (e.Exception != null)
                {
                    this.ShowError("Authentication Error e.Exception = ", e.Exception);
                }
                else
                {
                    this.ShowError("Authentication Error e.Message = ", e.Message);
                }
                BeginLoadingInitialUrl();
            };

            //---------------------------------------------------------------------------------
            //
            // Build the UI
            //
            webView = new WebView(this)
            {
                Id = 42,
            };
            webView.Settings.JavaScriptEnabled = true;
            webView.SetWebViewClient(new Client(this));
            SetContentView(webView);
            //---------------------------------------------------------------------------------

            //
            // Restore the UI state or start over
            //
            if (savedInstanceState != null)
            {
                webView.RestoreState(savedInstanceState);
            }
            else
            {
                if (Intent.GetBooleanExtra("ClearCookies", true))
                {
                    WebAuthenticator.ClearCookies();
                }

                BeginLoadingInitialUrl();
            }

            return;
        }
Esempio n. 5
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            //
            // Load the state either from a configuration change or from the intent.
            //
            state = LastNonConfigurationInstance as State;
            if (state == null && Intent.HasExtra("StateKey"))
            {
                var stateKey = Intent.GetStringExtra("StateKey");
                state = StateRepo.Remove(stateKey);
            }
            if (state == null)
            {
                Finish();
                return;
            }

            Title = state.Authenticator.Title;

            //
            // Watch for completion
            //
            state.Authenticator.Completed += (s, e) => {
                SetResult(e.IsAuthenticated ? Result.Ok : Result.Canceled);
                Finish();
            };
            state.Authenticator.Error += (s, e) => {
                if (e.Exception != null)
                {
                    this.ShowError("Authentication Error", e.Exception);
                }
                else
                {
                    this.ShowError("Authentication Error", e.Message);
                }
                BeginLoadingInitialUrl();
            };

            //
            // Build the UI
            //
            webView = new WebView(this)
            {
                Id = 42,
            };
            webView.Settings.JavaScriptEnabled = true;
            webView.SetWebViewClient(new Client(this));
            SetContentView(webView);

            //
            // Restore the UI state or start over
            //
            if (savedInstanceState != null)
            {
                webView.RestoreState(savedInstanceState);
            }
            else
            {
                if (Intent.GetBooleanExtra("ClearCookies", true))
                {
                    WebAuthenticator.ClearCookies();
                }

                BeginLoadingInitialUrl();
            }
        }
        public WebAuthenticatorController(WebAuthenticator authenticator, bool is_using_wkwebview)
        {
            WebViewConfiguration.IOS.IsUsingWKWebView = is_using_wkwebview;

            this.authenticator = authenticator;

            authenticator.Error             += HandleError;
            authenticator.BrowsingCompleted += HandleBrowsingCompleted;

            //
            // Create the UI
            //
            if (authenticator.AllowCancel)
            {
                NavigationItem.LeftBarButtonItem =
                    new UIBarButtonItem
                    (
                        UIBarButtonSystemItem.Cancel,
                        delegate
                {
                    Cancel();
                    #region
                    //---------------------------------------------------------------------------------------
                    /// Pull Request - manually added/fixed
                    ///		OAuth2Authenticator changes to work with joind.in OAuth #91
                    ///		https://github.com/xamarin/Xamarin.Auth/pull/91
                    ///
                    DismissViewControllerAsync(true);
                    ///---------------------------------------------------------------------------------------
                    #endregion
                }
                    );
            }

            activity = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.White);
            NavigationItem.RightBarButtonItem = new UIBarButtonItem(activity);

            if (WebViewConfiguration.IOS.IsUsingWKWebView == false)
            {
                #if DEBUG
                StringBuilder sb1 = new StringBuilder();
                sb1.Append("Embedded WebView using - UIWebView");
                System.Diagnostics.Debug.WriteLine(sb1.ToString());
                #endif

                ui_web_view = new UIWebView(View.Bounds)
                {
                    Delegate         = new UIWebViewDelegate(this),
                    AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight,
                };
                web_view = ui_web_view;
                View.AddSubview((UIWebView)web_view);
            }
            else
            {
                #if DEBUG
                StringBuilder sb1 = new StringBuilder();
                sb1.Append("Embedded WebView using - WKWebView");
                System.Diagnostics.Debug.WriteLine(sb1.ToString());
                #endif

                var wk_web_view_configuration = new WebKit.WKWebViewConfiguration();

                if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
                {
                    wk_web_view_configuration.WebsiteDataStore = WKWebsiteDataStore.NonPersistentDataStore;
                }

                wk_web_view = new WebKit.WKWebView(View.Frame, wk_web_view_configuration)
                {
                    UIDelegate         = new WKWebViewUIDelegate(this),
                    NavigationDelegate = new WKWebViewNavigationDelegate(this),
                    AutoresizingMask   = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight,
                    //  cheating!
                    //  http://www.useragentstring.com/pages/useragentstring.php?typ=Browser
                    CustomUserAgent = WebViewConfiguration.IOS.UserAgent,
                };
                web_view = wk_web_view;
                View.AddSubview((WKWebView)web_view);
            }
            #if DEBUG
            authenticator.Title = "Auth " + web_view.GetType().ToString();
            #endif

            Title = authenticator.Title;
            View.BackgroundColor = UIColor.Black;

            // InvalidOperation - either delegates or events!
            //this.webView.LoadFinished += WebView_LoadFinished;
            //
            // Locate our initial URL
            //
            BeginLoadingInitialUrl();

            #if DEBUG
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"WebAuthenticatorController ");
            sb.AppendLine($"        WebViewConfiguration.IsUsingWKWebView = {WebViewConfiguration.IOS.IsUsingWKWebView}");
            sb.AppendLine($"        authenticator.IsUsingNativeUI         = {authenticator.IsUsingNativeUI}");
            sb.AppendLine($"        authenticator.Title                   = {authenticator.Title}");
            System.Diagnostics.Debug.WriteLine(sb.ToString());
            #endif

            return;
        }
 public WebAuthenticatorController(WebAuthenticator authenticator)
     : this(authenticator, WebViewConfiguration.IOS.IsUsingWKWebView)
 {
     return;
 }
Esempio n. 8
0
        public NativeAuthSafariViewControllerDelegate(WebAuthenticator wa)
        {
            authenticator = wa;

            return;
        }
Esempio n. 9
0
            /// <summary>
            /// Whether the UIWebView should begin loading data.
            /// </summary>
            /// <returns><c>true</c>, if start load was shoulded, <c>false</c> otherwise.</returns>
            /// <param name="webView">Web view.</param>
            /// <param name="request">Request.</param>
            /// <param name="navigationType">Navigation type.</param>
            public override bool ShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
            {
                NSUrl nsUrl = request.Url;

                string msg = null;

                #if DEBUG
                StringBuilder sb = new StringBuilder();
                sb.AppendLine($"UIWebViewDelegate.ShouldStartLoad ");
                sb.AppendLine($"        nsUrl.AbsoluteString = {nsUrl.AbsoluteString}");
                System.Diagnostics.Debug.WriteLine(sb.ToString());
                #endif

                WebAuthenticator         wa  = null;
                WebRedirectAuthenticator wra = null;

                wa  = this.controller.authenticator as WebAuthenticator;
                wra = this.controller.authenticator as WebRedirectAuthenticator;

                #if DEBUG
                if (wa != null)
                {
                    msg = String.Format("WebAuthenticatorController.authenticator as WebAuthenticator");
                    System.Diagnostics.Debug.WriteLine(msg);
                }
                if (wra != null)
                {
                    msg = String.Format("WebAuthenticatorController.authenticator as WebRedirectAuthenticator");
                    System.Diagnostics.Debug.WriteLine(msg);
                }

                msg = String.Format("WebAuthenticatorController.ShouldStartLoad {0}", nsUrl.AbsoluteString);
                System.Diagnostics.Debug.WriteLine(msg);
                #endif

                bool is_loadable_url = false;
                if (nsUrl != null && !controller.authenticator.HasCompleted)
                {
                    Uri url;
                    if (Uri.TryCreate(nsUrl.AbsoluteString, UriKind.Absolute, out url))
                    {
                        string host   = url.Host.ToLower();
                        string scheme = url.Scheme;

                        #if DEBUG
                        msg = String.Format("WebAuthenticatorController.ShouldStartLoad {0}", url.AbsoluteUri);
                        System.Diagnostics.Debug.WriteLine(msg);
                        msg = string.Format("                          Host   = {0}", host);
                        System.Diagnostics.Debug.WriteLine(msg);
                        msg = string.Format("                          Scheme = {0}", scheme);
                        System.Diagnostics.Debug.WriteLine(msg);
                        #endif

                        if (host == "localhost" || host == "127.0.0.1" || host == "::1")
                        {
                            is_loadable_url = false;
                            this.controller.DismissViewControllerAsync(true);
                        }
                        else
                        {
                            is_loadable_url = true;
                        }

                        controller.authenticator.OnPageLoading(url);
                    }
                }

                if (wra != null)
                {
                    // TODO: class refactoring
                    // OAuth2Authenticator is WebRedirectAuthenticator wra
                    wra.IsLoadableRedirectUri = is_loadable_url;
                    return(wra.IsLoadableRedirectUri);
                }
                else if (wa != null)
                {
                    // TODO: class refactoring
                    // OAuth1Authenticator is WebRedirectAuthenticator wra
                    return(is_loadable_url);
                }

                return(false);
            }