1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "useriam_common.h"
17094332d3Sopenharmony_ci
18094332d3Sopenharmony_ci#include <sys/stat.h>
19094332d3Sopenharmony_ci#include <unistd.h>
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_ci#include "pool.h"
22094332d3Sopenharmony_ci#include "idm_database.h"
23094332d3Sopenharmony_ci#include "coauth.h"
24094332d3Sopenharmony_ci#include "context_manager.h"
25094332d3Sopenharmony_ci#include "adaptor_log.h"
26094332d3Sopenharmony_ci#include "ed25519_key.h"
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_cinamespace OHOS {
29094332d3Sopenharmony_cinamespace UserIam {
30094332d3Sopenharmony_cinamespace Common {
31094332d3Sopenharmony_cistatic bool g_isInitUserIAM = false;
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_ciint32_t Init()
34094332d3Sopenharmony_ci{
35094332d3Sopenharmony_ci    if (g_isInitUserIAM) {
36094332d3Sopenharmony_ci        LOG_INFO("already init");
37094332d3Sopenharmony_ci        return RESULT_SUCCESS;
38094332d3Sopenharmony_ci    }
39094332d3Sopenharmony_ci
40094332d3Sopenharmony_ci    if (InitUserAuthContextList() != RESULT_SUCCESS) {
41094332d3Sopenharmony_ci        LOG_ERROR("init context list failed");
42094332d3Sopenharmony_ci        goto FAIL;
43094332d3Sopenharmony_ci    }
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_ci    if (InitResourcePool() != RESULT_SUCCESS) {
46094332d3Sopenharmony_ci        LOG_ERROR("init resource pool failed");
47094332d3Sopenharmony_ci        goto FAIL;
48094332d3Sopenharmony_ci    }
49094332d3Sopenharmony_ci    if (InitUserInfoList() != RESULT_SUCCESS) {
50094332d3Sopenharmony_ci        LOG_ERROR("init user info failed");
51094332d3Sopenharmony_ci        goto FAIL;
52094332d3Sopenharmony_ci    }
53094332d3Sopenharmony_ci    if (InitCoAuth() != RESULT_SUCCESS) {
54094332d3Sopenharmony_ci        LOG_ERROR("init user auth failed");
55094332d3Sopenharmony_ci        goto FAIL;
56094332d3Sopenharmony_ci    }
57094332d3Sopenharmony_ci    if (GenerateKeyPair() != RESULT_SUCCESS) {
58094332d3Sopenharmony_ci        LOG_ERROR("init ed25519 key failed");
59094332d3Sopenharmony_ci        goto FAIL;
60094332d3Sopenharmony_ci    }
61094332d3Sopenharmony_ci
62094332d3Sopenharmony_ci    LOG_INFO("init success");
63094332d3Sopenharmony_ci    g_isInitUserIAM = true;
64094332d3Sopenharmony_ci    return RESULT_SUCCESS;
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_ciFAIL:
67094332d3Sopenharmony_ci    Close();
68094332d3Sopenharmony_ci    return RESULT_UNKNOWN;
69094332d3Sopenharmony_ci}
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ciint32_t Close()
72094332d3Sopenharmony_ci{
73094332d3Sopenharmony_ci    DestoryUserAuthContextList();
74094332d3Sopenharmony_ci    DestoryCoAuth();
75094332d3Sopenharmony_ci    DestroyUserInfoList();
76094332d3Sopenharmony_ci    DestroyResourcePool();
77094332d3Sopenharmony_ci    DestoryEd25519KeyPair();
78094332d3Sopenharmony_ci    g_isInitUserIAM = false;
79094332d3Sopenharmony_ci    return RESULT_SUCCESS;
80094332d3Sopenharmony_ci}
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_cibool IsIAMInited()
83094332d3Sopenharmony_ci{
84094332d3Sopenharmony_ci    return g_isInitUserIAM;
85094332d3Sopenharmony_ci}
86094332d3Sopenharmony_ci} // Common
87094332d3Sopenharmony_ci} // UserIam
88094332d3Sopenharmony_ci} // OHOS