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);
            }
        }
        /// <summary>
        ///     Logs currently authenticated user out by deleting locally persisted credentials and invoking the server to revoke
        ///     the user auth tokens
        /// </summary>
        /// <returns>true if server logout was successful</returns>
        public async Task <bool> Logout()
        {
            Account account = AccountManager.GetAccount();

            if (account != null)
            {
                LoginOptions options = account.GetLoginOptions();
                AccountManager.DeleteAccount();
                OAuth2.ClearCookies(options);
                bool loggedOut = await OAuth2.RevokeAuthToken(options, account.RefreshToken);

                if (loggedOut)
                {
                    GetRestClient();
                }
                return(loggedOut);
            }
            GetRestClient();
            return(await Task.Factory.StartNew(() => true));
        }
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();
            }
        }