Java Code Examples for android.accounts.AccountManager#peekAuthToken()

The following examples show how to use android.accounts.AccountManager#peekAuthToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiAuthenticator.java    From ApiClient with Apache License 2.0 6 votes vote down vote up
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    final AccountManager am = AccountManager.get(mContext);
    String authToken = am.peekAuthToken(account, authTokenType);

    // If we get an authToken - we return it
    if (!TextUtils.isEmpty(authToken)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        result.putString(AccountManager.KEY_AUTHTOKEN, account.name);
        return result;
    }

    // If we get here, then we couldn't access the user's password - so we
    // need to re-prompt them for their credentials. We do that by creating
    // an intent to display our AuthenticatorActivity.
    final Intent intent = new Intent(mContext, mAuthActivityClass);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type);
    intent.putExtra(AccountManager.AUTHENTICATOR_ATTRIBUTES_NAME, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}
 
Example 2
Source File: AuthHelper.java    From android-atleap with Apache License 2.0 6 votes vote down vote up
/**
 * Recreate authToken for the specified account.
 * @param context context
 * @param account account
 * @param authTokenType authTokenType
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity, could be <code>null</code>
 */
public static void reCreateAuthToken(Context context, Account account, String authTokenType, String[] requiredFeatures, Bundle options, Activity activity) {
    final AccountManager am = AccountManager.get(context);
    boolean isAccountExist = isAccountExist(context, account);
    if (!isAccountExist) {
        am.addAccount(account.type, authTokenType, requiredFeatures, options, activity, null, null);
        return;
    }

    String authToken = am.peekAuthToken(account, authTokenType);

    if (TextUtils.isEmpty(authToken)) {
        am.getAuthToken(account, authTokenType, options, activity, null, null);
        return;
    }
    am.invalidateAuthToken(account.type, authToken);
    am.getAuthToken(account, authTokenType, options, activity, null, null);
}
 
Example 3
Source File: AuthHelper.java    From android-atleap with Apache License 2.0 6 votes vote down vote up
/**
 * Recreate authToken for the specified account. Do not use this method from main thread.
 * @param context context
 * @param account account
 * @param accountType accountType
 * @param authTokenType authTokenType
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity, could be <code>null</code>
 */
public static void reCreateAuthTokenBlocking(Context context, Account account, String accountType, String authTokenType, String[] requiredFeatures, Bundle options, Activity activity) {
    boolean isAccountExist = isAccountExist(context, account);
    if (!isAccountExist) {
        addAccountBlocking(context, accountType, authTokenType, requiredFeatures, options, activity);
        return;
    }

    final AccountManager am = AccountManager.get(context);
    String authToken = am.peekAuthToken(account, authTokenType);

    if (TextUtils.isEmpty(authToken)) {
        getAuthTokenWithoutCheck(context, account, authTokenType, options, activity);
        return;
    }

    am.invalidateAuthToken(account.type, authToken);

    getAuthTokenWithoutCheck(context, account, authTokenType, options, activity);
}
 
Example 4
Source File: AuthHelper.java    From android-atleap with Apache License 2.0 6 votes vote down vote up
/**
 * Invalidate auth token for specified account
 * @param account account to invalidate auth token
 * @param authTokenType auth token type
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity (cannot be <code>null</code>)
 */
public static void invalidateAuthToken(Account account, String authTokenType, String[] requiredFeatures, Bundle options, Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null");
    }
    if (account == null) {
        throw new IllegalArgumentException("account cannot be null");
    }
    Context context = activity.getApplicationContext();
    final AccountManager am = AccountManager.get(context);
    String authToken = am.peekAuthToken(account, authTokenType);
    if (!TextUtils.isEmpty(authToken)) {
        am.invalidateAuthToken(account.type, authToken);
    }
    am.addAccount(account.type, authTokenType, requiredFeatures, options, activity, null, null);
}
 
Example 5
Source File: Accounts.java    From indigenous-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns users.
 *
 * @return User[]
 */
public List<User> getAllUsers() {
    List<User> users = new ArrayList<>();
    AccountManager accountManager = AccountManager.get(context);
    for (Account a : accountManager.getAccountsByType(ACCOUNT_TYPE)) {
        User user = new User();
        user.setAccount(a);
        user.setMe(a.name);
        String token = "";
        try {
            token = accountManager.peekAuthToken(a, IndieAuthActivity.TOKEN_TYPE);
        }
        catch (Exception ignored) {}

        user.setAccessToken(token);
        user.setAvatar(accountManager.getUserData(a, "author_avatar"));
        user.setName(accountManager.getUserData(a, "author_name"));
        user.setTokenEndpoint(accountManager.getUserData(a, "token_endpoint"));
        user.setAuthorizationEndpoint(accountManager.getUserData(a, "authorization_endpoint"));
        user.setMicrosubEndpoint(accountManager.getUserData(a, "microsub_endpoint"));
        user.setMicropubEndpoint(accountManager.getUserData(a, "micropub_endpoint"));
        user.setMicropubMediaEndpoint(accountManager.getUserData(a, "micropub_media_endpoint"));
        user.setSyndicationTargets(accountManager.getUserData(a, "syndication_targets"));
        user.setPostTypes(accountManager.getUserData(a, "post_types"));
        user.setAccount(a);
        users.add(user);
    }
    return users;
}
 
