* Copyright (c) 2025-2026 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_DM_AUTH_CONTEXT_V2_H
#define OHOS_DM_AUTH_CONTEXT_V2_H
#include <map>
#include <string>
#include "access_control_profile.h"
#include "authentication.h"
#include "auth_manager.h"
#include "auth_ui_state_manager.h"
#include "dm_ability_manager.h"
#include "dm_anonymous.h"
#include "dm_auth_message_processor.h"
#include "dm_constants.h"
#include "dm_device_info.h"
#include "dm_log.h"
#include "dm_timer.h"
#include "ffrt.h"
#include "hichain_auth_connector.h"
#include "hichain_connector.h"
#include "softbus_connector.h"
#include "softbus_session.h"
namespace OHOS {
namespace DistributedHardware {
class DmAuthStateMachine;
class DmAuthMessageProcessor;
using CleanNotifyCallback = std::function<void(uint64_t, int32_t)>;
using StopTimerAndDelDpCallback = std::function<void(const std::string&, int32_t, uint64_t)>;
enum DmAuthType : int32_t {
AUTH_TYPE_CRE = 0,
AUTH_TYPE_PIN,
AUTH_TYPE_QR_CODE,
AUTH_TYPE_NFC,
AUTH_TYPE_NO_INTER_ACTION,
AUTH_TYPE_IMPORT_AUTH_CODE,
AUTH_TYPE_PIN_ULTRASONIC,
AUTH_TYPE_UNKNOW,
};
enum DmAuthDirection {
DM_AUTH_SOURCE = 0,
DM_AUTH_SINK,
};
enum DmBindType {
DM_AUTH_USERID = 1,
DM_AUTH_SYSTEM_SERVICE,
DM_AUTH_APP_SERVICE,
DM_AUTH_DEVICEID,
};
enum DmAuthSide {
DM_AUTH_LOCAL_SIDE = 0,
DM_AUTH_REMOTE_SIDE,
};
enum DmAuthScope {
DM_AUTH_SCOPE_INVALID = 0,
DM_AUTH_SCOPE_DEVICE,
DM_AUTH_SCOPE_USER,
DM_AUTH_SCOPE_APP,
DM_AUTH_SCOPE_LNN,
DM_AUTH_SCOPE_MAX,
};
enum {
DM_UNKNOWN_TYPE = 0,
DM_SAME_ACCOUNT_TYPE = 1,
DM_SHARE_TYPE = 2,
DM_POINT_TO_POINT_TYPE = 256,
DM_SAME_GROUP_TYPE = 3,
};
enum DmUltrasonicInfo {
DM_Ultrasonic_Forward = 0,
DM_Ultrasonic_Reverse = 1,
DM_Ultrasonic_Invalid = 2,
};
typedef struct DmProxyAccess {
std::string pkgName;
std::string pkgLabel;
std::string bundleInfo;
std::string bundleName;
std::string peerBundleName;
int64_t tokenId;
std::string tokenIdHash;
int32_t bindLevel;
std::string transmitCredentialId;
std::string transmitPublicKey;
std::string publicKey;
int32_t sessionKeyId;
int32_t transmitSessionKeyId;
int64_t transmitSkTimeStamp;
int64_t skTimeStamp;
bool isAuthed;
std::string aclTypeList;
std::string credTypeList;
std::string aclStrList;
std::map<int32_t, std::string> credentialInfos;
std::map<int32_t, DistributedDeviceProfile::AccessControlProfile> aclProfiles;
std::string credInfoJson;
std::string aclTypeJson;
int64_t serviceId;
std::string serviceInfo;
std::string extraInfo;
} DmProxyAccess;
struct DmProxyAuthContext {
std::string proxyContextId;
std::string customData;
std::string pkgLabel;
bool needBind{true};
bool needAgreeCredential{true};
bool needAuth{true};
bool IsNeedSetProxyRelationShip{false};
DmProxyAccess proxyAccesser;
DmProxyAccess proxyAccessee;
bool operator==(const DmProxyAuthContext &other) const
{
return (proxyContextId == other.proxyContextId);
}
bool operator<(const DmProxyAuthContext &other) const
{
return proxyContextId < other.proxyContextId;
}
};
struct DmPeerTargetAddress {
std::string peerBrMacAddress;
std::string peerBleMacAddress;
std::string peerWifiMacAddress;
std::string peerActionMacAddress;
std::string peerWifiChannel;
std::string peerWifiIp;
uint16_t peerWifiPort;
};
struct DmPeerTarget {
DmBindType peerType;
std::string peerDeviceId;
int64_t peerServiceId;
int64_t peerSaTokenId;
std::string peerBundleName;
DmPeerTargetAddress peerTargetAddress;
};
struct DmAccess {
std::string deviceName;
int32_t deviceType;
std::string deviceId;
std::string deviceIdHash;
std::string addr;
int32_t userId{-1};
int32_t displayId{-1};
std::string accountId;
std::string accountIdHash;
int64_t tokenId;
std::string tokenIdHash;
std::string networkId;
std::string bundleName;
std::string pkgName;
std::string language;
int64_t serviceId;
std::string accesserHapSignature;
int32_t bindLevel;
int32_t lnnBindType;
int32_t transmitBindType;
std::string lnnCredentialId;
std::string transmitCredentialId;
std::string lnnPublicKey;
std::string transmitPublicKey;
std::vector<int32_t> bindType;
std::string publicKey;
int32_t status;
int32_t sessionKeyId;
int32_t transmitSessionKeyId;
int32_t lnnSessionKeyId{0};
int32_t oldBindLevel;
int64_t transmitSkTimeStamp;
int64_t lnnSkTimeStamp{0};
int64_t skTimeStamp;
uint64_t certRandom{0};
bool isAuthed;
bool isUserLevelAuthed;
bool isOnline;
bool isGenerateLnnCredential{true};
bool isPutLnnAcl{true};
std::string dmVersion;
std::string edition;
std::string aclTypeList;
std::string credTypeList;
std::string aclStrList;
std::map<int32_t, std::string> credentialInfos;
std::map<int32_t, DistributedDeviceProfile::AccessControlProfile> aclProfiles;
std::string accountGroupIdHash;
std::string oldBundleName;
std::string extraInfo;
std::string cert;
bool isCommonFlag{false};
bool isGeneratedLnnCredThisBind{false};
bool isGeneratedTransmitThisBind{false};
std::string serviceInfo;
};
struct DmAuthContext {
bool isOnline;
uint64_t logicalSessionId;
DmMessageType msgType;
int32_t sessionId;
int64_t requestId;
DMLocalServiceInfoAuthBoxType authBoxType{DMLocalServiceInfoAuthBoxType::STATE3};
UiAction pinInputResult;
UiAction confirmOperation{UiAction::USER_OPERATION_TYPE_ALLOW_AUTH};
DmAuthType authType{DmAuthType::AUTH_TYPE_PIN};
std::vector<DmAuthType> authTypeList;
uint32_t currentAuthTypeIdx{0};
int32_t inputPinAuthFailTimes{0};
std::string pinCode{""};
bool serviceInfoFound{false};
int32_t connDelayCloseTime;
int32_t reason{DM_OK};
int32_t reply;
std::string userOperationParam;
int32_t state;
int32_t hmlActionId = 0;
int64_t remainingFrozenTime = 0;
bool authenticating;
bool successFinished{false};
bool isAppCredentialVerified{false};
bool hmlEnable160M{false};
std::string businessId;
std::string pkgName;
std::string pkgLabel;
std::string importCodeBundleName;
std::string appThumbnail;
std::string appOperation;
std::string customData;
std::string connSessionType;
std::string extraInfo;
DmAuthDirection direction;
ProcessInfo processInfo;
DmPeerTarget peerTarget;
DmUltrasonicInfo ultrasonicInfo = DmUltrasonicInfo::DM_Ultrasonic_Forward;
DmAccess accesser;
DmAccess accessee;
std::multimap<DmAccess, DmAccess> proxy;
bool isNeedJoinLnn{true};
bool IsProxyBind{false};
bool isServiceBind{false};
bool IsCallingProxyAsSubject{true};
bool IsNeedSetProxy{false};
bool isNeedAuthenticate{true};
std::vector<DmProxyAuthContext> subjectProxyOnes;
std::vector<DmProxyAuthContext> subjectServiceOnes;
std::string reUseCreId;
std::string title;
std::string srvExtarInfo;
int32_t aclLifeCycleDays{ACL_LIFE_CYCLE_DAYS_NOT_CONFIGURED};
std::shared_ptr<DmAuthStateMachine> authStateMachine;
std::shared_ptr<AuthUiStateManager> authUiStateMgr;
std::shared_ptr<HiChainConnector> hiChainConnector;
std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector;
std::shared_ptr<DmAuthMessageProcessor> authMessageProcessor;
std::shared_ptr<SoftbusConnector> softbusConnector;
std::shared_ptr<IDeviceManagerServiceListener> listener;
std::shared_ptr<IAuthentication> authPtr;
std::shared_ptr<DmTimer> timer;
std::string transmitData;
std::string importPkgName = "";
std::string importAuthCode = "";
std::map<int32_t, std::shared_ptr<IAuthentication>> authenticationMap;
PeerTargetId peerTargetId;
bool pinNegotiateStarted{false};
bool isAuthenticateDevice{false};
bool needBind{true};
bool needAgreeCredential{true};
bool needAuth{true};
bool pinCodeFlag{false};
bool ncmBindTarget{false};
ffrt::mutex certMtx_;
ffrt::mutex certCVMtx_;
ffrt::condition_variable certCV_;
CleanNotifyCallback cleanNotifyCallback{nullptr};
StopTimerAndDelDpCallback stopTimerAndDelDpCallback{nullptr};
std::vector<int64_t> serviceId;
std::vector<DistributedDeviceProfile::ServiceInfo> serviceInfos;
std::string GetDeviceId(DmAuthSide side);
int32_t GetUserId(DmAuthSide side);
std::string GetCredentialId(DmAuthSide side, DmAuthScope authorizedScope);
std::string GetPublicKey(DmAuthSide side, DmAuthScope authorizedScope);
void SetCredentialId(DmAuthSide side, DmAuthScope authorizedScope, const std::string &credentialId);
void SetPublicKey(DmAuthSide side, DmAuthScope authorizedScope, const std::string &publicKey);
std::string GetAccountId(DmAuthSide side);
std::string GetAclBundleName(const DmAuthDirection &direction, DmProxyAuthContext &proxyAuthContext);
};
}
}
#endif