Esempio 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 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 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);
        }
Esempio n. 4
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 testDeserializeIdentityResponseWithoutMobilePolicy()
        {
            string           idUrl          = "https://login.salesforce.com/id/00D50000000IZ3ZEAW/00550000001fg5OAAQ";
            string           userId         = "00550000001fg5OAAQ";
            string           organizationId = "00D50000000IZ3ZEAW";
            string           userName       = "******";
            string           partialResponseWithoutMobilePolicy = "{\"id\":\"" + idUrl + "\",\"user_id\":\"" + userId + "\",\"organization_id\":\"" + organizationId + "\",\"username\":\"" + userName + "\"}";
            IdentityResponse idResponse = JsonConvert.DeserializeObject <IdentityResponse>(partialResponseWithoutMobilePolicy);

            Assert.AreEqual(idUrl, idResponse.IdentityUrl);
            Assert.AreEqual(userId, idResponse.UserId);
            Assert.AreEqual(organizationId, idResponse.OrganizationId);
            Assert.AreEqual(userName, idResponse.UserName);
            Assert.IsNull(idResponse.MobilePolicy);
        }
        /// <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);
        }
        public void testDeserializeIdentityResponseWithMobilePolicy()
        {
            string           idUrl          = "https://login.salesforce.com/id/00D50000000IZ3ZEAW/00550000001fg5OAAQ";
            string           userId         = "00550000001fg5OAAQ";
            string           organizationId = "00D50000000IZ3ZEAW";
            string           userName       = "******";
            string           partialResponseWithMobilePolicy = "{\"id\":\"" + idUrl + "\",\"user_id\":\"" + userId + "\",\"organization_id\":\"" + organizationId + "\",\"username\":\"" + userName + "\",\"mobile_policy\":{\"pin_length\":6,\"screen_lock\":1}}";
            IdentityResponse idResponse = JsonConvert.DeserializeObject <IdentityResponse>(partialResponseWithMobilePolicy);

            Assert.AreEqual(idUrl, idResponse.IdentityUrl);
            Assert.AreEqual(userId, idResponse.UserId);
            Assert.AreEqual(organizationId, idResponse.OrganizationId);
            Assert.AreEqual(userName, idResponse.UserName);
            Assert.IsNotNull(idResponse.MobilePolicy);
            Assert.AreEqual(6, idResponse.MobilePolicy.PinLength);
            Assert.AreEqual(1, idResponse.MobilePolicy.ScreenLockTimeout);
        }
Esempio n. 8
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);
        }