fix : add auth success event listener

Signed-off-by: hejiaogirl <hejiao@huawei.com>
Change-Id: I36735eec4fb660f7e97dae088e48926cd067ddb7
This commit is contained in:
hejiaogirl 2024-03-12 19:59:04 +08:00
parent 32f1627907
commit 4a98ff70ad
49 changed files with 1329 additions and 40 deletions

View File

@ -83,6 +83,7 @@
"user_auth_client.h",
"user_auth_client_callback.h",
"user_auth_client_defines.h",
"user_auth_event_listener_stub.h",
"user_idm_client.h",
"user_idm_client_callback.h",
"user_idm_client_defines.h"

View File

@ -50,6 +50,10 @@ public:
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);
int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener) override;
int32_t UnRegistUserAuthSuccessEventListener(
const sptr<AuthEventListenerInterface> &listener) override;
private:
ResultCode SetPropertyInner(int32_t userId, const SetPropertyRequest &request,

View File

@ -437,6 +437,53 @@ int32_t UserAuthClientImpl::GetEnrolledState(int32_t apiVersion, AuthType authTy
}
return ret;
}
int32_t UserAuthClientImpl::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGI("start");
if (!listener) {
IAM_LOGE("listener is nullptr");
return GENERAL_ERROR;
}
auto proxy = GetProxy();
if (!proxy) {
IAM_LOGE("proxy is nullptr");
return GENERAL_ERROR;
}
int32_t ret = proxy->RegistUserAuthSuccessEventListener(authType, listener);
if (ret != SUCCESS) {
IAM_LOGE("Regist userAuth success event listener failed");
return ret;
}
return SUCCESS;
}
int32_t UserAuthClientImpl::UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGI("start");
if (!listener) {
IAM_LOGE("listener is nullptr");
return GENERAL_ERROR;
}
auto proxy = GetProxy();
if (!proxy) {
IAM_LOGE("proxy is nullptr");
return GENERAL_ERROR;
}
int32_t ret = proxy->UnRegistUserAuthSuccessEventListener(listener);
if (ret != SUCCESS) {
IAM_LOGE("unRegist userAuth success event listener failed");
return ret;
}
return SUCCESS;
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -27,6 +27,7 @@
OHOS::UserIam::UserAuth::UserIdmClient::GetInstance*;
OHOS::UserIam::UserAuth::Attributes*;
OHOS::UserIam::UserAuth::UserAuthClientImpl::GetEnrolledState*;
OHOS::UserIam::UserAuth::AuthEventListenerStub::OnRemoteRequest*;
};
local:
*;

View File

@ -29,6 +29,8 @@ struct UserAuthTrace {
int32_t authResult = -1;
uint64_t timeSpan = 0;
uint32_t authWidgetType = 0;
int32_t userId = 0;
int32_t callerType = 0;
};
struct UserAuthFwkTrace {

View File

@ -42,6 +42,7 @@ ohos_source_set("userauth_client_ipc") {
"src/executor_callback_stub.cpp",
"src/executor_messenger_proxy.cpp",
"src/user_auth_callback_stub.cpp",
"src/user_auth_event_listener_stub.cpp",
"src/user_auth_proxy.cpp",
"src/user_idm_callback_stub.cpp",
"src/user_idm_proxy.cpp",
@ -88,6 +89,7 @@ ohos_source_set("userauth_service_ipc") {
"src/executor_callback_proxy.cpp",
"src/executor_messenger_stub.cpp",
"src/user_auth_callback_proxy.cpp",
"src/user_auth_event_listener_proxy.cpp",
"src/user_auth_stub.cpp",
"src/user_idm_callback_proxy.cpp",
"src/user_idm_stub.cpp",

View File

@ -24,6 +24,7 @@
#include "attributes.h"
#include "user_auth_callback_interface.h"
#include "user_auth_client_callback.h"
#include "user_auth_interface_ipc_interface_code.h"
#include "widget_callback_interface.h"
@ -62,6 +63,12 @@ public:
virtual int32_t GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState) = 0;
virtual int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener) = 0;
virtual int32_t UnRegistUserAuthSuccessEventListener(
const sptr<AuthEventListenerInterface> &listener) = 0;
DECLARE_INTERFACE_DESCRIPTOR(u"ohos.UserIam.UserAuth.IUserAuth");
};
} // namespace UserAuth

View File

@ -40,6 +40,9 @@ enum UserAuthInterfaceCode : uint32_t {
USER_AUTH_ON_SEND_COMMAND,
USER_AUTH_REG_WIDGET_CB,
USER_AUTH_GET_ENROLLED_STATE,
USER_AUTH_REG_EVENT_LISTENER,
USER_AUTH_UNREG_EVENT_LISTENER,
USER_AUTH_EVENT_LISTENER_NOTIFY,
};
} // namespace UserAuth
} // namespace UserIam

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2024 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 USER_AUTH_EVENT_LISTERNR_PROXY_H
#define USER_AUTH_EVENT_LISTERNR_PROXY_H
#include <iremote_proxy.h>
#include "user_auth_client_callback.h"
namespace OHOS {
namespace UserIam {
namespace UserAuth {
class AuthEventListenerProxy : public IRemoteProxy<AuthEventListenerInterface>, public NoCopyable {
public:
explicit AuthEventListenerProxy(const sptr<IRemoteObject> &object)
: IRemoteProxy<AuthEventListenerInterface>(object)
{
}
~AuthEventListenerProxy() override = default;
void OnNotifyAuthSuccessEvent(int32_t userId, AuthType authType, int32_t callerType,
std::string &callerName) override;
private:
static inline BrokerDelegator<AuthEventListenerProxy> delegator_;
bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply);
};
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS
#endif // USER_AUTH_EVENT_LISTERNR_PROXY_H

View File

@ -47,6 +47,10 @@ public:
int32_t Notice(NoticeType noticeType, const std::string &eventData) override;
int32_t RegisterWidgetCallback(int32_t version, sptr<WidgetCallbackInterface> &callback) override;
int32_t GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState) override;
int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener) override;
int32_t UnRegistUserAuthSuccessEventListener(
const sptr<AuthEventListenerInterface> &listener) override;
private:
static inline BrokerDelegator<UserAuthProxy> delegator_;

View File

