1 /*
2  * Copyright (C) 2023 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 "account_auth_plugin_proxy.h"
17 #include "device_auth_defines.h"
18 #include "hc_dev_info.h"
19 #include "hc_log.h"
20 #include "hc_types.h"
21 
22 static AccountAuthExtPlug *g_accountAuthPlugin = NULL;
23 static AccountAuthExtPlugCtx *g_accountAuthPlugCtx = NULL;
24 
InitAccountAuthPluginCtx(void)25 static int32_t InitAccountAuthPluginCtx(void)
26 {
27     g_accountAuthPlugCtx = (AccountAuthExtPlugCtx *)HcMalloc(sizeof(AccountAuthExtPlugCtx), 0);
28     if (g_accountAuthPlugCtx == NULL) {
29         LOGE("[ACCOUNT_AUTH_PLUGIN]: Malloc memory failed.");
30         return HC_ERR_INVALID_PARAMS;
31     }
32     g_accountAuthPlugCtx->getStoragePath = GetAccountStoragePath;
33     return HC_SUCCESS;
34 }
35 
SetAccountAuthPlugin(const CJson * inputParams,AccountAuthExtPlug * accountAuthPlugin)36 int32_t SetAccountAuthPlugin(const CJson *inputParams, AccountAuthExtPlug *accountAuthPlugin)
37 {
38     g_accountAuthPlugin = accountAuthPlugin;
39     if (g_accountAuthPlugin == NULL || g_accountAuthPlugin->createSession == NULL ||
40         g_accountAuthPlugin->excuteCredMgrCmd == NULL || g_accountAuthPlugin->processSession == NULL ||
41         g_accountAuthPlugin->destroySession == NULL) {
42         LOGE("[ACCOUNT_AUTH_PLUGIN]: SetAccountAuthPlugin:Input params is invalid.");
43         return HC_ERR_INVALID_PARAMS;
44     }
45     int32_t res = InitAccountAuthPluginCtx();
46     if (res != HC_SUCCESS) {
47         LOGE("[ACCOUNT_AUTH_PLUGIN]: Init account auth ctx failed.");
48         return res;
49     }
50     return g_accountAuthPlugin->base.init(&g_accountAuthPlugin->base,
51         inputParams, (const ExtPluginCtx *)g_accountAuthPlugCtx);
52 }
53 
ExcuteCredMgrCmd(int32_t osAccountId,int32_t cmdId,const CJson * in,CJson * out)54 int32_t ExcuteCredMgrCmd(int32_t osAccountId, int32_t cmdId, const CJson *in, CJson *out)
55 {
56     if (g_accountAuthPlugin == NULL || g_accountAuthPlugin->excuteCredMgrCmd == NULL) {
57         LOGE("[ACCOUNT_AUTH_PLUGIN]: excuteCredMgrCmd: Input params is invalid.");
58         return HC_ERR_INVALID_PARAMS;
59     }
60     return g_accountAuthPlugin->excuteCredMgrCmd(osAccountId, cmdId, in, out);
61 }
62 
CreateAuthSession(int32_t * sessionId,const CJson * in,CJson * out)63 int32_t CreateAuthSession(int32_t *sessionId, const CJson *in, CJson *out)
64 {
65     if (g_accountAuthPlugin == NULL || g_accountAuthPlugin->createSession == NULL) {
66         LOGE("[ACCOUNT_AUTH_PLUGIN]: createSession: Input params is invalid.");
67         return HC_ERR_INVALID_PARAMS;
68     }
69     return g_accountAuthPlugin->createSession(sessionId, in, out);
70 }
71 
ProcessAuthSession(int32_t * sessionId,const CJson * in,CJson * out,int32_t * status)72 int32_t ProcessAuthSession(int32_t *sessionId, const CJson *in, CJson *out, int32_t *status)
73 {
74     if (g_accountAuthPlugin == NULL || g_accountAuthPlugin->processSession == NULL) {
75         LOGE("[ACCOUNT_AUTH_PLUGIN]: processSession: Input params is invalid.");
76         return HC_ERR_INVALID_PARAMS;
77     }
78     return g_accountAuthPlugin->processSession(sessionId, in, out, status);
79 }
80 
DestroyAuthSession(int32_t sessionId)81 int32_t DestroyAuthSession(int32_t sessionId)
82 {
83     if (g_accountAuthPlugin == NULL || g_accountAuthPlugin->destroySession == NULL) {
84         LOGE("[ACCOUNT_AUTH_PLUGIN]: destroySession: Input params is invalid.");
85         return HC_ERR_INVALID_PARAMS;
86     }
87     return g_accountAuthPlugin->destroySession(sessionId);
88 }
89 
DestoryAccountAuthPlugin(void)90 void DestoryAccountAuthPlugin(void)
91 {
92     if (g_accountAuthPlugin != NULL && g_accountAuthPlugin->base.destroy != NULL) {
93         g_accountAuthPlugin->base.destroy(&g_accountAuthPlugin->base);
94         g_accountAuthPlugin = NULL;
95     }
96     if (g_accountAuthPlugCtx != NULL) {
97         HcFree(g_accountAuthPlugCtx);
98         g_accountAuthPlugCtx = NULL;
99     }
100 }
101 
HasAccountAuthPlugin(void)102 int32_t HasAccountAuthPlugin(void)
103 {
104     if (g_accountAuthPlugin == NULL || g_accountAuthPlugin->excuteCredMgrCmd == NULL) {
105         LOGI("[ACCOUNT_AUTH_PLUGIN]: processSession: plugin is invalid.");
106         return HC_ERR_NOT_SUPPORT;
107     }
108     return HC_SUCCESS;
109 }