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
26namespace OHOS {
27namespace DistributedHardware {
28PinAuth::PinAuth()
29{
30    pinAuthUi_ = std::make_shared<PinAuthUi>();
31    LOGI("PinAuth constructor");
32}
33
34PinAuth::~PinAuth()
35{
36}
37
38int32_t PinAuth::ShowAuthInfo(std::string &authToken, std::shared_ptr<DmAuthManager> authManager)
39{
40    nlohmann::json jsonObject = nlohmann::json::parse(authToken, nullptr, false);
41    if (jsonObject.is_discarded()) {
42        LOGE("DecodeRequestAuth jsonStr error");
43        return ERR_DM_FAILED;
44    }
45    if (!IsInt32(jsonObject, PIN_CODE_KEY)) {
46        LOGE("err json string, first time");
47        return ERR_DM_FAILED;
48    }
49    return pinAuthUi_->ShowPinDialog(jsonObject[PIN_CODE_KEY].get<int32_t>(), authManager);
50}
51
52int32_t PinAuth::StartAuth(std::string &authToken, std::shared_ptr<DmAuthManager> authManager)
53{
54    return pinAuthUi_->InputPinDialog(authManager);
55}
56
57extern "C" IAuthentication *CreatePinAuthObject(void)
58{
59    return new PinAuth;
60}
61} // namespace DistributedHardware
62} // namespace OHOS
63