Index: tools/android/kerberos/SpnegoAuthenticator/src/org/chromium/tools/spnegoauthenticator/SpnegoAuthenticator.java |
diff --git a/tools/android/kerberos/SpnegoAuthenticator/src/org/chromium/tools/spnegoauthenticator/SpnegoAuthenticator.java b/tools/android/kerberos/SpnegoAuthenticator/src/org/chromium/tools/spnegoauthenticator/SpnegoAuthenticator.java |
index 16133ad6c952b385a5717c957fdb4b5a09ac661b..7d2c08485a73fa53b0de7811c6284ca15fa15ee9 100644 |
--- a/tools/android/kerberos/SpnegoAuthenticator/src/org/chromium/tools/spnegoauthenticator/SpnegoAuthenticator.java |
+++ b/tools/android/kerberos/SpnegoAuthenticator/src/org/chromium/tools/spnegoauthenticator/SpnegoAuthenticator.java |
@@ -9,7 +9,11 @@ |
import android.accounts.AccountAuthenticatorResponse; |
import android.accounts.AccountManager; |
import android.accounts.NetworkErrorException; |
+import android.app.Notification; |
+import android.app.NotificationManager; |
+import android.app.PendingIntent; |
import android.content.Context; |
+import android.content.Intent; |
import android.os.Bundle; |
import org.chromium.base.Log; |
@@ -18,66 +22,85 @@ |
import java.util.Arrays; |
/** |
- * AccountAuthenticator implementation that automatically creates a dummy account and returns a |
- * dummy token when asked. |
+ * AccountAuthenticator implementation |
*/ |
public class SpnegoAuthenticator extends AbstractAccountAuthenticator { |
- private static final String TAG = "tools_SpnegoAuth"; |
- private static final String ACCOUNT_NAME = "DummySpnegoAccount"; |
+ |
+ private static final String TAG = Constants.TAG; |
+ private final Context mContext; |
/** |
* @param context |
*/ |
public SpnegoAuthenticator(Context context) { |
super(context); |
- ensureTestAccountExists(context); |
+ mContext = context; |
} |
@Override |
- public Bundle addAccount(AccountAuthenticatorResponse arg0, String accountType, String arg2, |
- String[] arg3, Bundle arg4) throws NetworkErrorException { |
- Log.w(TAG, "addAccount(): Not supported."); |
- Bundle result = new Bundle(); |
- result.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_REQUEST); |
- result.putString(AccountManager.KEY_ERROR_MESSAGE, "Can't add new SPNEGO accounts"); |
- return result; |
+ public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, |
+ String authTokenType, String[] requiredFeatures, Bundle options) |
+ throws NetworkErrorException { |
+ Log.d(TAG, "addAccount()"); |
+ |
+ // Delegate to the activity to get the account information from the user. |
+ Bundle bundle = new Bundle(); |
+ bundle.putParcelable(AccountManager.KEY_INTENT, |
+ SpnegoAuthenticatorActivity.getAddAccountIntent(mContext, response)); |
+ return bundle; |
} |
@Override |
- public Bundle confirmCredentials(AccountAuthenticatorResponse arg0, Account arg1, Bundle arg2) |
- throws NetworkErrorException { |
- Bundle result = new Bundle(); |
- result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
- return result; |
+ public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, |
+ Bundle options) throws NetworkErrorException { |
+ Log.d(TAG, "confirmCredentials(%s)", account.name); |
+ return unsupportedOperationBundle("confirmCredentials"); |
} |
@Override |
- public Bundle editProperties(AccountAuthenticatorResponse arg0, String arg1) { |
- return new Bundle(); |
+ public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { |
+ Log.d(TAG, "editProperties(%s)", accountType); |
+ return unsupportedOperationBundle("editProperties"); |
} |
@Override |
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, |
String authTokenType, Bundle options) throws NetworkErrorException { |
+ Log.d(TAG, "getAuthToken(%s)", account.name); |
+ |
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, "DummyAuthToken"); |
- result.putInt(HttpNegotiateConstants.KEY_SPNEGO_RESULT, 0); |
- Log.d(TAG, "getAuthToken(): Returning dummy SPNEGO auth token"); |
+ if (AccountData.get(account.name, mContext).isAuthenticated()) { |
+ Log.d(TAG, "getAuthToken(): Returning dummy SPNEGO auth token"); |
+ result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
+ result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
+ result.putString(AccountManager.KEY_AUTHTOKEN, Constants.AUTH_TOKEN); |
+ result.putInt(HttpNegotiateConstants.KEY_SPNEGO_RESULT, 0); |
+ } else { |
+ Log.d(TAG, "getAuthToken(): Asking for credentials confirmation"); |
+ Intent intent = SpnegoAuthenticatorActivity.getConfirmCredentialsIntent( |
+ mContext, account.name, response); |
+ result.putParcelable(AccountManager.KEY_INTENT, intent); |
+ |
+ // We need to show a notification in case the caller can't use the intent directly. |
+ showConfirmCredentialsNotification(mContext, intent); |
+ } |
+ |
return result; |
} |
@Override |
public String getAuthTokenLabel(String authTokenType) { |
+ Log.d(TAG, "getAuthTokenLabel(%s)", authTokenType); |
return "Spnego " + authTokenType; |
} |
@Override |
- public Bundle hasFeatures(AccountAuthenticatorResponse arg0, Account arg1, String[] features) |
- throws NetworkErrorException { |
+ public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, |
+ String[] features) throws NetworkErrorException { |
Log.d(TAG, "hasFeatures(%s)", Arrays.asList(features)); |
Bundle result = new Bundle(); |
+ |
+ // All our accounts only have the SPNEGO feature, other features are not supported. |
for (String feature : features) { |
if (!feature.equals(HttpNegotiateConstants.SPNEGO_FEATURE)) { |
result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
@@ -89,18 +112,34 @@ public Bundle hasFeatures(AccountAuthenticatorResponse arg0, Account arg1, Strin |
} |
@Override |
- public Bundle updateCredentials(AccountAuthenticatorResponse arg0, Account arg1, String arg2, |
- Bundle arg3) throws NetworkErrorException { |
- Log.w(TAG, "updateCredentials(): Not supported."); |
- Bundle result = new Bundle(); |
- result.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_REQUEST); |
- result.putString(AccountManager.KEY_ERROR_MESSAGE, "Can't update credentials."); |
- return result; |
+ public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, |
+ String authTokenType, Bundle options) throws NetworkErrorException { |
+ Log.d(TAG, "updateCredentials(%s)", account.name); |
+ return unsupportedOperationBundle("updateCredentials"); |
} |
- private void ensureTestAccountExists(Context context) { |
- AccountManager am = AccountManager.get(context); |
- Account account = new Account(ACCOUNT_NAME, context.getString(R.string.account_type)); |
- am.addAccountExplicitly(account, null, null); |
+ private void showConfirmCredentialsNotification(Context context, Intent intent) { |
+ PendingIntent notificationAction = |
+ PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
+ Notification notification = new Notification.Builder(context) |
+ .setContentTitle("Authentication required") |
+ .setContentText("Credential confirmation required for the Spnego account") |
+ .setSmallIcon(android.R.drawable.stat_sys_warning) |
+ .setContentIntent(notificationAction) |
+ .setAutoCancel(true).build(); |
+ |
+ NotificationManager notificationManager = |
+ (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
+ |
+ notificationManager.notify(Constants.CONFIRM_CREDENTIAL_NOTIFICATION_ID, notification); |
+ } |
+ |
+ /** Returns a bundle containing a standard error response. */ |
+ private Bundle unsupportedOperationBundle(String operationName) { |
+ Bundle result = new Bundle(); |
+ result.putInt( |
+ AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION); |
+ result.putString(AccountManager.KEY_ERROR_MESSAGE, "Unsupported method: " + operationName); |
+ return result; |
} |
} |