1 /*
2  * Copyright (c) 2024 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 REMOTE_AUTH_CONTEXT_H
17 #define REMOTE_AUTH_CONTEXT_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 
23 #include "authentication_impl.h"
24 #include "remote_executor_proxy.h"
25 #include "simple_auth_context.h"
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 struct RemoteAuthContextParam {
31     AuthType authType;
32     std::string connectionName;
33     std::string collectorNetworkId;
34     std::vector<uint8_t> executorInfoMsg;
35 };
36 
37 class RemoteAuthContext : public SimpleAuthContext {
38 public:
39     RemoteAuthContext(uint64_t contextId, std::shared_ptr<Authentication> auth, RemoteAuthContextParam &param,
40         std::shared_ptr<ContextCallback> callback);
41     ~RemoteAuthContext() override;
42     ContextType GetContextType() const override;
43 
44     void SetExecutorInfoMsg(std::vector<uint8_t> msg);
45 
46     void OnMessage(const std::string &connectionName, const std::string &srcEndPoint,
47         const std::shared_ptr<Attributes> &request, std::shared_ptr<Attributes> &reply);
48     void OnConnectStatus(const std::string &connectionName, ConnectStatus connectStatus);
49 
50     void OnTimeOut();
51 
52 protected:
53     bool OnStart() override;
54 
55 private:
56     bool StartAuth();
57     void StartAuthDelayed();
58     bool SetupConnection();
59     bool SendQueryExecutorInfoMsg();
60 
61     std::recursive_mutex mutex_;
62     int32_t authType_ = INVALID_AUTH_TYPE;
63     std::string connectionName_;
64     std::string collectorNetworkId_;
65     std::vector<uint8_t> executorInfoMsg_;
66 
67     bool needSetupConnection_ = false;
68     std::string endPointName_;
69     std::shared_ptr<RemoteExecutorProxy> remoteExecutorProxy_ = nullptr;
70     std::optional<uint32_t> cancelTimerId_ = std::nullopt;
71 };
72 } // namespace UserAuth
73 } // namespace UserIam
74 } // namespace OHOS
75 #endif // REMOTE_AUTH_CONTEXT_H