@ -44,6 +44,8 @@ private:
bool ReadWidgetParam(MessageParcel &data, AuthParam &authParam, WidgetParam &widgetParam);
int32_t RegisterWidgetCallbackStub(MessageParcel &data, MessageParcel &reply);
int32_t GetEnrolledStateStub(MessageParcel &data, MessageParcel &reply);
int32_t RegistUserAuthSuccessEventListenerStub(MessageParcel &data, MessageParcel &reply);
int32_t UnRegistUserAuthSuccessEventListenerStub(MessageParcel &data, MessageParcel &reply);
};
} // namespace UserAuth
} // namespace UserIam

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2024 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.
*/
#include "user_auth_event_listener_proxy.h"
#include "iam_logger.h"
#include "iam_common_defines.h"
#include "user_auth_interface.h"
#define LOG_TAG "USER_AUTH_SA"
namespace OHOS {
namespace UserIam {
namespace UserAuth {
bool AuthEventListenerProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
{
IAM_LOGI("start");
sptr<IRemoteObject> remote = Remote();
if (remote == nullptr) {
IAM_LOGE("get remote failed");
return false;
}
MessageOption option(MessageOption::TF_ASYNC);
int32_t result = remote->SendRequest(code, data, reply, option);
if (result != OHOS::NO_ERROR) {
IAM_LOGE("send request failed, result = %{public}d", result);
return false;
}
IAM_LOGI("end");
return true;
}
void AuthEventListenerProxy::OnNotifyAuthSuccessEvent(int32_t userId, AuthType authType, int32_t callerType,
std::string &callerName)
{
IAM_LOGI("start");
MessageParcel data;
MessageParcel reply;
if (!data.WriteInterfaceToken(AuthEventListenerProxy::GetDescriptor())) {
IAM_LOGE("write descriptor failed");
return;
}
if (!data.WriteInt32(userId)) {
IAM_LOGE("write userId failed");
return;
}
if (!data.WriteInt32(static_cast<int32_t>(authType))) {
IAM_LOGE("write authType failed");
return;
}
if (!data.WriteString(callerName)) {
IAM_LOGE("write callerName failed");
return;
}
if (!data.WriteInt32(callerType)) {
IAM_LOGE("write callerType failed");
return;
}
bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_EVENT_LISTENER_NOTIFY, data, reply);
if (!ret) {
IAM_LOGE("send request failed");
}
return;
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 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.
*/
#include "user_auth_event_listener_stub.h"
#include <cinttypes>
#include "iam_logger.h"
#include "user_auth_interface.h"
#define LOG_TAG "USER_AUTH_SA"
namespace OHOS {
namespace UserIam {
namespace UserAuth {
int32_t AuthEventListenerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
MessageOption &option)
{
IAM_LOGI("code = %{public}u, flags = %{public}d", code, option.GetFlags());
if (AuthEventListenerStub::GetDescriptor() != data.ReadInterfaceToken()) {
IAM_LOGE("descriptor is not matched");
return GENERAL_ERROR;
}
if (code == UserAuthInterfaceCode::USER_AUTH_EVENT_LISTENER_NOTIFY) {
return OnNotifyAuthSuccessEventStub(data, reply);
}
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
int32_t AuthEventListenerStub::OnNotifyAuthSuccessEventStub(MessageParcel &data, MessageParcel &reply)
{
IAM_LOGI("start");
int32_t userId = 0;
if (!data.ReadInt32(userId)) {
IAM_LOGE("failed to read userId");
return READ_PARCEL_ERROR;
}
int32_t authType = 0;
if (!data.ReadInt32(authType)) {
IAM_LOGE("failed to read authType");
return READ_PARCEL_ERROR;
}
std::string callerName;
if (!data.ReadString(callerName)) {
IAM_LOGE("failed to read callerName");
return READ_PARCEL_ERROR;
}
int32_t callerType = 0;
if (!data.ReadInt32(callerType)) {
IAM_LOGE("failed to read callerType");
return READ_PARCEL_ERROR;
}
OnNotifyAuthSuccessEvent(userId, static_cast<AuthType>(authType), callerType, callerName);
return SUCCESS;
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -555,6 +555,83 @@ int32_t UserAuthProxy::GetEnrolledState(int32_t apiVersion, AuthType authType, E
enrolledState.credentialCount = credentialCount;
return SUCCESS;
}
int32_t UserAuthProxy::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener)
{
if (listener == nullptr) {
IAM_LOGE("listener is nullptr");
return GENERAL_ERROR;
}
MessageParcel data;
MessageParcel reply;
if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
IAM_LOGE("failed to write descriptor");
return WRITE_PARCEL_ERROR;
}
std::vector<int32_t> authTypeList;
for (auto &iter : authType) {
authTypeList.emplace_back(static_cast<int32_t>(iter));
}
if (!data.WriteInt32Vector(authTypeList)) {
IAM_LOGE("failed to write authType");
return WRITE_PARCEL_ERROR;
}
if (!data.WriteRemoteObject(listener->AsObject())) {
IAM_LOGE("failed to write listener");
return WRITE_PARCEL_ERROR;
}
bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_REG_EVENT_LISTENER, data, reply);
if (!ret) {
IAM_LOGE("failed to send register event listener callback IPC request");
return GENERAL_ERROR;
}
int32_t result = GENERAL_ERROR;
if (!reply.ReadInt32(result)) {
IAM_LOGE("failed to read result");
return READ_PARCEL_ERROR;
}
return result;
}
int32_t UserAuthProxy::UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener)
{
if (listener == nullptr) {
IAM_LOGE("listener is nullptr");
return GENERAL_ERROR;
}
MessageParcel data;
MessageParcel reply;
if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
IAM_LOGE("failed to write descriptor");
return WRITE_PARCEL_ERROR;
}
if (!data.WriteRemoteObject(listener->AsObject())) {
IAM_LOGE("failed to write listener");
return WRITE_PARCEL_ERROR;
}
bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_UNREG_EVENT_LISTENER, data, reply);
if (!ret) {
IAM_LOGE("failed to send register event listener callback IPC request");
return GENERAL_ERROR;
}
int32_t result = GENERAL_ERROR;
if (!reply.ReadInt32(result)) {
IAM_LOGE("failed to read result");
return READ_PARCEL_ERROR;
}
return result;
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -22,6 +22,7 @@
#include "iam_scope_guard.h"
#include "iam_common_defines.h"
#include "user_auth_callback_proxy.h"
#include "user_auth_event_listener_proxy.h"
#include "widget_callback_proxy.h"
#include "user_auth_common_defines.h"
@ -68,6 +69,10 @@ int32_t UserAuthStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Messag
return RegisterWidgetCallbackStub(data, reply);
case UserAuthInterfaceCode::USER_AUTH_GET_ENROLLED_STATE:
return GetEnrolledStateStub(data, reply);
case UserAuthInterfaceCode::USER_AUTH_REG_EVENT_LISTENER:
return RegistUserAuthSuccessEventListenerStub(data, reply);
case UserAuthInterfaceCode::USER_AUTH_UNREG_EVENT_LISTENER:
return UnRegistUserAuthSuccessEventListenerStub(data, reply);
default:
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
@ -518,6 +523,63 @@ int32_t UserAuthStub::GetEnrolledStateStub(MessageParcel &data, MessageParcel &r
}
return SUCCESS;
}
int32_t UserAuthStub::RegistUserAuthSuccessEventListenerStub(MessageParcel &data, MessageParcel &reply)
{
IAM_LOGI("enter");
ON_SCOPE_EXIT(IAM_LOGI("leave"));
std::vector<int32_t> authType;
if (!data.ReadInt32Vector(&authType)) {
IAM_LOGE("failed to read authTypeList");
return READ_PARCEL_ERROR;
}
sptr<IRemoteObject> obj = data.ReadRemoteObject();
if (obj == nullptr) {
IAM_LOGE("failed to read remote object");
return READ_PARCEL_ERROR;
}
sptr<AuthEventListenerInterface> listener = iface_cast<AuthEventListenerProxy>(obj);
if (listener == nullptr) {
IAM_LOGE("authEventListener listener is nullptr");
return GENERAL_ERROR;
}
std::vector<AuthType> authTypeList;
for (auto &iter : authType) {
authTypeList.emplace_back(static_cast<AuthType>(iter));
}
int32_t result = RegistUserAuthSuccessEventListener(authTypeList, listener);
if (!reply.WriteInt32(result)) {
IAM_LOGE("failed to write regist event listener result");
return WRITE_PARCEL_ERROR;
}
IAM_LOGI("RegistUserAuthSuccessEventListenerStub success");
return SUCCESS;
}
int32_t UserAuthStub::UnRegistUserAuthSuccessEventListenerStub(MessageParcel &data, MessageParcel &reply)
{
IAM_LOGI("enter");
ON_SCOPE_EXIT(IAM_LOGI("leave"));
sptr<IRemoteObject> obj = data.ReadRemoteObject();
if (obj == nullptr) {
IAM_LOGE("failed to read remote object");
return READ_PARCEL_ERROR;
}
sptr<AuthEventListenerInterface> listener = iface_cast<AuthEventListenerProxy>(obj);
if (listener == nullptr) {
IAM_LOGE("authEventListener listener is nullptr");
return GENERAL_ERROR;
}
int32_t result = UnRegistUserAuthSuccessEventListener(listener);
if (!reply.WriteInt32(result)) {
IAM_LOGE("failed to write regist event listener result");
return WRITE_PARCEL_ERROR;
}
IAM_LOGI("UnRegistUserAuthSuccessEventListener success");
return SUCCESS;
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -106,6 +106,25 @@ public:
* @return Return CancelIdentification result(0:success; other:failed).
*/
virtual int32_t CancelIdentification(uint64_t contextId) = 0;
/**
* @brief Regist authentication success event listener, support repeated registration.
*
* @param authType Auth type list supported by executor, auth type include PIN, FACE, FINGERPRINT.
* @param listener Callback of authentication success event.
* @return Return regist result(0:success; other:failed).
*/
virtual int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener) = 0;
/**
* @brief unRegist authentication success event listener.
*
* @param listener Callback of authentication success event.
* @return Return unregist result(0:success; other:failed).
*/
virtual int32_t UnRegistUserAuthSuccessEventListener(
const sptr<AuthEventListenerInterface> &listener) = 0;
};
} // namespace UserAuth
} // namespace UserIam

View File

