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 
17 namespace KSaneCore
18 {
19 
20 static Authentication *s_instance = nullptr;
21 Q_GLOBAL_STATIC(QMutex, s_mutex)
22 
23 struct Authentication::Private {
24  struct AuthStruct {
25  QString resource;
26  QString username;
27  QString password;
28  };
29 
30  QList<AuthStruct> authList;
31 };
32 
33 Authentication *Authentication::getInstance()
34 {
35 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
36  QMutexLocker<QMutex> locker(s_mutex);
37 #else
38  QMutexLocker locker(s_mutex);
39 #endif
40 
41  if (s_instance == nullptr) {
42  s_instance = new Authentication();
43  }
44  return s_instance;
45 }
46 
47 Authentication::Authentication() : d(new Private) {}
48 
49 Authentication::~Authentication()
50 {
51 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
52  QMutexLocker<QMutex> locker(s_mutex);
53 #else
54  QMutexLocker locker(s_mutex);
55 #endif
56  d->authList.clear();
57  delete d;
58 }
59 
60 void Authentication::setDeviceAuth(const QString &resource, const QString &username, const QString &password)
61 {
62  // This is a short list so we do not need a QMap...
63  int i;
64  for (i = 0; i < d->authList.size(); i++) {
65  if (resource == d->authList.at(i).resource) {
66  // update the existing node
67  d->authList[i].username = username;
68  d->authList[i].password = password;
69  break;
70  }
71  }
72  if (i == d->authList.size()) {
73  // Add a new list node
74  Private::AuthStruct tmp;
75  tmp.resource = resource;
76  tmp.username = username;
77  tmp.password = password;
78  d->authList << tmp;
79  }
80 }
81 
82 void Authentication::clearDeviceAuth(const QString &resource)
83 {
84  // This is a short list so we do not need a QMap...
85  for (int i = 0; i < d->authList.size(); i++) {
86  if (resource == d->authList.at(i).resource) {
87  d->authList.removeAt(i);
88  return;
89  }
90  }
91 }
92 
93 /** static function called by sane_open to get authorization from user */
94 void Authentication::authorization(SANE_String_Const resource, SANE_Char *username, SANE_Char *password)
95 {
96  qCDebug(KSANECORE_LOG) << resource;
97  // This is vague in the standard... what can I find in the resource string?
98  // I have found that "resource contains the backend name + "$MD5$....."
99  // it does not contain unique identifiers like ":libusb:001:004"
100  // -> remove $MD5 and later before comparison...
101  QString res = QString::fromUtf8(resource);
102  int end = res.indexOf(QStringLiteral("$MD5$"));
103  res = res.left(end);
104  qCDebug(KSANECORE_LOG) << res;
105 
106  const QList<Private::AuthStruct> list = getInstance()->d->authList;
107  for (const auto &authItem : list) {
108  qCDebug(KSANECORE_LOG) << res << authItem.resource;
109  if (authItem.resource.contains(res)) {
110  qstrncpy(username, authItem.username.toLocal8Bit().constData(), SANE_MAX_USERNAME_LEN);
111  qstrncpy(password, authItem.password.toLocal8Bit().constData(), SANE_MAX_PASSWORD_LEN);
112  break;
113  }
114  }
115 }
116 
117 } // namespace KSaneCore
QString fromUtf8(const char *str, int size)
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString left(int n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 03:58:29 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.