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
20using namespace Kleo;
21
22class KeyserverConfig::Private
23{
24public:
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
37KeyserverConfig::Private::Private()
38{
39}
40
41KeyserverConfig::KeyserverConfig()
42 : d{std::make_unique<Private>()}
43{
44}
45
46KeyserverConfig::~KeyserverConfig() = default;
47
48KeyserverConfig::KeyserverConfig(const KeyserverConfig &other)
49 : d{std::make_unique<Private>(*other.d)}
50{
51}
52
53KeyserverConfig &KeyserverConfig::operator=(const KeyserverConfig &other)
54{
55 *d = *other.d;
56 return *this;
57}
58
59KeyserverConfig::KeyserverConfig(KeyserverConfig &&other) = default;
60
61KeyserverConfig &KeyserverConfig::operator=(KeyserverConfig &&other) = default;
62
63KeyserverConfig 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
99QUrl 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
143QString KeyserverConfig::host() const
144{
145 return d->host;
146}
147
148void KeyserverConfig::setHost(const QString &host)
149{
150 d->host = host;
151}
152
153int KeyserverConfig::port() const
154{
155 return d->port;
156}
157
158void KeyserverConfig::setPort(int port)
159{
160 d->port = port;
161}
162
163KeyserverAuthentication KeyserverConfig::authentication() const
164{
165 return d->authentication;
166}
167
168void KeyserverConfig::setAuthentication(KeyserverAuthentication authentication)
169{
170 d->authentication = authentication;
171}
172
173QString KeyserverConfig::user() const
174{
175 return d->user;
176}
177
178void KeyserverConfig::setUser(const QString &user)
179{
180 d->user = user;
181}
182
183QString KeyserverConfig::password() const
184{
185 return d->password;
186}
187
188void KeyserverConfig::setPassword(const QString &password)
189{
190 d->password = password;
191}
192
193KeyserverConnection KeyserverConfig::connection() const
194{
195 return d->connection;
196}
197
198void KeyserverConfig::setConnection(KeyserverConnection connection)
199{
200 d->connection = connection;
201}
202
203QString KeyserverConfig::ldapBaseDn() const
204{
205 return d->baseDn;
206}
207
208void KeyserverConfig::setLdapBaseDn(const QString &baseDn)
209{
210 d->baseDn = baseDn;
211}
212
213QStringList KeyserverConfig::additionalFlags() const
214{
215 return d->additionalFlags;
216}
217
218void KeyserverConfig::setAdditionalFlags(const QStringList &flags)
219{
220 d->additionalFlags = flags;
221}
bool isEmpty() const const
void push_back(parameter_type value)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString join(QChar separator) const const
SkipEmptyParts
QString fragment(ComponentFormattingOptions options) const const
bool hasFragment() const const
bool hasQuery() const const
QString host(ComponentFormattingOptions options) const const
QString password(ComponentFormattingOptions options) const const
int port(int defaultPort) const const
QString query(ComponentFormattingOptions options) const const
void setFragment(const QString &fragment, ParsingMode mode)
void setHost(const QString &host, ParsingMode mode)
void setPassword(const QString &password, ParsingMode mode)
void setPort(int port)
void setQuery(const QString &query, ParsingMode mode)
void setScheme(const QString &scheme)
void setUserName(const QString &userName, ParsingMode mode)
QString userName(ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.