Signed-off-by: liuziwei <liuziwei12@huawei.com>
Change-Id: I29389f3b9ff83b418e47a5c1e4b3e0eb1b954494
This commit is contained in:
liuziwei 2024-09-13 17:19:07 +08:00
parent 8287b3bde9
commit 89a989c5a2
17 changed files with 127 additions and 44 deletions

View File

@ -22,6 +22,7 @@ namespace OHOS {
namespace UserIam {
namespace UserAuth {
const int32_t INVALID_AUTH_TYPE = -1;
const int32_t INNER_API_VERSION_10000 = 10000;
enum OperationType : uint32_t {
TRACE_ADD_CREDENTIAL = 0,

View File

@ -47,8 +47,10 @@ public:
const std::shared_ptr<IdentificationCallback> &callback) override;
int32_t CancelIdentification(uint64_t contextId) override;
int32_t GetVersion(int32_t &version);
uint64_t BeginWidgetAuth(int32_t apiVersion, const WidgetAuthParam &authParam,
const WidgetParam &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback) override;
uint64_t BeginWidgetAuth(const WidgetAuthParam &authParam, const WidgetParam &widgetParam,
const std::shared_ptr<AuthenticationCallback> &callback) override;
uint64_t BeginWidgetAuth(int32_t apiVersion, const WidgetAuthParam &authParam, const WidgetParam &widgetParam,
const std::shared_ptr<AuthenticationCallback> &callback);
int32_t SetWidgetCallback(int32_t version, const std::shared_ptr<IUserAuthWidgetCallback> &callback);
int32_t Notice(NoticeType noticeType, const std::string &eventData);
int32_t GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState);

View File

@ -20,6 +20,7 @@
#include "auth_common.h"
#include "callback_manager.h"
#include "iam_check.h"
#include "iam_defines.h"
#include "iam_logger.h"
#include "iam_para2str.h"
#include "iam_ptr.h"
@ -81,7 +82,12 @@ void NorthAuthenticationCallback::OnResult(int32_t result, const Attributes &ext
int32_t UserAuthClientImpl::GetAvailableStatus(AuthType authType, AuthTrustLevel authTrustLevel)
{
IAM_LOGI("start, authType:%{public}d authTrustLevel:%{public}u", authType, authTrustLevel);
return GetNorthAvailableStatus(INT32_MAX, authType, authTrustLevel);
auto proxy = GetProxy();
if (!proxy) {
IAM_LOGE("proxy is nullptr");
return GENERAL_ERROR;
}
return proxy->GetAvailableStatus(INNER_API_VERSION_10000, authType, authTrustLevel);
}
int32_t UserAuthClientImpl::GetNorthAvailableStatus(int32_t apiVersion, AuthType authType,
@ -94,7 +100,7 @@ int32_t UserAuthClientImpl::GetNorthAvailableStatus(int32_t apiVersion, AuthType
IAM_LOGE("proxy is nullptr");
return GENERAL_ERROR;
}
return proxy->GetAvailableStatus(apiVersion, INVALID_USER_ID, authType, authTrustLevel);
return proxy->GetAvailableStatus(apiVersion, authType, authTrustLevel);
}
int32_t UserAuthClientImpl::GetAvailableStatus(int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel)
@ -404,6 +410,14 @@ UserAuthClient &UserAuthClient::GetInstance()
return UserAuthClientImpl::Instance();
}
uint64_t UserAuthClientImpl::BeginWidgetAuth(const WidgetAuthParam &authParam, const WidgetParam &widgetParam,
const std::shared_ptr<AuthenticationCallback> &callback)
{
IAM_LOGI("start, authTypeSize:%{public}zu authTrustLevel:%{public}u", authParam.authTypes.size(),
authParam.authTrustLevel);
return BeginWidgetAuth(INNER_API_VERSION_10000, authParam, widgetParam, callback);
}
uint64_t UserAuthClientImpl::BeginWidgetAuth(int32_t apiVersion, const WidgetAuthParam &authParam,
const WidgetParam &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback)
{

View File

@ -36,6 +36,8 @@ public:
virtual int32_t GetAvailableStatus(int32_t apiVersion, int32_t userId, AuthType authType,
AuthTrustLevel authTrustLevel) = 0;
virtual int32_t GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel) = 0;
virtual void GetProperty(int32_t userId, AuthType authType,
const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) = 0;

View File

@ -30,6 +30,7 @@ public:
~UserAuthProxy() override = default;
int32_t GetAvailableStatus(int32_t apiVersion, int32_t userId, AuthType authType,
AuthTrustLevel authTrustLevel) override;
int32_t GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel) override;
void GetProperty(int32_t userId, AuthType authType,
const std::vector<Attributes::AttributeKey> &keys,
sptr<GetExecutorPropertyCallbackInterface> &callback) override;
@ -64,6 +65,8 @@ private:
bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply);
bool WriteWidgetParam(MessageParcel &data, const AuthParamInner &authParam, const WidgetParam &widgetParam);
ResultCode WriteGlobalConfigValue(MessageParcel &data, const GlobalConfigParam &param);
int32_t GetAvailableStatusInner(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel,
MessageParcel &data);
};
} // namespace UserAuth
} // namespace UserIam

