Example usage for android.accounts AccountManager clearPassword

List of usage examples for android.accounts AccountManager clearPassword

Introduction

In this page you can find the example usage for android.accounts AccountManager clearPassword.

Prototype

public void clearPassword(final Account account) 

Source Link

Document

Forgets a saved password.

Usage

From source file:com.owncloud.android.oc_framework.operations.RemoteOperation.java

/**
 * Asynchronous execution of the operation 
 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}, 
 * and result posting./*w w w .  ja v  a 2 s. co m*/
 * 
 * TODO refactor && clean the code; now it's a mess
 */
@Override
public final void run() {
    RemoteOperationResult result = null;
    boolean repeat = false;
    do {
        try {
            if (mClient == null) {
                if (mAccount != null && mContext != null) {
                    if (mCallerActivity != null) {
                        mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext,
                                mCallerActivity);
                    } else {
                        mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext);
                    }
                } else {
                    throw new IllegalStateException(
                            "Trying to run a remote operation asynchronously with no client instance or account");
                }
            }

        } catch (IOException e) {
            Log.e(TAG, "Error while trying to access to " + mAccount.name,
                    new AccountsException("I/O exception while trying to authorize the account", e));
            result = new RemoteOperationResult(e);

        } catch (AccountsException e) {
            Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
            result = new RemoteOperationResult(e);
        }

        if (result == null)
            result = run(mClient);

        repeat = false;
        if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() &&
        //                    (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
                (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) {
            /// possible fail due to lack of authorization in an operation performed in foreground
            Credentials cred = mClient.getCredentials();
            String ssoSessionCookie = mClient.getSsoSessionCookie();
            if (cred != null || ssoSessionCookie != null) {
                /// confirmed : unauthorized operation
                AccountManager am = AccountManager.get(mContext);
                boolean bearerAuthorization = (cred != null && cred instanceof BearerCredentials);
                boolean samlBasedSsoAuthorization = (cred == null && ssoSessionCookie != null);
                if (bearerAuthorization) {
                    am.invalidateAuthToken(mAccount.type, ((BearerCredentials) cred).getAccessToken());
                } else if (samlBasedSsoAuthorization) {
                    am.invalidateAuthToken(mAccount.type, ssoSessionCookie);
                } else {
                    am.clearPassword(mAccount);
                }
                mClient = null;
                repeat = true; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
                result = null;
            }
        }
    } while (repeat);

    final RemoteOperationResult resultToSend = result;
    if (mListenerHandler != null && mListener != null) {
        mListenerHandler.post(new Runnable() {
            @Override
            public void run() {
                mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
            }
        });
    }
}

From source file:com.owncloud.android.operations.RemoteOperation.java

/**
 * Asynchronous execution of the operation 
 * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}, 
 * and result posting.//from  ww  w. j a  va  2 s. co m
 * 
 * TODO refactor && clean the code; now it's a mess
 */
@Override
public final void run() {
    RemoteOperationResult result = null;
    boolean repeat = false;
    do {
        try {
            if (mClient == null) {
                if (mAccount != null && mContext != null) {
                    if (mCallerActivity != null) {
                        mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, mCallerActivity);
                    } else {
                        mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext);
                    }
                } else {
                    throw new IllegalStateException(
                            "Trying to run a remote operation asynchronously with no client instance or account");
                }
            }

        } catch (IOException e) {
            Log_OC.e(TAG, "Error while trying to access to " + mAccount.name,
                    new AccountsException("I/O exception while trying to authorize the account", e));
            result = new RemoteOperationResult(e);

        } catch (AccountsException e) {
            Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e);
            result = new RemoteOperationResult(e);
        }

        if (result == null)
            result = run(mClient);

        repeat = false;
        if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() &&
        //                    (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
                (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) {
            /// possible fail due to lack of authorization in an operation performed in foreground
            Credentials cred = mClient.getCredentials();
            String ssoSessionCookie = mClient.getSsoSessionCookie();
            if (cred != null || ssoSessionCookie != null) {
                /// confirmed : unauthorized operation
                AccountManager am = AccountManager.get(mContext);
                boolean bearerAuthorization = (cred != null && cred instanceof BearerCredentials);
                boolean samlBasedSsoAuthorization = (cred == null && ssoSessionCookie != null);
                if (bearerAuthorization) {
                    am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE,
                            ((BearerCredentials) cred).getAccessToken());
                } else if (samlBasedSsoAuthorization) {
                    am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, ssoSessionCookie);
                } else {
                    am.clearPassword(mAccount);
                }
                mClient = null;
                repeat = true; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity
                result = null;
            }
        }
    } while (repeat);

    final RemoteOperationResult resultToSend = result;
    if (mListenerHandler != null && mListener != null) {
        mListenerHandler.post(new Runnable() {
            @Override
            public void run() {
                mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
            }
        });
    }
}

