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 "useriam_common.h"
17 
18 #include <sys/stat.h>
19 #include <unistd.h>
20 
21 #include "pool.h"
22 #include "idm_database.h"
23 #include "coauth.h"
24 #include "context_manager.h"
25 #include "adaptor_log.h"
26 #include "ed25519_key.h"
27 
28 namespace OHOS {
29 namespace UserIam {
30 namespace Common {
31 static bool g_isInitUserIAM = false;
32 
Init()33 int32_t Init()
34 {
35     if (g_isInitUserIAM) {
36         LOG_INFO("already init");
37         return RESULT_SUCCESS;
38     }
39 
40     if (InitUserAuthContextList() != RESULT_SUCCESS) {
41         LOG_ERROR("init context list failed");
42         goto FAIL;
43     }
44 
45     if (InitResourcePool() != RESULT_SUCCESS) {
46         LOG_ERROR("init resource pool failed");
47         goto FAIL;
48     }
49     if (InitUserInfoList() != RESULT_SUCCESS) {
50         LOG_ERROR("init user info failed");
51         goto FAIL;
52     }
53     if (InitCoAuth() != RESULT_SUCCESS) {
54         LOG_ERROR("init user auth failed");
55         goto FAIL;
56     }
57     if (GenerateKeyPair() != RESULT_SUCCESS) {
58         LOG_ERROR("init ed25519 key failed");
59         goto FAIL;
60     }
61 
62     LOG_INFO("init success");
63     g_isInitUserIAM = true;
64     return RESULT_SUCCESS;
65 
66 FAIL:
67     Close();
68     return RESULT_UNKNOWN;
69 }
70 
Close()71 int32_t Close()
72 {
73     DestoryUserAuthContextList();
74     DestoryCoAuth();
75     DestroyUserInfoList();
76     DestroyResourcePool();
77     DestoryEd25519KeyPair();
78     g_isInitUserIAM = false;
79     return RESULT_SUCCESS;
80 }
81 
IsIAMInited()82 bool IsIAMInited()
83 {
84     return g_isInitUserIAM;
85 }
86 } // Common
87 } // UserIam
88 } // OHOS