Example 6
Source File: PredatorAuthenticator.java    From Capstone-Project with MIT License 5 votes vote down vote up
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    // Check if the context was null or not.
    if (mContextWeakReference.get() == null) {
        return null;
    }

    // Check if any authToken is available or not.
    AccountManager accountManager = AccountManager.get(mContextWeakReference.get());
    String authToken = accountManager.peekAuthToken(account, authTokenType);

    // If the authToken is available, return it.
    if (!TextUtils.isEmpty(authToken)) {
        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
        return result;
    }

    // If the authToken is unavailable, retry for a new one.
    Intent intent = new Intent(mContextWeakReference.get(), AuthenticatorActivity.class);
    intent.putExtra(Constants.Authenticator.ACCOUNT_TYPE, account.type);
    intent.putExtra(Constants.Authenticator.AUTH_TOKEN_TYPE, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

    Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);

    return bundle;
}
 
Example 7
Source File: AuthenticatorManager.java    From account-authenticator with Apache License 2.0 5 votes vote down vote up
public Bundle getAccessTokenFromCache(Account account, String authTokenType, AccountManager accountManager) {
    Bundle result;
    String cachedAuthToken = accountManager.peekAuthToken(account, authTokenType);
    String refreshToken = accountManager.getPassword(account);
    if (cachedAuthToken != null) {
        result = makeResultBundle(account, refreshToken, cachedAuthToken);
        return result;
    }
    return null;
}
 
Example 8
Source File: NotificationManager.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches notifications coming from the school and saves them in the internal app's database
 */
public void updateNotifications() {
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);

    if (accounts.length > 0) {
        String authToken = accountManager.peekAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE);
        Call<List<MonETSNotification>> notificationCall = service.getAllNotifications(authToken);
        notificationCall.enqueue(createCallback());
    }
}
 
Example 9
Source File: BaseAuthenticator.java    From android-atleap with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {

    Log.v(TAG, "getAuthToken for account=" + account + " and tokenType=" + authTokenType);

    final AccountManager am = AccountManager.get(mContext);

    String authToken = am.peekAuthToken(account, authTokenType);

    if (!TextUtils.isEmpty(authToken)) {
        Log.v(TAG, "Auth token is in the cache. Retuning.");
        return createAccountManagerResult(account, authToken);
    }

    Log.v(TAG, "Trying to get password from cache");

    final String password = am.getPassword(account);
    if (TextUtils.isEmpty(authToken)) {
        if (password != null) {
            Log.v(TAG, "Password is in cache. Trying to authenticate again.");
            try {
                authToken = authenticateOnServer(account, password, authTokenType, options, response);
            } catch (Exception e) {
                Log.w(TAG, "Cannot authenticate on the server", e);
            }
        }
    }


    if (!TextUtils.isEmpty(authToken)) {
        Log.v(TAG, "Auth token was received from the server");
        return createAccountManagerResult(account, authToken);
    }


    Log.v(TAG, "Auth token was not received. Starting auth activity.");
    return createAuthActivityIntentBundle(response, account.name, account.type, authTokenType, password, options);
}
 
Example 10
Source File: AuthHelper.java    From android-atleap with Apache License 2.0 5 votes vote down vote up
/**
 * Check if exist auth token for specified account
 * @param context context
 * @param account account to check
 * @param authTokenType auth token type
 * @return <code>true</code> if authToken for this account exist
 */
public static boolean isAccountEnabled(Context context, Account account, String authTokenType) {
    if (account == null)
        return false;
    final AccountManager accountManager = AccountManager.get(context);
    String authToken = accountManager.peekAuthToken(account, authTokenType);
    if (TextUtils.isEmpty(authToken)) {
        return false;
    } else {
        return true;
    }
}
 
Example 11
Source File: Accounts.java    From indigenous-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Gets the default user.
 *
 * @return User
 */
public User getDefaultUser() {
    User user = new User();

    SharedPreferences preferences = context.getSharedPreferences("indigenous", MODE_PRIVATE);
    String accountName = preferences.getString("account", "");
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    if (accounts.length > 0) {
        for (Account account : accounts) {
            if (account.name.equals(accountName)) {
                user.setValid(true);
                user.setMe(accountName);

                String token = "";
                try {
                    token = accountManager.peekAuthToken(account, IndieAuthActivity.TOKEN_TYPE);
                }
                catch (Exception ignored) {}

                user.setAccessToken(token);
                user.setAvatar(accountManager.getUserData(account, "author_avatar"));
                user.setName(accountManager.getUserData(account, "author_name"));
                user.setTokenEndpoint(accountManager.getUserData(account, "token_endpoint"));
                user.setAuthorizationEndpoint(accountManager.getUserData(account, "authorization_endpoint"));
                user.setMicrosubEndpoint(accountManager.getUserData(account, "microsub_endpoint"));
                user.setMicropubEndpoint(accountManager.getUserData(account, "micropub_endpoint"));
                user.setMicropubMediaEndpoint(accountManager.getUserData(account, "micropub_media_endpoint"));
                user.setSyndicationTargets(accountManager.getUserData(account, "syndication_targets"));
                user.setPostTypes(accountManager.getUserData(account, "post_types"));
                user.setAccount(account);
            }
        }
    }
    // Anonymous user.
    else {
        user.setValid(true);
        user.setAnonymous(true);
        user.setMe("https://indigenous.realize.be");
        user.setName("Anonymous");
        user.setAccessToken(Preferences.getPreference(context, "anonymous_token", ""));
        user.setMicrosubEndpoint(Preferences.getPreference(context, "anonymous_microsub_endpoint", context.getString(R.string.anonymous_microsub_endpoint)));
        user.setMicropubEndpoint(Preferences.getPreference(context, "anonymous_micropub_endpoint", ""));
    }

    return user;
}