query devide id

Signed-off-by: fundavid <fangjiawei8@huawei.com>
Change-Id: Ibbd92b545b18243971a224a62f69352e8b74e25f
This commit is contained in:
ZhouShimiao 2024-11-15 20:03:02 +08:00
parent c5b2058ed3
commit 17f3b38859
16 changed files with 154 additions and 32 deletions

View File

@ -32,6 +32,7 @@ enum class AccountMgrInterfaceCode : uint32_t {
SET_OHOS_ACCOUNT_INFO_BY_USER_ID = 8,
SUBSCRIBE_DISTRIBUTED_ACCOUNT_EVENT = 9,
UNSUBSCRIBE_DISTRIBUTED_ACCOUNT_EVENT = 10,
QUERY_DISTRIBUTE_VIRTUAL_DEVICE_ID = 11,
QUERY_DEVICE_ACCOUNT_ID = 104,
GET_APP_ACCOUNT_SERVICE = 105,
GET_OS_ACCOUNT_SERVICE = 106,

View File

@ -34,6 +34,7 @@ public:
const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr) override;
ErrCode SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr) override;
ErrCode QueryOhosAccountInfo(OhosAccountInfo &accountInfo) override;
ErrCode QueryDistributedVirtualDeviceId(std::string &dvid) override;
ErrCode GetOhosAccountInfo(OhosAccountInfo &ohosAccountInfo) override;
ErrCode GetOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &ohosAccountInfo) override;
ErrCode QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &info) override;

View File

@ -23,6 +23,7 @@
"OHOS::AccountSA::OhosAccountKitsImpl::GetOsAccountService()";
"OHOS::AccountSA::OhosAccountKitsImpl::GetAccountIAMService()";
"OHOS::AccountSA::OhosAccountKitsImpl::QueryOhosAccountInfo(OHOS::AccountSA::OhosAccountInfo&)";
"OHOS::AccountSA::OhosAccountKitsImpl::QueryDistributedVirtualDeviceId(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&)";
"OHOS::AccountSA::WriteOhosAccountInfo(OHOS::MessageParcel&, OHOS::AccountSA::OhosAccountInfo const&)";
"OHOS::AccountSA::ReadOhosAccountInfo(OHOS::MessageParcel&, OHOS::AccountSA::OhosAccountInfo&)";
"OHOS::AccountSA::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(std::__h::function<void (int, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>)> const&)";

View File

