1 /*
2 * Copyright (C) 2022 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 "identify_funcs.h"
17
18 #include "securec.h"
19
20 #include "adaptor_log.h"
21 #include "context_manager.h"
22 #include "executor_message.h"
23 #include "idm_database.h"
24 #include "auth_level.h"
25
DoIdentify(const IdentifyParam param,LinkedList ** schedule)26 int32_t DoIdentify(const IdentifyParam param, LinkedList **schedule)
27 {
28 if (schedule == NULL) {
29 LOG_ERROR("schedule is null");
30 return RESULT_BAD_PARAM;
31 }
32 UserAuthContext *identifyContext = GenerateIdentifyContext(param);
33 if (identifyContext == NULL) {
34 LOG_ERROR("authContext is null");
35 return RESULT_GENERAL_ERROR;
36 }
37 int32_t ret = CopySchedules(identifyContext, schedule);
38 if (ret != RESULT_SUCCESS) {
39 LOG_ERROR("get schedule failed");
40 DestoryContext(identifyContext);
41 return ret;
42 }
43 return ret;
44 }
45
DoUpdateIdentify(uint64_t contextId,const Buffer * scheduleResult,int32_t * userId,UserAuthTokenHal * token,int32_t * result)46 int32_t DoUpdateIdentify(uint64_t contextId, const Buffer *scheduleResult, int32_t *userId, UserAuthTokenHal *token,
47 int32_t *result)
48 {
49 if (!IsBufferValid(scheduleResult) || token == NULL || userId == NULL || result == NULL) {
50 LOG_ERROR("param is null");
51 return RESULT_BAD_PARAM;
52 }
53
54 UserAuthContext *identifyContext = GetContext(contextId);
55 if (identifyContext == NULL) {
56 LOG_ERROR("identifyContext is null");
57 return RESULT_UNKNOWN;
58 }
59 ExecutorResultInfo *executorResultInfo = CreateExecutorResultInfo(scheduleResult);
60 if (executorResultInfo == NULL) {
61 LOG_ERROR("executorResultInfo is null");
62 DestoryContext(identifyContext);
63 return RESULT_UNKNOWN;
64 }
65 *result = executorResultInfo->result;
66 if (*result != RESULT_SUCCESS) {
67 DestoryContext(identifyContext);
68 DestoryExecutorResultInfo(executorResultInfo);
69 return RESULT_SUCCESS;
70 }
71 uint64_t credentialId;
72 int32_t ret = FillInContext(identifyContext, &credentialId, executorResultInfo);
73 if (ret != RESULT_SUCCESS) {
74 LOG_ERROR("get info failed");
75 DestoryContext(identifyContext);
76 DestoryExecutorResultInfo(executorResultInfo);
77 return ret;
78 }
79 ret = GetTokenDataAndSign(identifyContext, credentialId, SCHEDULE_MODE_IDENTIFY, token);
80 if (ret != RESULT_SUCCESS) {
81 LOG_ERROR("get token failed");
82 (void)memset_s(token, sizeof(UserAuthTokenHal), 0, sizeof(UserAuthTokenHal));
83 DestoryContext(identifyContext);
84 DestoryExecutorResultInfo(executorResultInfo);
85 return ret;
86 }
87 *userId = identifyContext->userId;
88 DestoryContext(identifyContext);
89 DestoryExecutorResultInfo(executorResultInfo);
90 return RESULT_SUCCESS;
91 }