summaryrefslogtreecommitdiff
path: root/common/device-side/bedstead/testapp/src/testapps/main/java/com/android/bedstead/testapp/TestAppAccountAuthenticator.java
blob: 558215ee20b4df8f3f51d7b6ec84eaa4d3e19cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bedstead.testapp;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

/**
 * An account authenticator which can be configured by tests.
 */
public final class TestAppAccountAuthenticator extends AbstractAccountAuthenticator {
    private static TestAppAccountAuthenticator sMockAuthenticator = null;
    private static final String ACCOUNT_NAME
            = "com.android.bedstead.testapp.AccountManagementApp.account.name";
    private static final String AUTH_TOKEN = "mockAuthToken";
    private static final String AUTH_TOKEN_LABEL = "mockAuthTokenLabel";
    private static final String ACCOUNT_PASSWORD = "password";

    public static synchronized TestAppAccountAuthenticator getAuthenticator(Context context) {
        if (null == sMockAuthenticator) {
            sMockAuthenticator = new TestAppAccountAuthenticator(context);
        }
        return sMockAuthenticator;
    }

    private final Context mContext;

    private TestAppAccountAuthenticator(Context context) {
        super(context);
        mContext = context;
    }

    private Bundle createResultBundle(String accountType) {
        return createResultBundle(accountType, ACCOUNT_NAME);
    }

    private Bundle createResultBundle(String accountType, String name) {
        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
        result.putString(AccountManager.KEY_AUTHTOKEN, AUTH_TOKEN);
        return result;
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
            String authTokenType, String[] requiredFeatures, Bundle options)
            throws NetworkErrorException {

        String name = options.getString("name", ACCOUNT_NAME);
        String password = options.getString("password", ACCOUNT_PASSWORD);
        ArrayList<String> features = options.getStringArrayList("features");
        if (features == null) {
            features = new ArrayList<>();
        }

        Account account = new Account(name, accountType);
        AccountManager accountManager = mContext.getSystemService(AccountManager.class);
        accountManager.addAccountExplicitly(account, password, new Bundle());

        accountManager.setUserData(account, "features", String.join(",", features));

        return createResultBundle(accountType, name);
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
        return createResultBundle(accountType);
    }

    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
            String authTokenType, Bundle options) throws NetworkErrorException {
        AccountManager accountManager = mContext.getSystemService(AccountManager.class);
        if (options.containsKey("features")) {
            accountManager.setUserData(account, "features",
                    String.join(",", options.getStringArrayList("features")));
        }

        return createResultBundle(/* accountType= */ null);
    }

    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
            Bundle options) throws NetworkErrorException {

        Bundle result = new Bundle();
        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
        return result;
    }

    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
            String authTokenType, Bundle options) throws NetworkErrorException {
        return createResultBundle(/* accountType= */ null);
    }

    @Override
    public String getAuthTokenLabel(String authTokenType) {
        return AUTH_TOKEN_LABEL;
    }

    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
            String[] features) throws NetworkErrorException {
        boolean hasFeatures;
        AccountManager accountManager = mContext.getSystemService(AccountManager.class);
        if (accountManager.getUserData(account, "features") == null) {
            hasFeatures = false;
        } else {
            hasFeatures = Arrays.asList(accountManager.getUserData(account, "features")
                    .split(",")).containsAll(Set.of(features));
        }

        Bundle result = new Bundle();
        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, hasFeatures);
        return result;
    }
}