blob: 371fd83d36f874fdf2b3a105a341bb93583366f8 [file] [log] [blame]
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -08001/*
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
17package com.android.volley.toolbox;
18
19import com.android.volley.AuthFailureError;
20
21import android.accounts.Account;
22import android.accounts.AccountManager;
23import android.accounts.AccountManagerFuture;
24import android.content.Context;
25import android.content.Intent;
26import 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 */
32public class AndroidAuthenticator implements Authenticator {
33 private final Context mContext;
34 private final Account mAccount;
35 private final String mAuthTokenType;
Evan Charlton2ff99932013-04-30 12:07:29 -070036 private final boolean mNotifyAuthFailure;
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080037
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 Charlton2ff99932013-04-30 12:07:29 -070045 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 Querud56b88a2012-11-07 07:48:57 -080057 mContext = context;
58 mAccount = account;
59 mAuthTokenType = authTokenType;
Evan Charlton2ff99932013-04-30 12:07:29 -070060 mNotifyAuthFailure = notifyAuthFailure;
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080061 }
62
63 /**
64 * Returns the Account being used by this authenticator.
65 */
66 public Account getAccount() {
67 return mAccount;
68 }
69
Ficus Kirkpatrick35d5cc32014-02-08 11:15:04 -080070 // TODO: Figure out what to do about notifyAuthFailure
71 @SuppressWarnings("deprecation")
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080072 @Override
73 public String getAuthToken() throws AuthFailureError {
74 final AccountManager accountManager = AccountManager.get(mContext);
75 AccountManagerFuture<Bundle> future = accountManager.getAuthToken(mAccount,
Evan Charlton2ff99932013-04-30 12:07:29 -070076 mAuthTokenType, mNotifyAuthFailure, null, null);
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080077 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}