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_response_state.h"
17 
18 #include "dm_auth_manager.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
Leave()24 int32_t AuthResponseState::Leave()
25 {
26     return DM_OK;
27 }
28 
SetAuthContext(std::shared_ptr<DmAuthResponseContext> context)29 int32_t AuthResponseState::SetAuthContext(std::shared_ptr<DmAuthResponseContext> context)
30 {
31     context_ = std::move(context);
32     return DM_OK;
33 }
34 
GetAuthContext()35 std::shared_ptr<DmAuthResponseContext> AuthResponseState::GetAuthContext()
36 {
37     return context_;
38 }
39 
SetAuthManager(std::shared_ptr<DmAuthManager> authManager)40 int32_t AuthResponseState::SetAuthManager(std::shared_ptr<DmAuthManager> authManager)
41 {
42     authManager_ = std::move(authManager);
43     return DM_OK;
44 }
45 
TransitionTo(std::shared_ptr<AuthResponseState> state)46 int32_t AuthResponseState::TransitionTo(std::shared_ptr<AuthResponseState> state) __attribute__((no_sanitize("cfi")))
47 {
48     LOGI("AuthResponseState::TransitionTo");
49     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
50     if (stateAuthManager == nullptr) {
51         LOGE("AuthResponseState::authManager_ null");
52         return ERR_DM_FAILED;
53     }
54     std::shared_ptr<DmAuthResponseContext> contextTemp = GetAuthContext();
55     state->SetAuthManager(stateAuthManager);
56     stateAuthManager->SetAuthResponseState(state);
57     state->SetAuthContext(contextTemp);
58     this->Leave();
59     state->Enter();
60     return DM_OK;
61 }
62 
GetStateType()63 int32_t AuthResponseInitState::GetStateType()
64 {
65     return AuthState::AUTH_RESPONSE_INIT;
66 }
67 
Enter()68 int32_t AuthResponseInitState::Enter()
69 {
70     LOGI("AuthResponse::AuthResponseInitState Enter");
71     return DM_OK;
72 }
73 
GetStateType()74 int32_t AuthResponseNegotiateState::GetStateType()
75 {
76     return AuthState::AUTH_RESPONSE_NEGOTIATE;
77 }
78 
Enter()79 int32_t AuthResponseNegotiateState::Enter()
80 {
81     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
82     if (stateAuthManager == nullptr) {
83         LOGE("AuthResponseNegotiateState authManager_ is null");
84         return ERR_DM_FAILED;
85     }
86     stateAuthManager->RespNegotiate(context_->sessionId);
87     return DM_OK;
88 }
89 
GetStateType()90 int32_t AuthResponseConfirmState::GetStateType()
91 {
92     return AuthState::AUTH_RESPONSE_CONFIRM;
93 }
94 
Enter()95 int32_t AuthResponseConfirmState::Enter()
96 {
97     LOGI("AuthResponse::AuthResponseConfirmState Enter");
98     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
99     if (stateAuthManager == nullptr) {
100         LOGE("AuthResponseConfirmState authManager_ is null");
101         return ERR_DM_FAILED;
102     }
103     stateAuthManager->ShowConfigDialog();
104     return DM_OK;
105 }
106 
GetStateType()107 int32_t AuthResponseGroupState::GetStateType()
108 {
109     return AuthState::AUTH_RESPONSE_GROUP;
110 }
111 
Enter()112 int32_t AuthResponseGroupState::Enter()
113 {
114     LOGI("AuthResponse::AuthResponseGroupState Enter");
115     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
116     if (stateAuthManager == nullptr) {
117         LOGE("AuthResponseGroupState authManager_ is null");
118         return ERR_DM_FAILED;
119     }
120     stateAuthManager->CreateGroup();
121     return DM_OK;
122 }
123 
GetStateType()124 int32_t AuthResponseShowState::GetStateType()
125 {
126     return AuthState::AUTH_RESPONSE_SHOW;
127 }
128 
Enter()129 int32_t AuthResponseShowState::Enter()
130 {
131     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
132     if (stateAuthManager == nullptr) {
133         LOGE("AuthResponseShowState authManager_ is null");
134         return ERR_DM_FAILED;
135     }
136     stateAuthManager->ShowAuthInfoDialog();
137     return DM_OK;
138 }
139 
GetStateType()140 int32_t AuthResponseFinishState::GetStateType()
141 {
142     return AuthState::AUTH_RESPONSE_FINISH;
143 }
144 
Enter()145 int32_t AuthResponseFinishState::Enter()
146 {
147     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
148     if (stateAuthManager == nullptr) {
149         LOGE("AuthResponseFinishState authManager_ is null");
150         return ERR_DM_FAILED;
151     }
152     stateAuthManager->AuthenticateFinish();
153     return DM_OK;
154 }
155 
GetStateType()156 int32_t AuthResponseCredential::GetStateType()
157 {
158     return AuthState::AUTH_RESPONSE_CREDENTIAL;
159 }
160 
Enter()161 int32_t AuthResponseCredential::Enter()
162 {
163     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
164     if (stateAuthManager == nullptr) {
165         LOGE("AuthResponseCredential authManager_ is null");
166         return ERR_DM_FAILED;
167     }
168     stateAuthManager->ResponseCredential();
169     return DM_OK;
170 }
171 
GetStateType()172 int32_t AuthResponseSyncDeleteAcl::GetStateType()
173 {
174     return AuthState::AUTH_RESPONSE_SYNCDELETE;
175 }
176 
Enter()177 int32_t AuthResponseSyncDeleteAcl::Enter()
178 {
179     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
180     if (stateAuthManager == nullptr) {
181         LOGE("AuthResponseSyncDeleteAcl authManager_ is null");
182         return ERR_DM_FAILED;
183     }
184     stateAuthManager->ResponseSyncDeleteAcl();
185     return DM_OK;
186 }
187 
GetStateType()188 int32_t AuthResponseSyncDeleteAclNone::GetStateType()
189 {
190     return AuthState::AUTH_RESPONSE_SYNCDELETE_DONE;
191 }
192 
Enter()193 int32_t AuthResponseSyncDeleteAclNone::Enter()
194 {
195     std::shared_ptr<DmAuthManager> stateAuthManager = authManager_.lock();
196     if (stateAuthManager == nullptr) {
197         LOGE("AuthResponseSyncDeleteAclNone authManager_ is null");
198         return ERR_DM_FAILED;
199     }
200     stateAuthManager->SyncDeleteAclDone();
201     return DM_OK;
202 }
203 } // namespace DistributedHardware
204 } // namespace OHOS
205