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 if (!GetEnableStatus(INVALID_USER_ID, param.authType)) {
34 LOG_ERROR("authType is not support %{public}d", param.authType);
35 return RESULT_TYPE_NOT_SUPPORT;
36 }
37 UserAuthContext *identifyContext = GenerateIdentifyContext(param);
38 if (identifyContext == NULL) {
39 LOG_ERROR("authContext is null");
40 return RESULT_GENERAL_ERROR;
41 }
42 ResultCode ret = CopySchedules(identifyContext, schedule);
43 if (ret != RESULT_SUCCESS) {
44 LOG_ERROR("get schedule failed");
45 DestroyContext(identifyContext);
46 return ret;
47 }
48 return ret;
49 }
50
DoUpdateIdentify(uint64_t contextId,const Buffer * scheduleResult,int32_t * userId,UserAuthTokenHal * token,int32_t * result)51 ResultCode DoUpdateIdentify(uint64_t contextId, const Buffer *scheduleResult, int32_t *userId,
52 UserAuthTokenHal *token, int32_t *result)
53 {
54 if (!IsBufferValid(scheduleResult) || token == NULL || userId == NULL || result == NULL) {
55 LOG_ERROR("param is null");
56 DestroyContextbyId(contextId);
57 return RESULT_BAD_PARAM;
58 }
59
60 UserAuthContext *identifyContext = GetContext(contextId);
61 if (identifyContext == NULL) {
62 LOG_ERROR("identifyContext is null");
63 return RESULT_GENERAL_ERROR;
64 }
65 ExecutorResultInfo *executorResultInfo = CreateExecutorResultInfo(scheduleResult);
66 if (executorResultInfo == NULL) {
67 LOG_ERROR("executorResultInfo is null");
68 DestroyContext(identifyContext);
69 return RESULT_GENERAL_ERROR;
70 }
71
72 ResultCode ret = RESULT_GENERAL_ERROR;
73 *result = executorResultInfo->result;
74 if (*result != RESULT_SUCCESS) {
75 LOG_ERROR("executor result is not success, result: %{pubilc}d", executorResultInfo->result);
76 goto EXIT;
77 }
78 uint64_t credentialId;
79 ret = FillInContext(identifyContext, &credentialId, executorResultInfo, SCHEDULE_MODE_IDENTIFY);
80 if (ret != RESULT_SUCCESS) {
81 LOG_ERROR("FillInContext fail");
82 goto EXIT;
83 }
84 ret = GetAuthTokenDataAndSign(identifyContext, credentialId, SCHEDULE_MODE_IDENTIFY, token);
85 if (ret != RESULT_SUCCESS) {
86 LOG_ERROR("get token failed");
87 goto EXIT;
88 }
89 *userId = identifyContext->userId;
90
91 EXIT:
92 DestroyExecutorResultInfo(executorResultInfo);
93 DestroyContext(identifyContext);
94 return ret;
95 }