From source file:com.cerema.cloud2.ui.activity.FileActivity.java

/**
 * Invalidates the credentials stored for the current OC account and requests new credentials to the user,
 * navigating to {@link AuthenticatorActivity}
 *
 * @param context   Android Context needed to access the {@link AccountManager}. Received as a parameter
 *                  to make the method accessible to {@link android.content.BroadcastReceiver}s.
 *//*from   w ww  .  j  a v a  2  s  . c om*/
protected void requestCredentialsUpdate(Context context) {

    try {
        /// step 1 - invalidate credentials of current account
        OwnCloudClient client;
        OwnCloudAccount ocAccount = new OwnCloudAccount(getAccount(), context);
        client = (OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(ocAccount));
        if (client != null) {
            OwnCloudCredentials cred = client.getCredentials();
            if (cred != null) {
                AccountManager am = AccountManager.get(context);
                if (cred.authTokenExpires()) {
                    am.invalidateAuthToken(getAccount().type, cred.getAuthToken());
                } else {
                    am.clearPassword(getAccount());
                }
            }
        }

        /// step 2 - request credentials to user
        Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
                AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
        updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivity(updateAccountCredentials);

    } catch (com.cerema.cloud2.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        Toast.makeText(context, R.string.auth_account_does_not_exist, Toast.LENGTH_SHORT).show();
    }

}

From source file:com.synox.android.ui.activity.FileActivity.java

/**
 * Invalidates the credentials stored for the current OC account and requests new credentials to the user,
 * navigating to {@link AuthenticatorActivity}
 *
 * @param context Android Context needed to access the {@link AccountManager}. Received as a parameter
 *                to make the method accessible to {@link android.content.BroadcastReceiver}s.
 *///from w  w  w .  ja v  a 2  s  .  c om
protected void requestCredentialsUpdate(Context context) {

    try {
        /// step 1 - invalidate credentials of current account
        OwnCloudClient client;
        OwnCloudAccount ocAccount = new OwnCloudAccount(getAccount(), context);
        client = (OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(ocAccount));
        if (client != null) {
            OwnCloudCredentials cred = client.getCredentials();
            if (cred != null) {
                AccountManager am = AccountManager.get(context);
                if (cred.authTokenExpires()) {
                    am.invalidateAuthToken(getAccount().type, cred.getAuthToken());
                } else {
                    am.clearPassword(getAccount());
                }
            }
        }

        /// step 2 - request credentials to user
        Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
                AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
        updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivity(updateAccountCredentials);

    } catch (com.synox.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        Toast.makeText(context, R.string.auth_account_does_not_exist, Toast.LENGTH_SHORT).show();
    }

}

From source file:com.hyrt.cnp.account.AccountAuthenticator.java

@Override
public Bundle getAuthToken(final AccountAuthenticatorResponse response, final Account account,
        final String authTokenType, final Bundle options) throws NetworkErrorException {
    Log.d(TAG, "Retrieving OAuth2 token");

    final Bundle bundle = new Bundle();

    if (!ACCOUNT_TYPE.equals(authTokenType))
        return bundle;

    AccountManager am = AccountManager.get(context);
    String password = am.getPassword(account);
    if (TextUtils.isEmpty(password)) {
        bundle.putParcelable(KEY_INTENT, createLoginIntent(response));
        return bundle;
    }//  w  ww  .  ja va 2 s. c  o m
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.set("username", account.name);
    params.set("password", password);
    User.UserModel userModel = null;
    try {
        userModel = getCustomRestTemplate().postForObject("http://api.chinaxueqian.com/account/login", params,
                User.UserModel.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (userModel == null || userModel.getData() == null || TextUtils.isEmpty(userModel.getData().getToken()))
        bundle.putParcelable(KEY_INTENT, createLoginIntent(response));
    else {
        bundle.putString(KEY_ACCOUNT_NAME, account.name);
        bundle.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
        bundle.putString(KEY_AUTHTOKEN, userModel.getData().getToken() + "&uuid="
                + userModel.getData().getUuid() + "&sid=" + userModel.getData().getNursery_id());
        am.clearPassword(account);
    }
    return bundle;
}