1 /*
2 * Copyright (c) 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 "os_account_adapter.h"
17 #include "device_auth_common.h"
18 #include "hc_log.h"
19
AddOsAccountIdToContextIfValid(CJson * context)20 int32_t AddOsAccountIdToContextIfValid(CJson *context)
21 {
22 int32_t osAccountId = ANY_OS_ACCOUNT;
23 (void)GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId);
24 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
25 LOGI("[OsAccountId]: %" LOG_PUB "d", osAccountId);
26 if (osAccountId == INVALID_OS_ACCOUNT) {
27 return HC_ERR_INVALID_PARAMS;
28 }
29 if (!CheckIsForegroundOsAccountId(osAccountId)) {
30 LOGE("This access is not from the foreground user, rejected it.");
31 return HC_ERR_CROSS_USER_ACCESS;
32 }
33 if (!IsOsAccountUnlocked(osAccountId)) {
34 LOGE("Os account is not unlocked!");
35 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
36 }
37 if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
38 LOGE("add operationCode to context fail.");
39 return HC_ERR_JSON_ADD;
40 }
41 return HC_SUCCESS;
42 }
43
CheckConfirmationExist(const CJson * context)44 int32_t CheckConfirmationExist(const CJson *context)
45 {
46 uint32_t confirmation = REQUEST_REJECTED;
47 if (GetUnsignedIntFromJson(context, FIELD_CONFIRMATION, &confirmation) != HC_SUCCESS) {
48 LOGE("Failed to get confimation from json!");
49 return HC_ERR_JSON_GET;
50 }
51 if (confirmation == REQUEST_ACCEPTED) {
52 LOGI("The service accepts this request!");
53 } else {
54 LOGW("The service rejects this request!");
55 }
56 return HC_SUCCESS;
57 }
58
AddChannelInfoToContext(int32_t channelType,int64_t channelId,CJson * context)59 int32_t AddChannelInfoToContext(int32_t channelType, int64_t channelId, CJson *context)
60 {
61 if (AddIntToJson(context, FIELD_CHANNEL_TYPE, channelType) != HC_SUCCESS) {
62 LOGE("add channelType to context fail.");
63 return HC_ERR_JSON_ADD;
64 }
65 if (AddByteToJson(context, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) {
66 LOGE("add channelId to context fail.");
67 return HC_ERR_JSON_ADD;
68 }
69 return HC_SUCCESS;
70 }