Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.volley.toolbox; |
| 18 | |
| 19 | import com.android.volley.AuthFailureError; |
| 20 | |
| 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountManager; |
| 23 | import android.accounts.AccountManagerFuture; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.os.Bundle; |
| 27 | |
| 28 | /** |
| 29 | * An Authenticator that uses {@link AccountManager} to get auth |
| 30 | * tokens of a specified type for a specified account. |
| 31 | */ |
| 32 | public class AndroidAuthenticator implements Authenticator { |
| 33 | private final Context mContext; |
| 34 | private final Account mAccount; |
| 35 | private final String mAuthTokenType; |
Evan Charlton | 2ff9993 | 2013-04-30 12:07:29 -0700 | [diff] [blame] | 36 | private final boolean mNotifyAuthFailure; |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Creates a new authenticator. |
| 40 | * @param context Context for accessing AccountManager |
| 41 | * @param account Account to authenticate as |
| 42 | * @param authTokenType Auth token type passed to AccountManager |
| 43 | */ |
| 44 | public AndroidAuthenticator(Context context, Account account, String authTokenType) { |
Evan Charlton | 2ff9993 | 2013-04-30 12:07:29 -0700 | [diff] [blame] | 45 | this(context, account, authTokenType, false); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates a new authenticator. |
| 50 | * @param context Context for accessing AccountManager |
| 51 | * @param account Account to authenticate as |
| 52 | * @param authTokenType Auth token type passed to AccountManager |
| 53 | * @param notifyAuthFailure Whether to raise a notification upon auth failure |
| 54 | */ |
| 55 | public AndroidAuthenticator(Context context, Account account, String authTokenType, |
| 56 | boolean notifyAuthFailure) { |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 57 | mContext = context; |
| 58 | mAccount = account; |
| 59 | mAuthTokenType = authTokenType; |
Evan Charlton | 2ff9993 | 2013-04-30 12:07:29 -0700 | [diff] [blame] | 60 | mNotifyAuthFailure = notifyAuthFailure; |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the Account being used by this authenticator. |
| 65 | */ |
| 66 | public Account getAccount() { |
| 67 | return mAccount; |
| 68 | } |
| 69 | |
Ficus Kirkpatrick | 35d5cc3 | 2014-02-08 11:15:04 -0800 | [diff] [blame] | 70 | // TODO: Figure out what to do about notifyAuthFailure |
| 71 | @SuppressWarnings("deprecation") |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 72 | @Override |
| 73 | public String getAuthToken() throws AuthFailureError { |
| 74 | final AccountManager accountManager = AccountManager.get(mContext); |
| 75 | AccountManagerFuture<Bundle> future = accountManager.getAuthToken(mAccount, |
Evan Charlton | 2ff9993 | 2013-04-30 12:07:29 -0700 | [diff] [blame] | 76 | mAuthTokenType, mNotifyAuthFailure, null, null); |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 77 | Bundle result; |
| 78 | try { |
| 79 | result = future.getResult(); |
| 80 | } catch (Exception e) { |
| 81 | throw new AuthFailureError("Error while retrieving auth token", e); |
| 82 | } |
| 83 | String authToken = null; |
| 84 | if (future.isDone() && !future.isCancelled()) { |
| 85 | if (result.containsKey(AccountManager.KEY_INTENT)) { |
| 86 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
| 87 | throw new AuthFailureError(intent); |
| 88 | } |
| 89 | authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
| 90 | } |
| 91 | if (authToken == null) { |
| 92 | throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType); |
| 93 | } |
| 94 | |
| 95 | return authToken; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void invalidateAuthToken(String authToken) { |
| 100 | AccountManager.get(mContext).invalidateAuthToken(mAccount.type, authToken); |
| 101 | } |
| 102 | } |