• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_account_authenticator_session_manager.h"
17 
18 #include "account_log_wrapper.h"
19 #include "app_account_authenticator_session.h"
20 #include "app_account_check_labels_session.h"
21 #include "app_mgr_constants.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 
25 namespace OHOS {
26 namespace AccountSA {
27 namespace {
28 constexpr size_t SESSION_MAX_NUM = 256;
29 }
30 
~AppAccountAuthenticatorSessionManager()31 AppAccountAuthenticatorSessionManager::~AppAccountAuthenticatorSessionManager()
32 {
33     sessionMap_.clear();
34 }
35 
GetInstance()36 AppAccountAuthenticatorSessionManager &AppAccountAuthenticatorSessionManager::GetInstance()
37 {
38     static AppAccountAuthenticatorSessionManager instance;
39     return instance;
40 }
41 
AddAccountImplicitly(const AuthenticatorSessionRequest & request)42 ErrCode AppAccountAuthenticatorSessionManager::AddAccountImplicitly(const AuthenticatorSessionRequest &request)
43 {
44     auto session = std::make_shared<AppAccountAuthenticatorSession>(ADD_ACCOUNT_IMPLICITLY, request);
45     return OpenSession(session);
46 }
47 
CreateAccountImplicitly(const AuthenticatorSessionRequest & request)48 ErrCode AppAccountAuthenticatorSessionManager::CreateAccountImplicitly(const AuthenticatorSessionRequest &request)
49 {
50     auto session = std::make_shared<AppAccountAuthenticatorSession>(CREATE_ACCOUNT_IMPLICITLY, request);
51     return OpenSession(session);
52 }
53 
Authenticate(const AuthenticatorSessionRequest & request)54 ErrCode AppAccountAuthenticatorSessionManager::Authenticate(const AuthenticatorSessionRequest &request)
55 {
56     auto session = std::make_shared<AppAccountAuthenticatorSession>(AUTHENTICATE, request);
57     return OpenSession(session);
58 }
59 
Auth(const AuthenticatorSessionRequest & request)60 ErrCode AppAccountAuthenticatorSessionManager::Auth(const AuthenticatorSessionRequest &request)
61 {
62     auto session = std::make_shared<AppAccountAuthenticatorSession>(AUTH, request);
63     return OpenSession(session);
64 }
65 
VerifyCredential(const AuthenticatorSessionRequest & request)66 ErrCode AppAccountAuthenticatorSessionManager::VerifyCredential(const AuthenticatorSessionRequest &request)
67 {
68     auto session = std::make_shared<AppAccountAuthenticatorSession>(VERIFY_CREDENTIAL, request);
69     return OpenSession(session);
70 }
71 
CheckAccountLabels(const AuthenticatorSessionRequest & request)72 ErrCode AppAccountAuthenticatorSessionManager::CheckAccountLabels(const AuthenticatorSessionRequest &request)
73 {
74     auto session = std::make_shared<AppAccountAuthenticatorSession>(CHECK_ACCOUNT_LABELS, request);
75     return OpenSession(session);
76 }
77 
IsAccountRemovable(const AuthenticatorSessionRequest & request)78 ErrCode AppAccountAuthenticatorSessionManager::IsAccountRemovable(const AuthenticatorSessionRequest &request)
79 {
80     auto session = std::make_shared<AppAccountAuthenticatorSession>(IS_ACCOUNT_REMOVABLE, request);
81     return OpenSession(session);
82 }
83 
SelectAccountsByOptions(const std::vector<AppAccountInfo> accounts,const AuthenticatorSessionRequest & request)84 ErrCode AppAccountAuthenticatorSessionManager::SelectAccountsByOptions(
85     const std::vector<AppAccountInfo> accounts, const AuthenticatorSessionRequest &request)
86 {
87     auto session = std::make_shared<AppAccountCheckLabelsSession>(accounts, request);
88     OpenSession(session);
89     return session->CheckLabels();
90 }
91 
SetAuthenticatorProperties(const AuthenticatorSessionRequest & request)92 ErrCode AppAccountAuthenticatorSessionManager::SetAuthenticatorProperties(const AuthenticatorSessionRequest &request)
93 {
94     auto session = std::make_shared<AppAccountAuthenticatorSession>(SET_AUTHENTICATOR_PROPERTIES, request);
95     return OpenSession(session);
96 }
97 
OpenSession(const std::shared_ptr<AppAccountAuthenticatorSession> & session)98 ErrCode AppAccountAuthenticatorSessionManager::OpenSession(
99     const std::shared_ptr<AppAccountAuthenticatorSession> &session)
100 {
101     if (session == nullptr) {
102         ACCOUNT_LOGE("failed to create AppAccountAuthenticatorSession");
103         return ERR_APPACCOUNT_SERVICE_OAUTH_SERVICE_EXCEPTION;
104     }
105     std::string sessionId = session->GetSessionId();
106     ErrCode result = ERR_OK;
107     {
108         std::lock_guard<std::mutex> lock(mutex_);
109         if (sessionMap_.size() == SESSION_MAX_NUM) {
110             ACCOUNT_LOGD("app account mgr service is busy");
111             return ERR_APPACCOUNT_SERVICE_OAUTH_BUSY;
112         }
113         result = session->Open();
114         if (result != ERR_OK) {
115             ACCOUNT_LOGE("failed to open session, result: %{public}d.", result);
116             return result;
117         }
118         sessionMap_.emplace(sessionId, session);
119         AuthenticatorSessionRequest request;
120         session->GetRequest(request);
121     }
122     session->AddClientDeathRecipient();
123     return ERR_OK;
124 }
125 
GetSession(const std::string & sessionId)126 std::shared_ptr<AppAccountAuthenticatorSession> AppAccountAuthenticatorSessionManager::GetSession(
127     const std::string &sessionId)
128 {
129     std::lock_guard<std::mutex> lock(mutex_);
130     auto it = sessionMap_.find(sessionId);
131     if (it == sessionMap_.end()) {
132         return nullptr;
133     }
134     return it->second;
135 }
136 
GetAuthenticatorCallback(const AuthenticatorSessionRequest & request,sptr<IRemoteObject> & callback)137 ErrCode AppAccountAuthenticatorSessionManager::GetAuthenticatorCallback(
138     const AuthenticatorSessionRequest &request, sptr<IRemoteObject> &callback)
139 {
140     callback = nullptr;
141     std::lock_guard<std::mutex> lock(mutex_);
142     auto it = sessionMap_.find(request.sessionId);
143     if ((it == sessionMap_.end()) || (it->second == nullptr)) {
144         ACCOUNT_LOGE("failed to find a session by id=%{private}s.", request.sessionId.c_str());
145         return ERR_APPACCOUNT_SERVICE_OAUTH_SESSION_NOT_EXIST;
146     }
147     return it->second->GetAuthenticatorCallback(request, callback);
148 }
149 
OnSessionServerDied(const std::string & sessionId)150 void AppAccountAuthenticatorSessionManager::OnSessionServerDied(const std::string &sessionId)
151 {
152     auto session = GetSession(sessionId);
153     if (session != nullptr) {
154         session->OnServerDied();
155     }
156 }
157 
OnSessionAbilityConnectDone(const std::string & sessionId,const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)158 void AppAccountAuthenticatorSessionManager::OnSessionAbilityConnectDone(const std::string &sessionId,
159     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
160 {
161     auto session = GetSession(sessionId);
162     if (session != nullptr) {
163         session->OnAbilityConnectDone(element, remoteObject, resultCode);
164     }
165 }
166 
OnSessionAbilityDisconnectDone(const std::string & sessionId,const AppExecFwk::ElementName & element,int resultCode)167 void AppAccountAuthenticatorSessionManager::OnSessionAbilityDisconnectDone(
168     const std::string &sessionId, const AppExecFwk::ElementName &element, int resultCode)
169 {
170     auto session = GetSession(sessionId);
171     if (session != nullptr) {
172         session->OnAbilityDisconnectDone(element, resultCode);
173     }
174 }
175 
OnSessionResult(const std::string & sessionId,int32_t resultCode,const AAFwk::Want & result)176 void AppAccountAuthenticatorSessionManager::OnSessionResult(
177     const std::string &sessionId, int32_t resultCode, const AAFwk::Want &result)
178 {
179     auto session = GetSession(sessionId);
180     if (session != nullptr) {
181         session->OnResult(resultCode, result);
182     }
183 }
184 
OnSessionRequestRedirected(const std::string & sessionId,AAFwk::Want & request)185 void AppAccountAuthenticatorSessionManager::OnSessionRequestRedirected(
186     const std::string &sessionId, AAFwk::Want &request)
187 {
188     auto session = GetSession(sessionId);
189     if (session != nullptr) {
190         session->OnRequestRedirected(request);
191     }
192 }
193 
OnSessionRequestContinued(const std::string & sessionId)194 void AppAccountAuthenticatorSessionManager::OnSessionRequestContinued(const std::string &sessionId)
195 {
196     auto session = GetSession(sessionId);
197     if (session != nullptr) {
198         session->OnRequestContinued();
199     }
200 }
201 
CloseSession(const std::string & sessionId)202 void AppAccountAuthenticatorSessionManager::CloseSession(const std::string &sessionId)
203 {
204     std::lock_guard<std::mutex> lock(mutex_);
205     auto it = sessionMap_.find(sessionId);
206     if (it == sessionMap_.end()) {
207         ACCOUNT_LOGI("session not exist, sessionId=%{private}s", sessionId.c_str());
208         return;
209     }
210     AuthenticatorSessionRequest request;
211     it->second->GetRequest(request);
212     sessionMap_.erase(it);
213 }
214 }  // namespace AccountSA
215 }  // namespace OHOS
216