• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef OHOS_DM_NEGOTIATE_PROCESS_V2_H
16 #define OHOS_DM_NEGOTIATE_PROCESS_V2_H
17 #include <iostream>
18 #include <memory>
19 #include <map>
20 
21 #include "dm_auth_context.h"
22 #include "dm_single_instance.h"
23 namespace OHOS {
24 namespace DistributedHardware {
25 enum CredType  : uint8_t {
26     DM_NO_CRED = 0,
27     DM_IDENTICAL_CREDTYPE = 1,
28     DM_SHARE_CREDTYPE = 2,
29     DM_P2P_CREDTYPE = 3,
30 };
31 
32 enum AclType  : uint8_t {
33     DM_NO_ACL = 0,
34     DM_IDENTICAL_ACL = 1,
35     DM_SHARE_ACL = 2,
36     DM_P2P_ACL = 3,
37 };
38 
39 enum AuthType  : uint8_t {
40     DM_INVALIED_AUTHTYPE = 0,
41     DM_INPUT_PINCODE = 1,
42     DM_IMPORT_AUTHTYPE = 2,
43 };
44 
45 class NegotiateSpec {
46 public:
47     CredType credType;
48     AclType aclType;
49     AuthType authType;
50 
NegotiateSpec(CredType credTypeTemp,AclType aclTypeTemp,AuthType authTypeTemp)51     NegotiateSpec(CredType credTypeTemp, AclType aclTypeTemp, AuthType authTypeTemp)
52         : credType(credTypeTemp), aclType(aclTypeTemp), authType(authTypeTemp) {}
53 
54     // 重载<运算符用于map排序
55     bool operator<(const NegotiateSpec& other) const
56     {
57         if (credType != other.credType) return credType < other.credType;
58         if (aclType != other.aclType) return aclType < other.aclType;
59         return authType < other.authType;
60     }
61 };
62 
63 class NegotiateHandler {
64 public:
65     virtual int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) = 0;
~NegotiateHandler()66     virtual ~NegotiateHandler() {};
67 };
68 
69 class NoCredNoAclInputAuthType : public NegotiateHandler {
70 public:
71     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~NoCredNoAclInputAuthType()72     virtual ~NoCredNoAclInputAuthType() {};
73 };
74 
75 class NoCredNoAclImportAuthType : public NegotiateHandler {
76 public:
77     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~NoCredNoAclImportAuthType()78     virtual ~NoCredNoAclImportAuthType() {};
79 };
80 
81 class IdentCredNoAclInputAuthType : public NegotiateHandler {
82 public:
83     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~IdentCredNoAclInputAuthType()84     virtual ~IdentCredNoAclInputAuthType() {};
85 };
86 
87 class IdentCredNoAclImportAuthType : public NegotiateHandler {
88 public:
89     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~IdentCredNoAclImportAuthType()90     virtual ~IdentCredNoAclImportAuthType() {};
91 };
92 
93 class IdentCredIdentAclInputAuthType : public NegotiateHandler {
94 public:
95     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~IdentCredIdentAclInputAuthType()96     virtual ~IdentCredIdentAclInputAuthType() {};
97 };
98 
99 class IdentCredIdentAclImportAuthType : public NegotiateHandler {
100 public:
101     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~IdentCredIdentAclImportAuthType()102     virtual ~IdentCredIdentAclImportAuthType() {};
103 };
104 
105 class IdentCredP2pAclInputAuthType : public NegotiateHandler {
106 public:
107     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~IdentCredP2pAclInputAuthType()108     virtual ~IdentCredP2pAclInputAuthType() {};
109 };
110 
111 class IdentCredP2pAclImportAuthType : public NegotiateHandler {
112 public:
113     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~IdentCredP2pAclImportAuthType()114     virtual ~IdentCredP2pAclImportAuthType() {};
115 };
116 
117 class ShareCredNoAclInputAuthType : public NegotiateHandler {
118 public:
119     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~ShareCredNoAclInputAuthType()120     virtual ~ShareCredNoAclInputAuthType() {};
121 };
122 
123 class ShareCredNoAclImportAuthType : public NegotiateHandler {
124 public:
125     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~ShareCredNoAclImportAuthType()126     virtual ~ShareCredNoAclImportAuthType() {};
127 };
128 
129 class ShareCredShareAclInputAuthType : public NegotiateHandler {
130 public:
131     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~ShareCredShareAclInputAuthType()132     virtual ~ShareCredShareAclInputAuthType() {};
133 };
134 
135 class ShareCredShareAclImportAuthType : public NegotiateHandler {
136 public:
137     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~ShareCredShareAclImportAuthType()138     virtual ~ShareCredShareAclImportAuthType() {};
139 };
140 
141 class ShareCredP2pAclInputAuthType : public NegotiateHandler {
142 public:
143     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~ShareCredP2pAclInputAuthType()144     virtual ~ShareCredP2pAclInputAuthType() {};
145 };
146 
147 class ShareCredP2pAclImportAuthType : public NegotiateHandler {
148 public:
149     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~ShareCredP2pAclImportAuthType()150     virtual ~ShareCredP2pAclImportAuthType() {};
151 };
152 
153 class P2pCredNoAclInputAuthType : public NegotiateHandler {
154 public:
155     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~P2pCredNoAclInputAuthType()156     virtual ~P2pCredNoAclInputAuthType() {};
157 };
158 
159 class P2pCredNoAclImportAuthType : public NegotiateHandler {
160 public:
161     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~P2pCredNoAclImportAuthType()162     virtual ~P2pCredNoAclImportAuthType() {};
163 };
164 
165 class P2pCredP2pAclInputAuthType : public NegotiateHandler {
166 public:
167     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~P2pCredP2pAclInputAuthType()168     virtual ~P2pCredP2pAclInputAuthType() {};
169 };
170 
171 class P2pCredP2pAclImportAuthType : public NegotiateHandler {
172 public:
173     int32_t NegotiateHandle(std::shared_ptr<DmAuthContext> context) override;
~P2pCredP2pAclImportAuthType()174     virtual ~P2pCredP2pAclImportAuthType() {};
175 };
176 
177 class NegotiateProcess {
178 DM_DECLARE_SINGLE_INSTANCE_BASE(NegotiateProcess);
179 public:
180     NegotiateProcess();
181     ~NegotiateProcess();
182     int32_t HandleNegotiateResult(std::shared_ptr<DmAuthContext> context);
183     int32_t HandleProxyNegotiateResult(std::shared_ptr<DmAuthContext> context, int32_t result);
184     bool IsNeedSetProxyRelationShip(std::shared_ptr<DmAuthContext> context, DmProxyAuthContext &proxyContext);
185     bool IsExistTheTokenId(const std::string extraData, const std::string tokenIdHash);
186 private:
187     CredType ConvertCredType(const std::string &credType);
188     AclType ConvertAclType(const std::string &aclType);
189     AuthType ConvertAuthType(const DmAuthType &authType);
190 private:
191     std::map<NegotiateSpec, std::unique_ptr<NegotiateHandler>> handlers_;
192 };
193 } // namespace DistributedHardware
194 } // namespace OHOS
195 #endif // OHOS_DM_AUTH_STATE_V2_H