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 #ifndef OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_AUTHENTICATOR_SESSION_MANAGER_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_AUTHENTICATOR_SESSION_MANAGER_H
18 
19 #include "application_state_observer_stub.h"
20 #include "app_account_common.h"
21 #include "app_account_info.h"
22 #include "app_mgr_interface.h"
23 #include "app_mgr_proxy.h"
24 #include "iapp_account_authenticator_callback.h"
25 #include "iremote_object.h"
26 #include "singleton.h"
27 #include "want_params.h"
28 
29 namespace OHOS {
30 namespace AccountSA {
31 class AppAccountAuthenticatorSession;
32 class AppAccountCheckLabelsSession;
33 class AppAccountAuthenticatorSessionManager;
34 
35 class SessionAppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
36 public:
37     explicit SessionAppStateObserver();
38     ~SessionAppStateObserver() override = default;
39 
40     void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override;
41 };
42 
43 class AppAccountAuthenticatorSessionManager {
44 public:
45     static AppAccountAuthenticatorSessionManager &GetInstance();
46     ErrCode AddAccountImplicitly(const AuthenticatorSessionRequest &request);
47     ErrCode Authenticate(const AuthenticatorSessionRequest &request);
48     ErrCode CreateAccountImplicitly(const AuthenticatorSessionRequest &request);
49     ErrCode Auth(const AuthenticatorSessionRequest &request);
50     ErrCode GetAuthenticatorCallback(const AuthenticatorSessionRequest &request, sptr<IRemoteObject> &callback);
51     ErrCode VerifyCredential(const AuthenticatorSessionRequest &request);
52     ErrCode CheckAccountLabels(const AuthenticatorSessionRequest &request);
53     ErrCode IsAccountRemovable(const AuthenticatorSessionRequest &request);
54     ErrCode SelectAccountsByOptions(
55         const std::vector<AppAccountInfo> accounts, const AuthenticatorSessionRequest &request);
56     ErrCode SetAuthenticatorProperties(const AuthenticatorSessionRequest &request);
57     void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData);
58     void Init();
59     void CloseSession(const std::string &sessionId);
60     ErrCode OpenSession(const std::shared_ptr<AppAccountAuthenticatorSession> &session);
61     std::shared_ptr<AppAccountAuthenticatorSession> GetSession(const std::string &sessionId);
62     void OnSessionServerDied(const std::string &sessionId);
63     void OnSessionAbilityConnectDone(const std::string &sessionId, const AppExecFwk::ElementName &element,
64         const sptr<IRemoteObject> &remoteObject, int32_t resultCode);
65     void OnSessionAbilityDisconnectDone(
66         const std::string &sessionId, const AppExecFwk::ElementName &element, int resultCode);
67     void OnSessionResult(const std::string &sessionId, int32_t resultCode, const AAFwk::Want &result);
68     void OnSessionRequestRedirected(const std::string &sessionId, AAFwk::Want &request);
69     void OnSessionRequestContinued(const std::string &sessionId);
70 
71 private:
72     AppAccountAuthenticatorSessionManager() = default;
73     ~AppAccountAuthenticatorSessionManager();
74     DISALLOW_COPY_AND_MOVE(AppAccountAuthenticatorSessionManager);
75     void RegisterApplicationStateObserver();
76     void UnregisterApplicationStateObserver();
77 
78 private:
79     std::mutex mutex_;
80     sptr<AppExecFwk::IAppMgr> iAppMgr_;
81     sptr<SessionAppStateObserver> appStateObserver_;
82     std::map<std::string, std::shared_ptr<AppAccountAuthenticatorSession>> sessionMap_;
83     std::map<std::string, std::set<std::string>> abilitySessions_;
84     bool isInitialized_ = false;
85 };
86 }  // namespace AccountSA
87 }  // namespace OHOS
88 
89 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_AUTHENTICATOR_SESSION_MANAGER_H
90