KSaneCore

authentication.cpp
1/*
2 * SPDX-FileCopyrightText: 2010 Kare Sars <kare dot sars at iki dot fi>
3 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "authentication.h"
9
10// Qt includes
11#include <QMutex>
12#include <QMutexLocker>
13#include <QList>
14
15#include <ksanecore_debug.h>
16
17namespace KSaneCore
18{
19
20static Authentication *s_instance = nullptr;
21Q_GLOBAL_STATIC(QMutex, s_mutex)
22
23struct Authentication::Private {
24 struct AuthStruct {
25 QString resource;
26 QString username;
27 QString password;
28 };
29
30 QList<AuthStruct> authList;
31};
32
33Authentication *Authentication::getInstance()
34{
35 QMutexLocker<QMutex> locker(s_mutex);
36
37 if (s_instance == nullptr) {
38 s_instance = new Authentication();
39 }
40 return s_instance;
41}
42
43Authentication::Authentication() : d(new Private) {}
44
45Authentication::~Authentication()
46{
47 QMutexLocker<QMutex> locker(s_mutex);
48 d->authList.clear();
49 delete d;
50}
51
52void Authentication::setDeviceAuth(const QString &resource, const QString &username, const QString &password)
53{
54 // This is a short list so we do not need a QMap...
55 int i;
56 for (i = 0; i < d->authList.size(); i++) {
57 if (resource == d->authList.at(i).resource) {
58 // update the existing node
59 d->authList[i].username = username;
60 d->authList[i].password = password;
61 break;
62 }
63 }
64 if (i == d->authList.size()) {
65 // Add a new list node
66 Private::AuthStruct tmp;
67 tmp.resource = resource;
68 tmp.username = username;
69 tmp.password = password;
70 d->authList << tmp;
71 }
72}
73
74void Authentication::clearDeviceAuth(const QString &resource)
75{
76 // This is a short list so we do not need a QMap...
77 for (int i = 0; i < d->authList.size(); i++) {
78 if (resource == d->authList.at(i).resource) {
79 d->authList.removeAt(i);
80 return;
81 }
82 }
83}
84
85/** static function called by sane_open to get authorization from user */
86void Authentication::authorization(SANE_String_Const resource, SANE_Char *username, SANE_Char *password)
87{
88 qCDebug(KSANECORE_LOG) << resource;
89 // This is vague in the standard... what can I find in the resource string?
90 // I have found that "resource contains the backend name + "$MD5$....."
91 // it does not contain unique identifiers like ":libusb:001:004"
92 // -> remove $MD5 and later before comparison...
93 QString res = QString::fromUtf8(resource);
94 int end = res.indexOf(QStringLiteral("$MD5$"));
95 res = res.left(end);
96 qCDebug(KSANECORE_LOG) << res;
97
98 const QList<Private::AuthStruct> list = getInstance()->d->authList;
99 for (const auto &authItem : list) {
100 qCDebug(KSANECORE_LOG) << res << authItem.resource;
101 if (authItem.resource.contains(res)) {
102 qstrncpy(username, authItem.username.toLocal8Bit().constData(), SANE_MAX_USERNAME_LEN);
103 qstrncpy(password, authItem.password.toLocal8Bit().constData(), SANE_MAX_PASSWORD_LEN);
104 break;
105 }
106 }
107}
108
109} // namespace KSaneCore
QString fromUtf8(QByteArrayView str)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString left(qsizetype n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:57:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.