1 /*
2 * Copyright (c) 2022-2023 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 "domain_account_plugin_service.h"
17
18 #include "account_log_wrapper.h"
19
20 namespace OHOS {
21 namespace AccountSA {
DomainAccountPluginService(const std::shared_ptr<DomainAccountPlugin> & plugin)22 DomainAccountPluginService::DomainAccountPluginService(const std::shared_ptr<DomainAccountPlugin> &plugin)
23 : innerPlugin_(plugin)
24 {}
25
~DomainAccountPluginService()26 DomainAccountPluginService::~DomainAccountPluginService()
27 {}
28
CheckAndInitExecEnv(const sptr<IDomainAccountCallback> & callback,DomainAccountCallbackClient ** callbackClient)29 ErrCode DomainAccountPluginService::CheckAndInitExecEnv(const sptr<IDomainAccountCallback> &callback,
30 DomainAccountCallbackClient **callbackClient)
31 {
32 if (innerPlugin_ == nullptr) {
33 ACCOUNT_LOGE("innerPlugin_ is nullptr");
34 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
35 }
36 *callbackClient = new (std::nothrow) DomainAccountCallbackClient(callback);
37 if (*callbackClient == nullptr) {
38 ACCOUNT_LOGE("failed to create domain account callback client");
39 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
40 }
41 return ERR_OK;
42 }
43
AuthCommonInterface(const DomainAccountInfo & info,const std::vector<uint8_t> & authData,const sptr<IDomainAccountCallback> & callback,AuthMode authMode)44 ErrCode DomainAccountPluginService::AuthCommonInterface(const DomainAccountInfo &info,
45 const std::vector<uint8_t> &authData, const sptr<IDomainAccountCallback> &callback, AuthMode authMode)
46 {
47 if (innerPlugin_ == nullptr) {
48 ACCOUNT_LOGE("innerPlugin_ is nullptr");
49 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
50 }
51 auto callbackClient = std::make_shared<DomainAccountCallbackClient>(callback);
52 if (callbackClient == nullptr) {
53 ACCOUNT_LOGE("failed to create DomainAuthCallbackClient");
54 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
55 }
56 switch (authMode) {
57 case AUTH_WITH_CREDENTIAL_MODE: {
58 innerPlugin_->Auth(info, authData, callbackClient);
59 break;
60 }
61 case AUTH_WITH_POPUP_MODE: {
62 innerPlugin_->AuthWithPopup(info, callbackClient);
63 break;
64 }
65 case AUTH_WITH_TOKEN_MODE: {
66 innerPlugin_->AuthWithToken(info, authData, callbackClient);
67 break;
68 }
69 default: {
70 ACCOUNT_LOGE("authMode is invalid");
71 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
72 }
73 }
74 return ERR_OK;
75 }
76
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const sptr<IDomainAccountCallback> & callback)77 ErrCode DomainAccountPluginService::Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
78 const sptr<IDomainAccountCallback> &callback)
79 {
80 return AuthCommonInterface(info, password, callback, AUTH_WITH_CREDENTIAL_MODE);
81 }
82
AuthWithPopup(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)83 ErrCode DomainAccountPluginService::AuthWithPopup(
84 const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &callback)
85 {
86 return AuthCommonInterface(info, {}, callback, AUTH_WITH_POPUP_MODE);
87 }
88
AuthWithToken(const DomainAccountInfo & info,const std::vector<uint8_t> & token,const sptr<IDomainAccountCallback> & callback)89 ErrCode DomainAccountPluginService::AuthWithToken(
90 const DomainAccountInfo &info, const std::vector<uint8_t> &token, const sptr<IDomainAccountCallback> &callback)
91 {
92 return AuthCommonInterface(info, token, callback, AUTH_WITH_TOKEN_MODE);
93 }
94
IsAccountTokenValid(const AccountSA::DomainAccountInfo & info,const std::vector<uint8_t> & token,const sptr<IDomainAccountCallback> & callback)95 ErrCode DomainAccountPluginService::IsAccountTokenValid(const AccountSA::DomainAccountInfo &info,
96 const std::vector<uint8_t> &token, const sptr<IDomainAccountCallback> &callback)
97 {
98 DomainAccountCallbackClient *callbackClient = nullptr;
99 ErrCode errCode = CheckAndInitExecEnv(callback, &callbackClient);
100 if (errCode != ERR_OK) {
101 return errCode;
102 }
103 std::shared_ptr<DomainAccountCallbackClient> callbackPtr(callbackClient);
104 innerPlugin_->IsAccountTokenValid(info, token, callbackPtr);
105 return ERR_OK;
106 }
107
GetAccessToken(const DomainAccountInfo & domainInfo,const std::vector<uint8_t> & accountToken,const GetAccessTokenOptions & option,const sptr<IDomainAccountCallback> & callback)108 ErrCode DomainAccountPluginService::GetAccessToken(const DomainAccountInfo &domainInfo,
109 const std::vector<uint8_t> &accountToken, const GetAccessTokenOptions &option,
110 const sptr<IDomainAccountCallback> &callback)
111 {
112 DomainAccountCallbackClient *callbackClient = nullptr;
113 ErrCode errCode = CheckAndInitExecEnv(callback, &callbackClient);
114 if (errCode != ERR_OK) {
115 return errCode;
116 }
117 std::shared_ptr<DomainAccountCallbackClient> callbackPtr(callbackClient);
118 innerPlugin_->GetAccessToken(domainInfo, accountToken, option, callbackPtr);
119 return ERR_OK;
120 }
121
GetAuthStatusInfo(const DomainAccountInfo & accountInfo,const sptr<IDomainAccountCallback> & callback)122 ErrCode DomainAccountPluginService::GetAuthStatusInfo(
123 const DomainAccountInfo &accountInfo, const sptr<IDomainAccountCallback> &callback)
124 {
125 if (innerPlugin_ == nullptr) {
126 ACCOUNT_LOGE("innerPlugin_ is nullptr");
127 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
128 }
129 auto callbackClient = std::make_shared<DomainAccountCallbackClient>(callback);
130 if (callbackClient == nullptr) {
131 ACCOUNT_LOGE("failed to create DomainAccountCallbackClient");
132 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
133 }
134 innerPlugin_->GetAuthStatusInfo(accountInfo, callbackClient);
135 return ERR_OK;
136 }
137
GetDomainAccountInfo(const GetDomainAccountInfoOptions & options,const sptr<IDomainAccountCallback> & callback)138 ErrCode DomainAccountPluginService::GetDomainAccountInfo(
139 const GetDomainAccountInfoOptions &options, const sptr<IDomainAccountCallback> &callback)
140 {
141 DomainAccountCallbackClient *callbackClient = nullptr;
142 ErrCode errCode = CheckAndInitExecEnv(callback, &callbackClient);
143 if (errCode != ERR_OK) {
144 return errCode;
145 }
146 std::shared_ptr<DomainAccountCallbackClient> callbackPtr(callbackClient);
147 innerPlugin_->GetDomainAccountInfo(options, callbackPtr);
148 return ERR_OK;
149 }
150
OnAccountBound(const DomainAccountInfo & info,const int32_t localId,const sptr<IDomainAccountCallback> & callback)151 ErrCode DomainAccountPluginService::OnAccountBound(const DomainAccountInfo &info, const int32_t localId,
152 const sptr<IDomainAccountCallback> &callback)
153 {
154 DomainAccountCallbackClient *callbackClient = nullptr;
155 ErrCode errCode = CheckAndInitExecEnv(callback, &callbackClient);
156 if (errCode != ERR_OK) {
157 return errCode;
158 }
159 std::shared_ptr<DomainAccountCallbackClient> callbackPtr(callbackClient);
160 innerPlugin_->OnAccountBound(info, localId, callbackPtr);
161 return ERR_OK;
162 }
163
OnAccountUnBound(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)164 ErrCode DomainAccountPluginService::OnAccountUnBound(const DomainAccountInfo &info,
165 const sptr<IDomainAccountCallback> &callback)
166 {
167 DomainAccountCallbackClient *callbackClient = nullptr;
168 ErrCode errCode = CheckAndInitExecEnv(callback, &callbackClient);
169 if (errCode != ERR_OK) {
170 return errCode;
171 }
172 std::shared_ptr<DomainAccountCallbackClient> callbackPtr(callbackClient);
173 innerPlugin_->OnAccountUnBound(info, callbackPtr);
174 return ERR_OK;
175 }
176 } // namespace AccountSA
177 } // namespace OHOS