@ -25,6 +25,7 @@
#define USER_AUTH_CLIENT_CALLBACK_H
#include "attributes.h"
#include "iremote_broker.h"
#include "user_auth_client_defines.h"
namespace OHOS {
@ -91,6 +92,21 @@ public:
*/
virtual void OnResult(int32_t result, const Attributes &extraInfo) = 0;
};
class AuthEventListenerInterface : public IRemoteBroker {
public:
/**
* @brief Notify the event of authencation success.
*
* @param userId The id of user who initiates authentication.
* @param authtype The authentication auth type{@link AuthType}.
* @param callerType The caller type who initiates authentication.
* @param callerName The caller name who initiates authentication.
*/
virtual void OnNotifyAuthSuccessEvent(int32_t userId, AuthType authtype, int32_t callerType,
std::string &callerName) = 0;
DECLARE_INTERFACE_DESCRIPTOR(u"ohos.UserIam.UserAuth.EventListenerCallback");
};
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 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 USER_AUTH_EVENT_LISTENER_STUB_H
#define USER_AUTH_EVENT_LISTENER_STUB_H
#include "iremote_stub.h"
#include "message_parcel.h"
#include "nocopyable.h"
#include "user_auth_client_callback.h"
namespace OHOS {
namespace UserIam {
namespace UserAuth {
class AuthEventListenerStub : public IRemoteStub<AuthEventListenerInterface>, public NoCopyable {
public:
int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
private:
int32_t OnNotifyAuthSuccessEventStub(MessageParcel &data, MessageParcel &reply);
};
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS
#endif // USER_AUTH_EVENT_LISTENER_STUB_H

View File

@ -46,6 +46,7 @@ public:
std::optional<int32_t> authType;
std::optional<uint32_t> authWidgetType;
std::optional<std::string> callerName;
std::optional<int32_t> callerType;
std::chrono::time_point<std::chrono::steady_clock> startTime;
std::chrono::time_point<std::chrono::steady_clock> endTime;
};
@ -77,6 +78,7 @@ public:
virtual void SetTraceAuthWidgetType(uint32_t authWidgetType) = 0;
virtual void SetTraceAuthTrustLevel(AuthTrustLevel atl) = 0;
virtual void SetCleaner(Context::ContextStopCallback callback) = 0;
virtual void SetTraceCallerType(int32_t callerType) = 0;
virtual void ProcessAuthResult(int32_t tip, const std::vector<uint8_t> &extraInfo) = 0;
virtual sptr<IamCallbackInterface> GetIamCallback() = 0;
virtual std::string GetCallerName() = 0;

View File

@ -51,6 +51,7 @@ public:
AuthTrustLevel atl {ATL1};
WidgetParam widgetParam {};
std::map<AuthType, AuthProfile> authProfileMap {};
int32_t callerType {0};
};
static std::shared_ptr<Context> CreateSimpleAuthContext(const Authentication::AuthenticationPara &para,
const std::shared_ptr<ContextCallback> &callback);

View File

@ -160,6 +160,11 @@ void ContextCallbackImpl::SetTraceAuthTrustLevel(AuthTrustLevel atl)
metaData_.atl = atl;
}
void ContextCallbackImpl::SetTraceCallerType(int32_t callerType)
{
metaData_.callerType = callerType;
}
void ContextCallbackImpl::SetCleaner(Context::ContextStopCallback callback)
{
stopCallback_ = callback;

View File

@ -41,6 +41,7 @@ public:
void SetTraceAuthWidgetType(uint32_t authWidgetType) override;
void SetTraceAuthTrustLevel(AuthTrustLevel atl) override;
void SetCleaner(Context::ContextStopCallback callback) override;
void SetTraceCallerType(int32_t callerType) override;
void ProcessAuthResult(int32_t tip, const std::vector<uint8_t> &extraInfo) override;
sptr<IamCallbackInterface> GetIamCallback() override;
std::string GetCallerName() override;

View File

@ -17,6 +17,7 @@
#include <cinttypes>
#include <sstream>
#include "auth_event_listener_manager.h"
#include "iam_logger.h"
#include "iam_time.h"
#include "hisysevent_adapter.h"
@ -118,6 +119,12 @@ void Trace::ProcessUserAuthEvent(const ContextCallbackNotifyListener::MetaData &
if (metaData.authType.has_value() && metaData.operationResult == SUCCESS) {
info.authType = metaData.authType.value();
}
if (metaData.userId.has_value()) {
info.userId = metaData.userId.value();
}
if (metaData.callerType.has_value()) {
info.callerType = metaData.callerType.value();
}
info.authResult = metaData.operationResult;
uint64_t timeSpan = std::chrono::duration_cast<std::chrono::milliseconds>(metaData.endTime -
metaData.startTime).count();
@ -126,6 +133,10 @@ void Trace::ProcessUserAuthEvent(const ContextCallbackNotifyListener::MetaData &
info.authWidgetType = metaData.authWidgetType.value();
}
ReportUserAuth(info);
if (info.authResult == SUCCESS) {
AuthEventListenerManager::GetInstance().OnNotifyAuthSuccessEvent(info.userId,
static_cast<AuthType>(info.authType), info.callerType, info.callerName);
}
IAM_LOGI("start to process user auth event");
}

View File

@ -133,6 +133,7 @@ std::shared_ptr<ContextCallback> WidgetContext::GetAuthContextCallback(AuthType
return nullptr;
}
widgetCallback->SetTraceCallerName(para_.callerName);
widgetCallback->SetTraceCallerType(para_.callerType);
widgetCallback->SetTraceRequestContextId(contextId_);
widgetCallback->SetTraceAuthTrustLevel(authTrustLevel);
widgetCallback->SetTraceAuthType(authType);

View File

@ -48,6 +48,7 @@ ohos_source_set("userauth_service_core") {
remove_configs = [ "//build/config/compiler:no_exceptions" ]
sources = [
"src/auth_event_listener_manager.cpp",
"src/auth_widget_helper.cpp",
"src/authentication_impl.cpp",
"src/credential_info_impl.cpp",

View File

@ -0,0 +1,78 @@
/*
* Copyright (c) 2024 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 IAM_AUTH_EVENT_LISTENER_MANAGER_H
#define IAM_AUTH_EVENT_LISTENER_MANAGER_H
#include <mutex>
#include <set>
#include <map>
#include "user_auth_interface.h"
namespace OHOS {
namespace UserIam {
namespace UserAuth {
using DeathRecipient = IRemoteObject::DeathRecipient;
class AuthEventListenerManager {
public:
static AuthEventListenerManager &GetInstance();
int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener);
int32_t UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener);
void OnNotifyAuthSuccessEvent(int32_t userId, AuthType authType, int32_t callerType, std::string &callerName);
int32_t AddDeathRecipient(const sptr<AuthEventListenerInterface> &listener);
int32_t RemoveDeathRecipient(const sptr<AuthEventListenerInterface> &listener);
std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>> GetDeathRecipientMap();
protected:
class AuthEventListenerDeathRecipient : public IRemoteObject::DeathRecipient, public NoCopyable {
public:
AuthEventListenerDeathRecipient() = default;
~AuthEventListenerDeathRecipient() override = default;
void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
};
AuthEventListenerManager() = default;
~AuthEventListenerManager() = default;
void AddAuthSuccessEventListener(AuthType authType, const sptr<AuthEventListenerInterface> &listener);
void RemoveAuthSuccessEventListener(AuthType authType, const sptr<AuthEventListenerInterface> &listener);
std::set<sptr<AuthEventListenerInterface>> GetListenerSet(AuthType authType);
std::mutex mutex_;
std::map<AuthType, std::set<sptr<AuthEventListenerInterface>>> eventListenerMap_;
std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>> deathRecipientMap_;
private:
struct FinderSet {
explicit FinderSet(sptr<IRemoteObject> remoteObject) : remoteObject_(remoteObject) {}
bool operator()(sptr<AuthEventListenerInterface> listener)
{
return listener->AsObject() == remoteObject_;
}
sptr<IRemoteObject> remoteObject_;
};
struct FinderMap {
explicit FinderMap(sptr<IRemoteObject> remoteObject) : remoteObject_(remoteObject) {}
bool operator()(std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>>::value_type &pair)
{
return pair.first->AsObject() == remoteObject_;
}
sptr<IRemoteObject> remoteObject_;
};
};
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS
#endif // IAM_AUTH_EVENT_LISTENER_MANAGER_H

View File

@ -35,6 +35,7 @@ public:
bool endAfterFirstFail;
std::string callerName;
int32_t sdkVersion;
int32_t callerType;
};
struct AuthResultInfo {

View File

@ -37,6 +37,7 @@ public:
std::vector<uint8_t> token;
std::string callerName;
int32_t sdkVersion;
int32_t callerType;
};
virtual ~Enrollment() = default;

View File

@ -0,0 +1,213 @@
/*
* Copyright (c) 2024 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.
*/
#include "auth_event_listener_manager.h"
#include <sstream>
#include "iam_check.h"
#include "iam_logger.h"
#include "iam_para2str.h"
#define LOG_TAG "USER_AUTH_SA"
namespace OHOS {
namespace UserIam {
namespace UserAuth {
using DeathRecipient = IRemoteObject::DeathRecipient;
AuthEventListenerManager &AuthEventListenerManager::GetInstance()
{
IAM_LOGI("start");
static AuthEventListenerManager authEventListenerManager;
return authEventListenerManager;
}
int32_t AuthEventListenerManager::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGI("start");
IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, GENERAL_ERROR);
std::lock_guard<std::mutex> lock(mutex_);
int32_t result = AddDeathRecipient(listener);
if (result != SUCCESS) {
IAM_LOGE("AddDeathRecipient fail");
return result;
}
for (const auto &iter : authType) {
AddAuthSuccessEventListener(iter, listener);
}
IAM_LOGI("RegistUserAuthSuccessEventListener success");
return SUCCESS;
}
int32_t AuthEventListenerManager::UnRegistUserAuthSuccessEventListener(
const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGI("start");
IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, GENERAL_ERROR);
std::lock_guard<std::mutex> lock(mutex_);
int32_t result = RemoveDeathRecipient(listener);
if (result != SUCCESS) {
IAM_LOGE("RemoveDeathRecipient fail");
return result;
}
RemoveAuthSuccessEventListener(AuthType::PIN, listener);
RemoveAuthSuccessEventListener(AuthType::FACE, listener);
RemoveAuthSuccessEventListener(AuthType::FINGERPRINT, listener);
IAM_LOGI("UnRegistUserAuthSuccessEventListener success");
return SUCCESS;
}
void AuthEventListenerManager::AddAuthSuccessEventListener(AuthType authType,
const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGI("AddAuthSuccessEventListener, authType:%{public}d", static_cast<int32_t>(authType));
auto iter = std::find_if(eventListenerMap_[authType].begin(), eventListenerMap_[authType].end(),
FinderSet(listener->AsObject()));
if (iter != eventListenerMap_[authType].end()) {
IAM_LOGE("listener is already registed");
return;
}
eventListenerMap_[authType].insert(listener);
IAM_LOGI("AddAuthSuccessEventListener success");
}
void AuthEventListenerManager::RemoveAuthSuccessEventListener(AuthType authType,
const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGI("RemoveAuthSuccessEventListener, authType:%{public}d", static_cast<int32_t>(authType));
auto iter = std::find_if(eventListenerMap_[authType].begin(), eventListenerMap_[authType].end(),
FinderSet(listener->AsObject()));
if (iter == eventListenerMap_[authType].end()) {
IAM_LOGE("listener is not registed");
return;
}
eventListenerMap_[authType].erase(listener);
IAM_LOGI("RemoveAuthSuccessEventListener success");
}
std::set<sptr<AuthEventListenerInterface>> AuthEventListenerManager::GetListenerSet(AuthType authType)
{
std::lock_guard<std::mutex> lock(mutex_);
std::set<sptr<AuthEventListenerInterface>> listenerSet(eventListenerMap_[authType]);
return listenerSet;
}
void AuthEventListenerManager::OnNotifyAuthSuccessEvent(int32_t userId, AuthType authType, int32_t callerType,
std::string &callerName)
{
IAM_LOGI("start");
std::set<sptr<AuthEventListenerInterface>> listenerSetTemp = GetListenerSet(authType);
for (auto &iter : listenerSetTemp) {
if (iter != nullptr) {
iter->OnNotifyAuthSuccessEvent(userId, authType, callerType, callerName);
IAM_LOGI("OnNotifyAuthSuccessEvent, userId: %{public}d, authType: %{public}d, callerName: %{public}s, "
"callerType: %{public}d", userId, static_cast<int32_t>(authType), callerName.c_str(), callerType);
}
}
}
int32_t AuthEventListenerManager::AddDeathRecipient(const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGI("start");
IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, GENERAL_ERROR);
auto obj = listener->AsObject();
if (obj == nullptr) {
IAM_LOGE("remote object is nullptr");
return GENERAL_ERROR;
}
auto iter = std::find_if(deathRecipientMap_.begin(), deathRecipientMap_.end(),
FinderMap(listener->AsObject()));
if (iter != deathRecipientMap_.end()) {
IAM_LOGE("deathRecipient is already registed");
return SUCCESS;
}
sptr<DeathRecipient> dr(new (std::nothrow) AuthEventListenerDeathRecipient());
if ((dr == nullptr) || (!obj->AddDeathRecipient(dr))) {
IAM_LOGE("AddDeathRecipient failed");
return GENERAL_ERROR;
}
deathRecipientMap_.emplace(listener, dr);
IAM_LOGI("AddDeathRecipient success");
return SUCCESS;
}
int32_t AuthEventListenerManager::RemoveDeathRecipient(const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGI("start");
IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, GENERAL_ERROR);
auto obj = listener->AsObject();
if (obj == nullptr) {
IAM_LOGE("remote object is nullptr");
return GENERAL_ERROR;
}
auto iter = std::find_if(deathRecipientMap_.begin(), deathRecipientMap_.end(),
FinderMap(listener->AsObject()));
if (iter == deathRecipientMap_.end()) {
IAM_LOGE("deathRecipient is not registed");
return SUCCESS;
}
sptr<DeathRecipient> deathRecipient = iter->second;
if (deathRecipient == nullptr) {
IAM_LOGE("deathRecipient is nullptr");
return GENERAL_ERROR;
}
obj->RemoveDeathRecipient(deathRecipient);
deathRecipientMap_.erase(listener);
IAM_LOGI("RemoveDeathRecipient success");
return SUCCESS;
}
std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>> AuthEventListenerManager::GetDeathRecipientMap()
{
IAM_LOGI("start");
return deathRecipientMap_;
}
void AuthEventListenerManager::AuthEventListenerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
{
IAM_LOGI("start");
if (remote == nullptr) {
IAM_LOGE("remote is nullptr");
return;
}
std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>> deathRecipientMap =
AuthEventListenerManager::GetInstance().GetDeathRecipientMap();
for (auto &iter : deathRecipientMap) {
if (iter.first != nullptr && remote == iter.first->AsObject()) {
int32_t result = AuthEventListenerManager::GetInstance().UnRegistUserAuthSuccessEventListener(iter.first);
if (result != SUCCESS) {
IAM_LOGE("UnRegistUserAuthSuccessEventListener fail");
return;
}
}
}
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -21,6 +21,7 @@
#include <optional>
#include <string>
#include "iam_common_defines.h"
#include "nocopyable.h"
namespace OHOS {
@ -47,7 +48,8 @@ public:
static bool CheckPermission(IPCObjectStub &stub, Permission permission);
static uint32_t GetAccessTokenId(IPCObjectStub &stub);
static uint32_t GetTokenId(IPCObjectStub &stub);
static bool GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::string &callerName);
static bool GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::string &callerName,
int32_t &callerType);
static bool CheckForegroundApplication(const std::string &bundleName);
class PeerDeathRecipient final : public IPCObjectProxy::DeathRecipient {
public:

View File

@ -55,6 +55,9 @@ public:
int32_t Notice(NoticeType noticeType, const std::string &eventData) override;
int32_t RegisterWidgetCallback(int32_t version, sptr<WidgetCallbackInterface> &callback) override;
int32_t GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState) override;
int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener) override;
int32_t UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener) override;
protected:
void OnStart() override;
@ -80,6 +83,9 @@ private:
bool CheckCallerIsSystemApp();
int32_t CheckAuthPermissionAndParam(int32_t authType, bool isBundleName, const std::string &callerName,
AuthTrustLevel authTrustLevel);
bool CheckAuthPermissionAndParam(AuthType authType, AuthTrustLevel authTrustLevel,
const std::shared_ptr<ContextCallback> &contextCallback, Attributes &extraInfo);
bool CheckAuthTypeIsValid(std::vector<AuthType> authType);
};
} // namespace UserAuth
} // namespace UserIam

