Example usage for android.accounts AccountManager setAuthToken

List of usage examples for android.accounts AccountManager setAuthToken

Introduction

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

Prototype

public void setAuthToken(Account account, final String authTokenType, final String authToken) 

Source Link

Document

Adds an auth token to the AccountManager cache for an account.

Usage

From source file:Main.java

public static void setSessionKey(AccountManager paramAccountManager, Account paramAccount, String paramString) {
    paramAccountManager.setAuthToken(paramAccount, "co.vine.android.basic_auth.token.secret", paramString);
}

From source file:me.philio.ghost.ui.LoginActivity.java

@Override
public void onSuccess(String email, String password, Token token, User user) {
    // Create the account
    Account account = new Account(AccountUtils.getName(mBlogUrl, email), getString(R.string.account_type));
    Bundle userdata = new Bundle();
    userdata.putString(KEY_BLOG_URL, mBlogUrl);
    userdata.putString(KEY_EMAIL, email);
    userdata.putString(KEY_ACCESS_TOKEN_TYPE, token.tokenType);
    userdata.putString(KEY_ACCESS_TOKEN_EXPIRES,
            Long.toString(System.currentTimeMillis() + (token.expires * 1000)));

    // Add account to the system
    AccountManager accountManager = AccountManager.get(this);
    accountManager.addAccountExplicitly(account, password, userdata);

    // Set the account auth tokens
    accountManager.setAuthToken(account, TOKEN_TYPE_ACCESS, token.accessToken);
    accountManager.setAuthToken(account, TOKEN_TYPE_REFRESH, token.refreshToken);

    // Create initial database records
    Blog blog = new Blog();
    blog.url = mBlogUrl;//from   w ww  . ja va 2 s .  co m
    blog.email = email;
    user.blog = blog;
    ActiveAndroid.beginTransaction();
    try {
        blog.save();
        user.save();
        ActiveAndroid.setTransactionSuccessful();
    } finally {
        ActiveAndroid.endTransaction();
    }

    // Enable sync for the account
    ContentResolver.setSyncAutomatically(account, getString(R.string.content_authority), true);
    SyncHelper.requestSync(account, getString(R.string.content_authority));

    // Set response intent
    Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java

private void storeAnonymousToken(final String token, final String authority, final AccountManager am,
        final Account a) {
    if (token == null)
        return;/*  w w w .java 2s  .c o m*/
    am.setAuthToken(a, authority, token);
    am.setAuthToken(a, Constants.TOKEN_TYPE_ANONYMOUS, token);
    am.addAccountExplicitly(a, null, null);
}

From source file:org.amahi.anywhere.activity.AuthenticationActivity.java

private void finishAuthentication(String authenticationToken) {
    AccountManager accountManager = AccountManager.get(this);

    Bundle authenticationBundle = new Bundle();

    Account account = new AmahiAccount(getUsername());

    if (accountManager.addAccountExplicitly(account, getPassword(), null)) {
        authenticationBundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        authenticationBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        authenticationBundle.putString(AccountManager.KEY_AUTHTOKEN, authenticationToken);

        accountManager.setAuthToken(account, account.type, authenticationToken);
    }//from  w  w  w. j a va 2s .  co m

    setAccountAuthenticatorResult(authenticationBundle);

    setResult(Activity.RESULT_OK);

    finish();
}

From source file:com.he5ed.lib.cloudprovider.auth.OAuth2Fragment.java

/**
 * Create a new user account or update the current user account
 *
 * @param user user information returned from server
 *//*from  w  ww.j a  va  2 s.c o  m*/
private void addAccount(User user) {
    boolean accountExist = false;
    AccountManager am = AccountManager.get(getActivity());
    // check if account already exist in AccountManager
    Account[] accounts = am.getAccountsByType(CloudProvider.ACCOUNT_TYPE);
    for (Account account : accounts) {
        if (account.name.equals(user.id)) {
            accountExist = true;
            break;
        }
    }

    Account account = new Account(user.id, CloudProvider.ACCOUNT_TYPE);
    Bundle userData = new Bundle(); // must be string value
    String accessToken = mTokenInfo.get(Authenticator.KEY_ACCESS_TOKEN);
    String refreshToken = mTokenInfo.get(Authenticator.KEY_REFRESH_TOKEN);
    String expiryDuration = mTokenInfo.get(Authenticator.KEY_EXPIRY);
    if (accountExist) {
        // update current account access token
        am.setAuthToken(account, CloudProvider.AUTH_TYPE, accessToken);
        if (refreshToken != null)
            am.setUserData(account, Authenticator.KEY_REFRESH_TOKEN, refreshToken);
        if (expiryDuration != null)
            am.setUserData(account, Authenticator.KEY_EXPIRY, expiryDuration);
    } else {
        // add new account into AccountManager
        if (refreshToken != null)
            userData.putString(Authenticator.KEY_REFRESH_TOKEN, refreshToken);
        if (expiryDuration != null)
            userData.putString(Authenticator.KEY_EXPIRY, expiryDuration);
        userData.putString(Authenticator.KEY_CLOUD_API, AuthHelper.getCloudApi(mCloudApi));
        userData.putString(Authenticator.KEY_USERNAME, user.name);
        userData.putString(Authenticator.KEY_EMAIL, user.email);
        userData.putString(Authenticator.KEY_AVATAR_URL, user.avatarUrl);

        am.addAccountExplicitly(account, null, userData);
        am.setAuthToken(account, CloudProvider.AUTH_TYPE, accessToken);
    }

    // send result back to AccountManager
    Bundle result = new Bundle();
    result.putString(AccountManager.KEY_ACCOUNT_NAME, user.id);
    result.putString(AccountManager.KEY_ACCOUNT_TYPE, CloudProvider.ACCOUNT_TYPE);
    ((AccountAuthenticatorActivity) getActivity()).setAccountAuthenticatorResult(result);

    getActivity().finish();
}

From source file:com.sefford.beauthentic.activities.LoginActivity.java

void createGoogleAccount(final GoogleSignInAccount acct) {
    final Account account = new Account(acct.getDisplayName(), AuthenticAuthenticator.ACCOUNT_TYPE);
    final AccountManager am = AccountManager.get(this);
    final Bundle data = new Bundle();
    data.putInt(AuthenticAuthenticator.EXTRA_TYPE, AuthenticAuthenticator.Type.GOOGLE.ordinal());
    data.putString(AccountManager.KEY_ACCOUNT_NAME, acct.getDisplayName());
    data.putString(AccountManager.KEY_AUTHTOKEN, acct.getIdToken());
    am.confirmCredentials(account, data, null, new AccountManagerCallback<Bundle>() {
        @Override/*w  ww.  j a v a 2s  .  c  om*/
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                final Bundle result = future.getResult();
                if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
                    Sessions.addAccount(am, account, "", Bundle.EMPTY);
                    am.setAuthToken(account, AuthenticAuthenticator.AUTHTOKEN_TYPE,
                            result.getString(AccountManager.KEY_AUTHTOKEN));
                    am.setUserData(account, AuthenticAuthenticator.EXTRA_TYPE,
                            Integer.toString(AuthenticAuthenticator.Type.GOOGLE.ordinal()));
                    notifyLoginToGCM(AuthenticAuthenticator.Type.GOOGLE.ordinal(), account.name, "",
                            result.getString(AccountManager.KEY_AUTHTOKEN));
                    googleApi.saveCredential(new Credential.Builder(acct.getEmail())
                            .setAccountType(IdentityProviders.GOOGLE).setName(acct.getDisplayName())
                            .setProfilePictureUri(acct.getPhotoUrl()).build(),
                            new SmartlockCredentialCallback());
                }
            } catch (OperationCanceledException e) {
                Snackbar.make(vLoginForm, R.string.error_operation_cancelled, Snackbar.LENGTH_LONG).show();
            } catch (IOException e) {
                Snackbar.make(vLoginForm, R.string.error_not_connected_to_internet, Snackbar.LENGTH_LONG)
                        .show();
            } catch (AuthenticatorException e) {
                Snackbar.make(vLoginForm, R.string.error_invalid_credentials, Snackbar.LENGTH_LONG).show();
            }
        }
    }, null);
}

