TestAccountType1Authenticator.java revision c1a892a967a902ba0407f206aede78b0a2c49e1e
1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.android.server.accounts; 17 18import android.accounts.AbstractAccountAuthenticator; 19import android.accounts.Account; 20import android.accounts.AccountAuthenticatorResponse; 21import android.accounts.AccountManager; 22import android.accounts.NetworkErrorException; 23import android.content.Context; 24import android.content.Intent; 25import android.os.Bundle; 26 27import com.android.frameworks.servicestests.R; 28 29import java.util.concurrent.atomic.AtomicInteger; 30 31/** 32 * This authenticator is to mock account authenticator to test AccountManagerService. 33 */ 34public class TestAccountType1Authenticator extends AbstractAccountAuthenticator { 35 private final AtomicInteger mTokenCounter = new AtomicInteger(0); 36 37 private final String mAccountType; 38 private final Context mContext; 39 40 public TestAccountType1Authenticator(Context context, String accountType) { 41 super(context); 42 mAccountType = accountType; 43 mContext = context; 44 } 45 46 @Override 47 public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { 48 throw new UnsupportedOperationException( 49 "editProperties is not yet supported by the TestAccountAuthenticator"); 50 } 51 52 @Override 53 public Bundle addAccount( 54 AccountAuthenticatorResponse response, 55 String accountType, 56 String authTokenType, 57 String[] requiredFeatures, 58 Bundle options) throws NetworkErrorException { 59 if (!mAccountType.equals(accountType)) { 60 throw new IllegalArgumentException("Request to the wrong authenticator!"); 61 } 62 63 Bundle result = new Bundle(); 64 result.putString(AccountManager.KEY_ACCOUNT_NAME, "test_account@test.com"); 65 result.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); 66 return result; 67 } 68 69 @Override 70 public Bundle confirmCredentials( 71 AccountAuthenticatorResponse response, 72 Account account, 73 Bundle options) throws NetworkErrorException { 74 throw new UnsupportedOperationException( 75 "confirmCredentials is not yet supported by the TestAccountAuthenticator"); 76 } 77 78 @Override 79 public Bundle getAuthToken( 80 AccountAuthenticatorResponse response, 81 Account account, 82 String authTokenType, 83 Bundle options) throws NetworkErrorException { 84 throw new UnsupportedOperationException( 85 "getAuthToken is not yet supported by the TestAccountAuthenticator"); 86 } 87 88 @Override 89 public String getAuthTokenLabel(String authTokenType) { 90 throw new UnsupportedOperationException( 91 "getAuthTokenLabel is not yet supported by the TestAccountAuthenticator"); 92 } 93 94 @Override 95 public Bundle updateCredentials( 96 AccountAuthenticatorResponse response, 97 Account account, 98 String authTokenType, 99 Bundle options) throws NetworkErrorException { 100 if (!mAccountType.equals(account.type)) { 101 throw new IllegalArgumentException("Request to the wrong authenticator!"); 102 } 103 Bundle result = new Bundle(); 104 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 105 result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); 106 return result; 107 } 108 109 @Override 110 public Bundle hasFeatures( 111 AccountAuthenticatorResponse response, 112 Account account, 113 String[] features) throws NetworkErrorException { 114 throw new UnsupportedOperationException( 115 "hasFeatures is not yet supported by the TestAccountAuthenticator"); 116 } 117 118 @Override 119 public Bundle startAddAccountSession( 120 AccountAuthenticatorResponse response, 121 String accountType, 122 String authTokenType, 123 String[] requiredFeatures, 124 Bundle options) throws NetworkErrorException { 125 if (!mAccountType.equals(accountType)) { 126 throw new IllegalArgumentException("Request to the wrong authenticator!"); 127 } 128 129 String accountName = null; 130 Bundle sessionBundle = null; 131 String password = null; 132 if (options != null) { 133 accountName = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME); 134 sessionBundle = options.getBundle( 135 AccountManagerServiceTestFixtures.KEY_ACCOUNT_SESSION_BUNDLE); 136 password = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_PASSWORD); 137 } 138 139 Bundle result = new Bundle(); 140 if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 141 // fill bundle with a success result. 142 result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle); 143 result.putString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN, 144 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 145 result.putString(AccountManager.KEY_PASSWORD, password); 146 result.putString(AccountManager.KEY_AUTHTOKEN, 147 Integer.toString(mTokenCounter.incrementAndGet())); 148 } else if (accountName.equals( 149 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 150 // Specify data to be returned by the eventual activity. 151 Intent eventualActivityResultData = new Intent(); 152 eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN, 153 Integer.toString(mTokenCounter.incrementAndGet())); 154 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_STATUS_TOKEN, 155 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 156 eventualActivityResultData.putExtra(AccountManager.KEY_PASSWORD, password); 157 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, 158 sessionBundle); 159 // Fill result with Intent. 160 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 161 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 162 eventualActivityResultData); 163 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 164 165 result.putParcelable(AccountManager.KEY_INTENT, intent); 166 } else { 167 // fill with error 168 fillResultWithError(result, options); 169 } 170 171 return result; 172 } 173 174 @Override 175 public Bundle startUpdateCredentialsSession( 176 AccountAuthenticatorResponse response, 177 Account account, 178 String authTokenType, 179 Bundle options) 180 throws NetworkErrorException { 181 182 if (!mAccountType.equals(account.type)) { 183 throw new IllegalArgumentException("Request to the wrong authenticator!"); 184 } 185 186 String accountName = null; 187 Bundle sessionBundle = null; 188 if (options != null) { 189 accountName = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME); 190 sessionBundle = options.getBundle( 191 AccountManagerServiceTestFixtures.KEY_ACCOUNT_SESSION_BUNDLE); 192 } 193 194 Bundle result = new Bundle(); 195 if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 196 // fill bundle with a success result. 197 result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle); 198 result.putString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN, 199 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 200 result.putString(AccountManager.KEY_PASSWORD, 201 AccountManagerServiceTestFixtures.ACCOUNT_PASSWORD); 202 result.putString(AccountManager.KEY_AUTHTOKEN, 203 Integer.toString(mTokenCounter.incrementAndGet())); 204 } else if (accountName.equals( 205 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 206 // Specify data to be returned by the eventual activity. 207 Intent eventualActivityResultData = new Intent(); 208 eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN, 209 Integer.toString(mTokenCounter.incrementAndGet())); 210 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_STATUS_TOKEN, 211 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 212 eventualActivityResultData.putExtra(AccountManager.KEY_PASSWORD, 213 AccountManagerServiceTestFixtures.ACCOUNT_PASSWORD); 214 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, 215 sessionBundle); 216 // Fill result with Intent. 217 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 218 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 219 eventualActivityResultData); 220 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 221 222 result.putParcelable(AccountManager.KEY_INTENT, intent); 223 } else { 224 // fill with error 225 fillResultWithError(result, options); 226 } 227 return result; 228 } 229 230 @Override 231 public Bundle finishSession(AccountAuthenticatorResponse response, 232 String accountType, 233 Bundle sessionBundle) throws NetworkErrorException { 234 235 if (!mAccountType.equals(accountType)) { 236 throw new IllegalArgumentException("Request to the wrong authenticator!"); 237 } 238 239 String accountName = null; 240 if (sessionBundle != null) { 241 accountName = sessionBundle.getString( 242 AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME); 243 } 244 245 Bundle result = new Bundle(); 246 if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 247 // add sessionBundle into result for verification purpose 248 result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle); 249 // fill bundle with a success result. 250 result.putString(AccountManager.KEY_ACCOUNT_NAME, 251 AccountManagerServiceTestFixtures.ACCOUNT_NAME); 252 result.putString(AccountManager.KEY_ACCOUNT_TYPE, 253 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 254 result.putString(AccountManager.KEY_AUTHTOKEN, 255 Integer.toString(mTokenCounter.incrementAndGet())); 256 } else if (accountName.equals( 257 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 258 // Specify data to be returned by the eventual activity. 259 Intent eventualActivityResultData = new Intent(); 260 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, 261 AccountManagerServiceTestFixtures.ACCOUNT_NAME); 262 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, 263 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 264 eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN, 265 Integer.toString(mTokenCounter.incrementAndGet())); 266 267 // Fill result with Intent. 268 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 269 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 270 eventualActivityResultData); 271 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 272 273 result.putParcelable(AccountManager.KEY_INTENT, intent); 274 } else { 275 // fill with error 276 fillResultWithError(result, sessionBundle); 277 } 278 return result; 279 } 280 281 @Override 282 public Bundle isCredentialsUpdateSuggested( 283 final AccountAuthenticatorResponse response, 284 Account account, 285 String statusToken) throws NetworkErrorException { 286 287 Bundle result = new Bundle(); 288 if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 289 // fill bundle with a success result. 290 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); 291 } else { 292 // fill with error 293 fillResultWithError( 294 result, 295 AccountManager.ERROR_CODE_INVALID_RESPONSE, 296 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 297 } 298 299 response.onResult(result); 300 return null; 301 } 302 303 private void fillResultWithError(Bundle result, Bundle options) { 304 int errorCode = AccountManager.ERROR_CODE_INVALID_RESPONSE; 305 String errorMsg = "Default Error Message"; 306 if (options != null) { 307 errorCode = options.getInt(AccountManager.KEY_ERROR_CODE); 308 errorMsg = options.getString(AccountManager.KEY_ERROR_MESSAGE); 309 } 310 fillResultWithError(result, errorCode, errorMsg); 311 } 312 313 private void fillResultWithError(Bundle result, int errorCode, String errorMsg) { 314 result.putInt(AccountManager.KEY_ERROR_CODE, errorCode); 315 result.putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg); 316 } 317}