1 /*
2 * Copyright (c) 2022 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 */
15
16 #include "inner_domain_account_manager.h"
17 #include "account_log_wrapper.h"
18 #include "domain_account_plugin_death_recipient.h"
19 #include "domain_auth_callback_proxy.h"
20 #include "iinner_os_account_manager.h"
21
22 namespace OHOS {
23 namespace AccountSA {
RegisterPlugin(const sptr<IDomainAccountPlugin> & plugin)24 ErrCode InnerDomainAccountManager::RegisterPlugin(const sptr<IDomainAccountPlugin> &plugin)
25 {
26 std::lock_guard<std::mutex> lock(mutex_);
27 if (plugin == nullptr) {
28 ACCOUNT_LOGE("the registered plugin is nullptr");
29 return ERR_ACCOUNT_COMMON_INVALID_PARAMTER;
30 }
31 if (plugin_ != nullptr) {
32 ACCOUNT_LOGE("plugin already exists");
33 return ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_ALREADY_EXIST;
34 }
35 auto deathRecipient = GetDeathRecipient();
36 if ((deathRecipient == nullptr) || (!plugin->AsObject()->AddDeathRecipient(deathRecipient))) {
37 ACCOUNT_LOGE("failed to add death recipient for plugin");
38 return ERR_ACCOUNT_COMMON_ADD_DEATH_RECIPIENT;
39 }
40 plugin_ = plugin;
41 return ERR_OK;
42 }
43
UnregisterPlugin()44 void InnerDomainAccountManager::UnregisterPlugin()
45 {
46 std::lock_guard<std::mutex> lock(mutex_);
47 if ((plugin_ != nullptr) && (plugin_->AsObject() != nullptr)) {
48 plugin_->AsObject()->RemoveDeathRecipient(deathRecipient_);
49 }
50 plugin_ = nullptr;
51 deathRecipient_ = nullptr;
52 }
53
StartAuth(const sptr<IDomainAccountPlugin> & plugin,const DomainAccountInfo & info,const std::vector<uint8_t> & password,const sptr<IDomainAuthCallback> & callback)54 ErrCode InnerDomainAccountManager::StartAuth(const sptr<IDomainAccountPlugin> &plugin, const DomainAccountInfo &info,
55 const std::vector<uint8_t> &password, const sptr<IDomainAuthCallback> &callback)
56 {
57 if (callback == nullptr) {
58 ACCOUNT_LOGE("invalid callback");
59 return ERR_ACCOUNT_COMMON_INVALID_PARAMTER;
60 }
61 DomainAuthResult emptyResult = {};
62 if (plugin == nullptr) {
63 ACCOUNT_LOGE("plugin is nullptr");
64 callback->OnResult(ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_NOT_EXIST, emptyResult);
65 return ERR_ACCOUNT_COMMON_INVALID_PARAMTER;
66 }
67 ErrCode errCode = plugin->Auth(info, password, callback);
68 if (errCode != ERR_OK) {
69 ACCOUNT_LOGE("failed to auth domain account, errCode: %{public}d", errCode);
70 callback->OnResult(errCode, emptyResult);
71 return errCode;
72 }
73 return ERR_OK;
74 }
75
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const sptr<IDomainAuthCallback> & callback)76 ErrCode InnerDomainAccountManager::Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
77 const sptr<IDomainAuthCallback> &callback)
78 {
79 std::lock_guard<std::mutex> lock(mutex_);
80 auto handler = GetEventHandler();
81 if (handler == nullptr) {
82 ACCOUNT_LOGE("failed to create EventHandler");
83 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
84 }
85 AppExecFwk::InnerEvent::Callback task =
86 std::bind(&InnerDomainAccountManager::StartAuth, this, plugin_, info, password, callback);
87 if (!handler->PostTask(task)) {
88 ACCOUNT_LOGE("failed to post task");
89 return ERR_ACCOUNT_COMMON_POST_TASK;
90 }
91 return ERR_OK;
92 }
93
AuthUser(int32_t userId,const std::vector<uint8_t> & password,const sptr<IDomainAuthCallback> & callback)94 ErrCode InnerDomainAccountManager::AuthUser(int32_t userId, const std::vector<uint8_t> &password,
95 const sptr<IDomainAuthCallback> &callback)
96 {
97 OsAccountInfo accountInfo;
98 ErrCode errCode = IInnerOsAccountManager::GetInstance()->QueryOsAccountById(userId, accountInfo);
99 if (errCode != ERR_OK) {
100 ACCOUNT_LOGE("get os account info failed, errCode: %{public}d", errCode);
101 return errCode;
102 }
103 DomainAccountInfo domainInfo;
104 accountInfo.GetDomainInfo(domainInfo);
105 if (domainInfo.accountName_.empty()) {
106 ACCOUNT_LOGE("the target user is not a domain account");
107 return ERR_ACCOUNT_COMMON_INVALID_PARAMTER;
108 }
109 std::lock_guard<std::mutex> lock(mutex_);
110 auto handler = GetEventHandler();
111 if (handler == nullptr) {
112 ACCOUNT_LOGE("failed to create EventHandler");
113 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
114 }
115 AppExecFwk::InnerEvent::Callback task =
116 std::bind(&InnerDomainAccountManager::StartAuth, this, plugin_, domainInfo, password, callback);
117 if (!handler->PostTask(task)) {
118 ACCOUNT_LOGE("failed to post task");
119 return ERR_ACCOUNT_COMMON_POST_TASK;
120 }
121 return ERR_OK;
122 }
123
GetAuthProperty(const DomainAccountInfo & info,DomainAuthProperty & property)124 ErrCode InnerDomainAccountManager::GetAuthProperty(const DomainAccountInfo &info, DomainAuthProperty &property)
125 {
126 std::lock_guard<std::mutex> lock(mutex_);
127 if (plugin_ == nullptr) {
128 ACCOUNT_LOGE("plugin not exists");
129 return ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_NOT_EXIST;
130 }
131 return plugin_->GetAuthProperty(info, property);
132 }
133
GetEventHandler()134 std::shared_ptr<AppExecFwk::EventHandler> InnerDomainAccountManager::GetEventHandler()
135 {
136 if (handler_ != nullptr) {
137 return handler_;
138 }
139 handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create());
140 return handler_;
141 }
142
GetDeathRecipient()143 sptr<IRemoteObject::DeathRecipient> InnerDomainAccountManager::GetDeathRecipient()
144 {
145 if (deathRecipient_ != nullptr) {
146 return deathRecipient_;
147 }
148 deathRecipient_ = new (std::nothrow) DomainAccountPluginDeathRecipient();
149 return deathRecipient_;
150 }
151
IsPluginAvailable()152 bool InnerDomainAccountManager::IsPluginAvailable()
153 {
154 std::lock_guard<std::mutex> lock(mutex_);
155 return plugin_ != nullptr;
156 }
157 } // namespace AccountSA
158 } // namespace OHOS
159