View File

@ -71,6 +71,8 @@ private:
void ClearRedundancyCredentialInner();
void SetAuthTypeTrace(const std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos,
const std::shared_ptr<ContextCallback> &contextCallback);
void StartEnroll(Enrollment::EnrollmentPara &para,
const std::shared_ptr<ContextCallback> &contextCallback, Attributes &extraInfo);
std::mutex mutex_;
};
} // namespace UserAuth

View File

@ -278,7 +278,8 @@ bool IpcCommon::CheckCallerIsSystemApp(IPCObjectStub &stub)
return false;
}
bool IpcCommon::GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::string &callerName)
bool IpcCommon::GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::string &callerName,
int32_t &callerType)
{
uint32_t tokenId = GetAccessTokenId(stub);
using namespace Security::AccessToken;
@ -307,6 +308,7 @@ bool IpcCommon::GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::stri
return true;
}
isBundleName = false;
callerType = static_cast<int32_t>(callingType);
IAM_LOGI("caller is not a hap or a native");
return false;
}

View File

@ -20,6 +20,7 @@
#include "accesstoken_kit.h"
#include "auth_common.h"
#include "auth_event_listener_manager.h"
#include "auth_widget_helper.h"
#include "context_factory.h"
#include "auth_common.h"
@ -318,12 +319,14 @@ uint64_t UserAuthService::Auth(int32_t apiVersion, const std::vector<uint8_t> &c
bool isBundleName = false;
std::string callerName = "";
Attributes extraInfo;
if ((!IpcCommon::GetCallerName(*this, isBundleName, callerName)) && isBundleName) {
int32_t callerType = 0;
if ((!IpcCommon::GetCallerName(*this, isBundleName, callerName, callerType)) && isBundleName) {
IAM_LOGE("get bundle name fail");
contextCallback->OnResult(GENERAL_ERROR, extraInfo);
return BAD_CONTEXT_ID;
}
contextCallback->SetTraceCallerName(callerName);
contextCallback->SetTraceCallerType(callerType);
int32_t checkRet = CheckAuthPermissionAndParam(authType, isBundleName, callerName, authTrustLevel);
if (checkRet != SUCCESS) {
IAM_LOGE("check auth permission and param fail");
@ -376,6 +379,27 @@ uint64_t UserAuthService::StartAuthContext(int32_t apiVersion, Authentication::A
return context->GetContextId();
}
bool UserAuthService::CheckAuthPermissionAndParam(AuthType authType, AuthTrustLevel authTrustLevel,
const std::shared_ptr<ContextCallback> &contextCallback, Attributes &extraInfo)
{
if (!CheckAuthTrustLevel(authTrustLevel)) {
IAM_LOGE("authTrustLevel is not in correct range");
contextCallback->OnResult(TRUST_LEVEL_NOT_SUPPORT, extraInfo);
return false;
}
if (!IpcCommon::CheckPermission(*this, ACCESS_USER_AUTH_INTERNAL_PERMISSION)) {
IAM_LOGE("failed to check permission");
contextCallback->OnResult(CHECK_PERMISSION_FAILED, extraInfo);
return false;
}
if (!SystemParamManager::GetInstance().IsAuthTypeEnable(authType)) {
IAM_LOGE("auth type not support");
contextCallback->OnResult(TYPE_NOT_SUPPORT, extraInfo);
return false;
}
return true;
}
uint64_t UserAuthService::AuthUser(int32_t userId, const std::vector<uint8_t> &challenge,
AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback)
{
@ -390,25 +414,15 @@ uint64_t UserAuthService::AuthUser(int32_t userId, const std::vector<uint8_t> &c
Attributes extraInfo;
bool isBundleName = false;
std::string callerName = "";
if ((!IpcCommon::GetCallerName(*this, isBundleName, callerName)) && isBundleName) {
int32_t callerType = 0;
if ((!IpcCommon::GetCallerName(*this, isBundleName, callerName, callerType)) && isBundleName) {
IAM_LOGE("get bundle name fail");
contextCallback->OnResult(GENERAL_ERROR, extraInfo);
return BAD_CONTEXT_ID;
}
contextCallback->SetTraceCallerName(callerName);
if (!CheckAuthTrustLevel(authTrustLevel)) {
IAM_LOGE("authTrustLevel is not in correct range");
contextCallback->OnResult(TRUST_LEVEL_NOT_SUPPORT, extraInfo);
return BAD_CONTEXT_ID;
}
if (!IpcCommon::CheckPermission(*this, ACCESS_USER_AUTH_INTERNAL_PERMISSION)) {
IAM_LOGE("failed to check permission");
contextCallback->OnResult(CHECK_PERMISSION_FAILED, extraInfo);
return BAD_CONTEXT_ID;
}
if (!SystemParamManager::GetInstance().IsAuthTypeEnable(authType)) {
IAM_LOGE("auth type not support");
contextCallback->OnResult(TYPE_NOT_SUPPORT, extraInfo);
contextCallback->SetTraceCallerType(callerType);
if (CheckAuthPermissionAndParam(authType, authTrustLevel, contextCallback, extraInfo) == false) {
return BAD_CONTEXT_ID;
}
Authentication::AuthenticationPara para = {};
@ -423,6 +437,7 @@ uint64_t UserAuthService::AuthUser(int32_t userId, const std::vector<uint8_t> &c
} else {
para.callerName = "N_" + callerName;
}
para.callerType = callerType;
para.sdkVersion = INNER_API_VERSION_10000;
return StartAuthContext(INNER_API_VERSION_10000, para, contextCallback);
}
@ -637,8 +652,10 @@ uint64_t UserAuthService::AuthWidget(int32_t apiVersion, const AuthParam &authPa
}
bool isBundleName = false;
std::string callerName = "";
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName));
int32_t callerType = 0;
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName, callerType));
contextCallback->SetTraceCallerName(callerName);
contextCallback->SetTraceCallerType(callerType);
Attributes extraInfo;
if (!IpcCommon::CheckPermission(*this, IS_SYSTEM_APP) &&
(widgetParam.windowMode != WindowModeType::UNKNOWN_WINDOW_MODE)) {
@ -672,6 +689,7 @@ uint64_t UserAuthService::AuthWidget(int32_t apiVersion, const AuthParam &authPa
} else {
para.callerName = "N_" + callerName;
}
para.callerType = callerType;
para.sdkVersion = apiVersion;
return StartWidgetContext(contextCallback, authParam, widgetParam, validType, para);
}
@ -806,6 +824,63 @@ int32_t UserAuthService::GetEnrolledState(int32_t apiVersion, AuthType authType,
return SUCCESS;
}
bool UserAuthService::CheckAuthTypeIsValid(std::vector<AuthType> authType)
{
if (authType.empty()) {
return false;
}
for (const auto &iter : authType) {
if (iter != AuthType::PIN && iter != AuthType::FACE && iter != AuthType::FINGERPRINT) {
return false;
}
}
return true;
}
int32_t UserAuthService::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGE("start");
IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, INVALID_PARAMETERS);
if (!CheckAuthTypeIsValid(authType)) {
IAM_LOGE("failed to check authType");
return INVALID_PARAMETERS;
}
if (!IpcCommon::CheckPermission(*this, ACCESS_USER_AUTH_INTERNAL_PERMISSION)) {
IAM_LOGE("failed to check permission");
return CHECK_PERMISSION_FAILED;
}
int32_t result = AuthEventListenerManager::GetInstance().RegistUserAuthSuccessEventListener(authType, listener);
if (result != SUCCESS) {
IAM_LOGE("failed to regist auth event listener");
return result;
}
return SUCCESS;
}
int32_t UserAuthService::UnRegistUserAuthSuccessEventListener(
const sptr<AuthEventListenerInterface> &listener)
{
IAM_LOGE("start");
IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, INVALID_PARAMETERS);
if (!IpcCommon::CheckPermission(*this, ACCESS_USER_AUTH_INTERNAL_PERMISSION)) {
IAM_LOGE("failed to check permission");
return CHECK_PERMISSION_FAILED;
}
int32_t result = AuthEventListenerManager::GetInstance().UnRegistUserAuthSuccessEventListener(listener);
if (result != SUCCESS) {
IAM_LOGE("failed to unregist auth event listener");
return result;
}
return SUCCESS;
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -217,6 +217,25 @@ bool UserIdmService::CheckEnrollPermissionAndEnableStatus(
return true;
}
void UserIdmService::StartEnroll(Enrollment::EnrollmentPara &para,
const std::shared_ptr<ContextCallback> &contextCallback, Attributes &extraInfo)
{
auto context = ContextFactory::CreateEnrollContext(para, contextCallback);
if (context == nullptr || !ContextPool::Instance().Insert(context)) {
IAM_LOGE("failed to insert context");
contextCallback->OnResult(GENERAL_ERROR, extraInfo);
return;
}
contextCallback->SetTraceRequestContextId(context->GetContextId());
auto cleaner = ContextHelper::Cleaner(context);
contextCallback->SetCleaner(cleaner);
if (!context->Start()) {
IAM_LOGE("failed to start enroll");
contextCallback->OnResult(context->GetLatestError(), extraInfo);
}
}
void UserIdmService::AddCredential(int32_t userId, const CredentialPara &credPara,
const sptr<IdmCallbackInterface> &callback, bool isUpdate)
{
@ -232,8 +251,10 @@ void UserIdmService::AddCredential(int32_t userId, const CredentialPara &credPar
}
bool isBundleName = false;
std::string callerName = "";
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName));
int32_t callerType = 0;
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName, callerType));
contextCallback->SetTraceCallerName(callerName);
contextCallback->SetTraceCallerType(callerType);
contextCallback->SetTraceUserId(userId);
contextCallback->SetTraceAuthType(credPara.authType);
@ -257,20 +278,8 @@ void UserIdmService::AddCredential(int32_t userId, const CredentialPara &credPar
} else {
para.callerName = "N_" + callerName;
}
auto context = ContextFactory::CreateEnrollContext(para, contextCallback);
if (context == nullptr || !ContextPool::Instance().Insert(context)) {
IAM_LOGE("failed to insert context");
contextCallback->OnResult(GENERAL_ERROR, extraInfo);
return;
}
contextCallback->SetTraceRequestContextId(context->GetContextId());
auto cleaner = ContextHelper::Cleaner(context);
contextCallback->SetCleaner(cleaner);
if (!context->Start()) {
IAM_LOGE("failed to start enroll");
contextCallback->OnResult(context->GetLatestError(), extraInfo);
}
para.callerType = callerType;
StartEnroll(para, contextCallback, extraInfo);
}
void UserIdmService::UpdateCredential(int32_t userId, const CredentialPara &credPara,
@ -348,7 +357,8 @@ int32_t UserIdmService::EnforceDelUser(int32_t userId, const sptr<IdmCallbackInt
}
bool isBundleName = false;
std::string callerName = "";
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName));
int32_t callerType = 0;
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName, callerType));
contextCallback->SetTraceUserId(userId);
if (!IpcCommon::CheckPermission(*this, ENFORCE_USER_IDM)) {
@ -393,7 +403,8 @@ void UserIdmService::DelUser(int32_t userId, const std::vector<uint8_t> authToke
}
bool isBundleName = false;
std::string callerName = "";
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName));
int32_t callerType = 0;
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName, callerType));
contextCallback->SetTraceUserId(userId);
if (!IpcCommon::CheckPermission(*this, MANAGE_USER_IDM_PERMISSION)) {
@ -438,7 +449,8 @@ void UserIdmService::DelCredential(int32_t userId, uint64_t credentialId,
}
bool isBundleName = false;
std::string callerName = "";
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName));
int32_t callerType = 0;
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName, callerType));
contextCallback->SetTraceUserId(userId);
if (!IpcCommon::CheckPermission(*this, MANAGE_USER_IDM_PERMISSION)) {
@ -569,7 +581,8 @@ void UserIdmService::ClearRedundancyCredentialInner()
}
bool isBundleName = false;
std::string callerName = "";
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName));
int32_t callerType = 0;
static_cast<void>(IpcCommon::GetCallerName(*this, isBundleName, callerName, callerType));
for (const auto &iter : userInfos) {
int32_t userId = iter->GetUserId();
@ -580,6 +593,7 @@ void UserIdmService::ClearRedundancyCredentialInner()
}
callbackForTrace->SetTraceUserId(userId);
callbackForTrace->SetTraceCallerName(callerName);
callbackForTrace->SetTraceCallerType(callerType);
std::vector<int32_t>::iterator it = std::find(accountInfo.begin(), accountInfo.end(), userId);
if (it == accountInfo.end()) {
ret = EnforceDelUserInner(userId, callbackForTrace, "DeleteRedundancy");

View File

@ -80,6 +80,7 @@ ohos_source_set("userauth_client_ipc_fuzzer") {
"../../../frameworks/native/ipc/src/executor_callback_stub.cpp",
"../../../frameworks/native/ipc/src/executor_messenger_proxy.cpp",
"../../../frameworks/native/ipc/src/user_auth_callback_stub.cpp",
"../../../frameworks/native/ipc/src/user_auth_event_listener_stub.cpp",
"../../../frameworks/native/ipc/src/user_auth_proxy.cpp",
"../../../frameworks/native/ipc/src/user_idm_callback_stub.cpp",
"../../../frameworks/native/ipc/src/user_idm_proxy.cpp",
@ -149,6 +150,7 @@ ohos_source_set("userauth_service_core_fuzzer") {
remove_configs = [ "//build/config/compiler:no_exceptions" ]
sources = [
"../../../services/core/src/auth_event_listener_manager.cpp",
"../../../services/core/src/auth_widget_helper.cpp",
"../../../services/core/src/authentication_impl.cpp",
"../../../services/core/src/credential_info_impl.cpp",
@ -281,6 +283,7 @@ ohos_source_set("userauth_service_ipc_fuzzer") {
"../../../frameworks/native/ipc/src/executor_callback_proxy.cpp",
"../../../frameworks/native/ipc/src/executor_messenger_stub.cpp",
"../../../frameworks/native/ipc/src/user_auth_callback_proxy.cpp",
"../../../frameworks/native/ipc/src/user_auth_event_listener_proxy.cpp",
"../../../frameworks/native/ipc/src/user_auth_stub.cpp",
"../../../frameworks/native/ipc/src/user_idm_callback_proxy.cpp",
"../../../frameworks/native/ipc/src/user_idm_stub.cpp",

View File

@ -25,6 +25,7 @@
#include "iam_logger.h"
#include "user_auth_service.h"
#include "user_auth_common_defines.h"
#include "user_auth_event_listener_stub.h"
#undef private
@ -117,6 +118,24 @@ public:
}
};
class DummyAuthEventListener : public AuthEventListenerInterface {
public:
~DummyAuthEventListener() override = default;
sptr<IRemoteObject> AsObject() override
{
sptr<IRemoteObject> tmp(nullptr);
return tmp;
}
void OnNotifyAuthSuccessEvent(int32_t userId, AuthType authType, int32_t callerType,
std::string &callerName) override
{
IAM_LOGI("notify: userId: %{public}d, authType: %{public}d, callerName: %{public}s,"
"callerType: %{public}d", userId, static_cast<int32_t>(authType), callerName.c_str(), callerType);
}
};
UserAuthService g_userAuthService(SUBSYS_USERIAM_SYS_ABILITY_USERAUTH, true);
void FuzzGetEnrolledState(Parcel &parcel)
@ -284,6 +303,26 @@ void FuzzRegisterWidgetCallback(Parcel &parcel)
IAM_LOGI("end");
}
void FuzzRegistUserAuthSuccessEventListener(Parcel &parcel)
{
IAM_LOGI("begin");
std::vector<int32_t> authType;
std::vector<AuthType> authTypeList;
parcel.ReadInt32Vector(&authType);
for (const auto &iter : authType) {
authTypeList.push_back(static_cast<AuthType>(iter));
}
sptr<AuthEventListenerInterface> callback(nullptr);
if (parcel.ReadBool()) {
callback = sptr<AuthEventListenerInterface>(new (std::nothrow) DummyAuthEventListener());
}
g_userAuthService.RegistUserAuthSuccessEventListener(authTypeList, callback);
g_userAuthService.UnRegistUserAuthSuccessEventListener(callback);
IAM_LOGI("end");
}
using FuzzFunc = decltype(FuzzGetAvailableStatus);
FuzzFunc *g_fuzzFuncs[] = {
FuzzGetEnrolledState,
@ -298,6 +337,7 @@ FuzzFunc *g_fuzzFuncs[] = {
FuzzAuthWidget,
FuzzNotice,
FuzzRegisterWidgetCallback,
FuzzRegistUserAuthSuccessEventListener,
};
void UserAuthFuzzTest(const uint8_t *data, size_t size)

View File

@ -19,6 +19,7 @@
#include <gtest/gtest.h>
#include "mock_ipc_client_utils.h"
#include "mock_remote_object.h"
#include "mock_user_auth_callback_service.h"
#include "mock_user_auth_service.h"
#include "mock_user_auth_client_callback.h"
#include "mock_iuser_auth_widget_callback.h"

View File

@ -19,6 +19,7 @@
#include <gmock/gmock.h>
#include "user_auth_callback_stub.h"
#include "user_auth_event_listener_stub.h"
namespace OHOS {
namespace UserIam {
@ -38,6 +39,12 @@ class MockSetExecutorPropertyCallbackService final : public SetExecutorPropertyC
public:
MOCK_METHOD1(OnSetExecutorPropertyResult, void(int32_t result));
};
class MockAuthEventListenerService final : public AuthEventListenerStub {
public:
MOCK_METHOD4(OnNotifyAuthSuccessEvent, void(int32_t userId, AuthType authtype, int32_t callerType,
std::string &callerName));
};
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -683,6 +683,97 @@ void UserAuthClientTest::CallRemoteObject(const std::shared_ptr<MockUserAuthServ
return OHOS::NO_ERROR;
});
}
HWTEST_F(UserAuthClientTest, UserAuthClientRegistUserAuthSuccessEventListener001, TestSize.Level0)
{
std::vector<AuthType> authTypeList;
authTypeList.push_back(AuthType::PIN);
authTypeList.push_back(AuthType::FACE);
authTypeList.push_back(AuthType::FINGERPRINT);
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListenerService();
EXPECT_NE(testCallback, nullptr);
auto service = Common::MakeShared<MockUserAuthService>();
EXPECT_NE(service, nullptr);
EXPECT_CALL(*service, RegistUserAuthSuccessEventListener(_, _)).Times(1);
ON_CALL(*service, RegistUserAuthSuccessEventListener)
.WillByDefault(
[](const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) {
return SUCCESS;
}
);
sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
sptr<IRemoteObject::DeathRecipient> dr(nullptr);
CallRemoteObject(service, obj, dr);
int32_t ret = UserAuthClientImpl::Instance().RegistUserAuthSuccessEventListener(authTypeList, testCallback);
EXPECT_EQ(ret, SUCCESS);
dr->OnRemoteDied(obj);
IpcClientUtils::ResetObj();
}
HWTEST_F(UserAuthClientTest, UserAuthClientRegistUserAuthSuccessEventListener002, TestSize.Level0)
{
std::vector<AuthType> authTypeList;
authTypeList.push_back(AuthType::PIN);
authTypeList.push_back(AuthType::FACE);
authTypeList.push_back(AuthType::FINGERPRINT);
int32_t ret = UserAuthClientImpl::Instance().RegistUserAuthSuccessEventListener(authTypeList, nullptr);
EXPECT_EQ(ret, GENERAL_ERROR);
}
HWTEST_F(UserAuthClientTest, UserAuthClientRegistUserAuthSuccessEventListener003, TestSize.Level0)
{
std::vector<AuthType> authTypeList;
authTypeList.push_back(AuthType::PIN);
authTypeList.push_back(AuthType::FACE);
authTypeList.push_back(AuthType::FINGERPRINT);
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListenerService();
EXPECT_NE(testCallback, nullptr);
int32_t ret = UserAuthClientImpl::Instance().RegistUserAuthSuccessEventListener(authTypeList, testCallback);
EXPECT_EQ(ret, GENERAL_ERROR);
}
HWTEST_F(UserAuthClientTest, UserAuthClientUnRegistUserAuthSuccessEventListener001, TestSize.Level0)
{
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListenerService();
EXPECT_NE(testCallback, nullptr);
auto service = Common::MakeShared<MockUserAuthService>();
EXPECT_NE(service, nullptr);
EXPECT_CALL(*service, UnRegistUserAuthSuccessEventListener(_)).Times(1);
ON_CALL(*service, UnRegistUserAuthSuccessEventListener)
.WillByDefault(
[](const sptr<AuthEventListenerInterface> &callback) {
return SUCCESS;
}
);
sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
sptr<IRemoteObject::DeathRecipient> dr(nullptr);
CallRemoteObject(service, obj, dr);
int32_t ret = UserAuthClientImpl::Instance().UnRegistUserAuthSuccessEventListener(testCallback);
EXPECT_EQ(ret, SUCCESS);
dr->OnRemoteDied(obj);
IpcClientUtils::ResetObj();
}
HWTEST_F(UserAuthClientTest, UserAuthClientUnRegistUserAuthSuccessEventListener002, TestSize.Level0)
{
int32_t ret = UserAuthClientImpl::Instance().UnRegistUserAuthSuccessEventListener(nullptr);
EXPECT_EQ(ret, GENERAL_ERROR);
}
HWTEST_F(UserAuthClientTest, UserAuthClientUnRegistUserAuthSuccessEventListener003, TestSize.Level0)
{
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListenerService();
EXPECT_NE(testCallback, nullptr);
int32_t ret = UserAuthClientImpl::Instance().UnRegistUserAuthSuccessEventListener(testCallback);
EXPECT_EQ(ret, GENERAL_ERROR);
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -20,6 +20,7 @@
#include "mock_remote_object.h"
#include "mock_user_auth_service.h"
#include "mock_user_auth_client_callback.h"
#include "mock_user_auth_callback_service.h"
#include "mock_iuser_auth_widget_callback.h"
#include "user_auth_callback_service.h"
#include "widget_callback_service.h"
@ -429,6 +430,58 @@ HWTEST_F(UserAuthProxyTest, UserAuthProxyRegisterWidgetCallback002, TestSize.Lev
sptr<WidgetCallbackInterface> callback(nullptr);
EXPECT_EQ(proxy->RegisterWidgetCallback(testVersion, callback), GENERAL_ERROR);
}
HWTEST_F(UserAuthProxyTest, UserAuthProxyRegistUserAuthSuccessEventListener001, TestSize.Level0)
{
sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
EXPECT_NE(obj, nullptr);
auto proxy = Common::MakeShared<UserAuthProxy>(obj);
EXPECT_NE(proxy, nullptr);
sptr<AuthEventListenerInterface> testCallback = new (std::nothrow) MockAuthEventListenerService();
auto service = Common::MakeShared<MockUserAuthService>();
EXPECT_NE(service, nullptr);
EXPECT_CALL(*service, RegistUserAuthSuccessEventListener(_, _))
.Times(Exactly(1))
.WillOnce([](const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) {
return SUCCESS;
});
EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
ON_CALL(*obj, SendRequest)
.WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
service->OnRemoteRequest(code, data, reply, option);
return SUCCESS;
});
std::vector<AuthType> authTypeList;
authTypeList.push_back(AuthType::PIN);
authTypeList.push_back(AuthType::FACE);
authTypeList.push_back(AuthType::FINGERPRINT);
proxy->RegistUserAuthSuccessEventListener(authTypeList, testCallback);
}
HWTEST_F(UserAuthProxyTest, UserAuthProxyUnRegistUserAuthSuccessEventListener001, TestSize.Level0)
{
sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
EXPECT_NE(obj, nullptr);
auto proxy = Common::MakeShared<UserAuthProxy>(obj);
EXPECT_NE(proxy, nullptr);
sptr<AuthEventListenerInterface> testCallback = new (std::nothrow) MockAuthEventListenerService();
auto service = Common::MakeShared<MockUserAuthService>();
EXPECT_NE(service, nullptr);
EXPECT_CALL(*service, UnRegistUserAuthSuccessEventListener(_))
.Times(Exactly(1))
.WillOnce([](const sptr<AuthEventListenerInterface> &callback) {
return SUCCESS;
});
EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
ON_CALL(*obj, SendRequest)
.WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
service->OnRemoteRequest(code, data, reply, option);
return SUCCESS;
});
proxy->UnRegistUserAuthSuccessEventListener(testCallback);
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -60,6 +60,7 @@ ohos_unittest("iam_services_test") {
"../../../services/context/src/widget_context.cpp",
"../../../services/context/src/widget_context_callback_impl.cpp",
"../../../services/context/src/widget_json.cpp",
"../../../services/core/src/auth_event_listener_manager.cpp",
"../../../services/core/src/auth_widget_helper.cpp",
"../../../services/core/src/authentication_impl.cpp",
"../../../services/core/src/credential_info_impl.cpp",

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2023 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 IAM_MOCK_AUTH_EVENT_LISTENER_INTERFACE_H
#define IAM_MOCK_AUTH_EVENT_LISTENER_INTERFACE_H
#include <gmock/gmock.h>
#include "user_auth_client_callback.h"
#include "user_auth_event_listener_stub.h"
namespace OHOS {
namespace UserIam {
namespace UserAuth {
class MockAuthEventListener final : public AuthEventListenerInterface {
public:
MOCK_METHOD0(AsObject, sptr<IRemoteObject>());
MOCK_METHOD4(OnNotifyAuthSuccessEvent, void(int32_t userId, AuthType authtype, int32_t callerType,
std::string &callerName));
};
class MockAuthEventListenerService final : public IRemoteStub<AuthEventListenerInterface> {
public:
MOCK_METHOD4(OnRemoteRequest,
int32_t(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option));
MOCK_METHOD4(OnNotifyAuthSuccessEvent, void(int32_t userId, AuthType authtype, int32_t callerType,
std::string &callerName));
};
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS
#endif // IAM_MOCK_AUTH_EVENT_LISTENER_INTERFACE_H

View File

@ -51,6 +51,7 @@ public:
MOCK_METHOD2(ProcessAuthResult, void(int32_t tip, const std::vector<uint8_t> &extraInfo));
MOCK_METHOD0(GetIamCallback, sptr<IamCallbackInterface>());
MOCK_METHOD0(GetCallerName, std::string());
MOCK_METHOD1(SetTraceCallerType, void(int32_t callerType));
};
class MockContext final : public Context {

View File

@ -15,7 +15,6 @@
#include "mock_ipc_common.h"
#include "iam_common_defines.h"
#include "iam_logger.h"
#ifdef HAS_OS_ACCOUNT_PART
#include "os_account_manager.h"
@ -135,10 +134,12 @@ void IpcCommon::SetSkipUserFlag(bool isSkip)
skipFlag_ = isSkip;
}
bool IpcCommon::GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::string &callerName)
bool IpcCommon::GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::string &callerName,
int32_t &callerType)
{
callerName = "";
isBundleName = true;
callerType = 0;
return true;
}

View File

@ -22,6 +22,7 @@
#include <string>
#include <set>
#include "iam_common_defines.h"
#include "nocopyable.h"
namespace OHOS {
@ -45,7 +46,8 @@ public:
static int32_t GetActiveUserId(std::optional<int32_t> &userId);
static int32_t GetAllUserId(std::vector<int32_t> &userIds);
static bool CheckPermission(IPCObjectStub &stub, Permission permission);
static bool GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::string &callerName);
static bool GetCallerName(IPCObjectStub &stub, bool &isBundleName, std::string &callerName,
int32_t &callerType);
static uint32_t GetAccessTokenId(IPCObjectStub &stub);
static void SetAccessTokenId(uint32_t tokenId, bool isSetTokenId);
static void AddPermission(Permission perm);

View File

@ -53,6 +53,9 @@ public:
MOCK_METHOD1(GetVersion, int32_t(int32_t &version));
MOCK_METHOD2(Notice, int32_t(NoticeType noticeType, const std::string &eventData));
MOCK_METHOD2(RegisterWidgetCallback, int32_t(int32_t version, sptr<WidgetCallbackInterface> &callback));
MOCK_METHOD2(RegistUserAuthSuccessEventListener, int32_t(const std::vector<AuthType> &authType,
const sptr<AuthEventListenerInterface> &callback));
MOCK_METHOD1(UnRegistUserAuthSuccessEventListener, int32_t(const sptr<AuthEventListenerInterface> &callback));
};
} // namespace UserAuth
} // namespace UserIam

