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 "auth_request_state.h"
17 
18 #include "dm_auth_manager.h"
19 #include "dm_constants.h"
20 
21 namespace OHOS {
22 namespace DistributedHardware {
Leave()23 int32_t AuthRequestState::Leave()
24 {
25     return DM_OK;
26 }
27 
SetAuthManager(std::shared_ptr<DmAuthManager> authManager)28 int32_t AuthRequestState::SetAuthManager(std::shared_ptr<DmAuthManager> authManager)
29 {
30     authManager_ = std::move(authManager);
31     return DM_OK;
32 }
33 
SetAuthContext(std::shared_ptr<DmAuthRequestContext> context)34 int32_t AuthRequestState::SetAuthContext(std::shared_ptr<DmAuthRequestContext> context)
35 {
36     context_ = std::move(context);
37     return DM_OK;
38 }
39 
GetAuthContext()40 std::shared_ptr<DmAuthRequestContext> AuthRequestState::GetAuthContext()
41 {
42     return context_;
43 }
44 
TransitionTo(std::shared_ptr<AuthRequestState> state)45 int32_t AuthRequestState::TransitionTo(std::shared_ptr<AuthRequestState> state)
46 {
47     LOGI("AuthRequestState::TransitionTo");
48     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
49     if (stateAuthManager == nullptr) {
50         LOGE("AuthRequestState::authManager_ null");
51         return ERR_DM_FAILED;
52     }
53     std::shared_ptr<DmAuthRequestContext> contextTemp = GetAuthContext();
54     state->SetAuthManager(stateAuthManager);
55     stateAuthManager->SetAuthRequestState(state);
56     state->SetAuthContext(contextTemp);
57     this->Leave();
58     state->Enter();
59     return DM_OK;
60 }
61 
GetStateType()62 int32_t AuthRequestInitState::GetStateType()
63 {
64     return AuthState::AUTH_REQUEST_INIT;
65 }
66 
Enter()67 int32_t AuthRequestInitState::Enter()
68 {
69     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
70     if (stateAuthManager == nullptr) {
71         LOGE("AuthRequestState::authManager_ null");
72         return ERR_DM_FAILED;
73     }
74     stateAuthManager->EstablishAuthChannel(context_->deviceId);
75     return DM_OK;
76 }
77 
GetStateType()78 int32_t AuthRequestNegotiateState::GetStateType()
79 {
80     return AuthState::AUTH_REQUEST_NEGOTIATE;
81 }
82 
Enter()83 int32_t AuthRequestNegotiateState::Enter()
84 {
85     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
86     if (stateAuthManager == nullptr) {
87         LOGE("AuthRequestState::authManager_ null");
88         return ERR_DM_FAILED;
89     }
90     stateAuthManager->StartNegotiate(context_->sessionId);
91     return DM_OK;
92 }
93 
GetStateType()94 int32_t AuthRequestNegotiateDoneState::GetStateType()
95 {
96     return AuthState::AUTH_REQUEST_NEGOTIATE_DONE;
97 }
98 
Enter()99 int32_t AuthRequestNegotiateDoneState::Enter()
100 {
101     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
102     if (stateAuthManager == nullptr) {
103         LOGE("AuthRequestState::authManager_ null");
104         return ERR_DM_FAILED;
105     }
106     stateAuthManager->SendAuthRequest(context_->sessionId);
107     return DM_OK;
108 }
109 
GetStateType()110 int32_t AuthRequestReplyState::GetStateType()
111 {
112     return AuthState::AUTH_REQUEST_REPLY;
113 }
114 
Enter()115 int32_t AuthRequestReplyState::Enter()
116 {
117     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
118     if (stateAuthManager == nullptr) {
119         LOGE("AuthRequestState::authManager_ null");
120         return ERR_DM_FAILED;
121     }
122     stateAuthManager->StartRespAuthProcess();
123     return DM_OK;
124 }
125 
GetStateType()126 int32_t AuthRequestJoinState::GetStateType()
127 {
128     return AuthState::AUTH_REQUEST_JOIN;
129 }
130 
Enter()131 int32_t AuthRequestJoinState::Enter()
132 {
133     LOGI("DmAuthManager::AuthRequestJoinState");
134     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
135     if (stateAuthManager == nullptr) {
136         LOGE("AuthRequestState::authManager_ null");
137         return ERR_DM_FAILED;
138     }
139     stateAuthManager->ShowStartAuthDialog();
140     return DM_OK;
141 }
142 
GetStateType()143 int32_t AuthRequestNetworkState::GetStateType()
144 {
145     return AuthState::AUTH_REQUEST_NETWORK;
146 }
147 
Enter()148 int32_t AuthRequestNetworkState::Enter()
149 {
150     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
151     if (stateAuthManager == nullptr) {
152         LOGE("AuthRequestState::authManager_ null");
153         return ERR_DM_FAILED;
154     }
155     stateAuthManager->JoinNetwork();
156     return DM_OK;
157 }
158 
GetStateType()159 int32_t AuthRequestFinishState::GetStateType()
160 {
161     return AuthState::AUTH_REQUEST_FINISH;
162 }
163 
Enter()164 int32_t AuthRequestFinishState::Enter()
165 {
166     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
167     if (stateAuthManager == nullptr) {
168         LOGE("AuthRequestState::authManager_ null");
169         return ERR_DM_FAILED;
170     }
171     stateAuthManager->AuthenticateFinish();
172     return DM_OK;
173 }
174 
175 //pkgbind
GetStateType()176 int32_t AuthRequestCredential::GetStateType()
177 {
178     return AuthState::AUTH_REQUEST_CREDENTIAL;
179 }
180 
Enter()181 int32_t AuthRequestCredential::Enter()
182 {
183     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
184     if (stateAuthManager == nullptr) {
185         LOGE("AuthRequestState::authManager_ null");
186         return ERR_DM_FAILED;
187     }
188     stateAuthManager->RequestCredential();
189     return DM_OK;
190 }
191 
GetStateType()192 int32_t AuthRequestCredentialDone::GetStateType()
193 {
194     return AuthState::AUTH_REQUEST_CREDENTIAL_DONE;
195 }
196 
Enter()197 int32_t AuthRequestCredentialDone::Enter()
198 {
199     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
200     if (stateAuthManager == nullptr) {
201         LOGE("AuthRequestState::authManager_ null");
202         return ERR_DM_FAILED;
203     }
204     stateAuthManager->RequestCredentialDone();
205     return DM_OK;
206 }
207 
GetStateType()208 int32_t AuthRequestDeleteInit::GetStateType()
209 {
210     return AuthState::AUTH_REQUEST_DELETE_INIT;
211 }
212 
Enter()213 int32_t AuthRequestDeleteInit::Enter()
214 {
215     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
216     if (stateAuthManager == nullptr) {
217         LOGE("AuthRequestState::authManager_ null");
218         return ERR_DM_FAILED;
219     }
220     stateAuthManager->EstablishUnbindChannel(context_->deviceId);
221     return DM_OK;
222 }
223 
GetStateType()224 int32_t AuthRequestSyncDeleteAcl::GetStateType()
225 {
226     return AuthState::AUTH_REQUEST_SYNCDELETE;
227 }
228 
Enter()229 int32_t AuthRequestSyncDeleteAcl::Enter()
230 {
231     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
232     if (stateAuthManager == nullptr) {
233         LOGE("AuthRequestState::authManager_ null");
234         return ERR_DM_FAILED;
235     }
236     stateAuthManager->RequestSyncDeleteAcl();
237     return DM_OK;
238 }
239 
GetStateType()240 int32_t AuthRequestSyncDeleteAclNone::GetStateType()
241 {
242     return AuthState::AUTH_REQUEST_SYNCDELETE_DONE;
243 }
244 
Enter()245 int32_t AuthRequestSyncDeleteAclNone::Enter()
246 {
247     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
248     if (stateAuthManager == nullptr) {
249         LOGE("AuthRequestState::authManager_ null");
250         return ERR_DM_FAILED;
251     }
252     stateAuthManager->SyncDeleteAclDone();
253     return DM_OK;
254 }
255 } // namespace DistributedHardware
256 } // namespace OHOS
257