| 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..1b94fb419b90cbd69b2d01c131e47e6165297306
|
| --- /dev/null
|
| +++ b/net/android/java/src/org/chromium/net/HttpNegotiateAuthenticator.java
|
| @@ -0,0 +1,103 @@
|
| +// 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 org.chromium.base.ApplicationStatus;
|
| +import org.chromium.base.CalledByNative;
|
| +import org.chromium.base.JNINamespace;
|
| +import org.chromium.base.ThreadUtils;
|
| +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 {
|
| + private static final String KEY_INCOMING_AUTH_TOKEN = "incomingAuthToken";
|
| + private static final String KEY_SPNEGO_CONTEXT = "spnegoContext";
|
| + private static final String KEY_CAN_DELEGATE = "canDelegate";
|
| +
|
| + private static final String SPNEGO_FEATURE = "SPNEGO";
|
| + private static final String SPNEGO_TOKEN_TYPE_BASE = "SPNEGO:HOSTBASED:";
|
| +
|
| + private String mSpnegoContext = null;
|
| + private final String mAccountType;
|
| + private AccountManagerFuture<Bundle> mFuture;
|
| +
|
| + private HttpNegotiateAuthenticator(String accountType) {
|
| + assert !android.text.TextUtils.isEmpty(accountType);
|
| + mAccountType = accountType;
|
| + }
|
| +
|
| + /**
|
| + * @param nativeObject The corresponding HttpAuthNegotiateAndroid C++ object
|
| + * @param accountType The Android account type to use.
|
| + */
|
| + @VisibleForTesting
|
| + @CalledByNative
|
| + static HttpNegotiateAuthenticator create(String accountType) {
|
| + return new HttpNegotiateAuthenticator(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
|
| + void getNextAuthToken(final long nativeResultObject, final String principal, String authToken,
|
| + boolean canDelegate) {
|
| + assert principal != null;
|
| + String authTokenType = SPNEGO_TOKEN_TYPE_BASE + principal;
|
| + Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
|
| + if (activity == null) {
|
| + nativeSetResult(nativeResultObject, false, null);
|
| + return;
|
| + }
|
| + 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.putString(KEY_SPNEGO_CONTEXT, mSpnegoContext);
|
| + options.putBoolean(KEY_CAN_DELEGATE, canDelegate);
|
| +
|
| + mFuture = 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.getString(KEY_SPNEGO_CONTEXT);
|
| + nativeSetResult(nativeResultObject, true,
|
| + result.getString(AccountManager.KEY_AUTHTOKEN));
|
| + } catch (OperationCanceledException | AuthenticatorException
|
| + | IOException e) {
|
| + nativeSetResult(nativeResultObject, false, null);
|
| + }
|
| + }
|
| +
|
| + }, new Handler(ThreadUtils.getUiThreadLooper()));
|
| + }
|
| +
|
| + @VisibleForTesting
|
| + native void nativeSetResult(
|
| + long nativeJavaNegotiateResultWrapper, boolean result, String authToken);
|
| +}
|
|
|