@ -160,6 +160,38 @@ ErrCode AccountProxy::SetOhosAccountInfoByUserId(
return result;
}
ErrCode AccountProxy::QueryDistributedVirtualDeviceId(std::string &dvid)
{
dvid = "";
MessageParcel data;
if (!data.WriteInterfaceToken(GetDescriptor())) {
ACCOUNT_LOGE("Failed to write descriptor!");
return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
}
MessageParcel reply;
ErrCode code = SendRequest(AccountMgrInterfaceCode::QUERY_DISTRIBUTE_VIRTUAL_DEVICE_ID, data, reply);
if (code != ERR_OK) {
ACCOUNT_LOGE("Failed to send request, code %{public}d.", code);
return code;
}
int32_t result = ERR_OK;
if (!reply.ReadInt32(result)) {
ACCOUNT_LOGE("Failed to read result");
return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
}
if (result != ERR_OK) {
ACCOUNT_LOGE("Failed to query dvid, result %{public}d.", result);
return result;
}
if (!reply.ReadString(dvid)) {
ACCOUNT_LOGE("Failed to read dvid");
return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
}
return ERR_OK;
}
ErrCode AccountProxy::QueryOhosAccountInfo(OhosAccountInfo &accountInfo)
{
MessageParcel data;

View File

@ -24,7 +24,7 @@ namespace OHOS {
namespace AccountSA {
std::function<void(int32_t, const std::string &)> ohosCallbackFunc()
{
return [](int32_t systemAbilityId, const std::string &deviceId) {
return [](int32_t systemAbilityId, const std::string &dvid) {
if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
OhosAccountKitsImpl::GetInstance().RestoreSubscribe();
}
@ -145,6 +145,17 @@ std::pair<bool, OhosAccountInfo> OhosAccountKitsImpl::QueryOhosAccountInfo()
return std::make_pair(result == ERR_OK, accountInfo);
}
ErrCode OhosAccountKitsImpl::QueryDistributedVirtualDeviceId(std::string &dvid)
{
auto accountProxy = GetService();
if (accountProxy == nullptr) {
ACCOUNT_LOGE("Get proxy failed");
return ERR_ACCOUNT_COMMON_GET_PROXY;
}
return accountProxy->QueryDistributedVirtualDeviceId(dvid);
}
ErrCode OhosAccountKitsImpl::QueryOhosAccountInfo(OhosAccountInfo &accountInfo)
{
auto accountProxy = GetService();

View File

@ -475,15 +475,9 @@ ErrCode OsAccount::SetOsAccountProfilePhoto(const int id, const std::string &pho
return proxy->SetOsAccountProfilePhoto(id, photo);
}
ErrCode OsAccount::GetDistributedVirtualDeviceId(std::string &deviceId)
ErrCode OsAccount::GetDistributedVirtualDeviceId(std::string &dvid)
{
deviceId = "";
OhosAccountInfo accountInfo;
ErrCode result = OhosAccountKitsImpl::GetInstance().QueryOhosAccountInfo(accountInfo);
if (result == ERR_OK && accountInfo.uid_ != DEFAULT_OHOS_ACCOUNT_UID) {
deviceId = accountInfo.uid_;
}
return result;
return OhosAccountKitsImpl::GetInstance().QueryDistributedVirtualDeviceId(dvid);
}
ErrCode OsAccount::ActivateOsAccount(const int id)

View File

@ -39,6 +39,7 @@ public:
virtual std::int32_t SetOhosAccountInfoByUserId(
const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr) = 0;
virtual ErrCode QueryOhosAccountInfo(OhosAccountInfo &accountInfo) = 0;
virtual ErrCode QueryDistributedVirtualDeviceId(std::string &dvid) = 0;
virtual ErrCode QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &info) = 0;
virtual ErrCode GetOhosAccountInfo(OhosAccountInfo &accountInfo) = 0;
virtual ErrCode GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &info) = 0;

View File

@ -37,6 +37,7 @@ public:
const int32_t userId, const OhosAccountInfo& ohosAccountInfo, const std::string& eventStr) final;
std::pair<bool, OhosAccountInfo> QueryOhosAccountInfo() final;
ErrCode QueryOhosAccountInfo(OhosAccountInfo &accountInfo);
ErrCode QueryDistributedVirtualDeviceId(std::string &dvid);
ErrCode GetOhosAccountInfo(OhosAccountInfo &accountInfo) final;
ErrCode GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &accountInfo) final;
std::pair<bool, OhosAccountInfo> QueryOhosAccountInfoByUserId(std::int32_t userId) final;

View File

@ -48,6 +48,7 @@ public:
ErrCode SetOhosAccountInfoByUserId(
const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr) override;
ErrCode QueryOhosAccountInfo(OhosAccountInfo &accountInfo) override;
ErrCode QueryDistributedVirtualDeviceId(std::string &dvid) override;
ErrCode QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &accountInfo) override;
ErrCode GetOhosAccountInfo(OhosAccountInfo &info) override;
ErrCode GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &info) override;

View File

@ -44,6 +44,8 @@ public:
ErrCode CmdGetOhosAccountInfo(MessageParcel &data, MessageParcel &reply);
ErrCode CmdGetOhosAccountInfoByUserId(MessageParcel &data, MessageParcel &reply);
ErrCode InnerQueryOhosAccountInfo(MessageParcel &data, MessageParcel &reply);
ErrCode InnerQueryDistributedVirtualDeviceId(MessageParcel &data, MessageParcel &reply);
ErrCode CmdQueryDistributedVirtualDeviceId(MessageParcel &data, MessageParcel &reply);
ErrCode CmdQueryOhosAccountInfo(MessageParcel &data, MessageParcel &reply);
ErrCode CmdQueryOhosAccountInfoByUserId(MessageParcel &data, MessageParcel &reply);

View File

