• 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_IAM_SERVICE_PARAM_INVALID_ERROR;
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_IAM_SERVICE_PARAM_INVALID_ERROR;
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_IAM_SERVICE_PARAM_INVALID_ERROR, 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_IAM_SERVICE_PARAM_INVALID_ERROR, 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_IAM_SERVICE_PARAM_INVALID_ERROR;
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_IAM_SERVICE_PARAM_INVALID_ERROR, 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_IAM_SERVICE_PARAM_INVALID_ERROR, 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_IAM_SERVICE_PARAM_INVALID_ERROR;
107     }
108     InnerAccountIAMManager::GetInstance().GetCredentialInfo(userId, authType, callback);
109     return ERR_OK;
110 }
111 
AuthUser(int32_t userId,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel authTrustLevel,const sptr<IIDMCallback> & callback)112 uint64_t AccountIAMService::AuthUser(int32_t userId, const std::vector<uint8_t> &challenge, AuthType authType,
113     AuthTrustLevel authTrustLevel, const sptr<IIDMCallback> &callback)
114 {
115     if ((userId == 0) && (!GetCurrentUserId(userId))) {
116         return ERR_ACCOUNT_IAM_SERVICE_PARAM_INVALID_ERROR;
117     }
118     return InnerAccountIAMManager::GetInstance().AuthUser(
119         userId, challenge, authType, authTrustLevel, callback);
120 }
121 
CancelAuth(uint64_t contextId)122 int32_t AccountIAMService::CancelAuth(uint64_t contextId)
123 {
124     return InnerAccountIAMManager::GetInstance().CancelAuth(contextId);
125 }
126 
GetAvailableStatus(AuthType authType,AuthTrustLevel authTrustLevel,int32_t & status)127 int32_t AccountIAMService::GetAvailableStatus(AuthType authType, AuthTrustLevel authTrustLevel, int32_t &status)
128 {
129     if (authTrustLevel < UserIam::UserAuth::ATL1 || authTrustLevel > UserIam::UserAuth::ATL4) {
130         ACCOUNT_LOGE("authTrustLevel is not in correct range");
131         return ERR_ACCOUNT_IAM_SERVICE_PARAM_INVALID_ERROR;
132     }
133     if (authType < UserIam::UserAuth::ALL) {
134         ACCOUNT_LOGE("authType is not in correct range");
135         return ERR_ACCOUNT_IAM_SERVICE_PARAM_INVALID_ERROR;
136     }
137     return InnerAccountIAMManager::GetInstance().GetAvailableStatus(authType, authTrustLevel, status);
138 }
139 
GetProperty(int32_t userId,const GetPropertyRequest & request,const sptr<IGetSetPropCallback> & callback)140 void AccountIAMService::GetProperty(
141     int32_t userId, const GetPropertyRequest &request, const sptr<IGetSetPropCallback> &callback)
142 {
143     Attributes emptyResult;
144     if ((userId == 0) && (!GetCurrentUserId(userId))) {
145         callback->OnResult(ERR_ACCOUNT_IAM_SERVICE_PARAM_INVALID_ERROR, emptyResult);
146         return;
147     }
148     return InnerAccountIAMManager::GetInstance().GetProperty(userId, request, callback);
149 }
150 
SetProperty(int32_t userId,const SetPropertyRequest & request,const sptr<IGetSetPropCallback> & callback)151 void AccountIAMService::SetProperty(
152     int32_t userId, const SetPropertyRequest &request, const sptr<IGetSetPropCallback> &callback)
153 {
154     Attributes emptyResult;
155     if ((userId == 0) && (!GetCurrentUserId(userId))) {
156         callback->OnResult(ERR_ACCOUNT_IAM_SERVICE_PARAM_INVALID_ERROR, emptyResult);
157         return;
158     }
159     InnerAccountIAMManager::GetInstance().SetProperty(userId, request, callback);
160 }
161 
GetAccountState(int32_t userId)162 IAMState AccountIAMService::GetAccountState(int32_t userId)
163 {
164     return InnerAccountIAMManager::GetInstance().GetState(userId);
165 }
166 
GetCurrentUserId(int32_t & userId)167 bool AccountIAMService::GetCurrentUserId(int32_t &userId)
168 {
169     std::vector<int32_t> userIds;
170     (void)IInnerOsAccountManager::GetInstance()->QueryActiveOsAccountIds(userIds);
171     if (userIds.empty()) {
172         ACCOUNT_LOGE("fail to get activated os account ids");
173         return false;
174     }
175     userId = userIds[0];
176     return true;
177 }
178 }  // namespace AccountSA
179 }  // namespace OHOS
180