Ejemplo n.º 1
0
        public static async Task <bool> SwitchToAccount(Account account)
        {
            if (account != null && account.UserId != null)
            {
                AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account);
                RestClient client = SDKManager.GlobalClientManager.PeekRestClient();
                if (client != null)
                {
                    OAuth2.ClearCookies(account.GetLoginOptions());
                    IdentityResponse identity = await OAuth2.CallIdentityService(account.IdentityUrl, client);

                    if (identity != null)
                    {
                        account.UserId   = identity.UserId;
                        account.UserName = identity.UserName;
                        account.Policy   = identity.MobilePolicy;
                        AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account);
                    }
                    OAuth2.RefreshCookies();
                    PlatformAdapter.SendToCustomLogger("AccountManager.SwitchToAccount - done, result = true", LoggingLevel.Verbose);
                    return(true);
                }
            }
            PlatformAdapter.SendToCustomLogger("AccountManager.SwitchToAccount - done, result = false", LoggingLevel.Verbose);
            return(false);
        }
 public static async Task<bool> SwitchToAccount(Account account)
 {
     if (account != null && account.UserId != null)
     {
         AuthStorageHelper.SavePinTimer();
         AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account);
         RestClient client = SDKManager.GlobalClientManager.PeekRestClient();
         if (client != null)
         {
             OAuth2.ClearCookies(account.GetLoginOptions());
             IdentityResponse identity = await OAuth2.CallIdentityService(account.IdentityUrl, client);
             if (identity != null)
             {
                 account.UserId = identity.UserId;
                 account.UserName = identity.UserName;
                 account.Policy = identity.MobilePolicy;
                 AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account);
             }
             OAuth2.RefreshCookies();
             PlatformAdapter.SendToCustomLogger("AccountManager.SwitchToAccount - done, result = true", LoggingLevel.Verbose);
             return true;
         }
     }
     PlatformAdapter.SendToCustomLogger("AccountManager.SwitchToAccount - done, result = false", LoggingLevel.Verbose);
     return false;
 }
        /// <summary>
        ///     Async method for refreshing the token, persisting the data in the encrypted settings and returning the updated
        ///     account
        ///     with the new access token.
        /// </summary>
        /// <param name="account"></param>
        /// <returns>Boolean based on if the refresh auth token succeeded or not</returns>
        public static async Task <Account> RefreshAuthToken(Account account)
        {
            if (account != null)
            {
                try
                {
                    AuthResponse response =
                        await RefreshAuthTokenRequest(account.GetLoginOptions(), account.RefreshToken);

                    account.AccessToken = response.AccessToken;
                    AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account);
                }
                catch (WebException ex)
                {
                    PlatformAdapter.SendToCustomLogger(
                        "OAuth2.RefreshAuthToken - Exception occurred when refreshing token:", LoggingLevel.Critical);
                    PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical);
                    Debug.WriteLine("Error refreshing token");
                    throw new OAuthException(ex.Message, ex.Status);
                }
                catch (Exception ex)
                {
                    PlatformAdapter.SendToCustomLogger(
                        "OAuth2.RefreshAuthToken - Exception occurred when refreshing token:", LoggingLevel.Critical);
                    PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical);
                    Debug.WriteLine("Error refreshing token");
                    throw new OAuthException(ex.Message, ex.InnerException);
                }
            }
            return(account);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Async method for refreshing the token, persisting the data in the encrypted settings and returning the updated
        ///     account
        ///     with the new access token.
        /// </summary>
        /// <param name="account"></param>
        /// <returns>Boolean based on if the refresh auth token succeeded or not</returns>
        public static async Task <Account> RefreshAuthTokenAsync(Account account)
        {
            LoggingService.Log("Atempting to refresh auth token", LoggingLevel.Verbose);

            if (account == null)
            {
                return(null);
            }

            try
            {
                var loginOptions = account.GetLoginOptions();

                // args
                var argsStr = string.Format(OauthRefreshQueryString, loginOptions.ClientId, account.RefreshToken);

                // Refresh url
                var refreshUrl = loginOptions.LoginUrl + OauthRefreshPath;

                // Post
                var call = HttpCall.CreatePost(refreshUrl, argsStr);

                var response = await call.ExecuteAndDeserializeAsync <AuthResponse>();

                account.AccessToken = response.AccessToken;
                account.IdentityUrl = response.IdentityUrl;

                await SDKServiceLocator.Get <IAuthHelper>().PersistCurrentAccountAsync(account);
            }
            catch (DeviceOfflineException ex)
            {
                LoggingService.Log("Failed to refresh the token because we were offline", LoggingLevel.Warning);
                LoggingService.Log(ex, LoggingLevel.Warning);
                throw;
            }
            catch (WebException ex)
            {
                LoggingService.Log("Exception occurred when refreshing token:", LoggingLevel.Critical);
                LoggingService.Log(ex, LoggingLevel.Critical);
                Debug.WriteLine("Error refreshing token");
                throw new OAuthException(ex.Message, ex);
            }
            catch (Exception ex)
            {
                LoggingService.Log("Exception occurred when refreshing token:", LoggingLevel.Critical);
                LoggingService.Log(ex, LoggingLevel.Critical);
                Debug.WriteLine("Error refreshing token");
                throw new OAuthException(ex.Message, ex);
            }

            return(account);
        }
