OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.net; |
| 6 |
| 7 import android.accounts.AccountManager; |
| 8 import android.accounts.AccountManagerCallback; |
| 9 import android.accounts.AccountManagerFuture; |
| 10 import android.accounts.AuthenticatorException; |
| 11 import android.accounts.OperationCanceledException; |
| 12 import android.app.Activity; |
| 13 import android.os.Bundle; |
| 14 import android.os.Handler; |
| 15 import android.os.Parcelable; |
| 16 |
| 17 import org.chromium.base.ApplicationStatus; |
| 18 import org.chromium.base.CalledByNative; |
| 19 import org.chromium.base.JNINamespace; |
| 20 import org.chromium.base.VisibleForTesting; |
| 21 |
| 22 import java.io.IOException; |
| 23 |
| 24 /** |
| 25 * Class to get Auth Tokens for HTTP Negotiate authentication (typically used fo
r Kerberos) |
| 26 * An instance of this class is created for each separate negotiation. |
| 27 */ |
| 28 @JNINamespace("net::android") |
| 29 public class HttpNegotiateAuthenticator { |
| 30 static final String KEY_INCOMING_AUTH_TOKEN = "incomingAuthToken"; |
| 31 static final String KEY_SPNEGO_CONTEXT = "spnegoContext"; |
| 32 |
| 33 static final String SPNEGO_FEATURE = "SPNEGO"; |
| 34 static final String SPNEGO_TOKEN_TYPE_BASE = "SPNEGO:HOSTBASED:"; |
| 35 |
| 36 private Parcelable mSpnegoContext = null; |
| 37 private final long mNativeObject; |
| 38 |
| 39 private HttpNegotiateAuthenticator(long nativeObject) { |
| 40 mNativeObject = nativeObject; |
| 41 } |
| 42 |
| 43 @CalledByNative |
| 44 private static HttpNegotiateAuthenticator create(long nativeObject) { |
| 45 return new HttpNegotiateAuthenticator(nativeObject); |
| 46 } |
| 47 |
| 48 /** |
| 49 * |
| 50 * @param accountType The Android account type; |
| 51 * @param principal The principal (must be host based). |
| 52 * @param authToken The previous auth token, if any. |
| 53 * @return false for immediate failure, true otherwise. |
| 54 */ |
| 55 @VisibleForTesting |
| 56 @CalledByNative |
| 57 boolean getNextAuthToken(String accountType, String principal, String authTo
ken) { |
| 58 assert accountType != null; |
| 59 assert principal != null; |
| 60 String authTokenType = SPNEGO_TOKEN_TYPE_BASE + principal; |
| 61 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); |
| 62 if (activity == null) return false; |
| 63 AccountManager am = AccountManager.get(activity); |
| 64 String features[] = {SPNEGO_FEATURE}; |
| 65 |
| 66 Bundle options = new Bundle(); |
| 67 |
| 68 if (authToken != null) options.putString(KEY_INCOMING_AUTH_TOKEN, authTo
ken); |
| 69 if (mSpnegoContext != null) options.putParcelable(KEY_SPNEGO_CONTEXT, mS
pnegoContext); |
| 70 |
| 71 am.getAuthTokenByFeatures(accountType, authTokenType, features, activity
, null, |
| 72 options, new AccountManagerCallback<Bundle>() { |
| 73 |
| 74 @Override |
| 75 public void run(AccountManagerFuture<Bundle> future) { |
| 76 try { |
| 77 Bundle result = future.getResult(); |
| 78 mSpnegoContext = result.getParcelable(KEY_SPNEGO_CON
TEXT); |
| 79 nativeSetResult(mNativeObject, true, |
| 80 result.getString(AccountManager.KEY_AUTHTOKE
N)); |
| 81 } catch (OperationCanceledException | AuthenticatorExcep
tion |
| 82 | IOException e) { |
| 83 nativeSetResult(mNativeObject, false, null); |
| 84 } |
| 85 } |
| 86 |
| 87 }, new Handler()); |
| 88 return true; |
| 89 } |
| 90 |
| 91 private native void nativeSetResult( |
| 92 long nativeAndroidAuthNegotiate, boolean result, String authToken); |
| 93 } |
OLD | NEW |