Beispiel #1
0
        public async Task ClearCookiesAsync(LoginOptions options)
        {
            if (Window.Current == null)
            {
                return;
            }
            var frame = Window.Current.Content as Frame;

            if (frame != null)
            {
                await frame.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    var loginUri = new Uri(OAuth2.ComputeAuthorizationUrl(options));
                    var myFilter = new HttpBaseProtocolFilter();
                    HttpCookieManager cookieManager = myFilter.CookieManager;
                    try
                    {
                        LoggingService.Log("attempting to clear cookies", LoggingLevel.Verbose);
                        HttpCookieCollection cookies = cookieManager.GetCookies(loginUri);
                        foreach (HttpCookie cookie in cookies)
                        {
                            cookieManager.DeleteCookie(cookie);
                        }
                        LoggingService.Log("clear cookies done", LoggingLevel.Verbose);
                    }
                    catch (ArgumentException ex)
                    {
                        LoggingService.Log("Exception occurred when clearing cookies", LoggingLevel.Critical);
                        LoggingService.Log(ex, LoggingLevel.Critical);
                    }
                });
            }
        }
Beispiel #2
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 async Task TestRefreshAuthToken()
        {
            // Try describe without being authenticated, expect 401
            Assert.AreEqual(HttpStatusCode.Unauthorized, await DoDescribe(null));

            var account = await OAuth2.RefreshAuthTokenAsync(TestCredentials.TestAccount);

            // Try describe again, expect 200
            Assert.AreEqual(HttpStatusCode.OK, await DoDescribe(account.AccessToken));
        }
        public async Task TestCallIdentityService()
        {
            // Get auth token and identity url (through refresh)
            var account = await OAuth2.RefreshAuthTokenAsync(TestCredentials.TestAccount);

            // Call the identity service
            IdentityResponse identityResponse = await OAuth2.CallIdentityServiceAsync(account.IdentityUrl, account.AccessToken);

            // Check username
            Assert.AreEqual(TestCredentials.Username, identityResponse.UserName);
        }
        public void TestComputeFrontDoorUrl()
        {
            string instanceUrl = "https://fake.instance";
            string accessToken = "FAKE_ACCESS_TOKEN";
            string url         = "https://target.url";

            string expectedUrl = "https://fake.instance/secur/frontdoor.jsp?display=touch&sid=FAKE_ACCESS_TOKEN&retURL=https://target.url";
            string actualUrl   = OAuth2.ComputeFrontDoorUrl(instanceUrl, accessToken, url);

            Assert.AreEqual(expectedUrl, actualUrl, "Wrong front door url");
        }
        public void testCallIdentityService()
        {
            // Get auth token and identity url (through refresh)
            LoginOptions loginOptions    = new LoginOptions(TestCredentials.LOGIN_URL, TestCredentials.CLIENT_ID, null, null);
            AuthResponse refreshResponse = OAuth2.RefreshAuthToken(loginOptions, TestCredentials.REFRESH_TOKEN).Result;

            // Call the identity service
            IdentityResponse identityResponse = OAuth2.CallIdentityService(refreshResponse.IdentityUrl, refreshResponse.AccessToken).Result;

            // Check username
            Assert.AreEqual("*****@*****.**", identityResponse.UserName);
        }
        public void TestRefreshAuthToken()
        {
            // Try describe without being authenticated, expect 401
            Assert.AreEqual(HttpStatusCode.Unauthorized, DoDescribe(null));

            // Get auth token (through refresh)
            LoginOptions loginOptions    = new LoginOptions(TestCredentials.LOGIN_URL, TestCredentials.CLIENT_ID, null, null);
            AuthResponse refreshResponse = OAuth2.RefreshAuthToken(loginOptions, TestCredentials.REFRESH_TOKEN).Result;

            // Try describe again, expect 200
            Assert.AreEqual(HttpStatusCode.OK, DoDescribe(refreshResponse.AccessToken));
        }