Ejemplo n.º 5
0
        public static async Task <bool> SwitchToAccountAsync(Account newAccount,
                                                             CancellationToken cancellationToken = default(CancellationToken))
        {
            var oldAccount = LoggedInAccount;

            if (newAccount?.UserId != null)
            {
                // save current user pin timer
                AuthStorageHelper.SavePinTimer();

                await AuthStorageHelper.PersistCurrentAccountAsync(newAccount);

                var client = SDKManager.GlobalClientManager.PeekRestClient();
                if (client != null)
                {
                    await AuthStorageHelper.ClearCookiesAsync(newAccount.GetLoginOptions());

                    var identity =
                        await OAuth2.CallIdentityServiceAsync(newAccount.IdentityUrl, client, cancellationToken);

                    if (identity != null)
                    {
                        newAccount.UserId   = identity.UserId;
                        newAccount.UserName = identity.UserName;
                        newAccount.Policy   = identity.MobilePolicy;
                        await AuthStorageHelper.PersistCurrentAccountAsync(newAccount);
                    }
                    AuthStorageHelper.RefreshCookies();
                    LoggingService.Log("switched accounts, result = true", LoggingLevel.Verbose);
                    return(true);
                }

                // log new user in
                LoggedInAccount = newAccount;

                RaiseAuthenticatedAccountChangedEvent(oldAccount, newAccount);
            }

            LoggingService.Log("switched accounts, result = false", LoggingLevel.Verbose);
            return(false);
        }
        /// <summary>
        ///     Async method for refreshing the token, persisting the data in the encrypted settings and returning the updated
        ///     account
        ///     with the new access token.
        /// </summary>
        /// <param name="account"></param>
        /// <returns>Boolean based on if the refresh auth token succeeded or not</returns>
        public static async Task<Account> RefreshAuthTokenAsync(Account account)
        {
            LoggingService.Log("Atempting to refresh auth token", LoggingLevel.Verbose);

            if (account == null)
            {
                return null;
            }

            try
            {
                var loginOptions = account.GetLoginOptions();

                // args
                var argsStr = string.Format(OauthRefreshQueryString, loginOptions.ClientId, account.RefreshToken);

                // Refresh url
                var refreshUrl = loginOptions.LoginUrl + OauthRefreshPath;

                // Post
                var call = HttpCall.CreatePost(refreshUrl, argsStr);

                var response = await call.ExecuteAndDeserializeAsync<AuthResponse>();

                account.AccessToken = response.AccessToken;
                account.IdentityUrl = response.IdentityUrl;

                await SDKServiceLocator.Get<IAuthHelper>().PersistCurrentAccountAsync(account);
            }
            catch (DeviceOfflineException ex)
            {
                LoggingService.Log("Failed to refresh the token because we were offline", LoggingLevel.Warning);
                LoggingService.Log(ex, LoggingLevel.Warning);
                throw;
            }
            catch (WebException ex)
            {
                LoggingService.Log("Exception occurred when refreshing token:", LoggingLevel.Critical);
                LoggingService.Log(ex, LoggingLevel.Critical);
                Debug.WriteLine("Error refreshing token");
                throw new OAuthException(ex.Message, ex);
            }
            catch (Exception ex)
            {
                LoggingService.Log("Exception occurred when refreshing token:", LoggingLevel.Critical);
                LoggingService.Log(ex, LoggingLevel.Critical);
                Debug.WriteLine("Error refreshing token");
                throw new OAuthException(ex.Message, ex);
            }

            return account;
        }
        public static async Task<bool> SwitchToAccount(Account newAccount)
        {
            var oldAccount = LoggedInAccount;

            if (newAccount?.UserId != null)
            {
                // save current user pin timer
                AuthStorageHelper.SavePinTimer();

                await AuthStorageHelper.PersistCurrentAccountAsync(newAccount);

                var client = SDKManager.GlobalClientManager.PeekRestClient();
                if (client != null)
                {
                    await AuthStorageHelper.ClearCookiesAsync(newAccount.GetLoginOptions());
                    var identity = await OAuth2.CallIdentityServiceAsync(newAccount.IdentityUrl, client);
                    if (identity != null)
                    {
                        newAccount.UserId = identity.UserId;
                        newAccount.UserName = identity.UserName;
                        newAccount.Policy = identity.MobilePolicy;
                        await AuthStorageHelper.PersistCurrentAccountAsync(newAccount);
                    }
                    AuthStorageHelper.RefreshCookies();
                    LoggingService.Log("switched accounts, result = true", LoggingLevel.Verbose);
                    return true;
                }

                // log new user in
                LoggedInAccount = newAccount;

                RaiseAuthenticatedAccountChangedEvent(oldAccount, newAccount);
            }

            LoggingService.Log("switched accounts, result = false", LoggingLevel.Verbose);
            return false;
        }
Ejemplo n.º 8
0
 /// <summary>
 ///     Async method for refreshing the token, persisting the data in the encrypted settings and returning the updated
 ///     account
 ///     with the new access token.
 /// </summary>
 /// <param name="account"></param>
 /// <returns>Boolean based on if the refresh auth token succeeded or not</returns>
 public static async Task<Account> RefreshAuthToken(Account account)
 {
     if (account != null)
     {
         try
         {
             AuthResponse response =
                 await RefreshAuthTokenRequest(account.GetLoginOptions(), account.RefreshToken);
             account.AccessToken = response.AccessToken;
             AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account);
         }
         catch (WebException ex)
         {
             PlatformAdapter.SendToCustomLogger(
                 "OAuth2.RefreshAuthToken - Exception occurred when refreshing token:", LoggingLevel.Critical);
             PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical);
             Debug.WriteLine("Error refreshing token");
             throw new OAuthException(ex.Message, ex.Status);
         }
         catch (Exception ex)
         {
             PlatformAdapter.SendToCustomLogger(
                 "OAuth2.RefreshAuthToken - Exception occurred when refreshing token:", LoggingLevel.Critical);
             PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical);
             Debug.WriteLine("Error refreshing token");
             throw new OAuthException(ex.Message, ex.InnerException);
         }
     }
     return account;
 }