Esempio n. 1
0
        private void DisplayUI(WebAuthenticator auth)
        {
#if __ANDROID__
            Forms.Context.StartActivity(auth.GetUI(Forms.Context));
#endif
#if __IOS__
            SocialLogin.iOS.AppDelegate.GetController().PresentViewController(auth.GetUI(), true, null);
#endif
        }
        protected override Task <string> GetAuthorizationCodeAsync()
        {
            var tcs = new TaskCompletionSource <string>();

            if (CurrentAuthenticator != null)
            {
                tcs.TrySetException(new InvalidOperationException("Authentication is already in progress."));
                return(tcs.Task);
            }

            CurrentAuthenticator = new WebRedirectAuthenticator(LoginUri, CallbackUri)
            {
                IsUsingNativeUI         = true, // Xamarin.Auth takes care about fallback
                ClearCookiesBeforeLogin = false
            };

            Intent intent = CurrentAuthenticator.GetUI(this.context);

            CurrentAuthenticator.Error += (sender, e) =>
            {
                string message = String.Format(CultureInfo.InvariantCulture, "Authentication failed with HTTP response code {0}.", e.Message);
                InvalidOperationException ex = (e.Exception == null)
                    ? new InvalidOperationException(message)
                    : new InvalidOperationException(message, e.Exception);

                tcs.TrySetException(ex);
                CurrentAuthenticator = null;
            };

            CurrentAuthenticator.Completed += (sender, e) =>
            {
                string authorizationCode;
                if (!e.IsAuthenticated)
                {
                    tcs.TrySetException(new InvalidOperationException("Authentication was cancelled by the user."));
                }
                else if (!e.Account.Properties.TryGetValue("authorization_code", out authorizationCode))
                {
                    tcs.TrySetException(new InvalidOperationException("Authentication failed: could not found \"authorization_code\"."));
                }
                else
                {
                    tcs.TrySetResult(authorizationCode);
                }
                CurrentAuthenticator = null;
            };

            context.StartActivity(intent);

            return(tcs.Task);
        }
Esempio n. 3
0
        private void SetupFacebook()
        {
            FacebookButton.TouchUpInside += delegate {
                Auth = new OAuth2Authenticator("990519181055992", "email",
                                               new Uri("https://www.facebook.com/v2.0/dialog/oauth/"),
                                               new Uri("http://www.facebook.com/connect/login_success.html"),
                                               null, false);

                Auth.Completed += OnFacebookAuthCompleted;

                var viewController = Auth.GetUI();
                PresentViewController(viewController, true, null);
            };
        }
Esempio n. 4
0
        private void SetupGoogle()
        {
            GoogleButton.TouchUpInside += delegate {
                Auth = new OAuth2Authenticator("742705120071-hkdhvr100f26g8agacuqm54a924eblf9.apps.googleusercontent.com", "", "https://www.googleapis.com/auth/userinfo.email",
                                               new Uri("https://accounts.google.com/o/oauth2/v2/auth"),
                                               new Uri("org.amffrance.finquiz:/oauth2redirect"),
                                               new Uri("https://www.googleapis.com/oauth2/v4/token"),
                                               null, true);

                Auth.Completed += OnGoogleAuthCompleted;

                var viewController = Auth.GetUI();
                PresentViewController(viewController, true, null);
            };
        }
Esempio n. 5
0
        private void SetupTwitter()
        {
            TwitterButton.TouchUpInside += delegate {
                Auth = new OAuth1Authenticator("6ozsrJfgvSe58Pl45MLk65PfH", "tneY3Wf4hxpFM8UpuSvw9bhy6P9vF11KUS731Suk2xmkGPUZgB",
                                               new Uri("https://api.twitter.com/oauth/request_token"),
                                               new Uri("https://api.twitter.com/oauth/authorize"),
                                               new Uri("https://api.twitter.com/oauth/access_token"),
                                               new Uri("https://mobile.twitter.com"),
                                               null, false);

                Auth.Completed += OnTwitterAuthCompleted;

                var viewController = Auth.GetUI();
                PresentViewController(viewController, true, null);
            };
        }
Esempio n. 6
0
        private void SetupFacebook()
        {
            var facebookButton = FindViewById <Button>(Resource.Id.facebookButton);

            facebookButton.Click += delegate {
                Auth = new OAuth2Authenticator("990519181055992", "email",
                                               new Uri("https://www.facebook.com/v2.0/dialog/oauth/"),
                                               new Uri("http://www.facebook.com/connect/login_success.html"),
                                               null, false);

                Auth.Completed += OnFacebookAuthCompleted;

                var intent = Auth.GetUI(this);
                StartActivity(intent);
            };
        }
