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