• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package com.android.cts.devicepolicy.accountcheck.tester;
17 
18 import android.accounts.AbstractAccountAuthenticator;
19 import android.accounts.Account;
20 import android.accounts.AccountAuthenticatorResponse;
21 import android.accounts.AccountManager;
22 import android.accounts.NetworkErrorException;
23 import android.app.Service;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.os.IBinder;
28 import android.os.SystemClock;
29 import android.text.TextUtils;
30 import android.util.Log;
31 
32 import java.util.Arrays;
33 import java.util.List;
34 
35 public class TestAuthenticator extends Service {
36     private static final String TAG = "TestAuthenticator";
37 
38     private static final String ACCOUNT_TYPE = "com.android.cts.devicepolicy.authcheck.tester";
39 
40 
41     private static Authenticator sInstance;
42 
43     @Override
onBind(Intent intent)44     public IBinder onBind(Intent intent) {
45         if (sInstance == null) {
46             sInstance = new Authenticator(getApplicationContext());
47 
48         }
49         return sInstance.getIBinder();
50     }
51 
52     @Override
onStartCommand(Intent intent, int flags, int startId)53     public int onStartCommand(Intent intent, int flags, int startId) {
54         if ("add_account".equals(intent.getAction())) {
55             final String[] features = intent.getStringArrayExtra("features");
56             createAccount(this, ACCOUNT_TYPE, features);
57         }
58 
59         stopSelf();
60         return 0;
61     }
62 
createAccount(Context context, String accountType, String[] requiredFeatures)63     private static Account createAccount(Context context, String accountType,
64             String[] requiredFeatures) {
65         if (requiredFeatures == null) {
66             requiredFeatures = new String[0];
67         }
68 
69         final String name = SystemClock.elapsedRealtimeNanos()
70                 + ":" + TextUtils.join(",", requiredFeatures);
71 
72         Log.i(TAG, "Adding account '" + name + "' for " + accountType
73                 + "... " + Arrays.asList(requiredFeatures));
74 
75         final Account account = new Account(name, accountType);
76         context.getSystemService(AccountManager.class).addAccountExplicitly(
77                 account, "password", new Bundle());
78         return account;
79     }
80 
81     public static class Authenticator extends AbstractAccountAuthenticator {
82 
83         private final Context mContxet;
84 
Authenticator(Context context)85         public Authenticator(Context context) {
86             super(context);
87             mContxet = context;
88         }
89 
90         @Override
addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options)91         public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
92                 String authTokenType, String[] requiredFeatures, Bundle options) {
93             return new Bundle();
94         }
95 
96         @Override
editProperties(AccountAuthenticatorResponse response, String accountType)97         public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
98             return new Bundle();
99         }
100 
101         @Override
updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)102         public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
103                 String authTokenType, Bundle options) throws NetworkErrorException {
104             return new Bundle();
105         }
106 
107         @Override
confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)108         public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
109                 Bundle options) throws NetworkErrorException {
110             return new Bundle();
111         }
112 
113         @Override
getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)114         public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
115                 String authTokenType, Bundle options) throws NetworkErrorException {
116             return new Bundle();
117         }
118 
119         @Override
getAuthTokenLabel(String authTokenType)120         public String getAuthTokenLabel(String authTokenType) {
121             return "token_label";
122         }
123 
124         @Override
hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)125         public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
126                 String[] features) throws NetworkErrorException {
127 
128             final int p = account.name.indexOf(':');
129 
130             boolean hasAll = true;
131             final List<String> hasFeatures =
132                     Arrays.asList(TextUtils.split(account.name.substring(p + 1), ","));
133             for (String requested : features) {
134                 if (!hasFeatures.contains(requested)) {
135                     hasAll = false;
136                     break;
137                 }
138             }
139 
140             Log.i(TAG, "Checking feature for account '" + account + "' for features="
141                     + Arrays.asList(features) + " result=" + hasAll);
142 
143             Bundle result = new Bundle();
144             result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, hasAll);
145             return result;
146         }
147     }
148 }
149