Skip to content

Commit

Permalink
Fast: Add FastTokenManager to OutgoingClient
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX committed May 23, 2024
1 parent 2a88769 commit c29537e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/client/QXmppOutgoingClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ QXmppOutgoingClientPrivate::QXmppOutgoingClientPrivate(QXmppOutgoingClient *qq)
streamAckManager(socket),
iqManager(qq, streamAckManager),
listener(qq),
fastTokenManager(config),
c2sStreamManager(qq),
csiManager(qq),
pingManager(qq),
Expand Down Expand Up @@ -310,6 +311,7 @@ void QXmppOutgoingClient::startSasl2Auth(const Sasl2::StreamFeature &sasl2Featur
sasl2Request.bindRequest = createBind2Request(sasl2Feature.bind2Feature->features);
}
// other extensions
d->fastTokenManager.onSasl2Authenticate(sasl2Request, sasl2Feature);
d->c2sStreamManager.onSasl2Authenticate(sasl2Request, sasl2Feature);

// start authentication
Expand All @@ -321,6 +323,7 @@ void QXmppOutgoingClient::startSasl2Auth(const Sasl2::StreamFeature &sasl2Featur
d->bind2Bound = std::move(success->bound);

// extensions
d->fastTokenManager.onSasl2Success(*success);
d->c2sStreamManager.onSasl2Success(*success);
if (d->bind2Bound) {
d->c2sStreamManager.onBind2Bound(*d->bind2Bound);
Expand Down
1 change: 1 addition & 0 deletions src/client/QXmppOutgoingClient_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class QXmppOutgoingClientPrivate
std::optional<Bind2Bound> bind2Bound;

std::variant<QXmppOutgoingClient *, StarttlsManager, NonSaslAuthManager, SaslManager, Sasl2Manager, C2sStreamManager *, BindManager> listener;
FastTokenManager fastTokenManager;
C2sStreamManager c2sStreamManager;
CarbonManager carbonManager;
CsiManager csiManager;
Expand Down
50 changes: 50 additions & 0 deletions src/client/QXmppSaslManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "QXmppConfiguration.h"
#include "QXmppConstants_p.h"
#include "QXmppCredentials.h"
#include "QXmppFutureUtils_p.h"
#include "QXmppSasl2UserAgent.h"
#include "QXmppSaslManager_p.h"
Expand All @@ -23,6 +24,7 @@

using namespace std::placeholders;
namespace views = std::views;
using std::ranges::empty;
using std::ranges::max;

namespace QXmpp::Private {
Expand Down Expand Up @@ -292,4 +294,52 @@ HandleElementResult Sasl2Manager::handleElement(const QDomElement &el)
return Rejected;
}

FastTokenManager::FastTokenManager(QXmppConfiguration &config)
: config(config)
{
}

bool FastTokenManager::enabledLocally() const
{
return config.sasl2UserAgent().has_value();
}

bool FastTokenManager::hasToken() const
{
return config.credentialData().htToken.has_value();
}

void FastTokenManager::onSasl2Authenticate(Sasl2::Authenticate &auth, const Sasl2::StreamFeature &feature)
{
auto selectMechanism = [](const auto &availableMechanisms) {
// find mechanisms supported by us
auto mechanisms = availableMechanisms |
views::transform(&SaslHtMechanism::fromString) |
views::filter([](const auto &v) { return v.has_value(); }) |
views::transform([](const auto &v) { return *v; }) |
views::filter([](const auto &m) { return m.channelBindingType == SaslHtMechanism::None; });

return empty(mechanisms) ? std::optional<SaslHtMechanism>() : max(mechanisms);
};

if (feature.fast && enabledLocally() && !hasToken()) {
// request token
if (auto mechanism = selectMechanism(feature.fast->mechanisms)) {
auth.tokenRequest = FastTokenRequest { mechanism->toString() };
}
}
}

void FastTokenManager::onSasl2Success(const Sasl2::Success &success)
{
if (success.token) {
config.credentialData().htToken = HtToken {
// FIXME mechanism
SaslHtMechanism { IanaHashAlgorithm::Sha256, SaslHtMechanism::None },
success.token->token,
success.token->expiry,
};
}
}

} // namespace QXmpp::Private
15 changes: 15 additions & 0 deletions src/client/QXmppSaslManager_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ class Sasl2Manager
std::optional<State> m_state;
};

// Authentication token management
class FastTokenManager
{
public:
explicit FastTokenManager(QXmppConfiguration &config);

bool enabledLocally() const;
bool hasToken() const;
void onSasl2Authenticate(Sasl2::Authenticate &auth, const Sasl2::StreamFeature &feature);
void onSasl2Success(const Sasl2::Success &success);

private:
QXmppConfiguration &config;
};

} // namespace QXmpp::Private

#endif // QXMPPSASLMANAGER_P_H

0 comments on commit c29537e

Please sign in to comment.