• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "compatible_auth_sub_session_util.h"
17 
18 #include "account_unrelated_group_auth.h"
19 #include "account_related_group_auth.h"
20 #include "hc_log.h"
21 #include "hc_types.h"
22 #include "os_account_adapter.h"
23 
AuthFormToModuleType(int32_t authForm)24 static int32_t AuthFormToModuleType(int32_t authForm)
25 {
26     int moduleType = INVALID_MODULE_TYPE;
27     if (authForm == AUTH_FORM_ACCOUNT_UNRELATED) {
28         moduleType = DAS_MODULE;
29     } else if ((authForm == AUTH_FORM_IDENTICAL_ACCOUNT) || (authForm == AUTH_FORM_ACROSS_ACCOUNT)) {
30         moduleType = ACCOUNT_MODULE;
31     } else {
32         LOGE("Invalid auth form!");
33     }
34     return moduleType;
35 }
36 
GetAuthModuleType(const CJson * in)37 int32_t GetAuthModuleType(const CJson *in)
38 {
39     int32_t authForm = AUTH_FORM_INVALID_TYPE;
40     if (GetIntFromJson(in, FIELD_AUTH_FORM, &authForm) != HC_SUCCESS) {
41         LOGE("Failed to get auth form!");
42         return INVALID_MODULE_TYPE;
43     }
44     return AuthFormToModuleType(authForm);
45 }
46 
GetDuplicatePkgName(const CJson * params)47 char *GetDuplicatePkgName(const CJson *params)
48 {
49     const char *pkgName = GetStringFromJson(params, FIELD_SERVICE_PKG_NAME);
50     if (pkgName == NULL) {
51         LOGE("Failed to get servicePkgName from json!");
52         return NULL;
53     }
54     uint32_t pkgNameLen = HcStrlen(pkgName) + 1;
55     char *returnPkgName = (char *)HcMalloc(pkgNameLen, 0);
56     if (returnPkgName == NULL) {
57         LOGE("Failed to allocate returnPkgName memory!");
58         return NULL;
59     }
60     if (memcpy_s(returnPkgName, pkgNameLen, pkgName, pkgNameLen) != EOK) {
61         LOGE("Failed to copy pkgName!");
62         HcFree(returnPkgName);
63         return NULL;
64     }
65     return returnPkgName;
66 }
67 
CombineAuthConfirmData(const CJson * confirmationJson,CJson * dataFromClient)68 int32_t CombineAuthConfirmData(const CJson *confirmationJson, CJson *dataFromClient)
69 {
70     int32_t osAccountId = ANY_OS_ACCOUNT;
71     (void)GetIntFromJson(confirmationJson, FIELD_OS_ACCOUNT_ID, &osAccountId);
72     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
73     if (osAccountId == INVALID_OS_ACCOUNT) {
74         LOGE("Invalid os account!");
75         return HC_ERR_INVALID_PARAMS;
76     }
77     if (AddIntToJson(dataFromClient, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
78         LOGE("Failed to add os accountId!");
79         return HC_ERR_JSON_ADD;
80     }
81     int32_t authForm = AUTH_FORM_INVALID_TYPE;
82     if (GetIntFromJson(dataFromClient, FIELD_AUTH_FORM, &authForm) != HC_SUCCESS) {
83         LOGE("Failed to get auth form!");
84         return HC_ERR_JSON_GET;
85     }
86     int32_t groupAuthType = GetAuthType(authForm);
87     BaseGroupAuth *groupAuthHandle = GetGroupAuth(groupAuthType);
88     if (groupAuthHandle == NULL) {
89         LOGE("Failed to get group auth handle!");
90         return HC_ERR_NOT_SUPPORT;
91     }
92     int32_t res = groupAuthHandle->combineServerConfirmParams(confirmationJson, dataFromClient);
93     if (res != HC_SUCCESS) {
94         LOGE("Failed to combine server confirm params!");
95     }
96     return res;
97 }
98 
GetAuthType(int32_t authForm)99 int32_t GetAuthType(int32_t authForm)
100 {
101     switch (authForm) {
102         case AUTH_FORM_ACCOUNT_UNRELATED:
103             return ACCOUNT_UNRELATED_GROUP_AUTH_TYPE;
104         case AUTH_FORM_IDENTICAL_ACCOUNT:
105             return ACCOUNT_RELATED_GROUP_AUTH_TYPE;
106         case AUTH_FORM_ACROSS_ACCOUNT:
107             return ACCOUNT_RELATED_GROUP_AUTH_TYPE;
108         default:
109             LOGE("Invalid authForm!");
110             return INVALID_GROUP_AUTH_TYPE;
111     }
112 }
113 
GetGroupAuth(int32_t groupAuthType)114 BaseGroupAuth *GetGroupAuth(int32_t groupAuthType)
115 {
116     switch (groupAuthType) {
117         case ACCOUNT_UNRELATED_GROUP_AUTH_TYPE:
118             LOGI("Non-account auth type.");
119             return GetAccountUnrelatedGroupAuth();
120         case ACCOUNT_RELATED_GROUP_AUTH_TYPE:
121             LOGI("Account-related auth type.");
122             return GetAccountRelatedGroupAuth();
123         default:
124             LOGE("Invalid auth type!");
125     }
126     return NULL;
127 }
128