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 16 #ifndef OHOS_DM_AUTH_MANAGER_V2_H 17 #define OHOS_DM_AUTH_MANAGER_V2_H 18 19 #include <memory> 20 21 #include "auth_ui_state_manager.h" 22 #include "dm_auth_manager_base.h" 23 #include "hichain_auth_connector.h" 24 #include "hichain_connector.h" 25 #include "json_object.h" 26 #include "softbus_connector.h" 27 #include "softbus_session.h" 28 29 namespace OHOS { 30 namespace DistributedHardware { 31 struct DmAuthContext; 32 33 class AuthManager : public AuthManagerBase, 34 public std::enable_shared_from_this<AuthManager> { 35 public: 36 AuthManager(std::shared_ptr<SoftbusConnector> softbusConnector, 37 std::shared_ptr<HiChainConnector> hiChainConnector, 38 std::shared_ptr<IDeviceManagerServiceListener> listener, 39 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector); 40 virtual ~AuthManager(); 41 42 // External API begin 43 /** 44 * @tc.name: DmAuthManager::OnUserOperation 45 * @tc.desc: User Operation of the DeviceManager Authenticate Manager 46 * @tc.type: FUNC 47 */ 48 virtual int32_t OnUserOperation(int32_t action, const std::string ¶ms) = 0; 49 50 /** 51 * @tc.name: AuthManager::GeneratePincode 52 * @tc.desc: Generate Pincode of the DeviceManager Authenticate Manager 53 * @tc.type: FUNC 54 */ 55 int32_t BindTarget(const std::string &sessionName, const PeerTargetId &targetId, 56 const std::map<std::string, std::string> &bindParam, int sessionId, uint64_t logicalSessionId); 57 58 /** 59 * @tc.name: AuthManager::OnUserOperation 60 * @tc.desc: User Operation of the DeviceManager Authenticate Manager 61 * @tc.type: FUNC 62 */ 63 std::string GeneratePincode(); 64 65 /** 66 * @tc.name: AuthManager::ImportAuthCode 67 * @tc.desc: Import auth code 68 * @tc.type: FUNC 69 */ 70 int32_t ImportAuthCode(const std::string &sessionName, const std::string &authCode); 71 72 /** 73 * @tc.name: AuthManager::RegisterUiStateCallback 74 * @tc.desc: Register ui state callback 75 * @tc.type: FUNC 76 */ 77 int32_t RegisterUiStateCallback(const std::string sessionName); 78 79 /** 80 * @tc.name: AuthManager::UnRegisterUiStateCallback 81 * @tc.desc: Unregister ui state callback 82 * @tc.type: FUNC 83 */ 84 int32_t UnRegisterUiStateCallback(const std::string sessionName); 85 86 /** 87 * @tc.name: AuthManager::UnAuthenticateDevice 88 * @tc.desc: UnAuthenticate Device of the DeviceManager Authenticate Manager 89 * @tc.type: FUNC 90 */ 91 int32_t UnAuthenticateDevice(const std::string &sessionName, const std::string &udid, int32_t bindLevel); 92 93 /** 94 * @brief UnBind device. 95 * @param sessionName package name. 96 * @param deviceId device id. 97 * @return Return 0 if success. 98 */ 99 int32_t UnBindDevice(const std::string &sessionName, const std::string &udid, 100 int32_t bindLevel, const std::string &extra); 101 102 void HandleDeviceNotTrust(const std::string &udid); 103 104 int32_t RegisterAuthenticationType(int32_t authenticationType); 105 void OnScreenLocked(); 106 int32_t StopAuthenticateDevice(const std::string &sessionName); 107 // External API begin end 108 109 // Internal API begin 110 void SetAuthContext(std::shared_ptr<DmAuthContext> context); 111 std::shared_ptr<DmAuthContext> GetAuthContext(); 112 static bool IsHmlSessionType(const std::string &sessionType); 113 void GetBindTargetParams(std::string &pkgName, PeerTargetId &targetId, 114 std::map<std::string, std::string> &bindParam); 115 void GetAuthCodeAndPkgName(std::string &pkgName, std::string &authCode); 116 void SetBindTargetParams(const PeerTargetId &targetId); 117 int32_t GetReason(); 118 119 // Internal API end 120 void RegisterCleanNotifyCallback(CleanNotifyCallback cleanNotifyCallback); 121 void ClearSoftbusSessionCallback(); 122 void PrepareSoftbusSessionCallback(); 123 void DeleteTimer(); 124 125 int32_t HandleBusinessEvents(const std::string &businessId, int32_t action); 126 127 protected: 128 std::shared_ptr<DmAuthContext> context_; 129 std::mutex bindParamMutex_; 130 std::map<std::string, std::string> bindParam_; 131 PeerTargetId targetId_; 132 133 int32_t GetPinCode(std::string &code); 134 void GetRemoteDeviceId(std::string &deviceId); 135 private: 136 void ParseHmlInfoInJsonObject(const JsonObject &jsonObject); 137 void ParseProxyJsonObject(const JsonObject &jsonObject); 138 void GetBindLevelByBundleName(std::string &bundleName, int32_t userId, int32_t &bindLevel); 139 void ParseJsonObject(const JsonObject &jsonObject); 140 void GetAuthParam(const std::string &sessionName, int32_t authType, 141 const std::string &deviceId, const std::string &extra); 142 std::string GetBundleName(const JsonObject &jsonObject); 143 void SetAuthType(int32_t authType); 144 bool IsAuthTypeSupported(const int32_t &authType); 145 bool IsAuthCodeReady(const std::string &sessionName); 146 int32_t CheckAuthParamVaild(const std::string &sessionName, int32_t authType, 147 const std::string &deviceId, const std::string &extra); 148 int32_t CheckProxyAuthParamVaild(const std::string &extra); 149 void InitAuthState(const std::string &sessionName, int32_t authType, 150 const std::string &deviceId, const std::string &extra); 151 int32_t AuthenticateDevice(const std::string &sessionName, int32_t authType, 152 const std::string &deviceId, const std::string &extra); 153 void ParseUltrasonicSide(const JsonObject &jsonObject); 154 void GetBindCallerInfo(); 155 int32_t GetBindLevel(int32_t bindLevel); 156 void GetIsNeedJoinLnnParam(const std::map<std::string, std::string> &bindParam); 157 }; 158 159 class AuthSrcManager : public AuthManager { 160 public: 161 AuthSrcManager(std::shared_ptr<SoftbusConnector> softbusConnector, 162 std::shared_ptr<HiChainConnector> hiChainConnector, 163 std::shared_ptr<IDeviceManagerServiceListener> listener, 164 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector); 165 virtual ~AuthSrcManager() override = default; 166 167 // External API begin 168 int32_t OnUserOperation(int32_t action, const std::string ¶ms) override; 169 // External API end 170 171 // IDmDeviceAuthCallback implement begin 172 bool AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) override; 173 void AuthDeviceError(int64_t requestId, int32_t errorCode) override; 174 void AuthDeviceFinish(int64_t requestId) override; 175 void AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) override; 176 char *AuthDeviceRequest(int64_t requestId, int operationCode, const char *reqParams) override; 177 // IDmDeviceAuthCallback implement end 178 179 // ISoftbusSessionCallback implement begin 180 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result) override; 181 void OnSessionClosed(int32_t sessionId) override; 182 void OnSessionDisable() override; 183 void OnDataReceived(int32_t sessionId, std::string message) override; 184 185 bool GetIsCryptoSupport() override; 186 // ISoftbusSessionCallback implement end 187 }; 188 189 class AuthSinkManager : public AuthManager { 190 public: 191 AuthSinkManager(std::shared_ptr<SoftbusConnector> softbusConnector, 192 std::shared_ptr<HiChainConnector> hiChainConnector, 193 std::shared_ptr<IDeviceManagerServiceListener> listener, 194 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector); 195 virtual ~AuthSinkManager() override = default; 196 197 // External API begin 198 int32_t OnUserOperation(int32_t action, const std::string ¶ms) override; 199 // External API end 200 201 // IDmDeviceAuthCallback implement begin 202 bool AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) override; 203 void AuthDeviceError(int64_t requestId, int32_t errorCode) override; 204 void AuthDeviceFinish(int64_t requestId) override; 205 void AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) override; 206 char *AuthDeviceRequest(int64_t requestId, int operationCode, const char *reqParams) override; 207 // IDmDeviceAuthCallback implement end 208 209 // ISoftbusSessionCallback implement begin 210 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result) override; 211 void OnSessionClosed(int32_t sessionId) override; 212 void OnDataReceived(int32_t sessionId, std::string message) override; 213 bool GetIsCryptoSupport() override; 214 // ISoftbusSessionCallback implement end 215 }; 216 } // namespace DistributedHardware 217 } // namespace OHOS 218 #endif // OHOS_DM_AUTH_MANAGER_V2_H