1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cstdint>
17 #include <vector>
18 
19 #include "securec.h"
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 
24 #include "asset_log.h"
25 #include "asset_mem.h"
26 #include "asset_system_api.h"
27 #include "asset_system_type.h"
28 
29 #include "asset_napi_check.h"
30 #include "asset_napi_common.h"
31 #include "asset_napi_post_query.h"
32 
33 namespace OHOS {
34 namespace Security {
35 namespace Asset {
36 namespace {
37 
38 const std::vector<uint32_t> REQUIRED_TAGS = {
39     SEC_ASSET_TAG_AUTH_CHALLENGE
40 };
41 
42 const std::vector<uint32_t> OPTIONAL_TAGS = {
43     SEC_ASSET_TAG_USER_ID
44 };
45 
CheckPostQueryArgs(const napi_env env,const std::vector<AssetAttr> & attrs)46 napi_status CheckPostQueryArgs(const napi_env env, const std::vector<AssetAttr> &attrs)
47 {
48     IF_FALSE_RETURN(CheckAssetRequiredTag(env, attrs, REQUIRED_TAGS), napi_invalid_arg);
49     std::vector<uint32_t> validTags;
50     validTags.insert(validTags.end(), REQUIRED_TAGS.begin(), REQUIRED_TAGS.end());
51     validTags.insert(validTags.end(), OPTIONAL_TAGS.begin(), OPTIONAL_TAGS.end());
52     IF_FALSE_RETURN(CheckAssetValueValidity(env, attrs), napi_invalid_arg);
53     return napi_ok;
54 }
55 
56 } // anonymous namespace
57 
NapiPostQuery(const napi_env env,napi_callback_info info,const NapiCallerArgs & args)58 napi_value NapiPostQuery(const napi_env env, napi_callback_info info, const NapiCallerArgs &args)
59 {
60     napi_async_execute_callback execute =
61         [](napi_env env, void *data) {
62             AsyncContext *context = static_cast<AsyncContext *>(data);
63             context->result = AssetPostQuery(&context->attrs[0], context->attrs.size());
64         };
65     return NapiAsync(env, info, execute, args, &CheckPostQueryArgs);
66 }
67 
NapiPostQuery(const napi_env env,napi_callback_info info)68 napi_value NapiPostQuery(const napi_env env, napi_callback_info info)
69 {
70     NapiCallerArgs args = { .expectArgNum = NORMAL_ARGS_NUM, .isUpdate = false, .isAsUser = false };
71     return NapiPostQuery(env, info, args);
72 }
73 
NapiPostQueryAsUser(const napi_env env,napi_callback_info info)74 napi_value NapiPostQueryAsUser(const napi_env env, napi_callback_info info)
75 {
76     NapiCallerArgs args = { .expectArgNum = AS_USER_ARGS_NUM, .isUpdate = false, .isAsUser = true };
77     return NapiPostQuery(env, info, args);
78 }
79 
NapiPostQuerySync(const napi_env env,napi_callback_info info)80 napi_value NapiPostQuerySync(const napi_env env, napi_callback_info info)
81 {
82     std::vector<AssetAttr> attrs;
83     NapiCallerArgs args = { .expectArgNum = NORMAL_ARGS_NUM, .isUpdate = false, .isAsUser = false };
84     do {
85         if (ParseParam(env, info, args, attrs) != napi_ok) {
86             break;
87         }
88 
89         if (CheckPostQueryArgs(env, attrs) != napi_ok) {
90             break;
91         }
92 
93         int32_t result = AssetPostQuery(&attrs[0], attrs.size());
94         CHECK_RESULT_BREAK(env, result);
95     } while (false);
96     FreeAssetAttrs(attrs);
97     return nullptr;
98 }
99 
100 } // Asset
101 } // Security
102 } // OHOS
103