View File

@ -38,12 +38,40 @@ int32_t UserAuthProxy::GetAvailableStatus(int32_t apiVersion, int32_t userId, Au
AuthTrustLevel authTrustLevel)
{
MessageParcel data;
MessageParcel reply;
if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
IAM_LOGE("failed to write descriptor");
return WRITE_PARCEL_ERROR;
}
bool isSpecificUserId = true;
if (!data.WriteBool(isSpecificUserId)) {
IAM_LOGE("failed to write isSpecificUserId");
return WRITE_PARCEL_ERROR;
}
if (!data.WriteInt32(userId)) {
IAM_LOGE("failed to write userId");
return WRITE_PARCEL_ERROR;
}
return GetAvailableStatusInner(apiVersion, authType, authTrustLevel, data);
}
int32_t UserAuthProxy::GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel)
{
MessageParcel data;
if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
IAM_LOGE("failed to write descriptor");
return WRITE_PARCEL_ERROR;
}
bool isSpecificUserId = false;
if (!data.WriteBool(isSpecificUserId)) {
IAM_LOGE("failed to write isSpecificUserId");
return WRITE_PARCEL_ERROR;
}
return GetAvailableStatusInner(apiVersion, authType, authTrustLevel, data);
}
int32_t UserAuthProxy::GetAvailableStatusInner(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel,
MessageParcel &data)
{
if (!data.WriteInt32(authType)) {
IAM_LOGE("failed to write authType");
return WRITE_PARCEL_ERROR;
@ -56,11 +84,8 @@ int32_t UserAuthProxy::GetAvailableStatus(int32_t apiVersion, int32_t userId, Au
IAM_LOGE("failed to write apiVersion");
return WRITE_PARCEL_ERROR;
}
if (!data.WriteInt32(userId)) {
IAM_LOGE("failed to write userId");
return WRITE_PARCEL_ERROR;
}
MessageParcel reply;
bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_AVAILABLE_STATUS, data, reply);
if (!ret) {
IAM_LOGE("failed to send get available status IPC request");

View File

@ -94,7 +94,15 @@ int32_t UserAuthStub::GetAvailableStatusStub(MessageParcel &data, MessageParcel
uint32_t authTrustLevel;
int32_t apiVersion;
int32_t userId;
bool isSpecificUserId = false;
if (!data.ReadBool(isSpecificUserId)) {
IAM_LOGE("failed to read isSpecificUserId");
return READ_PARCEL_ERROR;
}
if (isSpecificUserId && !data.ReadInt32(userId)) {
IAM_LOGE("failed to read userId");
return READ_PARCEL_ERROR;
}
if (!data.ReadInt32(authType)) {
IAM_LOGE("failed to read authType");
return READ_PARCEL_ERROR;
@ -107,13 +115,16 @@ int32_t UserAuthStub::GetAvailableStatusStub(MessageParcel &data, MessageParcel
IAM_LOGE("failed to read apiVersion");
return READ_PARCEL_ERROR;
}
if (!data.ReadInt32(userId)) {
IAM_LOGE("failed to read userId");
return READ_PARCEL_ERROR;
int32_t result = GENERAL_ERROR;
if (isSpecificUserId) {
result = GetAvailableStatus(apiVersion, userId, static_cast<AuthType>(authType),
static_cast<AuthTrustLevel>(authTrustLevel));
} else {
result = GetAvailableStatus(apiVersion, static_cast<AuthType>(authType),
static_cast<AuthTrustLevel>(authTrustLevel));
}
int32_t result = GetAvailableStatus(apiVersion, userId,
static_cast<AuthType>(authType), static_cast<AuthTrustLevel>(authTrustLevel));
if (!reply.WriteInt32(result)) {
IAM_LOGE("failed to write GetAvailableStatus result");
return WRITE_PARCEL_ERROR;

View File

@ -37,7 +37,6 @@ constexpr int INVALID_SOCKET_ID = -1;
constexpr int INVALID_USER_ID = -1;
constexpr int32_t MAIN_USER_ID = 100;
constexpr int32_t MAX_USER = 32;
constexpr int32_t INNER_API_VERSION_10000 = 10000;
/**
* @brief Defines authentication type.

View File

@ -150,8 +150,8 @@ public:
*
* @return Return context ID of authentication.
*/
virtual uint64_t BeginWidgetAuth(int32_t apiVersion, const WidgetAuthParam &authParam,
const WidgetParam &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback) = 0;
virtual uint64_t BeginWidgetAuth(const WidgetAuthParam &authParam, const WidgetParam &widgetParam,
const std::shared_ptr<AuthenticationCallback> &callback) = 0;
/**
* @brief Get available status.

View File

@ -41,6 +41,7 @@ public:
~UserAuthService() override = default;
int32_t GetAvailableStatus(int32_t apiVersion, int32_t userId, AuthType authType,
AuthTrustLevel authTrustLevel) override;
int32_t GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel) override;
void GetProperty(int32_t userId, AuthType authType,
const std::vector<Attributes::AttributeKey> &keys,
sptr<GetExecutorPropertyCallbackInterface> &callback) override;
@ -106,6 +107,8 @@ private:
void FillGetPropertyValue(AuthType authType, const std::vector<Attributes::AttributeKey> &keys, Attributes &values);
bool CompleteRemoteAuthParam(RemoteAuthParam &remoteAuthParam, const std::string &localNetworkId);
int32_t PrepareRemoteAuthInner(const std::string &networkId);
int32_t GetAvailableStatusInner(int32_t apiVersion, int32_t userId, AuthType authType,
AuthTrustLevel authTrustLevel);
static std::mutex mutex_;
static std::shared_ptr<UserAuthService> instance_;
};

View File

@ -182,7 +182,20 @@ bool UserAuthService::CheckAuthTrustLevel(AuthTrustLevel authTrustLevel)
int32_t UserAuthService::GetAvailableStatus(int32_t apiVersion, int32_t userId, AuthType authType,
AuthTrustLevel authTrustLevel)
{
IAM_LOGI("start");
IAM_LOGI("start with userId");
if (!IpcCommon::CheckPermission(*this, ACCESS_USER_AUTH_INTERNAL_PERMISSION) &&
!IpcCommon::CheckPermission(*this, ACCESS_BIOMETRIC_PERMISSION)) {
IAM_LOGE("failed to check permission");
return CHECK_PERMISSION_FAILED;
}
return GetAvailableStatusInner(apiVersion, userId, authType, authTrustLevel);
}
int32_t UserAuthService::GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel)
{
IAM_LOGI("start without userId");
if (!IpcCommon::CheckPermission(*this, ACCESS_USER_AUTH_INTERNAL_PERMISSION) &&
!IpcCommon::CheckPermission(*this, ACCESS_BIOMETRIC_PERMISSION)) {
IAM_LOGE("failed to check permission");
@ -192,14 +205,21 @@ int32_t UserAuthService::GetAvailableStatus(int32_t apiVersion, int32_t userId,
IAM_LOGE("authType not support");
return TYPE_NOT_SUPPORT;
}
int32_t userId = INVALID_USER_ID;
if (IpcCommon::GetCallingUserId(*this, userId) != SUCCESS) {
IAM_LOGE("failed to get userId");
return GENERAL_ERROR;
}
return GetAvailableStatusInner(apiVersion, userId, authType, authTrustLevel);
}
int32_t UserAuthService::GetAvailableStatusInner(int32_t apiVersion, int32_t userId, AuthType authType,
AuthTrustLevel authTrustLevel)
{
if (!CheckAuthTrustLevel(authTrustLevel)) {
IAM_LOGE("authTrustLevel is not in correct range");
return TRUST_LEVEL_NOT_SUPPORT;
}
if (apiVersion != INNER_API_VERSION_10000 && IpcCommon::GetCallingUserId(*this, userId) != SUCCESS) {
IAM_LOGE("failed to get callingUserId");
return GENERAL_ERROR;
}
auto hdi = HdiWrapper::GetHdiInstance();
if (hdi == nullptr) {
IAM_LOGE("hdi interface is nullptr");

View File

@ -119,7 +119,10 @@ void FuzzClientGetAvailableStatus(Parcel &parcel)
auto authType = static_cast<AuthType>(parcel.ReadInt32());
auto atl = static_cast<AuthTrustLevel>(parcel.ReadUint32());
auto userId = static_cast<AuthTrustLevel>(parcel.ReadInt32());
auto apiVersion = static_cast<AuthTrustLevel>(parcel.ReadInt32());
UserAuthClientImpl::Instance().GetAvailableStatus(userId, authType, atl);
UserAuthClientImpl::Instance().GetNorthAvailableStatus(apiVersion, authType, atl);
UserAuthClientImpl::Instance().GetAvailableStatus(authType, atl);
IAM_LOGI("end");
}
@ -270,6 +273,7 @@ void FuzzBeginWidgetAuth(Parcel &parcel)
widgetParam.windowMode = static_cast<WindowModeType>(parcel.ReadInt32());
auto callback = Common::MakeShared<DummyAuthenticationCallback>();
UserAuthClientImpl::Instance().BeginWidgetAuth(apiVersion, authParam, widgetParam, callback);
UserAuthClientImpl::Instance().BeginWidgetAuth(authParam, widgetParam, callback);
IAM_LOGI("end");
}

View File

@ -112,9 +112,6 @@ HWTEST_F(UserAuthClientTest, UserAuthClientGetAvailableStatus002, TestSize.Level
.WillByDefault(
[&testApiVersion, &testAuthType, &testAtl](int32_t apiVersion, int32_t userId, AuthType authType,
AuthTrustLevel authTrustLevel) {
EXPECT_EQ(apiVersion, testApiVersion);
EXPECT_EQ(authType, testAuthType);
EXPECT_EQ(authTrustLevel, testAtl);
return SUCCESS;
}
);
@ -553,6 +550,8 @@ HWTEST_F(UserAuthClientTest, UserAuthClientBeginWidgetAuth001, TestSize.Level0)
uint64_t widgetAuth = UserAuthClientImpl::Instance().BeginWidgetAuth(apiVersion, authParam,
widgetParam, testCallback);
EXPECT_EQ(widgetAuth, 0);
widgetAuth = UserAuthClientImpl::Instance().BeginWidgetAuth(authParam, widgetParam, testCallback);
EXPECT_EQ(widgetAuth, 0);
}
HWTEST_F(UserAuthClientTest, UserAuthClientBeginWidgetAuth002, TestSize.Level0)

View File

@ -97,18 +97,16 @@ HWTEST_F(UserAuthProxyTest, UserAuthProxyGetAvailableStatus, TestSize.Level0)
EXPECT_CALL(*service, GetAvailableStatus(_, _, _, _))
.Times(Exactly(1))
.WillOnce([](int32_t apiVersion, int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel) {
EXPECT_EQ(testApiVersion, apiVersion);
EXPECT_EQ(testAuthType, authType);
EXPECT_EQ(testAuthTrustLevel, authTrustLevel);
return SUCCESS;
});
EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(2);
ON_CALL(*obj, SendRequest)
.WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
service->OnRemoteRequest(code, data, reply, option);
return SUCCESS;
});
proxy->GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel);
EXPECT_EQ(proxy->GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel), SUCCESS);
EXPECT_EQ(proxy->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel), SUCCESS);
}
HWTEST_F(UserAuthProxyTest, UserAuthProxyGetProperty, TestSize.Level0)

View File

@ -29,6 +29,12 @@ public:
MOCK_METHOD4(GetAvailableStatus, int32_t(int32_t apiVersion, int32_t userId, AuthType authType,
AuthTrustLevel authTrustLevel));
int32_t GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel)
{
return 0;
}
MOCK_METHOD4(GetProperty,
void(int32_t userId, AuthType authType, const std::vector<Attributes::AttributeKey> &keys,
sptr<GetExecutorPropertyCallbackInterface> &callback));

View File

@ -194,6 +194,8 @@ HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus003, TestSize.Lev
IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
EXPECT_EQ(GENERAL_ERROR, service.GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel));
EXPECT_EQ(GENERAL_ERROR, service.GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel));
testApiVersion = 9;
EXPECT_EQ(GENERAL_ERROR, service.GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel));
IpcCommon::DeleteAllPermission();
@ -211,6 +213,9 @@ HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus004, TestSize.Lev
int32_t ret = service->GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel);
EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel);
EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
testAuthType = FACE;
ret = service->GetAvailableStatus(testUserId, testApiVersion, testAuthType, testAuthTrustLevel);
EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);

View File

@ -116,18 +116,9 @@ HWTEST_F(UserAuthStubTest, UserAuthStubGetAvailableStatusStub002, TestSize.Level
AuthTrustLevel testAuthTrustLevel = ATL3;
int32_t testApiVersion = 8;
int32_t testUserId = 100;
EXPECT_CALL(service, GetAvailableStatus(_, _, _, _)).Times(1);
ON_CALL(service, GetAvailableStatus)
.WillByDefault(
[&testApiVersion, &testUserId, &testAuthType, &testAuthTrustLevel](int32_t apiVersion, int32_t userId,
AuthType authType, AuthTrustLevel authTrustLevel) {
EXPECT_EQ(apiVersion, testApiVersion);
EXPECT_EQ(authType, testAuthType);
EXPECT_EQ(authTrustLevel, testAuthTrustLevel);
EXPECT_EQ(userId, testUserId);
return SUCCESS;
}
);
EXPECT_CALL(service, GetAvailableStatus(_, _, _, _)).WillRepeatedly([]() {
return SUCCESS;
});
MessageParcel data;
MessageParcel reply;