QXmpp  Version: 1.15.1
QXmppSaslManager_p.h
1 // SPDX-FileCopyrightText: 2012 Jeremy LainĂ© <jeremy.laine@m4x.org>
2 // SPDX-FileCopyrightText: 2024 Linus Jahn <lnj@kaidan.im>
3 //
4 // SPDX-License-Identifier: LGPL-2.1-or-later
5 
6 #ifndef QXMPPSASLMANAGER_P_H
7 #define QXMPPSASLMANAGER_P_H
8 
9 #include "QXmppAuthenticationError.h"
10 #include "QXmppOutgoingClient.h"
11 #include "QXmppPromise.h"
12 #include "QXmppSasl_p.h"
13 #include "QXmppTask.h"
14 
15 #include <optional>
16 
17 class QXmppConfiguration;
19 
20 namespace QXmpp::Private {
21 
22 class SendDataInterface;
23 
24 // Authentication using SASL
25 class SaslManager
26 {
27 public:
28  using AuthError = std::pair<QString, AuthenticationError>;
29  using AuthResult = std::variant<Success, AuthError>;
30  static constexpr QStringView TaskName = u"SASL 1 authentication";
31 
32  explicit SaslManager(SendDataInterface *socket) : m_socket(socket) { }
33 
34  QXmppTask<AuthResult> authenticate(const QXmppConfiguration &config, const QList<QString> &availableMechanisms, QXmppLoggable *parent);
35  HandleElementResult handleElement(const QDomElement &el);
36 
37 private:
38  SendDataInterface *m_socket;
39  std::unique_ptr<QXmppSaslClient> m_saslClient;
40  std::optional<QXmppPromise<AuthResult>> m_promise;
41 };
42 
43 // Authentication using SASL 2
44 class Sasl2Manager
45 {
46 public:
47  using AuthError = std::pair<QString, AuthenticationError>;
48  using AuthResult = std::variant<Sasl2::Success, AuthError>;
49  static constexpr QStringView TaskName = u"SASL 2 authentication";
50 
51  explicit Sasl2Manager(SendDataInterface *socket) : m_socket(socket) { }
52 
53  static bool hasAvailableMechanism(const QXmppConfiguration &config, const QList<QString> &mechanisms);
54 
55  QXmppTask<AuthResult> authenticate(Sasl2::Authenticate &&authenticate, const QXmppConfiguration &config, const Sasl2::StreamFeature &feature, QXmppLoggable *loggable);
56  HandleElementResult handleElement(const QDomElement &);
57  bool fastUsed() const { return m_fastUsed; }
58 
59 private:
60  struct State {
61  std::unique_ptr<QXmppSaslClient> sasl;
63  std::optional<Sasl2::Continue> unsupportedContinue;
64  };
65 
66  SendDataInterface *m_socket;
67  std::optional<State> m_state;
68  bool m_fastUsed = false;
69 };
70 
71 // Authentication token management
72 class FastTokenManager
73 {
74 public:
75  explicit FastTokenManager(QXmppConfiguration &config);
76 
77  static bool isFastEnabled(const QXmppConfiguration &);
78  bool hasToken() const;
79  void onSasl2Authenticate(Sasl2::Authenticate &auth, const Sasl2::StreamFeature &feature);
80  void onSasl2Success(const Sasl2::Success &success);
81  void onSasl2Failure();
82  bool fastFailed() const { return m_fastFailed; }
83  bool tokenChanged() const { return m_tokenChanged; }
84 
85 private:
86  QXmppConfiguration &config;
87  std::optional<SaslHtMechanism> requestedMechanism;
88  bool m_tokenChanged = false;
89  bool m_fastFailed = false;
90 };
91 
92 } // namespace QXmpp::Private
93 
94 #endif // QXMPPSASLMANAGER_P_H
The QXmppConfiguration class holds configuration options.
Definition: QXmppConfiguration.h:36
The QXmppStreamFeatures class represents the features returned by an XMPP server or client...
Definition: QXmppStreamFeatures.h:22
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:108
Definition: QXmppTask.h:67
Definition: Algorithms.h:14