Search in sources :

Example 1 with ClientManager

use of com.salesforce.androidsdk.rest.ClientManager in project SalesforceMobileSDK-Android by forcedotcom.

the class OAuthWebviewHelper method addAccount.

protected void addAccount(final UserAccount account) {
    ClientManager clientManager = new ClientManager(getContext(), SalesforceSDKManager.getInstance().getAccountType(), loginOptions, SalesforceSDKManager.getInstance().shouldLogoutWhenTokenRevoked());
    // Create account name (shown in Settings -> Accounts & sync)
    String accountName = buildAccountName(accountOptions.username, accountOptions.instanceUrl);
    // New account
    Bundle extras = clientManager.createNewAccount(accountName, accountOptions.username, accountOptions.refreshToken, accountOptions.authToken, accountOptions.instanceUrl, loginOptions.getLoginUrl(), accountOptions.identityUrl, getOAuthClientId(), accountOptions.orgId, accountOptions.userId, accountOptions.communityId, accountOptions.communityUrl, accountOptions.firstName, accountOptions.lastName, accountOptions.displayName, accountOptions.email, accountOptions.photoUrl, accountOptions.thumbnailUrl, accountOptions.additionalOauthValues, accountOptions.lightningDomain, accountOptions.lightningSid, accountOptions.vfDomain, accountOptions.vfSid, accountOptions.contentDomain, accountOptions.contentSid, accountOptions.csrfToken);
    /*
    	 * Registers for push notifications, if push notification client ID is present.
    	 * This step needs to happen after the account has been added by client
    	 * manager, so that the push service has all the account info it needs.
    	 */
    final Context appContext = SalesforceSDKManager.getInstance().getAppContext();
    final String pushNotificationId = BootConfig.getBootConfig(appContext).getPushNotificationClientId();
    if (!TextUtils.isEmpty(pushNotificationId)) {
        PushMessaging.register(appContext, account);
    }
    callback.onAccountAuthenticatorResult(extras);
    if (SalesforceSDKManager.getInstance().getIsTestRun()) {
        logAddAccount(account);
    } else {
        threadPool.execute(new Runnable() {

            @Override
            public void run() {
                logAddAccount(account);
            }
        });
    }
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) ClientManager(com.salesforce.androidsdk.rest.ClientManager)

Example 2 with ClientManager

use of com.salesforce.androidsdk.rest.ClientManager in project SalesforceMobileSDK-Android by forcedotcom.

the class PushService method getRestClient.

private RestClient getRestClient(UserAccount account) {
    final ClientManager cm = SalesforceSDKManager.getInstance().getClientManager();
    RestClient client = null;
    /*
    	 * The reason we can't directly call 'peekRestClient()' here is because
    	 * ClientManager does not hand out a rest client when a logout is in
    	 * progress. Hence, we build a rest client here manually, with the
    	 * available data in the 'account' object.
    	 */
    if (cm != null) {
        try {
            final AccMgrAuthTokenProvider authTokenProvider = new AccMgrAuthTokenProvider(cm, account.getInstanceServer(), account.getAuthToken(), account.getRefreshToken());
            final ClientInfo clientInfo = new ClientInfo(new URI(account.getInstanceServer()), new URI(account.getLoginServer()), new URI(account.getIdUrl()), account.getAccountName(), account.getUsername(), account.getUserId(), account.getOrgId(), account.getCommunityId(), account.getCommunityUrl(), account.getFirstName(), account.getLastName(), account.getDisplayName(), account.getEmail(), account.getPhotoUrl(), account.getThumbnailUrl(), account.getAdditionalOauthValues(), account.getLightningDomain(), account.getLightningSid(), account.getVFDomain(), account.getVFSid(), account.getContentDomain(), account.getContentSid(), account.getCSRFToken());
            client = new RestClient(clientInfo, account.getAuthToken(), HttpAccess.DEFAULT, authTokenProvider);
        } catch (Exception e) {
            SalesforceSDKLogger.e(TAG, "Failed to get rest client", e);
        }
    }
    return client;
}
Also used : ClientManager(com.salesforce.androidsdk.rest.ClientManager) RestClient(com.salesforce.androidsdk.rest.RestClient) ClientInfo(com.salesforce.androidsdk.rest.RestClient.ClientInfo) URI(java.net.URI) IOException(java.io.IOException) AccMgrAuthTokenProvider(com.salesforce.androidsdk.rest.ClientManager.AccMgrAuthTokenProvider)

Example 3 with ClientManager

use of com.salesforce.androidsdk.rest.ClientManager in project SalesforceMobileSDK-Android by forcedotcom.

the class UserAccountManager method switchToUser.

/**
 * Switches to the specified user account.
 *
 * @param user the user account to switch to
 * @param userSwitchType a {@code USER_SWITCH_TYPE} constant
 * @param extras a optional Bundle of extras to pass additional
 *               information during user switch
 *
 * @see #switchToUser(UserAccount)
 */
public void switchToUser(UserAccount user, int userSwitchType, Bundle extras) {
    if (user == null || !doesUserAccountExist(user)) {
        switchToNewUser();
        return;
    }
    final UserAccount curUser = getCurrentUser();
    /*
		 * Checks if we are attempting to switch to the current user.
		 * In this case, there's nothing to be done.
		 */
    if (user.equals(curUser)) {
        return;
    }
    final ClientManager cm = new ClientManager(context, accountType, SalesforceSDKManager.getInstance().getLoginOptions(), true);
    final Account account = cm.getAccountByName(user.getAccountName());
    storeCurrentUserInfo(user.getUserId(), user.getOrgId());
    cm.peekRestClient(account);
    sendUserSwitchIntent(userSwitchType, extras);
}
Also used : Account(android.accounts.Account) ClientManager(com.salesforce.androidsdk.rest.ClientManager)

