• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "asset_napi_post_query.h"
17 
18 #include "asset_log.h"
19 #include "asset_system_api.h"
20 #include "asset_system_type.h"
21 
22 #include "asset_napi_check.h"
23 #include "asset_napi_common.h"
24 
25 namespace OHOS {
26 namespace Security {
27 namespace Asset {
28 namespace {
29 const uint32_t QUERY_ARG_COUNT = 1;
30 const uint32_t QUERY_ARG_COUNT_AS_USER = 2;
31 
32 const std::vector<uint32_t> REQUIRED_TAGS = {
33     SEC_ASSET_TAG_AUTH_CHALLENGE
34 };
35 
36 const std::vector<uint32_t> OPTIONAL_TAGS = {
37     SEC_ASSET_TAG_GROUP_ID,
38     SEC_ASSET_TAG_USER_ID
39 };
40 
CheckPostQueryArgs(const napi_env env,const std::vector<AssetAttr> & attrs)41 napi_status CheckPostQueryArgs(const napi_env env, const std::vector<AssetAttr> &attrs)
42 {
43     IF_ERROR_THROW_RETURN(env, CheckAssetRequiredTag(env, attrs, REQUIRED_TAGS, SEC_ASSET_INVALID_ARGUMENT));
44     std::vector<uint32_t> validTags;
45     validTags.insert(validTags.end(), REQUIRED_TAGS.begin(), REQUIRED_TAGS.end());
46     validTags.insert(validTags.end(), OPTIONAL_TAGS.begin(), OPTIONAL_TAGS.end());
47     IF_ERROR_THROW_RETURN(env, CheckAssetValueValidity(env, attrs, SEC_ASSET_INVALID_ARGUMENT));
48     return napi_ok;
49 }
50 
ParseAttrMap(napi_env env,napi_callback_info info,BaseContext * context)51 napi_status ParseAttrMap(napi_env env, napi_callback_info info, BaseContext *context)
52 {
53     napi_value argv[MAX_ARGS_NUM] = { 0 };
54     IF_ERR_RETURN(ParseJsArgs(env, info, argv, QUERY_ARG_COUNT));
55     IF_ERR_RETURN(ParseJsMap(env, argv[0], context->attrs));
56     IF_ERR_RETURN(CheckPostQueryArgs(env, context->attrs));
57     return napi_ok;
58 }
59 
ParseAttrMapAsUser(napi_env env,napi_callback_info info,BaseContext * context)60 napi_status ParseAttrMapAsUser(napi_env env, napi_callback_info info, BaseContext *context)
61 {
62     napi_value argv[MAX_ARGS_NUM] = { 0 };
63     IF_ERR_RETURN(ParseJsArgs(env, info, argv, QUERY_ARG_COUNT_AS_USER));
64     uint32_t index = 0;
65     IF_ERR_RETURN(ParseJsUserId(env, argv[index++], context->attrs));
66     IF_ERR_RETURN(ParseJsMap(env, argv[index++], context->attrs));
67     IF_ERR_RETURN(CheckPostQueryArgs(env, context->attrs));
68     return napi_ok;
69 }
70 } // anonymous namespace
71 
NapiPostQuery(const napi_env env,napi_callback_info info,bool asUser,bool async)72 napi_value NapiPostQuery(const napi_env env, napi_callback_info info, bool asUser, bool async)
73 {
74     auto context = std::unique_ptr<BaseContext>(new (std::nothrow)BaseContext());
75     NAPI_THROW(env, context == nullptr, SEC_ASSET_OUT_OF_MEMORY, "Unable to allocate memory for Context.");
76 
77     context->parse = asUser ? ParseAttrMapAsUser : ParseAttrMap;
78     context->execute = [](napi_env env, void *data) {
79         BaseContext *context = static_cast<BaseContext *>(data);
80         context->result = AssetPostQuery(&context->attrs[0], context->attrs.size());
81     };
82 
83     context->resolve = [](napi_env env, BaseContext *context) -> napi_value {
84         return CreateJsUndefined(env);
85     };
86 
87     if (async) {
88         return CreateAsyncWork(env, info, std::move(context), __func__);
89     } else {
90         return CreateSyncWork(env, info, context.get());
91     }
92 }
93 
NapiPostQuery(const napi_env env,napi_callback_info info)94 napi_value NapiPostQuery(const napi_env env, napi_callback_info info)
95 {
96     return NapiPostQuery(env, info, false, true);
97 }
98 
NapiPostQueryAsUser(const napi_env env,napi_callback_info info)99 napi_value NapiPostQueryAsUser(const napi_env env, napi_callback_info info)
100 {
101     return NapiPostQuery(env, info, true, true);
102 }
103 
NapiPostQuerySync(const napi_env env,napi_callback_info info)104 napi_value NapiPostQuerySync(const napi_env env, napi_callback_info info)
105 {
106     return NapiPostQuery(env, info, false, false);
107 }
108 
109 } // Asset
110 } // Security
111 } // OHOS
112