1 /*
2  * Copyright (C) 2015 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 
17 package com.android.cts.managedprofile;
18 
19 import android.accounts.AbstractAccountAuthenticator;
20 import android.accounts.Account;
21 import android.accounts.AccountAuthenticatorResponse;
22 import android.accounts.AccountManager;
23 import android.accounts.NetworkErrorException;
24 import android.content.Context;
25 import android.os.Bundle;
26 import android.util.Log;
27 
28 /* package */ class AccountAuthenticator extends AbstractAccountAuthenticator {
29     private static final String TAG = "AccountAuthenticator";
30     private static AccountAuthenticator sAuthenticator = null;
31     private static final String KEY_ACCOUNT_SECRET = "key_secret";
32     private static final String ACCOUNT_SECRET = "super_secret";
33     public static final String ACCOUNT_NAME = "CTS";
34     public static final String ACCOUNT_TYPE = "com.android.cts.test";
35     public static final Account TEST_ACCOUNT = new Account(ACCOUNT_NAME, ACCOUNT_TYPE);
36     private static final String AUTH_TOKEN = "authToken";
37     private static final String AUTH_TOKEN_LABEL = "authTokenLabel";
38 
39     private final Context mContext;
40 
AccountAuthenticator(Context context)41     private AccountAuthenticator(Context context) {
42         super(context);
43         mContext = context;
44     }
45 
createAuthTokenBundle()46     private Bundle createAuthTokenBundle() {
47         Bundle result = new Bundle();
48         result.putString(AccountManager.KEY_ACCOUNT_NAME, ACCOUNT_NAME);
49         result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
50         result.putString(AccountManager.KEY_AUTHTOKEN, AUTH_TOKEN);
51 
52         return result;
53     }
54 
createAccountSecretBundle()55     private Bundle createAccountSecretBundle() {
56         Bundle result = createAuthTokenBundle();
57         result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
58         result.putString(KEY_ACCOUNT_SECRET, ACCOUNT_SECRET);
59 
60         return result;
61     }
62 
createResultBundle(boolean value)63     private Bundle createResultBundle(boolean value) {
64         Bundle result = new Bundle();
65         result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, value);
66 
67         return result;
68     }
69 
70     @Override
addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options)71     public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
72             String authTokenType, String[] requiredFeatures, Bundle options)
73             throws NetworkErrorException {
74         return createAuthTokenBundle();
75     }
76 
77     @Override
editProperties(AccountAuthenticatorResponse response, String accountType)78     public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
79         return createAuthTokenBundle();
80     }
81 
82     @Override
updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)83     public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
84             String authTokenType, Bundle options) throws NetworkErrorException {
85         return createAuthTokenBundle();
86     }
87 
88     @Override
confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)89     public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
90             Bundle options) throws NetworkErrorException {
91 
92         Bundle result = new Bundle();
93         result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
94         return result;
95     }
96 
97     @Override
getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)98     public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
99             String authTokenType, Bundle options) throws NetworkErrorException {
100         return createAuthTokenBundle();
101     }
102 
103     @Override
getAuthTokenLabel(String authTokenType)104     public String getAuthTokenLabel(String authTokenType) {
105         return AUTH_TOKEN_LABEL;
106     }
107 
108     @Override
hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)109     public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
110             String[] features) throws NetworkErrorException {
111 
112         Bundle result = new Bundle();
113         result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
114         return result;
115     }
116 
117     @Override
getAccountCredentialsForCloning(AccountAuthenticatorResponse response, Account account)118     public Bundle getAccountCredentialsForCloning(AccountAuthenticatorResponse response, Account account) {
119         if (TEST_ACCOUNT.equals(account)) {
120             return createAccountSecretBundle();
121         } else {
122             Log.e(TAG, "failed in getAccountCredentialsForCloning. account: " + account);
123             return createResultBundle(false);
124         }
125     }
126 
127     @Override
addAccountFromCredentials(AccountAuthenticatorResponse response, Account account, Bundle accountCredentials)128     public Bundle addAccountFromCredentials(AccountAuthenticatorResponse response, Account account,
129             Bundle accountCredentials) {
130         if (accountCredentials != null && TEST_ACCOUNT.equals(account)
131                 && ACCOUNT_SECRET.equals(accountCredentials.getString(KEY_ACCOUNT_SECRET))) {
132             AccountManager.get(mContext).addAccountExplicitly(TEST_ACCOUNT, null, null);
133             return createResultBundle(true);
134         } else {
135             Log.e(TAG, "failed in addAccountFromCredentials. Bundle values: " + accountCredentials
136                     + " account: " + account);
137             return createResultBundle(false);
138         }
139     }
140 
getAuthenticator(Context context)141     public static synchronized AccountAuthenticator getAuthenticator(Context context) {
142         if (sAuthenticator == null) {
143             sAuthenticator = new AccountAuthenticator(context.getApplicationContext());
144         }
145         return sAuthenticator;
146     }
147 }
148