From source file:com.sefford.beauthentic.activities.LoginActivity.java

void performLogin() {
    final AccountManager am = AccountManager.get(this);
    final Bundle data = new Bundle();
    data.putString(AuthenticAuthenticator.EXTRA_PASSWORD, etPassword.getText().toString());
    data.putInt(AuthenticAuthenticator.EXTRA_TYPE, AuthenticAuthenticator.Type.PASSWORD.ordinal());
    final Account account = new Account(etUsername.getText().toString(), AuthenticAuthenticator.ACCOUNT_TYPE);
    am.getAuthToken(account, "", data, true, new AccountManagerCallback<Bundle>() {
        @Override/*from  w ww.  ja v  a 2 s .  c  o  m*/
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                final Bundle result = future.getResult();
                if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
                    Sessions.addAccount(am, account, etPassword.getText().toString(), Bundle.EMPTY);
                    am.setAuthToken(account, AuthenticAuthenticator.AUTHTOKEN_TYPE,
                            result.getString(AccountManager.KEY_AUTHTOKEN));
                    am.setUserData(account, AuthenticAuthenticator.EXTRA_TYPE,
                            Integer.toString(AuthenticAuthenticator.Type.PASSWORD.ordinal()));
                    notifyLoginToGCM(AuthenticAuthenticator.Type.PASSWORD.ordinal(), account.name,
                            etPassword.getText().toString(), result.getString(AccountManager.KEY_AUTHTOKEN));
                    googleApi
                            .saveCredential(
                                    new Credential.Builder(account.name)
                                            .setPassword(etPassword.getText().toString()).build(),
                                    new SmartlockCredentialCallback());
                } else {
                    Snackbar.make(vLoginForm, R.string.error_invalid_credentials, Snackbar.LENGTH_LONG).show();
                }
            } catch (OperationCanceledException e) {
                Snackbar.make(vLoginForm, R.string.error_operation_cancelled, Snackbar.LENGTH_LONG).show();
            } catch (IOException e) {
                Snackbar.make(vLoginForm, R.string.error_not_connected_to_internet, Snackbar.LENGTH_LONG)
                        .show();
            } catch (AuthenticatorException e) {
                Snackbar.make(vLoginForm, R.string.error_invalid_credentials, Snackbar.LENGTH_LONG).show();
            }
        }

    }, null);
}