• 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 "pin_auth.h"
17 
18 #include <memory>
19 
20 #include "dm_anonymous.h"
21 #include "dm_constants.h"
22 #include "dm_log.h"
23 #include "nlohmann/json.hpp"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 const int32_t MAX_VERIFY_TIMES = 3;
PinAuth()28 PinAuth::PinAuth()
29 {
30     LOGI("PinAuth constructor");
31 }
32 
~PinAuth()33 PinAuth::~PinAuth()
34 {
35 }
36 
CloseAuthInfo(const int32_t & pageId,std::shared_ptr<DmAuthManager> authManager)37 int32_t PinAuth::CloseAuthInfo(const int32_t &pageId, std::shared_ptr<DmAuthManager> authManager)
38 {
39     LOGI("ClosePage hap start");
40     if (authManager == nullptr) {
41         LOGE("PinAuthUi::authManager is null");
42         return ERR_DM_FAILED;
43     }
44     LOGI("ClosePage hap end");
45     authManager->CancelDisplay();
46     return DM_OK;
47 }
48 
ShowAuthInfo(std::string & authToken,std::shared_ptr<DmAuthManager> authManager)49 int32_t PinAuth::ShowAuthInfo(std::string &authToken, std::shared_ptr<DmAuthManager> authManager)
50 {
51     LOGI("ShowConfigDialog end");
52     return DM_OK;
53 }
54 
StartAuth(std::string & authToken,std::shared_ptr<DmAuthManager> authManager)55 int32_t PinAuth::StartAuth(std::string &authToken, std::shared_ptr<DmAuthManager> authManager)
56 {
57     LOGI("StartAuth end");
58     return DM_OK;
59 }
60 
VerifyAuthentication(std::string & authToken,const std::string & authParam)61 int32_t PinAuth::VerifyAuthentication(std::string &authToken, const std::string &authParam)
62 {
63     times_ += 1;
64     if (authParam.length() == 1) {
65         if (authParam == "0") {
66             return DM_OK;
67         }
68         LOGE("Peer rejection");
69         return ERR_DM_FAILED;
70     }
71     nlohmann::json authParamJson = nlohmann::json::parse(authParam, nullptr, false);
72     if (authParamJson.is_discarded()) {
73         LOGE("DecodeRequestAuth jsonStr error");
74         return ERR_DM_FAILED;
75     }
76     nlohmann::json authTokenJson = nlohmann::json::parse(authToken, nullptr, false);
77     if (authTokenJson.is_discarded()) {
78         LOGE("DecodeRequestAuth jsonStr error");
79         return ERR_DM_FAILED;
80     }
81     if (!IsInt32(authTokenJson, PIN_CODE_KEY)) {
82         LOGE("err authTokenJson string, first time");
83         return ERR_DM_FAILED;
84     }
85     if (!IsInt32(authParamJson, PIN_CODE_KEY)) {
86         LOGE("err authParamJson string, first time");
87         return ERR_DM_FAILED;
88     }
89     int32_t code = authTokenJson[PIN_CODE_KEY].get<int32_t>();
90     int32_t inputPinCode = authParamJson[PIN_CODE_KEY].get<int32_t>();
91     if (code == inputPinCode) {
92         return DM_OK;
93     } else if (times_ < MAX_VERIFY_TIMES) {
94         return ERR_DM_INPUT_PARA_INVALID;
95     } else {
96         return ERR_DM_FAILED;
97     }
98 }
99 } // namespace DistributedHardware
100 } // namespace OHOS
101