• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import account_appAccount from '@ohos.account.appAccount';
16
17var TAG = "[AccountTest]"
18var authenticator = null
19var accountLabels = {
20    "zhangsan": ["male", "30-40", "level4"],
21    "lisi": ["female"]
22}
23var accountCredentials = {
24    "zhangsan": {
25        "PIN": "123456",
26        "NUMBER": "12356789"
27    },
28    "lisi": {
29        "FACE": "X00001"
30    }
31}
32var accountRemovability = {
33    "zhangsan": false,
34    "lisi": true
35}
36var properties = {}
37
38class MyAuthenticator extends account_appAccount.Authenticator {
39
40    addAccountImplicitly(authType, callerBundleName, options, callback) {
41        console.log(TAG + "authType: " + authType + "callerBundleName: " + callerBundleName + ", options: " + JSON.stringify(options))
42        let appAccountMgr = account_appAccount.createAppAccountManager();
43        let newAccountName = "addNewAccountName"
44        appAccountMgr.createAccount(newAccountName, (err) => {
45            let authResult = {
46                name: newAccountName,
47                owner: "com.example.accountauthenticator"
48            }
49            callback.onResult(0, authResult);
50        });
51    }
52
53    authenticate(name, authType, callerBundleName, options, callback) {
54        console.log(TAG + "name: " + name + "authType: " + authType + "callerBundleName: " + callerBundleName + ", options: " + JSON.stringify(options))
55        let appAccountMgr = account_appAccount.createAppAccountManager();
56        appAccountMgr.createAccount(name, (err) => {
57            callback.onResult(0, {
58                name: name,
59                authType: authType,
60                token: "123456"
61            });
62        })
63    }
64
65    createAccountImplicitly(options, callback) {
66        console.log(TAG + "options: " + JSON.stringify(options))
67        let appAccountMgr = account_appAccount.createAppAccountManager();
68        let newAccountName = "createNewAccountName"
69        appAccountMgr.createAccount(newAccountName, (err) => {
70            let authResult = {
71                account: {
72                    name: newAccountName,
73                    owner: "com.example.accountauthenticator"
74                }
75            }
76            callback.onResult(0, authResult);
77        });
78    }
79
80    auth(name, authType, options, callback) {
81        console.log(TAG + "name: " + name + "authType: " + authType + ", options: " + JSON.stringify(options))
82        let authResult = {
83            account: {
84                name: name,
85                owner: "com.example.accountauthenticator"
86            },
87            tokenInfo: {
88                authType: "getSocialData",
89                token: "xxxxxxxxx"
90            }
91        }
92        callback.onResult(0, authResult);
93    }
94
95    verifyCredential(name, options, callback) {
96        console.log(TAG + "name: " + name + ", options: " + JSON.stringify(options))
97        if (name == "xiaoming") {
98            callback.onRequestContinued()
99            return
100        }
101        var credentialInfo = undefined
102        try {
103            credentialInfo = accountCredentials[name]
104        } catch (err) {
105            console.log(TAG + " no credential")
106            callback.onResult(0, {"booleanResult": false})
107            return
108        }
109        if (options.credentialType == undefined || options.credential == undefined) {
110            callback.onRequestRedirected({
111                bundleName: "com.example.accountauthenticator",
112                abilityName: "VerifyAbility",
113                parameters: {
114                    credentialType: options.credentialType, credential: options.credential
115                }
116            })
117            return
118        }
119        try {
120            var credential = credentialInfo[options.credentialType.toUpperCase()]
121            if (credential == options.credential) {
122                callback.onResult(0, {"booleanResult": true})
123            } else {
124                callback.onResult(0, {"booleanResult": false})
125            }
126        } catch(err) {
127            console.log(TAG + " check credential error");
128            callback.onResult(0, {"booleanResult": false});
129        }
130    }
131
132    checkAccountLabels(name, labels, callback) {
133        console.log(TAG + "name: " + name + ", labels: " + JSON.stringify(labels) + ", callback: " + callback)
134        if (labels.length == 0) {
135            callback.onResult(0, {"booleanResult": true})
136            return
137        }
138        var allLabels = []
139        try {
140            allLabels = accountLabels[name]
141        } catch (err) {
142            console.log("no labels")
143            allLabels == undefined
144        }
145        if (allLabels == undefined || allLabels.length == 0) {
146            callback.onResult(0, {"booleanResult": false})
147            return
148        }
149        for (var i = 0; i < labels.length; ++i) {
150            if (allLabels.indexOf(labels[i]) == -1) {
151                callback.onResult(0, {"booleanResult": false})
152                return
153            }
154        }
155        callback.onResult(0, {"booleanResult": true})
156    }
157
158    setProperties(options, callback) {
159        console.log(TAG + "options: " + JSON.stringify(options))
160        callback.onResult(10016, {})
161    }
162
163    checkAccountRemovable(name, callback) {
164        console.log(TAG + "name: " + name)
165        var isRemovable = false;
166        try {
167            isRemovable = accountRemovability[name]
168        } catch (err) {
169            console.log(TAG + "error: " + JSON.stringify(err))
170        }
171        callback.onResult(0, {"booleanResult": isRemovable})
172    }
173}
174
175export default {
176    onStart(want) {
177        console.info('ServiceAbility onStart');
178    },
179    async onStop() {
180        console.info('ServiceAbility onStop');
181    },
182    onConnect(want) {
183        console.info('ServiceAbility onConnect');
184        authenticator = new MyAuthenticator();
185        return authenticator.getRemoteObject();
186    },
187    onReconnect(want) {
188        console.info('ServiceAbility onReconnect');
189    },
190    onDisconnect() {
191        console.info('ServiceAbility onDisconnect');
192    },
193    onCommand(want, restart, startId) {
194        console.info('ServiceAbility onCommand');
195    }
196};