Example 4 with ClientManager

use of com.salesforce.androidsdk.rest.ClientManager in project SalesforceMobileSDK-Android by forcedotcom.

the class UserAccountManager method refreshToken.

/**
 * Attempts to refresh the access token for this user by making an API call
 * to the "/token" endpoint. If the call succeeds, the new token is persisted.
 * If the call fails and the refresh token is no longer valid, the user is logged out.
 * This should NOT be called from the main thread because it makes a network request.
 *
 * @param userAccount User account whose token should be refreshed. Use 'null' for current user.
 */
public synchronized void refreshToken(UserAccount userAccount) {
    userAccount = (userAccount == null) ? getCurrentUser() : userAccount;
    if (userAccount == null) {
        return;
    }
    try {
        final ClientManager clientManager = SalesforceSDKManager.getInstance().getClientManager();
        final ClientManager.AccMgrAuthTokenProvider authTokenProvider = new ClientManager.AccMgrAuthTokenProvider(clientManager, userAccount.getInstanceServer(), userAccount.getAuthToken(), userAccount.getRefreshToken());
        authTokenProvider.getNewAuthToken();
    } catch (Exception e) {
        SalesforceSDKLogger.e(TAG, "Exception thrown while attempting to refresh token", e);
    }
}
Also used : ClientManager(com.salesforce.androidsdk.rest.ClientManager)

Example 5 with ClientManager

use of com.salesforce.androidsdk.rest.ClientManager in project SalesforceMobileSDK-Android by forcedotcom.

the class SalesforceSDKManager method logout.

/**
 * Destroys the stored authentication credentials (removes the account)
 * and, if requested, restarts the app.
 *
 * @param account Account.
 * @param frontActivity Front activity.
 * @param showLoginPage If true, displays the login page after removing the account.
 */
public void logout(Account account, Activity frontActivity, final boolean showLoginPage) {
    EventBuilderHelper.createAndStoreEvent("userLogout", null, TAG, null);
    final ClientManager clientMgr = new ClientManager(context, getAccountType(), null, shouldLogoutWhenTokenRevoked());
    isLoggingOut = true;
    final AccountManager mgr = AccountManager.get(context);
    String refreshToken = null;
    String loginServer = null;
    if (account != null) {
        final String encryptionKey = SalesforceSDKManager.getEncryptionKey();
        refreshToken = SalesforceSDKManager.decrypt(mgr.getPassword(account), encryptionKey);
        loginServer = SalesforceSDKManager.decrypt(mgr.getUserData(account, AuthenticatorService.KEY_INSTANCE_URL), encryptionKey);
    }
    /*
		 * Makes a call to un-register from push notifications, only
		 * if the refresh token is available.
		 */
    final UserAccount userAcc = getUserAccountManager().buildUserAccount(account);
    int numAccounts = mgr.getAccountsByType(getAccountType()).length;
    if (PushMessaging.isRegistered(context, userAcc) && refreshToken != null) {
        unregisterPush(clientMgr, showLoginPage, refreshToken, loginServer, account, frontActivity, (numAccounts == 1));
    } else {
        removeAccount(clientMgr, showLoginPage, refreshToken, loginServer, account, frontActivity);
    }
}
Also used : ClientManager(com.salesforce.androidsdk.rest.ClientManager) UserAccountManager(com.salesforce.androidsdk.accounts.UserAccountManager) AccountManager(android.accounts.AccountManager) UserAccount(com.salesforce.androidsdk.accounts.UserAccount)

Aggregations

ClientManager (com.salesforce.androidsdk.rest.ClientManager)14 Context (android.content.Context)4 UserAccount (com.salesforce.androidsdk.accounts.UserAccount)4 Account (android.accounts.Account)2 AccountManager (android.accounts.AccountManager)2 Application (android.app.Application)2 UserAccountManager (com.salesforce.androidsdk.accounts.UserAccountManager)2 LoginOptions (com.salesforce.androidsdk.rest.ClientManager.LoginOptions)2 RestClient (com.salesforce.androidsdk.rest.RestClient)2 EventsListenerQueue (com.salesforce.androidsdk.util.test.EventsListenerQueue)2 SharedPreferences (android.content.SharedPreferences)1 Bundle (android.os.Bundle)1 SmallTest (androidx.test.filters.SmallTest)1 UserAccountTest (com.salesforce.androidsdk.accounts.UserAccountTest)1 SalesforceAnalyticsManager (com.salesforce.androidsdk.analytics.SalesforceAnalyticsManager)1 AnalyticsManager (com.salesforce.androidsdk.analytics.manager.AnalyticsManager)1 DeviceAppAttributes (com.salesforce.androidsdk.analytics.model.DeviceAppAttributes)1 BootConfig (com.salesforce.androidsdk.config.BootConfig)1 AccMgrAuthTokenProvider (com.salesforce.androidsdk.rest.ClientManager.AccMgrAuthTokenProvider)1 ClientInfo (com.salesforce.androidsdk.rest.RestClient.ClientInfo)1