Esempio n. 7
0
        private void SetupTwitter()
        {
            var twitterButton = FindViewById <Button>(Resource.Id.twitterButton);

            twitterButton.Click += delegate {
                Auth = new OAuth1Authenticator("6ozsrJfgvSe58Pl45MLk65PfH", "tneY3Wf4hxpFM8UpuSvw9bhy6P9vF11KUS731Suk2xmkGPUZgB",
                                               new Uri("https://api.twitter.com/oauth/request_token"),
                                               new Uri("https://api.twitter.com/oauth/authorize"),
                                               new Uri("https://api.twitter.com/oauth/access_token"),
                                               new Uri("https://mobile.twitter.com"),
                                               null, false);

                Auth.Completed += OnTwitterAuthCompleted;

                var intent = Auth.GetUI(this);
                StartActivity(intent);
            };
        }
Esempio n. 8
0
        private void SetupGoogle()
        {
            var googleButton = FindViewById <Button>(Resource.Id.googleButton);

            googleButton.Click += delegate {
                Auth = new OAuth2Authenticator("742705120071-bep44ot1uchpo1mlhhcr242kh84ehgb1.apps.googleusercontent.com", "", "https://www.googleapis.com/auth/userinfo.email",
                                               new Uri("https://accounts.google.com/o/oauth2/v2/auth"),
                                               new Uri("org.amffrance.finquiz:/oauth2redirect"),
                                               new Uri("https://www.googleapis.com/oauth2/v4/token"),
                                               null, true);

                Auth.Completed += OnGoogleAuthCompleted;
                Auth.Error     += (s, e) =>
                {
                    var alertBuilder = new AlertDialog.Builder(this);
                    alertBuilder.SetTitle(e.Message);
                    alertBuilder.SetMessage(e.Exception.ToString());
                    alertBuilder.Create().Show();
                };

                var intent = Auth.GetUI(this);
                StartActivity(intent);
            };
        }
        protected override Task <string> GetAuthorizationCodeAsync()
        {
            var tcs = new TaskCompletionSource <string>();

            if (CurrentAuthenticator != null)
            {
                tcs.TrySetException(new InvalidOperationException("Authentication is already in progress."));
                return(tcs.Task);
            }

            CurrentAuthenticator = new WebRedirectAuthenticator(LoginUri, CallbackUri)
            {
                IsUsingNativeUI         = ObjCRuntime.Class.GetHandle("SFSafariViewController") != IntPtr.Zero,
                ClearCookiesBeforeLogin = false
            };

            UIViewController c = CurrentAuthenticator.GetUI();

            UIViewController    controller = null;
            UIPopoverController popover    = null;

            CurrentAuthenticator.Error += (o, e) =>
            {
                NSAction completed = () =>
                {
                    Exception ex = e.Exception ?? new Exception(e.Message);
                    tcs.TrySetException(ex);
                };

                if (controller != null)
                {
                    controller.DismissViewController(true, completed);
                }

                if (popover != null)
                {
                    popover.Dismiss(true);
                    completed();
                }
                CurrentAuthenticator = null;
            };

            CurrentAuthenticator.Completed += (o, e) =>
            {
                NSAction completed = () =>
                {
                    string authorizationCode;
                    if (!e.IsAuthenticated)
                    {
                        tcs.TrySetException(new InvalidOperationException("Authentication was cancelled by the user."));
                    }
                    else if (!e.Account.Properties.TryGetValue("authorization_code", out authorizationCode))
                    {
                        tcs.TrySetException(new InvalidOperationException("Authentication failed: could not found \"authorization_code\"."));
                    }
                    else
                    {
                        tcs.TrySetResult(authorizationCode);
                    }
                };

                if (controller != null)
                {
                    controller.DismissViewController(true, completed);
                }

                if (popover != null)
                {
                    popover.Dismiss(true);
                    completed();
                }
                CurrentAuthenticator = null;
            };

            controller = view as UIViewController;
            if (controller != null)
            {
                controller.PresentViewController(c, true, null);
            }
            else
            {
                UIView          v         = view as UIView;
                UIBarButtonItem barButton = view as UIBarButtonItem;

                popover = new UIPopoverController(c);

                if (barButton != null)
                {
                    popover.PresentFromBarButtonItem(barButton, UIPopoverArrowDirection.Any, true);
                }
                else
                {
                    popover.PresentFromRect(rect, v, UIPopoverArrowDirection.Any, true);
                }
            }

            return(tcs.Task);
        }