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