View File

@ -21,6 +21,7 @@
#include "iam_ptr.h"
#include "executor_messenger_service.h"
#include "mock_auth_event_listener.h"
#include "mock_context.h"
#include "mock_iuser_auth_interface.h"
#include "mock_ipc_common.h"
@ -1603,6 +1604,95 @@ HWTEST_F(UserAuthServiceTest, UserAuthServiceRegisterWidgetCallback_006, TestSiz
EXPECT_EQ(service.RegisterWidgetCallback(1, testCallback), ResultCode::CHECK_PERMISSION_FAILED);
IpcCommon::DeleteAllPermission();
}
HWTEST_F(UserAuthServiceTest, UserAuthServiceRegistEventListerner_001, TestSize.Level0)
{
UserAuthService service(200, true);
sptr<AuthEventListenerInterface> testCallback = nullptr;
std::vector<AuthType> authTypeList;
IpcCommon::AddPermission(IS_SYSTEM_APP);
EXPECT_EQ(service.RegistUserAuthSuccessEventListener(authTypeList, testCallback), ResultCode::INVALID_PARAMETERS);
IpcCommon::DeleteAllPermission();
}
HWTEST_F(UserAuthServiceTest, UserAuthServiceRegistEventListerner_002, TestSize.Level0)
{
UserAuthService service(200, true);
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListener();
std::vector<AuthType> authTypeList;
IpcCommon::AddPermission(IS_SYSTEM_APP);
EXPECT_EQ(service.RegistUserAuthSuccessEventListener(authTypeList, testCallback),
ResultCode::CHECK_PERMISSION_FAILED);
IpcCommon::DeleteAllPermission();
}
HWTEST_F(UserAuthServiceTest, UserAuthServiceRegistEventListerner_003, TestSize.Level0)
{
UserAuthService service(200, true);
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListener();
std::vector<AuthType> authTypeList;
authTypeList.push_back(AuthType::PIN);
authTypeList.push_back(AuthType::FACE);
authTypeList.push_back(AuthType::FINGERPRINT);
IpcCommon::AddPermission(IS_SYSTEM_APP);
EXPECT_EQ(service.RegistUserAuthSuccessEventListener(authTypeList, testCallback),
ResultCode::CHECK_PERMISSION_FAILED);
IpcCommon::DeleteAllPermission();
}
HWTEST_F(UserAuthServiceTest, UserAuthServiceRegistEventListerner_004, TestSize.Level0)
{
UserAuthService service(200, true);
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListener();
std::vector<AuthType> authTypeList;
authTypeList.push_back(AuthType::PIN);
authTypeList.push_back(AuthType::FACE);
authTypeList.push_back(AuthType::FINGERPRINT);
IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
EXPECT_EQ(service.RegistUserAuthSuccessEventListener(authTypeList, testCallback), ResultCode::GENERAL_ERROR);
IpcCommon::DeleteAllPermission();
}
HWTEST_F(UserAuthServiceTest, UserAuthServiceUnRegistEventListerner_001, TestSize.Level0)
{
UserAuthService service(200, true);
sptr<AuthEventListenerInterface> testCallback = nullptr;
IpcCommon::AddPermission(IS_SYSTEM_APP);
EXPECT_EQ(service.UnRegistUserAuthSuccessEventListener(testCallback), ResultCode::INVALID_PARAMETERS);
IpcCommon::DeleteAllPermission();
}
HWTEST_F(UserAuthServiceTest, UserAuthServiceUnRegistEventListerner_002, TestSize.Level0)
{
UserAuthService service(200, true);
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListener();
IpcCommon::AddPermission(IS_SYSTEM_APP);
EXPECT_EQ(service.UnRegistUserAuthSuccessEventListener(testCallback), ResultCode::CHECK_PERMISSION_FAILED);
IpcCommon::DeleteAllPermission();
}
HWTEST_F(UserAuthServiceTest, UserAuthServiceUnRegistEventListerner_003, TestSize.Level0)
{
UserAuthService service(200, true);
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListener();
IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
EXPECT_EQ(service.UnRegistUserAuthSuccessEventListener(testCallback), ResultCode::GENERAL_ERROR);
IpcCommon::DeleteAllPermission();
}
HWTEST_F(UserAuthServiceTest, UserAuthServiceUnRegistEventListerner_004, TestSize.Level0)
{
UserAuthService service(200, true);
sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListener();
std::vector<AuthType> authTypeList;
authTypeList.push_back(AuthType::PIN);
authTypeList.push_back(AuthType::FACE);
authTypeList.push_back(AuthType::FINGERPRINT);
IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
EXPECT_EQ(service.RegistUserAuthSuccessEventListener(authTypeList, testCallback), ResultCode::GENERAL_ERROR);
EXPECT_EQ(service.UnRegistUserAuthSuccessEventListener(testCallback), ResultCode::GENERAL_ERROR);
IpcCommon::DeleteAllPermission();
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -16,6 +16,7 @@
#include "user_auth_stub_test.h"
#include "iam_common_defines.h"
#include "mock_auth_event_listener.h"
#include "mock_user_auth_callback.h"
#include "mock_user_auth_service.h"
@ -506,6 +507,64 @@ HWTEST_F(UserAuthStubTest, UserAuthStubGetVersionStub, TestSize.Level0)
EXPECT_TRUE(reply.ReadInt32(result));
EXPECT_EQ(result, SUCCESS);
}
HWTEST_F(UserAuthStubTest, UserAuthStubRegistUserAuthSuccessEventListenerStub, TestSize.Level0)
{
MockUserAuthService service;
sptr<MockAuthEventListenerService> callback(new (std::nothrow) MockAuthEventListenerService());
EXPECT_CALL(service, RegistUserAuthSuccessEventListener(_, _)).Times(1);
ON_CALL(service, RegistUserAuthSuccessEventListener)
.WillByDefault(
[](const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) {
return SUCCESS;
}
);
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_SYNC);
uint32_t code = UserAuthInterfaceCode::USER_AUTH_REG_EVENT_LISTENER;
EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
std::vector<int32_t> authType;
authType.push_back(static_cast<int32_t>(PIN));
authType.push_back(static_cast<int32_t>(FACE));
authType.push_back(static_cast<int32_t>(FINGERPRINT));
EXPECT_TRUE(data.WriteInt32Vector(authType));
EXPECT_NE(callback->AsObject(), nullptr);
EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
int32_t result;
EXPECT_TRUE(reply.ReadInt32(result));
EXPECT_EQ(result, SUCCESS);
}
HWTEST_F(UserAuthStubTest, UserAuthStubUnRegistUserAuthSuccessEventListenerStub, TestSize.Level0)
{
MockUserAuthService service;
sptr<MockAuthEventListenerService> callback(new (std::nothrow) MockAuthEventListenerService());
EXPECT_CALL(service, UnRegistUserAuthSuccessEventListener(_)).Times(1);
ON_CALL(service, UnRegistUserAuthSuccessEventListener)
.WillByDefault(
[](const sptr<AuthEventListenerInterface> &callback) {
return SUCCESS;
}
);
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_SYNC);
uint32_t code = UserAuthInterfaceCode::USER_AUTH_UNREG_EVENT_LISTENER;
EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
EXPECT_NE(callback->AsObject(), nullptr);
EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
int32_t result;
EXPECT_TRUE(reply.ReadInt32(result));
EXPECT_EQ(result, SUCCESS);
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS