mirror of
https://github.com/openharmony/useriam_user_auth.git
synced 2026-07-19 14:23:34 -04:00
+19
-2
@@ -41,7 +41,7 @@
|
||||
"//base/user_iam/user_auth/interfaces/kits/napi:userauthnorth"
|
||||
],
|
||||
"inner_kits": [
|
||||
{
|
||||
{
|
||||
"type": "so",
|
||||
"name": "//base/user_iam/user_auth/interfaces/innerkits/native:userauth_framework",
|
||||
"header": {
|
||||
@@ -57,7 +57,7 @@
|
||||
"header_base": "//base/user_iam/user_auth/interfaces/innerkits/native/include"
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"type": "so",
|
||||
"name": "//base/user_iam/user_auth/interfaces/innerkits/napi:userauth",
|
||||
"header": {
|
||||
@@ -73,6 +73,23 @@
|
||||
],
|
||||
"header_base": "//base/user_iam/user_auth/interfaces/innerkits/napi/include"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "so",
|
||||
"name": "//base/user_iam/user_auth/interfaces/innerkits/napi:userauth_napi",
|
||||
"header": {
|
||||
"header_files": [
|
||||
"auth_build.h",
|
||||
"auth_common.h",
|
||||
"auth_hilog_wrapper.h",
|
||||
"auth_object.h",
|
||||
"authapi_callback.h",
|
||||
"result_convert.h",
|
||||
"user_auth_helper.h",
|
||||
"user_auth_impl.h"
|
||||
],
|
||||
"header_base": "//base/user_iam/user_auth/interfaces/innerkits/napi/include"
|
||||
}
|
||||
}
|
||||
],
|
||||
"test": [
|
||||
|
||||
@@ -58,3 +58,43 @@ ohos_shared_library("userauth") {
|
||||
part_name = "user_auth"
|
||||
subsystem_name = "user_iam"
|
||||
}
|
||||
|
||||
ohos_shared_library("userauth_napi") {
|
||||
cflags = [
|
||||
"-fPIC",
|
||||
"-g3",
|
||||
]
|
||||
|
||||
defines = [ "USER_AUTH_FOR_API6" ]
|
||||
|
||||
sources = [
|
||||
"src/auth_build.cpp",
|
||||
"src/auth_native_module.cpp",
|
||||
"src/authapi_callback.cpp",
|
||||
"src/result_convert.cpp",
|
||||
"src/user_auth_helper.cpp",
|
||||
"src/user_auth_impl.cpp",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"${userauth_innerkits_path}/native:userauth_framework",
|
||||
"//foundation/ace/napi:ace_napi",
|
||||
"//utils/native/base:utils",
|
||||
]
|
||||
|
||||
configs = [ "${userauth_utils_path}:utils_config" ]
|
||||
|
||||
public_configs = [ ":userauth_public_config" ]
|
||||
|
||||
external_deps = [
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
"ipc:ipc_core",
|
||||
"safwk:system_ability_fwk",
|
||||
"samgr_standard:samgr_proxy",
|
||||
]
|
||||
|
||||
relative_install_dir = "module/useriam"
|
||||
|
||||
part_name = "user_auth"
|
||||
subsystem_name = "user_iam"
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#ifndef OHOS_USERAUTH_COMMON_H
|
||||
#define OHOS_USERAUTH_COMMON_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
@@ -80,6 +81,17 @@ struct SetPropertyInfo {
|
||||
int32_t setResult;
|
||||
};
|
||||
|
||||
struct ExecuteInfo {
|
||||
bool isPromise;
|
||||
napi_env env;
|
||||
std::string type;
|
||||
std::string level;
|
||||
napi_ref callbackRef;
|
||||
napi_deferred deferred;
|
||||
napi_value promise;
|
||||
int32_t result;
|
||||
};
|
||||
|
||||
struct AuthInfo {
|
||||
AuthInfo() : asyncWork(nullptr) {};
|
||||
CallBackInfo callBackInfo;
|
||||
|
||||
@@ -39,6 +39,7 @@ class AuthApiCallback : public UserAuthCallback {
|
||||
public:
|
||||
AuthApiCallback(AuthInfo *authInfo);
|
||||
AuthApiCallback(AuthUserInfo *userInfo);
|
||||
AuthApiCallback(ExecuteInfo *executeInfo);
|
||||
virtual ~AuthApiCallback();
|
||||
void onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) override;
|
||||
void onResult(const int32_t result, const AuthResult extraInfo) override;
|
||||
@@ -51,9 +52,11 @@ private:
|
||||
void OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner);
|
||||
void OnUserAuthResult(const int32_t result, const AuthResult extraInfo);
|
||||
void OnAuthResult(const int32_t result, const AuthResult extraInfo);
|
||||
void OnExecuteResult(const int32_t result);
|
||||
|
||||
AuthInfo *authInfo_;
|
||||
AuthUserInfo *userInfo_;
|
||||
ExecuteInfo *executeInfo_;
|
||||
};
|
||||
|
||||
class GetPropApiCallback : public GetPropCallback {
|
||||
|
||||
@@ -100,6 +100,8 @@ napi_value SetProperty(napi_env env, napi_callback_info info);
|
||||
*/
|
||||
napi_value Auth(napi_env env, napi_callback_info info);
|
||||
|
||||
napi_value Execute(napi_env env, napi_callback_info info);
|
||||
|
||||
/**
|
||||
* @brief user authentication
|
||||
*
|
||||
|
||||
@@ -39,6 +39,7 @@ public:
|
||||
napi_value GetProperty(napi_env env, napi_callback_info info);
|
||||
napi_value SetProperty(napi_env env, napi_callback_info info);
|
||||
napi_value Auth(napi_env env, napi_callback_info info);
|
||||
napi_value Execute(napi_env env, napi_callback_info info);
|
||||
napi_value AuthUser(napi_env env, napi_callback_info info);
|
||||
napi_value CancelAuth(napi_env env, napi_callback_info info);
|
||||
|
||||
@@ -56,6 +57,9 @@ private:
|
||||
napi_value AuthUserWrap(napi_env env, AuthUserInfo *userInfo);
|
||||
napi_value BuildAuthUserInfo(napi_env env, AuthUserInfo *userInfo);
|
||||
|
||||
bool GetExecuteInfo(napi_env env, napi_value* argv, ExecuteInfo* executeInfo);
|
||||
napi_value DoExecute(ExecuteInfo* executeInfo);
|
||||
|
||||
static void SetPropertyExecute(napi_env env, void *data);
|
||||
static void SetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data);
|
||||
static void SetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data);
|
||||
|
||||
@@ -31,7 +31,11 @@ extern "C" __attribute__((constructor)) void RegisterModule(void)
|
||||
.nm_flags = 0, // normal
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = ModuleInit,
|
||||
#ifdef USER_AUTH_FOR_API6
|
||||
.nm_modname = "userIAM.userAuth",
|
||||
#else
|
||||
.nm_modname = "UserAuth",
|
||||
#endif
|
||||
.nm_priv = nullptr,
|
||||
.reserved = {}};
|
||||
napi_module_register(&module);
|
||||
|
||||
@@ -27,12 +27,21 @@ AuthApiCallback::AuthApiCallback(AuthInfo *authInfo)
|
||||
{
|
||||
authInfo_ = authInfo;
|
||||
userInfo_ = nullptr;
|
||||
executeInfo_ = nullptr;
|
||||
}
|
||||
|
||||
AuthApiCallback::AuthApiCallback(AuthUserInfo *userInfo)
|
||||
{
|
||||
authInfo_ = nullptr;
|
||||
userInfo_ = userInfo;
|
||||
executeInfo_ = nullptr;
|
||||
}
|
||||
|
||||
AuthApiCallback::AuthApiCallback(ExecuteInfo *executeInfo)
|
||||
{
|
||||
authInfo_ = nullptr;
|
||||
userInfo_ = nullptr;
|
||||
executeInfo_ = executeInfo;
|
||||
}
|
||||
|
||||
AuthApiCallback::~AuthApiCallback()
|
||||
@@ -269,6 +278,61 @@ void AuthApiCallback::OnAuthResult(const int32_t result, const AuthResult extraI
|
||||
uv_queue_work(loop, work, [] (uv_work_t *work) {}, OnAuthResultWork);
|
||||
}
|
||||
|
||||
static void OnExecuteResultWork(uv_work_t *work, int status)
|
||||
{
|
||||
HILOG_INFO("Do OnExecuteResultWork");
|
||||
ExecuteInfo *executeInfo = reinterpret_cast<ExecuteInfo *>(work->data);
|
||||
if (executeInfo == nullptr) {
|
||||
HILOG_ERROR("executeInfo is null");
|
||||
delete work;
|
||||
return;
|
||||
}
|
||||
napi_env env = executeInfo->env;
|
||||
napi_value result;
|
||||
if (napi_create_int32(env, executeInfo->result, &result) != napi_ok) {
|
||||
HILOG_ERROR("napi_create_int32 faild");
|
||||
delete work;
|
||||
delete executeInfo;
|
||||
return;
|
||||
}
|
||||
napi_value undefined;
|
||||
napi_get_undefined(env, &undefined);
|
||||
if (executeInfo->isPromise) {
|
||||
HILOG_INFO("do promise %{public}d", napi_resolve_deferred(env, executeInfo->deferred, result));
|
||||
} else {
|
||||
napi_value callback;
|
||||
napi_get_reference_value(env, executeInfo->callbackRef, &callback);
|
||||
napi_value callResult = nullptr;
|
||||
HILOG_INFO("do callback %{public}d", napi_call_function(env, undefined, callback, 1, &result, &callResult));
|
||||
}
|
||||
delete executeInfo;
|
||||
delete work;
|
||||
}
|
||||
|
||||
void AuthApiCallback::OnExecuteResult(const int32_t result)
|
||||
{
|
||||
HILOG_INFO("AuthApiCallback OnExecuteResult start");
|
||||
uv_loop_s *loop(nullptr);
|
||||
napi_get_uv_event_loop(executeInfo_->env, &loop);
|
||||
if (loop == nullptr) {
|
||||
HILOG_ERROR("loop is null");
|
||||
delete executeInfo_;
|
||||
executeInfo_ = nullptr;
|
||||
return;
|
||||
}
|
||||
uv_work_t *work = new (std::nothrow) uv_work_t;
|
||||
if (work == nullptr) {
|
||||
HILOG_ERROR("work is null");
|
||||
delete executeInfo_;
|
||||
executeInfo_ = nullptr;
|
||||
return;
|
||||
}
|
||||
executeInfo_->result = result;
|
||||
work->data = reinterpret_cast<void *>(executeInfo_);
|
||||
executeInfo_ = nullptr;
|
||||
uv_queue_work(loop, work, [] (uv_work_t *work) {}, OnExecuteResultWork);
|
||||
}
|
||||
|
||||
void AuthApiCallback::onResult(const int32_t result, const AuthResult extraInfo)
|
||||
{
|
||||
HILOG_INFO("AuthApiCallback onResult start result = %{public}d", result);
|
||||
@@ -285,6 +349,11 @@ void AuthApiCallback::onResult(const int32_t result, const AuthResult extraInfo)
|
||||
} else {
|
||||
HILOG_ERROR("AuthApiCallback onResult authInfo_ is nullptr ");
|
||||
}
|
||||
if (executeInfo_ != nullptr) {
|
||||
OnExecuteResult(result);
|
||||
} else {
|
||||
HILOG_ERROR("AuthApiCallback onResult executeInfo_ is nullptr ");
|
||||
}
|
||||
HILOG_INFO("AuthApiCallback onResult end");
|
||||
}
|
||||
|
||||
|
||||
@@ -144,6 +144,18 @@ napi_value Auth(napi_env env, napi_callback_info info)
|
||||
return userAuthImpl->Auth(env, info);
|
||||
}
|
||||
|
||||
napi_value Execute(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t argcAsync = 0;
|
||||
napi_value args[ARGS_MAX_COUNT] = {nullptr};
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr));
|
||||
UserAuthImpl *userAuthImpl = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl));
|
||||
HILOG_INFO("UserAuthHelper, Execute");
|
||||
return userAuthImpl->Execute(env, info);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief user authentication
|
||||
*
|
||||
@@ -194,6 +206,7 @@ void Init(napi_env env, napi_value exports)
|
||||
napi_status status;
|
||||
napi_property_descriptor exportFuncs[] = {
|
||||
DECLARE_NAPI_FUNCTION("constructor", UserAuth::Constructor),
|
||||
DECLARE_NAPI_FUNCTION("getAuthenticator", UserAuth::Constructor),
|
||||
};
|
||||
status = napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
|
||||
if (status != napi_ok) {
|
||||
@@ -221,6 +234,7 @@ napi_value GetCtor(napi_env env)
|
||||
DECLARE_NAPI_FUNCTION("auth", UserAuth::Auth),
|
||||
DECLARE_NAPI_FUNCTION("authUser", UserAuth::AuthUser),
|
||||
DECLARE_NAPI_FUNCTION("cancelAuth", UserAuth::CancelAuth),
|
||||
DECLARE_NAPI_FUNCTION("execute", UserAuth::Execute),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_class(env, "UserAuth", NAPI_AUTO_LENGTH, UserAuthServiceConstructor, nullptr,
|
||||
sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
|
||||
#include "user_auth_impl.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "user_auth.h"
|
||||
#include "userauth_callback.h"
|
||||
#include "userauth_info.h"
|
||||
@@ -384,6 +386,107 @@ napi_value UserAuthImpl::BuildAuthInfo(napi_env env, AuthInfo *authInfo)
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value UserAuthImpl::DoExecute(ExecuteInfo* executeInfo)
|
||||
{
|
||||
AuthApiCallback *object = new (std::nothrow) AuthApiCallback(executeInfo);
|
||||
if (object == nullptr) {
|
||||
delete executeInfo;
|
||||
return nullptr;
|
||||
}
|
||||
std::shared_ptr<AuthApiCallback> callback;
|
||||
callback.reset(object);
|
||||
std::map<std::string, AuthTurstLevel> convertAuthTurstLevel = {
|
||||
{ "S1", ATL1 },
|
||||
{ "S2", ATL2 },
|
||||
{ "S3", ATL3 },
|
||||
{ "S4", ATL4 },
|
||||
};
|
||||
if (convertAuthTurstLevel.count(executeInfo->level) == 0) {
|
||||
HILOG_ERROR("execute convertAuthTurstLevel is 0");
|
||||
delete executeInfo;
|
||||
return nullptr;
|
||||
}
|
||||
UserAuth::GetInstance().Auth(0, FACE, convertAuthTurstLevel[executeInfo->level], callback);
|
||||
if (executeInfo->isPromise) {
|
||||
return executeInfo->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
NAPI_CALL(executeInfo->env, napi_get_null(executeInfo->env, &result));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value UserAuthImpl::Execute(napi_env env, napi_callback_info info)
|
||||
{
|
||||
HILOG_INFO("%{public}s, start", __func__);
|
||||
ExecuteInfo *executeInfo = new (std::nothrow) ExecuteInfo();
|
||||
if (executeInfo == nullptr) {
|
||||
HILOG_ERROR("%{public}s executeInfo nullptr", __func__);
|
||||
return nullptr;
|
||||
}
|
||||
executeInfo->env = env;
|
||||
size_t argc = ARGS_MAX_COUNT;
|
||||
size_t callbackIndex = 0;
|
||||
napi_value argv[ARGS_MAX_COUNT] = {nullptr};
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
|
||||
if (argc < PARAM2) {
|
||||
HILOG_ERROR("%{public}s argc check fail", __func__);
|
||||
delete executeInfo;
|
||||
return nullptr;
|
||||
}
|
||||
if (!GetExecuteInfo(env, argv, executeInfo)) {
|
||||
HILOG_ERROR("%{public}s GetExecuteInfo fail", __func__);
|
||||
delete executeInfo;
|
||||
return nullptr;
|
||||
}
|
||||
callbackIndex = argc - 1;
|
||||
napi_valuetype valuetype;
|
||||
NAPI_CALL(env, napi_typeof(env, argv[callbackIndex], &valuetype));
|
||||
if (valuetype == napi_function) {
|
||||
executeInfo->isPromise = false;
|
||||
NAPI_CALL(env, napi_create_reference(env, argv[callbackIndex], 1, &executeInfo->callbackRef));
|
||||
} else {
|
||||
executeInfo->isPromise = true;
|
||||
NAPI_CALL(env, napi_create_promise(env, &executeInfo->deferred, &executeInfo->promise));
|
||||
}
|
||||
return DoExecute(executeInfo);
|
||||
}
|
||||
|
||||
bool UserAuthImpl::GetExecuteInfo(napi_env env, napi_value* argv, ExecuteInfo* executeInfo)
|
||||
{
|
||||
HILOG_INFO("%{public}s, start.", __func__);
|
||||
napi_valuetype valuetype;
|
||||
napi_typeof(env, argv[PARAM0], &valuetype);
|
||||
if (valuetype != napi_string) {
|
||||
return false;
|
||||
}
|
||||
char *str = nullptr;
|
||||
size_t len = 0;
|
||||
napi_get_value_string_utf8(env, argv[PARAM0], nullptr, 0, &len);
|
||||
if (len > 0) {
|
||||
str = new char[len + 1]();
|
||||
napi_get_value_string_utf8(env, argv[PARAM0], str, len + 1, &len);
|
||||
executeInfo->type = str;
|
||||
delete[] str;
|
||||
}
|
||||
napi_typeof(env, argv[PARAM1], &valuetype);
|
||||
if (valuetype != napi_string) {
|
||||
return false;
|
||||
}
|
||||
napi_get_value_string_utf8(env, argv[PARAM1], nullptr, 0, &len);
|
||||
if (len > 0) {
|
||||
str = new char[len + 1]();
|
||||
napi_get_value_string_utf8(env, argv[PARAM1], str, len + 1, &len);
|
||||
executeInfo->level = str;
|
||||
delete[] str;
|
||||
}
|
||||
if (executeInfo->type.compare("FACE_ONLY") != 0) {
|
||||
HILOG_ERROR("%{public}s check type fail.", __func__);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
napi_value UserAuthImpl::Auth(napi_env env, napi_callback_info info)
|
||||
{
|
||||
HILOG_INFO("%{public}s, start", __func__);
|
||||
|
||||
Reference in New Issue
Block a user