Libkleo

keyserverconfig.cpp
1 /*
2  kleo/keyserverconfig.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  SPDX-FileCopyrightText: 2021 g10 Code GmbH
6  SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
7 
8  SPDX-License-Identifier: GPL-2.0-or-later
9 */
10 
11 #include <config-libkleo.h>
12 
13 #include "keyserverconfig.h"
14 
15 #include <libkleo/algorithm.h>
16 
17 #include <QString>
18 #include <QUrl>
19 
20 using namespace Kleo;
21 
22 class KeyserverConfig::Private
23 {
24 public:
25  explicit Private();
26 
27  QString host;
28  int port = -1; // -1 == use default port
29  KeyserverAuthentication authentication = KeyserverAuthentication::Anonymous;
30  QString user;
31  QString password;
32  KeyserverConnection connection = KeyserverConnection::Default;
33  QString baseDn;
34  QStringList additionalFlags;
35 };
36 
37 KeyserverConfig::Private::Private()
38 {
39 }
40 
41 KeyserverConfig::KeyserverConfig()
42  : d{std::make_unique<Private>()}
43 {
44 }
45 
46 KeyserverConfig::~KeyserverConfig() = default;
47 
48 KeyserverConfig::KeyserverConfig(const KeyserverConfig &other)
49  : d{std::make_unique<Private>(*other.d)}
50 {
51 }
52 
53 KeyserverConfig &KeyserverConfig::operator=(const KeyserverConfig &other)
54 {
55  *d = *other.d;
56  return *this;
57 }
58 
59 KeyserverConfig::KeyserverConfig(KeyserverConfig &&other) = default;
60 
61 KeyserverConfig &KeyserverConfig::operator=(KeyserverConfig &&other) = default;
62 
63 KeyserverConfig KeyserverConfig::fromUrl(const QUrl &url)
64 {
65  KeyserverConfig config;
66 
67  config.d->host = url.host();
68  config.d->port = url.port();
69  config.d->user = url.userName();
70  config.d->password = url.password();
71  if (!config.d->user.isEmpty()) {
72  config.d->authentication = KeyserverAuthentication::Password;
73  }
74  if (url.hasFragment()) {
75  const auto flags = transformInPlace(url.fragment().split(QLatin1Char{','}, Qt::SkipEmptyParts), [](const auto &flag) {
76  return flag.trimmed().toLower();
77  });
78  for (const auto &flag : flags) {
79  if (flag == QLatin1StringView{"starttls"}) {
80  config.d->connection = KeyserverConnection::UseSTARTTLS;
81  } else if (flag == QLatin1StringView{"ldaptls"}) {
82  config.d->connection = KeyserverConnection::TunnelThroughTLS;
83  } else if (flag == QLatin1StringView{"plain"}) {
84  config.d->connection = KeyserverConnection::Plain;
85  } else if (flag == QLatin1StringView{"ntds"}) {
86  config.d->authentication = KeyserverAuthentication::ActiveDirectory;
87  } else {
88  config.d->additionalFlags.push_back(flag);
89  }
90  }
91  }
92  if (url.hasQuery()) {
93  config.d->baseDn = url.query();
94  }
95 
96  return config;
97 }
98 
99 QUrl KeyserverConfig::toUrl() const
100 {
101  QUrl url;
102 
103  url.setScheme(QStringLiteral("ldap"));
104  // set host to empty string if it's a null string; this ensures that the URL has an authority and always gets a "//" after the scheme
105  url.setHost(d->host.isNull() ? QStringLiteral("") : d->host);
106  if (d->port != -1) {
107  url.setPort(d->port);
108  }
109  if (!d->user.isEmpty()) {
110  url.setUserName(d->user);
111  }
112  if (!d->password.isEmpty()) {
113  url.setPassword(d->password);
114  }
115  if (!d->baseDn.isEmpty()) {
116  url.setQuery(d->baseDn);
117  }
118 
119  QStringList flags;
120  switch (d->connection) {
121  case KeyserverConnection::UseSTARTTLS:
122  flags.push_back(QStringLiteral("starttls"));
123  break;
124  case KeyserverConnection::TunnelThroughTLS:
125  flags.push_back(QStringLiteral("ldaptls"));
126  break;
127  case KeyserverConnection::Plain:
128  flags.push_back(QStringLiteral("plain"));
129  break;
130  case KeyserverConnection::Default:; // omit connection flag to use default
131  }
132  if (d->authentication == KeyserverAuthentication::ActiveDirectory) {
133  flags.push_back(QStringLiteral("ntds"));
134  }
135  std::copy(std::cbegin(d->additionalFlags), std::cend(d->additionalFlags), std::back_inserter(flags));
136  if (!flags.isEmpty()) {
137  url.setFragment(flags.join(QLatin1Char{','}));
138  }
139 
140  return url;
141 }
142 
143 QString KeyserverConfig::host() const
144 {
145  return d->host;
146 }
147 
148 void KeyserverConfig::setHost(const QString &host)
149 {
150  d->host = host;
151 }
152 
153 int KeyserverConfig::port() const
154 {
155  return d->port;
156 }
157 
158 void KeyserverConfig::setPort(int port)
159 {
160  d->port = port;
161 }
162 
163 KeyserverAuthentication KeyserverConfig::authentication() const
164 {
165  return d->authentication;
166 }
167 
168 void KeyserverConfig::setAuthentication(KeyserverAuthentication authentication)
169 {
170  d->authentication = authentication;
171 }
172 
173 QString KeyserverConfig::user() const
174 {
175  return d->user;
176 }
177 
178 void KeyserverConfig::setUser(const QString &user)
179 {
180  d->user = user;
181 }
182 
183 QString KeyserverConfig::password() const
184 {
185  return d->password;
186 }
187 
188 void KeyserverConfig::setPassword(const QString &password)
189 {
190  d->password = password;
191 }
192 
193 KeyserverConnection KeyserverConfig::connection() const
194 {
195  return d->connection;
196 }
197 
198 void KeyserverConfig::setConnection(KeyserverConnection connection)
199 {
200  d->connection = connection;
201 }
202 
203 QString KeyserverConfig::ldapBaseDn() const
204 {
205  return d->baseDn;
206 }
207 
208 void KeyserverConfig::setLdapBaseDn(const QString &baseDn)
209 {
210  d->baseDn = baseDn;
211 }
212 
213 QStringList KeyserverConfig::additionalFlags() const
214 {
215  return d->additionalFlags;
216 }
217 
218 void KeyserverConfig::setAdditionalFlags(const QStringList &flags)
219 {
220  d->additionalFlags = flags;
221 }
bool hasQuery() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString userName(QUrl::ComponentFormattingOptions options) const const
void push_back(const T &value)
QString query(QUrl::ComponentFormattingOptions options) const const
void setHost(const QString &host, QUrl::ParsingMode mode)
bool hasFragment() const const
void setScheme(const QString &scheme)
SkipEmptyParts
void setPort(int port)
bool isEmpty() const const
QString join(const QString &separator) const const
void setQuery(const QString &query, QUrl::ParsingMode mode)
QString host(QUrl::ComponentFormattingOptions options) const const
void setPassword(const QString &password, QUrl::ParsingMode mode)
QString fragment(QUrl::ComponentFormattingOptions options) const const
int port(int defaultPort) const const
void setFragment(const QString &fragment, QUrl::ParsingMode mode)
QString password(QUrl::ComponentFormattingOptions options) const const
void setUserName(const QString &userName, QUrl::ParsingMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:56:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.