• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #include "appaccount_common.h"
16 
17 #include "account_error_no.h"
18 #include "account_log_wrapper.h"
19 #include "app_account_constants.h"
20 #include "app_account_manager.h"
21 
22 namespace OHOS::AccountSA {
AuthenticatorAsyncCallback(std::function<void (RetDataBool)> cRef,std::function<void (ErrCArrAppAccountInfo)> sRef)23 AuthenticatorAsyncCallback::AuthenticatorAsyncCallback(
24     std::function<void(RetDataBool)> cRef,
25     std::function<void(ErrCArrAppAccountInfo)> sRef)
26     : checkAccountLabelsCallbackRef(cRef), selectAccountsCallbackRef(sRef) {}
27 
~AuthenticatorAsyncCallback()28 AuthenticatorAsyncCallback::~AuthenticatorAsyncCallback() {}
29 
OnResult(int32_t resultCode,const AAFwk::Want & result)30 ErrCode AuthenticatorAsyncCallback::OnResult(int32_t resultCode, const AAFwk::Want &result)
31 {
32     {
33         std::lock_guard<std::mutex> lock(mutex_);
34         if (isDone) {
35             return ERR_OK;
36         }
37         isDone = true;
38     }
39 
40     this->errCode = resultCode;
41     this->onResultRetBool = result.GetBoolParam(Constants::KEY_BOOLEAN_RESULT, false);
42     this->onResultRetNames = result.GetStringArrayParam(Constants::KEY_ACCOUNT_NAMES);
43     this->onResultRetOwners = result.GetStringArrayParam(Constants::KEY_ACCOUNT_OWNERS);
44     return ERR_OK;
45 }
46 
OnRequestRedirected(const AAFwk::Want & request)47 ErrCode AuthenticatorAsyncCallback::OnRequestRedirected(const AAFwk::Want &request)
48 {
49     return ERR_OK;
50 }
51 
OnRequestContinued()52 ErrCode AuthenticatorAsyncCallback::OnRequestContinued()
53 {
54     return ERR_OK;
55 }
56 
CallbackEnter(uint32_t code)57 ErrCode AuthenticatorAsyncCallback::CallbackEnter([[maybe_unused]] uint32_t code)
58 {
59     return ERR_OK;
60 }
61 
CallbackExit(uint32_t code,int32_t result)62 ErrCode AuthenticatorAsyncCallback::CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result)
63 {
64     switch (code) {
65         case static_cast<uint32_t>(IAppAccountAuthenticatorCallbackIpcCode::COMMAND_ON_RESULT): {
66             if (result == ERR_INVALID_DATA) {
67                 AAFwk::Want resultWant;
68                 OnResult(ERR_JS_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION, resultWant);
69                 return ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE;
70             }
71             break;
72         }
73         case static_cast<uint32_t>(IAppAccountAuthenticatorCallbackIpcCode::COMMAND_ON_REQUEST_REDIRECTED): {
74             if (result == ERR_INVALID_DATA) {
75                 AAFwk::Want request;
76                 OnRequestRedirected(request);
77                 return ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE;
78             }
79             break;
80         }
81         default:
82             return ERR_NONE;
83     }
84     return ERR_NONE;
85 }
86 
AppAccountManagerCallback(JSAuthCallback callback)87 AppAccountManagerCallback::AppAccountManagerCallback(JSAuthCallback callback): callback_(callback) {}
88 
~AppAccountManagerCallback()89 AppAccountManagerCallback::~AppAccountManagerCallback() {}
90 
OnResult(int32_t resultCode,const AAFwk::Want & result)91 ErrCode AppAccountManagerCallback::OnResult(int32_t resultCode, const AAFwk::Want &result)
92 {
93     {
94         std::lock_guard<std::mutex> lock(mutex_);
95         if (isDone) {
96             return ERR_OK;
97         }
98         isDone = true;
99     }
100 
101     this->errCode = resultCode;
102     // account: AppAccountInfo
103     this->nameResult = result.GetStringParam(Constants::KEY_ACCOUNT_NAMES);
104     this->ownerResult = result.GetStringParam(Constants::KEY_ACCOUNT_OWNERS);
105     //tokenInfo: AuthTokenInfo
106     this->authTypeResult = result.GetStringParam(Constants::KEY_AUTH_TYPE);
107     this->tokenResult = result.GetStringParam(Constants::KEY_TOKEN);
108     return ERR_OK;
109 }
110 
OnRequestRedirected(const AAFwk::Want & request)111 ErrCode AppAccountManagerCallback::OnRequestRedirected(const AAFwk::Want &request)
112 {
113     return ERR_OK;
114 }
115 
OnRequestContinued()116 ErrCode AppAccountManagerCallback::OnRequestContinued()
117 {
118     return ERR_OK;
119 }
120 
CallbackEnter(uint32_t code)121 ErrCode AppAccountManagerCallback::CallbackEnter([[maybe_unused]] uint32_t code)
122 {
123     return ERR_OK;
124 }
125 
CallbackExit(uint32_t code,int32_t result)126 ErrCode AppAccountManagerCallback::CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result)
127 {
128     switch (code) {
129         case static_cast<uint32_t>(IAppAccountAuthenticatorCallbackIpcCode::COMMAND_ON_RESULT): {
130             if (result == ERR_INVALID_DATA) {
131                 AAFwk::Want resultWant;
132                 OnResult(ERR_JS_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION, resultWant);
133                 return ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE;
134             }
135             break;
136         }
137         case static_cast<uint32_t>(IAppAccountAuthenticatorCallbackIpcCode::COMMAND_ON_REQUEST_REDIRECTED): {
138             if (result == ERR_INVALID_DATA) {
139                 AAFwk::Want request;
140                 OnRequestRedirected(request);
141                 return ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE;
142             }
143             break;
144         }
145         default:
146             return ERR_NONE;
147     }
148     return ERR_NONE;
149 }
150 
SubscribePtr(const AppAccountSubscribeInfo & subscribeInfo)151 SubscribePtr::SubscribePtr(const AppAccountSubscribeInfo &subscribeInfo) : AppAccountSubscriber(subscribeInfo) {}
152 
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)153 void SubscribePtr::OnAccountsChanged(const std::vector<AppAccountInfo>  &accounts) {}
SetCallbackRef(std::function<void (CArrAppAccountInfo)> callbackRef)154 void SubscribePtr::SetCallbackRef(std::function<void(CArrAppAccountInfo)> callbackRef)
155 {
156     ref_ = callbackRef;
157 }
158 } // namespace::OHOS::AccountSA