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_GROUP_ID,
44 SEC_ASSET_TAG_USER_ID
45 };
46
CheckPostQueryArgs(const napi_env env,const std::vector<AssetAttr> & attrs)47 napi_status CheckPostQueryArgs(const napi_env env, const std::vector<AssetAttr> &attrs)
48 {
49 IF_FALSE_RETURN(CheckAssetRequiredTag(env, attrs, REQUIRED_TAGS), napi_invalid_arg);
50 std::vector<uint32_t> validTags;
51 validTags.insert(validTags.end(), REQUIRED_TAGS.begin(), REQUIRED_TAGS.end());
52 validTags.insert(validTags.end(), OPTIONAL_TAGS.begin(), OPTIONAL_TAGS.end());
53 IF_FALSE_RETURN(CheckAssetValueValidity(env, attrs), napi_invalid_arg);
54 return napi_ok;
55 }
56
57 } // anonymous namespace
58
NapiPostQuery(const napi_env env,napi_callback_info info,const NapiCallerArgs & args)59 napi_value NapiPostQuery(const napi_env env, napi_callback_info info, const NapiCallerArgs &args)
60 {
61 napi_async_execute_callback execute =
62 [](napi_env env, void *data) {
63 AsyncContext *context = static_cast<AsyncContext *>(data);
64 context->result = AssetPostQuery(&context->attrs[0], context->attrs.size());
65 };
66 return NapiAsync(env, info, execute, args, &CheckPostQueryArgs);
67 }
68
NapiPostQuery(const napi_env env,napi_callback_info info)69 napi_value NapiPostQuery(const napi_env env, napi_callback_info info)
70 {
71 NapiCallerArgs args = { .expectArgNum = NORMAL_ARGS_NUM, .isUpdate = false, .isAsUser = false };
72 return NapiPostQuery(env, info, args);
73 }
74
NapiPostQueryAsUser(const napi_env env,napi_callback_info info)75 napi_value NapiPostQueryAsUser(const napi_env env, napi_callback_info info)
76 {
77 NapiCallerArgs args = { .expectArgNum = AS_USER_ARGS_NUM, .isUpdate = false, .isAsUser = true };
78 return NapiPostQuery(env, info, args);
79 }
80
NapiPostQuerySync(const napi_env env,napi_callback_info info)81 napi_value NapiPostQuerySync(const napi_env env, napi_callback_info info)
82 {
83 std::vector<AssetAttr> attrs;
84 NapiCallerArgs args = { .expectArgNum = NORMAL_ARGS_NUM, .isUpdate = false, .isAsUser = false };
85 do {
86 if (ParseParam(env, info, args, attrs) != napi_ok) {
87 break;
88 }
89
90 if (CheckPostQueryArgs(env, attrs) != napi_ok) {
91 break;
92 }
93
94 int32_t result = AssetPostQuery(&attrs[0], attrs.size());
95 CHECK_RESULT_BREAK(env, result);
96 } while (false);
97 FreeAssetAttrs(attrs);
98 return nullptr;
99 }
100
101 } // Asset
102 } // Security
103 } // OHOS
104