File: otpprovider.cpp

package info (click to toggle)
plasma-pass 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 504 kB
  • sloc: cpp: 1,316; xml: 28; makefile: 2; sh: 1
file content (78 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "otpprovider.h"

#include <QUrl>
#include <QUrlQuery>
#include <QDebug>
#include <QDateTime>

#include <KLocalizedString>

#include <chrono>

#include <liboath/oath.h>

using namespace PlasmaPass;
using namespace std::chrono_literals;

namespace {

static const QString otpAuthSchema = QStringLiteral("otpauth://");
static const QString secretQueryItem = QStringLiteral("secret");

QString parseOtpType(const QUrl &url)
{
    return url.host();
}

} // namespace

OTPProvider::OTPProvider(const QString &path, QObject *parent)
    : ProviderBase(path, parent)
{
    setSecretTimeout(30s);
}

ProviderBase::HandlingResult OTPProvider::handleSecret(QStringView secret)
{
    if (!secret.startsWith(otpAuthSchema)) {
        return HandlingResult::Continue;
    }

    QUrl url(secret.toString());
    const auto otpType = parseOtpType(url);
    if (otpType == QLatin1String("totp")) {
        handleTOTP(url);
    } else {
        setError(i18n("Unsupported OTP type %1", otpType));
        return HandlingResult::Stop;
    }

    return HandlingResult::Stop;
}


void OTPProvider::handleTOTP(const QUrl &url)
{
    const QUrlQuery query(url.query());
    const auto secret = query.queryItemValue(secretQueryItem).toUtf8();

    char *decodedSecret = {};
    size_t decodedSecretLen = 0;
    oath_base32_decode(secret.data(), secret.size(), &decodedSecret, &decodedSecretLen);

    char output_otp[6] = {};
    oath_totp_generate(decodedSecret, decodedSecretLen,
            QDateTime::currentDateTime().toSecsSinceEpoch(),
            OATH_TOTP_DEFAULT_TIME_STEP_SIZE,
            OATH_TOTP_DEFAULT_START_TIME,
            6,
            output_otp);

    setSecret(QString::fromLatin1(output_otp, sizeof(output_otp)));
}

#include "moc_otpprovider.cpp"