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.test; | |
6 | |
7 import android.accounts.AbstractAccountAuthenticator; | |
8 import android.accounts.Account; | |
9 import android.accounts.AccountAuthenticatorResponse; | |
10 import android.accounts.AccountManager; | |
11 import android.accounts.AuthenticatorException; | |
12 import android.accounts.NetworkErrorException; | |
13 import android.accounts.OperationCanceledException; | |
14 import android.app.Activity; | |
15 import android.content.Context; | |
16 import android.os.Bundle; | |
17 | |
18 import org.chromium.base.ApplicationStatus; | |
19 import org.chromium.base.CalledByNative; | |
20 import org.chromium.net.HttpNegotiateConstants; | |
21 | |
22 import java.io.IOException; | |
23 | |
24 /** | |
25 * Dummy Android authenticator, to test SPNEGO/Keberos support on Android. This is deliberately | |
26 * minimal, and is not intended as an example of how to write a real SPNEGO Auth enticator. | |
27 */ | |
28 public class DummySpnegoAuthenticator extends AbstractAccountAuthenticator { | |
29 private static final String ACCOUNT_TYPE = "org.chromium.test.DummySpnegoAut henticator"; | |
30 private static final String ACCOUNT_NAME = "DummySpnegoAccount"; | |
31 private static int sResult; | |
32 private static String sToken; | |
33 private static final int GSS_S_COMPLETE = 0; | |
34 private static final int GSS_S_FAILURE = 2; | |
35 | |
36 /** | |
37 * @param context | |
38 */ | |
39 public DummySpnegoAuthenticator(Context context) { | |
40 super(context); | |
41 } | |
42 | |
43 @Override | |
44 public Bundle addAccount(AccountAuthenticatorResponse arg0, String accountTy pe, String arg2, | |
45 String[] arg3, Bundle arg4) throws NetworkErrorException { | |
46 Bundle result = new Bundle(); | |
47 result.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_B AD_REQUEST); | |
48 result.putString(AccountManager.KEY_ERROR_MESSAGE, "Can't add new SPNEGO accounts"); | |
49 return result; | |
50 } | |
51 | |
52 @Override | |
53 public Bundle confirmCredentials(AccountAuthenticatorResponse arg0, Account arg1, Bundle arg2) | |
54 throws NetworkErrorException { | |
55 Bundle result = new Bundle(); | |
56 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); | |
57 return result; | |
58 } | |
59 | |
60 @Override | |
61 public Bundle editProperties(AccountAuthenticatorResponse arg0, String arg1) { | |
62 return new Bundle(); | |
63 } | |
64 | |
65 @Override | |
66 public Bundle getAuthToken(AccountAuthenticatorResponse response, Account ac count, | |
67 String authTokenType, Bundle options) throws NetworkErrorException { | |
68 Bundle result = new Bundle(); | |
69 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); | |
70 result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); | |
71 result.putString(AccountManager.KEY_AUTHTOKEN, sToken); | |
72 result.putInt(HttpNegotiateConstants.KEY_SPNEGO_RESULT, sResult); | |
cbentzel
2015/06/30 12:53:55
Should this also be providing a SPNEGO_CONTEXT, wi
aberent
2015/07/02 21:13:37
We could do; however SPNEGO context handling is, I
cbentzel
2015/07/08 18:27:11
Acknowledged.
| |
73 return result; | |
74 } | |
75 | |
76 @Override | |
77 public String getAuthTokenLabel(String arg0) { | |
78 return "Spnego " + arg0; | |
79 } | |
80 | |
81 @Override | |
82 public Bundle hasFeatures(AccountAuthenticatorResponse arg0, Account arg1, S tring[] features) | |
83 throws NetworkErrorException { | |
84 Bundle result = new Bundle(); | |
85 for (String feature : features) { | |
86 if (!feature.equals(HttpNegotiateConstants.SPNEGO_FEATURE)) { | |
87 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); | |
88 return result; | |
89 } | |
90 } | |
91 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); | |
92 return result; | |
93 } | |
94 | |
95 @Override | |
96 public Bundle updateCredentials(AccountAuthenticatorResponse arg0, Account a rg1, String arg2, | |
97 Bundle arg3) throws NetworkErrorException { | |
98 Bundle result = new Bundle(); | |
99 result.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_B AD_REQUEST); | |
100 result.putString(AccountManager.KEY_ERROR_MESSAGE, "Can't add new SPNEGO accounts"); | |
101 return result; | |
102 } | |
103 | |
104 /** | |
105 * Called from tests, sets up the test account, if it doesn't already exist | |
106 */ | |
107 @CalledByNative | |
108 private static void ensureTestAccountExists() { | |
109 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); | |
110 AccountManager am = AccountManager.get(activity); | |
111 Account account = new Account(ACCOUNT_NAME, ACCOUNT_TYPE); | |
112 am.addAccountExplicitly(account, null, null); | |
113 } | |
114 | |
115 /** | |
116 * Called from tests to tidy up test accounts. | |
117 */ | |
118 @SuppressWarnings("deprecation") | |
cbentzel
2015/06/30 12:53:55
why was this @SuppressWarnings needed? Is it due t
aberent
2015/07/02 21:13:37
Yes. It is for removeAccount. I don't know of a cl
| |
119 @CalledByNative | |
120 private static void removeTestAccounts() { | |
121 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); | |
122 AccountManager am = AccountManager.get(activity); | |
123 String features[] = {HttpNegotiateConstants.SPNEGO_FEATURE}; | |
124 try { | |
125 Account accounts[] = | |
126 am.getAccountsByTypeAndFeatures(ACCOUNT_TYPE, features, null , null).getResult(); | |
127 for (Account account : accounts) { | |
128 // Deprecated, but the replacement not available on Android JB. | |
129 am.removeAccount(account, null, null).getResult(); | |
130 } | |
131 } catch (OperationCanceledException | AuthenticatorException | IOExcepti on e) { | |
132 // Should never happen. This is tidy-up after the tests. Ignore. | |
133 } | |
134 } | |
135 | |
136 /** | |
137 * Called from tests to set the result that should be returned from the next call to | |
138 * getAuthToken. | |
139 * @param gssApiResult The GSSAPI result. | |
140 * @param token The token to return. | |
141 */ | |
142 @CalledByNative | |
143 private static void setNextResult(int gssApiResult, String token) { | |
144 // This only handles the result values currently used in the tests. | |
145 switch (gssApiResult) { | |
146 case GSS_S_COMPLETE: | |
147 sResult = 0; | |
148 break; | |
149 case GSS_S_FAILURE: | |
150 sResult = HttpNegotiateConstants.ERR_MISSING_AUTH_CREDENTIALS; | |
151 break; | |
152 default: | |
153 sResult = HttpNegotiateConstants.ERR_UNEXPECTED; | |
154 break; | |
155 } | |
156 sToken = token; | |
157 } | |
158 } | |
OLD | NEW |