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_INVOKER_CONTEXT_H
17 #define REMOTE_AUTH_INVOKER_CONTEXT_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <optional>
23 
24 #include "attributes.h"
25 #include "authentication_impl.h"
26 #include "base_context.h"
27 #include "remote_auth_context.h"
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 struct RemoteAuthInvokerContextParam {
33     std::string connectionName;
34     std::string verifierNetworkId;
35     std::string collectorNetworkId;
36     uint32_t tokenId;
37     uint32_t collectorTokenId;
38     std::string callerName;
39     int32_t callerType;
40 };
41 
42 class RemoteAuthInvokerContext : public BaseContext {
43 public:
44     RemoteAuthInvokerContext(uint64_t contextId, AuthParamInner authParam, RemoteAuthInvokerContextParam param,
45         std::shared_ptr<ContextCallback> callback);
46     ~RemoteAuthInvokerContext() override;
47     ContextType GetContextType() const override;
48     uint32_t GetTokenId() const override;
49 
50     void OnMessage(const std::string &connectionName, const std::string &srcEndPoint,
51         const std::shared_ptr<Attributes> &request, std::shared_ptr<Attributes> &reply);
52     void OnConnectStatus(const std::string &connectionName, ConnectStatus connectStatus);
53 
54     void SetVerifierContextId(uint64_t contextId);
55     void OnTimeOut();
56 
57 protected:
58     bool OnStart() override;
59     void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr) override;
60     bool OnStop() override;
61 
62 private:
63     int32_t ProcAuthTipMsg(Attributes &message);
64     int32_t ProcAuthResultMsg(Attributes &message);
65     int32_t ProcAuthResultMsgInner(Attributes &message, int32_t &resultCode, Attributes &attr);
66 
67     bool SendRequest();
68 
69     AuthParamInner authParam_;
70     std::string connectionName_;
71     std::string verifierNetworkId_;
72     std::string collectorNetworkId_;
73     std::string verifierUdid_;
74     uint32_t tokenId_ = 0;
75     uint32_t collectorTokenId_ = 0;
76     std::string callerName_;
77     int32_t callerType_ = -1;
78     std::shared_ptr<ContextCallback> callback_ {nullptr};
79 
80     std::recursive_mutex mutex_;
81     std::shared_ptr<Attributes> request_ = nullptr;
82     std::string endPointName_;
83     std::optional<uint64_t> verifierContextId_;
84     std::optional<uint32_t> cancelTimerId_;
85 };
86 } // namespace UserAuth
87 } // namespace UserIam
88 } // namespace OHOS
89 #endif // REMOTE_AUTH_INVOKER_CONTEXT_H