public void StartLoginFlow(LoginOptions loginOptions)
        {
            Uri loginUri    = new Uri(OAuth2.ComputeAuthorizationUrl(loginOptions));
            Uri callbackUri = new Uri(loginOptions.CallbackUrl);

            OAuth2.ClearCookies(loginOptions);
            WebAuthenticationBroker.AuthenticateAndContinue(loginUri, callbackUri, null, WebAuthenticationOptions.None);
        }
Exemplo n.º 2
0
        private async void DoAuthFlow(LoginOptions loginOptions)
        {
            Uri loginUri    = new Uri(OAuth2.ComputeAuthorizationUrl(loginOptions));
            Uri callbackUri = new Uri(loginOptions.CallbackUrl);

            OAuth2.ClearCookies(loginOptions);
            WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, loginUri, callbackUri);

            if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
            {
                Uri          responseUri  = new Uri(webAuthenticationResult.ResponseData.ToString());
                AuthResponse authResponse = OAuth2.ParseFragment(responseUri.Fragment.Substring(1));
                PlatformAdapter.Resolve <IAuthHelper>().EndLoginFlow(loginOptions, authResponse);
            }
        }
        public void StartLoginFlow(LoginOptions loginOptions)
        {
            if (loginOptions == null || String.IsNullOrWhiteSpace(loginOptions.CallbackUrl) ||
                String.IsNullOrWhiteSpace(loginOptions.LoginUrl))
            {
                return;
            }
            try
            {
                var loginUri    = new Uri(OAuth2.ComputeAuthorizationUrl(loginOptions));
                var callbackUri = new Uri(loginOptions.CallbackUrl);
                WebAuthenticationBroker.AuthenticateAndContinue(loginUri, callbackUri, null,
                                                                WebAuthenticationOptions.None);
            }
            catch (Exception ex)
            {
                PlatformAdapter.SendToCustomLogger("AccountPage.StartLoginFlow - Exception occured", LoggingLevel.Critical);
                PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical);

                PlatformAdapter.Resolve <IAuthHelper>().StartLoginFlow();
            }
        }
Exemplo n.º 4
0
        private async void DoAuthFlow(LoginOptions loginOptions)
        {
            loginOptions.DisplayType = LoginOptions.DefaultStoreDisplayType;
            var loginUri    = new Uri(OAuth2.ComputeAuthorizationUrl(loginOptions));
            var callbackUri = new Uri(loginOptions.CallbackUrl);

            OAuth2.ClearCookies(loginOptions);
            WebAuthenticationResult webAuthenticationResult;

            try
            {
                PlatformAdapter.SendToCustomLogger(
                    "AccountPage.DoAuthFlow - calling WebAuthenticationBroker.AuthenticateAsync()", LoggingLevel.Verbose);
                if (loginOptions.UseTwoParamAuthAsyncMethod)
                {
                    webAuthenticationResult =
                        await
                        WebAuthenticationBroker.AuthenticateAsync(loginOptions.BrokerOptions, loginUri);
                }
                else
                {
                    webAuthenticationResult =
                        await
                        WebAuthenticationBroker.AuthenticateAsync(loginOptions.BrokerOptions, loginUri, callbackUri);
                }
            }
            // If a bad URI was passed in the user is shown an error message by the WebAuthenticationBroken, when user
            // taps back arrow we are then thrown a FileNotFoundException, but since user already saw error message we
            // should just swallow that exception
            catch (FileNotFoundException)
            {
                SetupAccountPage();
                return;
            }
            catch (Exception ex)
            {
                PlatformAdapter.SendToCustomLogger("AccountPage.StartLoginFlow - Exception occured", LoggingLevel.Critical);
                PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical);

                DisplayErrorDialog(LocalizedStrings.GetString("generic_error"));
                SetupAccountPage();
                return;
            }

            if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
            {
                var responseUri = new Uri(webAuthenticationResult.ResponseData);
                if (!String.IsNullOrWhiteSpace(responseUri.Query) &&
                    responseUri.Query.IndexOf("error", StringComparison.CurrentCultureIgnoreCase) >= 0)
                {
                    DisplayErrorDialog(LocalizedStrings.GetString("generic_authentication_error"));
                    SetupAccountPage();
                }
                else
                {
                    try
                    {
                        AuthResponse authResponse = OAuth2.ParseFragment(responseUri.Fragment.Substring(1));
                        PlatformAdapter.SendToCustomLogger("AccountPage.DoAuthFlow - calling EndLoginFlow()", LoggingLevel.Verbose);
                        await PlatformAdapter.Resolve <IAuthHelper>().EndLoginFlow(loginOptions, authResponse);
                    }
                    catch (Exception ex)
                    {
                        DisplayErrorDialog($"Login failed: { ex.Message }");
                        SetupAccountPage();
                    }
                }
            }
            else if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.UserCancel)
            {
                SetupAccountPage();
            }
            else
            {
                DisplayErrorDialog(LocalizedStrings.GetString("generic_error"));
                SetupAccountPage();
            }
        }
        private async void DoAuthFlow(LoginOptions loginOptions)
        {
            loginOptions.DisplayType = LoginOptions.DefaultStoreDisplayType;
            var loginUri    = new Uri(OAuth2.ComputeAuthorizationUrl(loginOptions));
            var callbackUri = new Uri(loginOptions.CallbackUrl);
            await SDKServiceLocator.Get <IAuthHelper>().ClearCookiesAsync(loginOptions);

            WebAuthenticationResult webAuthenticationResult = null;
            var hasWebAuthErrors = false;

            try
            {
                LoggingService.Log("Launching web authentication broker", LoggingLevel.Verbose);

                webAuthenticationResult =
                    await
                    WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, loginUri, callbackUri);
            }
            // If a bad URI was passed in the user is shown an error message by the WebAuthenticationBroken, when user
            // taps back arrow we are then thrown a FileNotFoundException, but since user already saw error message we
            // should just swallow that exception
            catch (FileNotFoundException)
            {
                SetupAccountPage();
                return;
            }
            catch (Exception ex)
            {
                LoggingService.Log("Exception occurred during login flow", LoggingLevel.Critical);
                LoggingService.Log(ex, LoggingLevel.Critical);

                hasWebAuthErrors = true;
            }

            if (hasWebAuthErrors)
            {
                await DisplayErrorDialogAsync(LocalizedStrings.GetString("generic_error"));

                SetupAccountPage();
                return;
            }

            if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
            {
                var responseUri = new Uri(webAuthenticationResult.ResponseData);
                if (!String.IsNullOrWhiteSpace(responseUri.Query) &&
                    responseUri.Query.IndexOf("error", StringComparison.CurrentCultureIgnoreCase) >= 0)
                {
                    await DisplayErrorDialogAsync(LocalizedStrings.GetString("generic_authentication_error"));

                    SetupAccountPage();
                }
                else
                {
                    AuthResponse authResponse = OAuth2.ParseFragment(responseUri.Fragment.Substring(1));

                    await SDKServiceLocator.Get <IAuthHelper>().OnLoginCompleteAsync(loginOptions, authResponse);
                }
            }
            else if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.UserCancel)
            {
                SetupAccountPage();
            }
            else
            {
                await DisplayErrorDialogAsync(LocalizedStrings.GetString("generic_error"));

                SetupAccountPage();
            }
        }