• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "account_iam_service.h"
17 
18 #include "account_log_wrapper.h"
19 #include "iaccount_iam_callback.h"
20 #include "iinner_os_account_manager.h"
21 #include "inner_account_iam_manager.h"
22 #include "ipc_skeleton.h"
23 
24 namespace OHOS {
25 namespace AccountSA {
AccountIAMService()26 AccountIAMService::AccountIAMService()
27 {}
28 
~AccountIAMService()29 AccountIAMService::~AccountIAMService()
30 {}
31 
OpenSession(int32_t userId,std::vector<uint8_t> & challenge)32 int32_t AccountIAMService::OpenSession(int32_t userId, std::vector<uint8_t> &challenge)
33 {
34     if ((userId == 0) && (!GetCurrentUserId(userId))) {
35         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
36     }
37     InnerAccountIAMManager::GetInstance().OpenSession(userId, challenge);
38     return ERR_OK;
39 }
40 
CloseSession(int32_t userId)41 int32_t AccountIAMService::CloseSession(int32_t userId)
42 {
43     if ((userId == 0) && (!GetCurrentUserId(userId))) {
44         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
45     }
46     InnerAccountIAMManager::GetInstance().CloseSession(userId);
47     return ERR_OK;
48 }
49 
AddCredential(int32_t userId,const CredentialParameters & credInfo,const sptr<IIDMCallback> & callback)50 void AccountIAMService::AddCredential(
51     int32_t userId, const CredentialParameters &credInfo, const sptr<IIDMCallback> &callback)
52 {
53     Attributes emptyResult;
54     if ((userId == 0) && (!GetCurrentUserId(userId))) {
55         callback->OnResult(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, emptyResult);
56         return;
57     }
58     InnerAccountIAMManager::GetInstance().AddCredential(userId, credInfo, callback);
59 }
60 
UpdateCredential(int32_t userId,const CredentialParameters & credInfo,const sptr<IIDMCallback> & callback)61 void AccountIAMService::UpdateCredential(int32_t userId, const CredentialParameters &credInfo,
62     const sptr<IIDMCallback> &callback)
63 {
64     Attributes emptyResult;
65     if ((userId == 0) && (!GetCurrentUserId(userId))) {
66         callback->OnResult(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, emptyResult);
67         return;
68     }
69     InnerAccountIAMManager::GetInstance().UpdateCredential(userId, credInfo, callback);
70 }
71 
Cancel(int32_t userId)72 int32_t AccountIAMService::Cancel(int32_t userId)
73 {
74     if ((userId == 0) && (!GetCurrentUserId(userId))) {
75         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
76     }
77     return InnerAccountIAMManager::GetInstance().Cancel(userId);
78 }
79 
DelCred(int32_t userId,uint64_t credentialId,const std::vector<uint8_t> & authToken,const sptr<IIDMCallback> & callback)80 void AccountIAMService::DelCred(
81     int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, const sptr<IIDMCallback> &callback)
82 {
83     Attributes emptyResult;
84     if ((userId == 0) && (!GetCurrentUserId(userId))) {
85         callback->OnResult(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, emptyResult);
86         return;
87     }
88     InnerAccountIAMManager::GetInstance().DelCred(userId, credentialId, authToken, callback);
89 }
90 
DelUser(int32_t userId,const std::vector<uint8_t> & authToken,const sptr<IIDMCallback> & callback)91 void AccountIAMService::DelUser(
92     int32_t userId, const std::vector<uint8_t> &authToken, const sptr<IIDMCallback> &callback)
93 {
94     Attributes emptyResult;
95     if ((userId == 0) && (!GetCurrentUserId(userId))) {
96         callback->OnResult(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, emptyResult);
97         return;
98     }
99     InnerAccountIAMManager::GetInstance().DelUser(userId, authToken, callback);
100 }
101 
GetCredentialInfo(int32_t userId,AuthType authType,const sptr<IGetCredInfoCallback> & callback)102 int32_t AccountIAMService::GetCredentialInfo(
103     int32_t userId, AuthType authType, const sptr<IGetCredInfoCallback> &callback)
104 {
105     if ((userId == 0) && (!GetCurrentUserId(userId))) {
106         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
107     }
108     InnerAccountIAMManager::GetInstance().GetCredentialInfo(userId, authType, callback);
109     return ERR_OK;
110 }
111 
AuthUser(int32_t userId,const AuthParam & authParam,const sptr<IIDMCallback> & callback,uint64_t & contextId)112 int32_t AccountIAMService::AuthUser(
113     int32_t userId, const AuthParam &authParam, const sptr<IIDMCallback> &callback, uint64_t &contextId)
114 {
115     if ((userId == 0) && (!GetCurrentUserId(userId))) {
116         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
117     }
118     return InnerAccountIAMManager::GetInstance().AuthUser(userId, authParam, callback, contextId);
119 }
120 
CancelAuth(uint64_t contextId)121 int32_t AccountIAMService::CancelAuth(uint64_t contextId)
122 {
123     return InnerAccountIAMManager::GetInstance().CancelAuth(contextId);
124 }
125 
GetAvailableStatus(AuthType authType,AuthTrustLevel authTrustLevel,int32_t & status)126 int32_t AccountIAMService::GetAvailableStatus(AuthType authType, AuthTrustLevel authTrustLevel, int32_t &status)
127 {
128     if (authTrustLevel < UserIam::UserAuth::ATL1 || authTrustLevel > UserIam::UserAuth::ATL4) {
129         ACCOUNT_LOGE("authTrustLevel is not in correct range");
130         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
131     }
132     if (authType < UserIam::UserAuth::ALL) {
133         ACCOUNT_LOGE("authType is not in correct range");
134         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
135     }
136     return InnerAccountIAMManager::GetInstance().GetAvailableStatus(authType, authTrustLevel, status);
137 }
138 
GetProperty(int32_t userId,const GetPropertyRequest & request,const sptr<IGetSetPropCallback> & callback)139 void AccountIAMService::GetProperty(
140     int32_t userId, const GetPropertyRequest &request, const sptr<IGetSetPropCallback> &callback)
141 {
142     Attributes emptyResult;
143     if ((userId == 0) && (!GetCurrentUserId(userId))) {
144         callback->OnResult(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, emptyResult);
145         return;
146     }
147     return InnerAccountIAMManager::GetInstance().GetProperty(userId, request, callback);
148 }
149 
SetProperty(int32_t userId,const SetPropertyRequest & request,const sptr<IGetSetPropCallback> & callback)150 void AccountIAMService::SetProperty(
151     int32_t userId, const SetPropertyRequest &request, const sptr<IGetSetPropCallback> &callback)
152 {
153     Attributes emptyResult;
154     if ((userId == 0) && (!GetCurrentUserId(userId))) {
155         callback->OnResult(ERR_ACCOUNT_COMMON_INVALID_PARAMETER, emptyResult);
156         return;
157     }
158     InnerAccountIAMManager::GetInstance().SetProperty(userId, request, callback);
159 }
160 
GetAccountState(int32_t userId)161 IAMState AccountIAMService::GetAccountState(int32_t userId)
162 {
163     return InnerAccountIAMManager::GetInstance().GetState(userId);
164 }
165 
GetCurrentUserId(int32_t & userId)166 bool AccountIAMService::GetCurrentUserId(int32_t &userId)
167 {
168     std::vector<int32_t> userIds;
169     (void)IInnerOsAccountManager::GetInstance().QueryActiveOsAccountIds(userIds);
170     if (userIds.empty()) {
171         ACCOUNT_LOGE("fail to get activated os account ids");
172         return false;
173     }
174     userId = userIds[0];
175     return true;
176 }
177 }  // namespace AccountSA
178 }  // namespace OHOS
179