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 | |
16 import org.chromium.base.ApplicationStatus; | |
17 import org.chromium.base.CalledByNative; | |
18 import org.chromium.base.JNINamespace; | |
19 import org.chromium.base.ThreadUtils; | |
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) An | |
26 * 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"; | |
cbentzel
2015/06/11 20:51:43
Nit: private rather than package-private? (been a
aberent
2015/06/15 15:52:19
Done.
| |
31 static final String KEY_SPNEGO_CONTEXT = "spnegoContext"; | |
32 static final String KEY_CAN_DELEGATE = "canDelegate"; | |
33 | |
34 static final String SPNEGO_FEATURE = "SPNEGO"; | |
35 static final String SPNEGO_TOKEN_TYPE_BASE = "SPNEGO:HOSTBASED:"; | |
36 | |
37 private String mSpnegoContext = null; | |
38 private final long mNativeObject; | |
39 private final String mAccountType; | |
40 private AccountManagerFuture<Bundle> mFuture; | |
41 | |
42 private HttpNegotiateAuthenticator(long nativeObject, String accountType) { | |
43 assert !android.text.TextUtils.isEmpty(accountType); | |
44 mNativeObject = nativeObject; | |
45 mAccountType = accountType; | |
46 } | |
47 | |
48 /** | |
49 * @param nativeObject The corresponding AndroidAuthNegotiate C++ object | |
50 * @param accountType The Android account type to use. | |
51 */ | |
52 @VisibleForTesting | |
53 @CalledByNative | |
54 static HttpNegotiateAuthenticator create(long nativeObject, String accountTy pe) { | |
55 return new HttpNegotiateAuthenticator(nativeObject, accountType); | |
56 } | |
57 | |
58 /** | |
59 * @param principal The principal (must be host based). | |
60 * @param authToken The previous auth token, if any. | |
61 * @return false for immediate failure, true otherwise. | |
62 */ | |
63 @VisibleForTesting | |
64 @CalledByNative | |
65 boolean getNextAuthToken(String principal, String authToken, boolean canDele gate) { | |
66 assert principal != null; | |
67 String authTokenType = SPNEGO_TOKEN_TYPE_BASE + principal; | |
68 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); | |
69 if (activity == null) return false; | |
70 AccountManager am = AccountManager.get(activity); | |
71 String features[] = {SPNEGO_FEATURE}; | |
72 | |
73 Bundle options = new Bundle(); | |
74 | |
75 if (authToken != null) options.putString(KEY_INCOMING_AUTH_TOKEN, authTo ken); | |
76 if (mSpnegoContext != null) options.putString(KEY_SPNEGO_CONTEXT, mSpneg oContext); | |
77 options.putBoolean(KEY_CAN_DELEGATE, canDelegate); | |
78 | |
79 mFuture = am.getAuthTokenByFeatures(mAccountType, authTokenType, feature s, activity, null, | |
80 options, new AccountManagerCallback<Bundle>() { | |
81 | |
82 @Override | |
83 public void run(AccountManagerFuture<Bundle> future) { | |
84 try { | |
85 Bundle result = future.getResult(); | |
86 mSpnegoContext = result.getString(KEY_SPNEGO_CONTEXT ); | |
87 nativeSetResult(mNativeObject, true, | |
88 result.getString(AccountManager.KEY_AUTHTOKE N)); | |
89 } catch (OperationCanceledException | AuthenticatorExcep tion | |
90 | IOException e) { | |
91 nativeSetResult(mNativeObject, false, null); | |
92 } | |
93 } | |
94 | |
95 }, new Handler(ThreadUtils.getUiThreadLooper())); | |
96 return true; | |
97 } | |
98 | |
99 @VisibleForTesting | |
100 native void nativeSetResult(long nativeAndroidAuthNegotiate, boolean result, String authToken); | |
101 } | |
OLD | NEW |