@ -60,6 +60,13 @@ public:
*/
ErrCode GetAccountInfoByUserId(std::int32_t userId, AccountInfo &info);
/**
* Get ohos account device id.
*
* @return ohos account device id.
*/
ErrCode QueryDistributedVirtualDeviceId(std::string &dvid);
/**
* Subscribe distributed account event by type.
*

View File

@ -346,6 +346,7 @@
"OHOS::AccountSA::AccountFileWatcherMgr::AddAccountInfoDigest(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&)";
"OHOS::AccountSA::OhosAccountManager::SubscribeDistributedAccountEvent(OHOS::AccountSA::DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE, OHOS::sptr<OHOS::IRemoteObject> const&)";
"OHOS::AccountSA::OhosAccountManager::UnsubscribeDistributedAccountEvent(OHOS::AccountSA::DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE, OHOS::sptr<OHOS::IRemoteObject> const&)";
"OHOS::AccountSA::OhosAccountManager::QueryDistributedVirtualDeviceId(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&)";
};
local:
*;

View File

@ -119,6 +119,11 @@ ErrCode AccountMgrService::SetOhosAccountInfoByUserId(
return res;
}
ErrCode AccountMgrService::QueryDistributedVirtualDeviceId(std::string &dvid)
{
return OhosAccountManager::GetInstance().QueryDistributedVirtualDeviceId(dvid);
}
ErrCode AccountMgrService::QueryOhosAccountInfo(OhosAccountInfo &accountInfo)
{
return QueryOhosAccountInfoByUserId(IPCSkeleton::GetCallingUid() / UID_TRANSFORM_DIVISOR, accountInfo);

View File

@ -75,6 +75,10 @@ AccountStub::AccountStub()
[this] (MessageParcel &data, MessageParcel &reply) { return this->CmdSetOhosAccountInfoByUserId(data, reply); };
stubFuncMap_[AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO] =
[this] (MessageParcel &data, MessageParcel &reply) { return this->CmdQueryOhosAccountInfo(data, reply); };
stubFuncMap_[AccountMgrInterfaceCode::QUERY_DISTRIBUTE_VIRTUAL_DEVICE_ID] =
[this] (MessageParcel &data, MessageParcel &reply) {
return this->CmdQueryDistributedVirtualDeviceId(data, reply);
};
stubFuncMap_[AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO] =
[this] (MessageParcel &data, MessageParcel &reply) { return this->CmdGetOhosAccountInfo(data, reply); };
stubFuncMap_[AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO_BY_USER_ID] =
@ -213,6 +217,25 @@ ErrCode AccountStub::CmdSetOhosAccountInfoByUserId(MessageParcel &data, MessageP
return InnerSetOhosAccountInfo(userId, data, reply);
}
ErrCode AccountStub::InnerQueryDistributedVirtualDeviceId(MessageParcel &data, MessageParcel &reply)
{
std::string dvid = "";
ErrCode result = QueryDistributedVirtualDeviceId(dvid);
if (!reply.WriteInt32(result)) {
ACCOUNT_LOGE("Failed to write reply, result=%{public}d.", result);
return IPC_STUB_WRITE_PARCEL_ERR;
}
if (result != ERR_OK) {
ACCOUNT_LOGE("Failed to get dvid");
return result;
}
if (!reply.WriteString(dvid)) {
ACCOUNT_LOGE("Failed to write dvid");
return IPC_STUB_WRITE_PARCEL_ERR;
}
return result;
}
ErrCode AccountStub::InnerQueryOhosAccountInfo(MessageParcel &data, MessageParcel &reply)
{
OhosAccountInfo info;
@ -274,6 +297,16 @@ ErrCode AccountStub::InnerGetOhosAccountInfo(MessageParcel &data, MessageParcel
return ERR_OK;
}
ErrCode AccountStub::CmdQueryDistributedVirtualDeviceId(MessageParcel &data, MessageParcel &reply)
{
if (!HasAccountRequestPermission(PERMISSION_MANAGE_USERS) &&
!HasAccountRequestPermission(PERMISSION_DISTRIBUTED_DATASYNC)) {
ACCOUNT_LOGE("Check permission failed");
return ERR_ACCOUNT_COMMON_PERMISSION_DENIED;
}
return InnerQueryDistributedVirtualDeviceId(data, reply);
}
ErrCode AccountStub::CmdQueryOhosAccountInfo(MessageParcel &data, MessageParcel &reply)
{
if (!HasAccountRequestPermission(PERMISSION_MANAGE_USERS) &&

View File

@ -73,17 +73,17 @@ std::string GetAccountEventStr(const std::map<std::string, std::string> &account
return defaultValue;
}
bool GetCallerBundleName(std::string &bundleName)
bool GetCallerBundleName(std::string &bundleName, bool &isSystemApp)
{
uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
Security::AccessToken::AccessTokenID tokenId = fullTokenId & TOKEN_ID_LOWMASK;
Security::AccessToken::ATokenTypeEnum tokenType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenId);
if ((tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) &&
(!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId))) {
isSystemApp = Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
Security::AccessToken::HapTokenInfo hapTokenInfo;
int result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapTokenInfo);
if (result) {
ACCOUNT_LOGE("failed to get hap token info, result = %{public}d", result);
ACCOUNT_LOGE("Failed to get hap token info, result = %{public}d", result);
return false;
}
bundleName = hapTokenInfo.bundleName;
@ -112,26 +112,8 @@ std::string ReturnOhosUdidWithSha256(const std::string &uid)
return ohosUidStr;
}
std::string GenerateOhosUdidWithSha256(const std::string &name, const std::string &uid)
std::string GenerateDVID(const std::string &bundleName, const std::string &uid)
{
if (name.empty() || name.length() > MAX_NAME_LENGTH) {
ACCOUNT_LOGE("input name empty or too long, length %{public}zu", name.length());
return std::string("");
}
if (uid.empty() || uid.length() > MAX_UID_LENGTH) {
ACCOUNT_LOGE("input uid empty or too long, length %{public}zu", uid.length());
return std::string("");
}
std::string bundleName = "";
if (!GetCallerBundleName(bundleName)) {
ACCOUNT_LOGE("GetCallerBundleName failed");
return std::string("");
}
if (bundleName.empty()) {
return ReturnOhosUdidWithSha256(uid);
}
unsigned char newId[OUTPUT_LENGTH_IN_BYTES + 1] = {};
mbedtls_md_context_t md_context;
mbedtls_md_init(&md_context);
@ -160,6 +142,29 @@ std::string GenerateOhosUdidWithSha256(const std::string &name, const std::strin
}
return ohosUidStr;
}
std::string GenerateOhosUdidWithSha256(const std::string &name, const std::string &uid)
{
if (name.empty() || name.length() > MAX_NAME_LENGTH) {
ACCOUNT_LOGE("Input name empty or too long, length %{public}zu", name.length());
return std::string("");
}
if (uid.empty() || uid.length() > MAX_UID_LENGTH) {
ACCOUNT_LOGE("Input uid empty or too long, length %{public}zu", uid.length());
return std::string("");
}
std::string bundleName = "";
bool isSystemApp = false;
if (!GetCallerBundleName(bundleName, isSystemApp) && !isSystemApp) {
return std::string("");
}
if (isSystemApp || bundleName.empty()) {
return ReturnOhosUdidWithSha256(uid);
}
return GenerateDVID(bundleName, uid);
}
}
/**
@ -244,6 +249,27 @@ AccountInfo OhosAccountManager::GetCurrentOhosAccountInfo()
return currOhosAccountInfo;
}
ErrCode OhosAccountManager::QueryDistributedVirtualDeviceId(std::string &dvid)
{
int32_t localId = AccountMgrService::GetInstance().GetCallingUserID();
AccountInfo accountInfo;
ErrCode errCode = GetAccountInfoByUserId(localId, accountInfo);
if (errCode != ERR_OK) {
ACCOUNT_LOGE("Get ohos account info failed, errcode=%{public}d, localId=%{public}d.", errCode, localId);
return errCode;
}
OhosAccountInfo ohosAccountInfo = accountInfo.ohosAccountInfo_;
if (ohosAccountInfo.uid_ == DEFAULT_OHOS_ACCOUNT_UID) {
return ERR_OK;
}
std::string bundleName = "";
bool isSystemApp = false;
GetCallerBundleName(bundleName, isSystemApp);
dvid = GenerateDVID(bundleName, ohosAccountInfo.GetRawUid());
return ERR_OK;
}
ErrCode OhosAccountManager::GetAccountInfoByUserId(std::int32_t userId, AccountInfo &info)
{
if (userId == 0) {

View File

@ -64,6 +64,11 @@ public:
return 0;
}
ErrCode QueryDistributedVirtualDeviceId(std::string &dvid) override
{
return 0;
}
sptr<IRemoteObject> GetAccountIAMService() override
{
return nullptr;