Beispiel #8
0
        /// <summary>
        ///     Create and persist Account for newly authenticated user
        /// </summary>
        /// <param name="loginOptions"></param>
        /// <param name="authResponse"></param>
        /// <param name="cancellationToken"></param>
        public static async Task <Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse,
                                                            CancellationToken cancellationToken = default(CancellationToken))
        {
            LoggingService.Log("Create account object", LoggingLevel.Verbose);

            var account = new Account(
                loginOptions.LoginUrl,
                loginOptions.ClientId,
                loginOptions.CallbackUrl,
                loginOptions.Scopes,
                authResponse.InstanceUrl,
                authResponse.IdentityUrl,
                authResponse.AccessToken,
                authResponse.RefreshToken)
            {
                CommunityId  = authResponse.CommunityId,
                CommunityUrl = authResponse.CommunityUrl
            };

            var cm = new ClientManager();

            cm.PeekRestClient();
            IdentityResponse identity = null;

            try
            {
                identity =
                    await
                    OAuth2.CallIdentityServiceAsync(authResponse.IdentityUrl, authResponse.AccessToken,
                                                    cancellationToken);
            }
            catch (JsonException ex)
            {
                LoggingService.Log(ex, LoggingLevel.Critical, "Exception occurred when retrieving account identity");
                Debug.WriteLine("Error retrieving account identity");
            }

            if (identity != null)
            {
                account.UserId         = identity.UserId;
                account.UserName       = identity.UserName;
                account.Policy         = identity.MobilePolicy;
                account.OrganizationId = identity.OrganizationId;

                await AuthStorageHelper.PersistCurrentAccountAsync(account);

                LoggedInAccount = account;
            }
            LoggingService.Log("Finished creating account", LoggingLevel.Verbose);
            return(account);
        }
        public void TestComputeAuthorizationUrl()
        {
            string loginUrl    = "https://login.salesforce.com";
            string clientId    = "TEST_CLIENT_ID";
            string callbackUrl = "test://sfdc";

            string[]     scopes       = { "web", "api" };
            LoginOptions loginOptions = new LoginOptions(loginUrl, clientId, callbackUrl, scopes);

            string expectedUrl = "https://login.salesforce.com/services/oauth2/authorize?display=touch&response_type=token&client_id=TEST_CLIENT_ID&redirect_uri=test://sfdc&scope=web%20api%20refresh_token";
            string actualUrl   = OAuth2.ComputeAuthorizationUrl(loginOptions);

            Assert.AreEqual(expectedUrl, actualUrl, "Wrong authorization url");
        }
        /// <summary>
        /// Create and persist Account for newly authenticated user
        /// </summary>
        /// <param name="loginOptions"></param>
        /// <param name="authResponse"></param>
        public async static Task <bool> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse)
        {
            Account account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes,
                                          authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken);
            var cm     = new ClientManager();
            var client = cm.PeekRestClient();
            IdentityResponse identity = await OAuth2.CallIdentityService(authResponse.IdentityUrl, authResponse.AccessToken);

            if (identity != null)
            {
                account.UserId   = identity.UserId;
                account.UserName = identity.UserName;
                AuthStorage.PersistCredentials(account);
                return(true);
            }
            return(false);
        }
Beispiel #11
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);
        }
Beispiel #12
0
        /// <summary>
        ///     Create and persist Account for newly authenticated user
        /// </summary>
        /// <param name="loginOptions"></param>
        /// <param name="authResponse"></param>
        public static async Task <Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse)
        {
            PlatformAdapter.SendToCustomLogger("AccountManager.CreateNewAccount - create account object", LoggingLevel.Verbose);
            var account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl,
                                      loginOptions.Scopes,
                                      authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken);

            account.CommunityId  = authResponse.CommunityId;
            account.CommunityUrl = authResponse.CommunityUrl;

            IdentityResponse identity = null;

            try
            {
                var cm = new ClientManager();
                cm.PeekRestClient();
                identity = await OAuth2.CallIdentityService(authResponse.IdentityUrl, authResponse.AccessToken);
            }
            catch (JsonException ex)
            {
                PlatformAdapter.SendToCustomLogger(
                    "AccountManager.CreateNewAccount - Exception occurred when retrieving account identity:",
                    LoggingLevel.Critical);
                PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical);
                Debug.WriteLine("Error retrieving account identity");
            }

            if (identity != null)
            {
                account.UserId   = identity.UserId;
                account.UserName = identity.UserName;
                account.Policy   = identity.MobilePolicy;
                AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account);
            }

            PlatformAdapter.SendToCustomLogger("AccountManager.CreateNewAccount - done", LoggingLevel.Verbose);
            return(account);
        }