Index: net/android/java/src/org/chromium/net/HttpNegotiateAuthenticator.java |
diff --git a/net/android/java/src/org/chromium/net/HttpNegotiateAuthenticator.java b/net/android/java/src/org/chromium/net/HttpNegotiateAuthenticator.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..16e7dacf497ebeac22f8c02217f696c0e9fa2d77 |
--- /dev/null |
+++ b/net/android/java/src/org/chromium/net/HttpNegotiateAuthenticator.java |
@@ -0,0 +1,101 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.net; |
+ |
+import android.accounts.AccountManager; |
+import android.accounts.AccountManagerCallback; |
+import android.accounts.AccountManagerFuture; |
+import android.accounts.AuthenticatorException; |
+import android.accounts.OperationCanceledException; |
+import android.app.Activity; |
+import android.os.Bundle; |
+import android.os.Handler; |
+import android.os.Parcelable; |
+ |
+import org.chromium.base.ApplicationStatus; |
+import org.chromium.base.CalledByNative; |
+import org.chromium.base.JNINamespace; |
+import org.chromium.base.VisibleForTesting; |
+ |
+import java.io.IOException; |
+ |
+/** |
+ * Class to get Auth Tokens for HTTP Negotiate authentication (typically used for Kerberos) |
+ * An instance of this class is created for each separate negotiation. |
+ */ |
+@JNINamespace("net::android") |
+public class HttpNegotiateAuthenticator { |
+ static final String KEY_INCOMING_AUTH_TOKEN = "incomingAuthToken"; |
+ static final String KEY_SPNEGO_CONTEXT = "spnegoContext"; |
+ static final String KEY_CAN_DELEGATE = "canDelegate"; |
+ |
+ static final String SPNEGO_FEATURE = "SPNEGO"; |
+ static final String SPNEGO_TOKEN_TYPE_BASE = "SPNEGO:HOSTBASED:"; |
+ |
+ private Parcelable mSpnegoContext = null; |
+ private final long mNativeObject; |
+ private String mAccountType; |
Bernhard Bauer
2015/05/18 15:11:53
Make this final?
aberent
2015/05/18 17:07:30
Done.
|
+ |
+ private HttpNegotiateAuthenticator(long nativeObject, String accountType) { |
+ mNativeObject = nativeObject; |
+ mAccountType = accountType; |
+ } |
+ |
+ /** |
+ * object creator |
Bernhard Bauer
2015/05/18 15:11:53
Nit: Capitalize and add punctuation (or remove it)
aberent
2015/05/18 17:07:30
Removed.
|
+ * @param nativeObject The corresponding AndroidAuthNegotiate C++ object |
+ * @param accountType The Android account type to use. |
+ */ |
+ @CalledByNative |
+ private static HttpNegotiateAuthenticator create(long nativeObject, String accountType) { |
+ return new HttpNegotiateAuthenticator(nativeObject, accountType); |
+ } |
+ |
+ /** |
+ * |
+ * @param principal The principal (must be host based). |
+ * @param authToken The previous auth token, if any. |
+ * @return false for immediate failure, true otherwise. |
+ */ |
+ @VisibleForTesting |
+ @CalledByNative |
+ boolean getNextAuthToken(String principal, String authToken, boolean canDelegate) { |
+ assert mAccountType != null; |
Bernhard Bauer
2015/05/18 15:11:53
Assert this in the constructor?
aberent
2015/05/18 17:07:30
Yes, although actually I should be testing for emp
|
+ assert principal != null; |
+ String authTokenType = SPNEGO_TOKEN_TYPE_BASE + principal; |
+ Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); |
+ if (activity == null) return false; |
+ AccountManager am = AccountManager.get(activity); |
+ String features[] = {SPNEGO_FEATURE}; |
+ |
+ Bundle options = new Bundle(); |
+ |
+ if (authToken != null) options.putString(KEY_INCOMING_AUTH_TOKEN, authToken); |
+ if (mSpnegoContext != null) options.putParcelable(KEY_SPNEGO_CONTEXT, mSpnegoContext); |
+ options.putBoolean(KEY_CAN_DELEGATE, canDelegate); |
+ |
+ am.getAuthTokenByFeatures(mAccountType, authTokenType, features, activity, null, |
+ options, new AccountManagerCallback<Bundle>() { |
+ |
+ @Override |
+ public void run(AccountManagerFuture<Bundle> future) { |
+ try { |
+ Bundle result = future.getResult(); |
+ mSpnegoContext = result.getParcelable(KEY_SPNEGO_CONTEXT); |
+ nativeSetResult(mNativeObject, true, |
+ result.getString(AccountManager.KEY_AUTHTOKEN)); |
+ } catch (OperationCanceledException | AuthenticatorException |
+ | IOException e) { |
+ nativeSetResult(mNativeObject, false, null); |
+ } |
+ } |
+ |
+ }, new Handler()); |
+ return true; |
+ } |
+ |
+ private native void nativeSetResult( |
+ long nativeAndroidAuthNegotiate, boolean result, String authToken); |
+} |