qa: Replace Qt module includes by class ones
[quassel.git] / src / core / coreauthhandler.cpp
index 1d46e65..ef24592 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "coreauthhandler.h"
 
 
 #include "coreauthhandler.h"
 
+#include <QtEndian>
+
 #ifdef HAVE_SSL
 #    include <QSslSocket>
 #endif
 #ifdef HAVE_SSL
 #    include <QSslSocket>
 #endif
@@ -29,6 +31,7 @@
 CoreAuthHandler::CoreAuthHandler(QTcpSocket* socket, QObject* parent)
     : AuthHandler(parent)
     , _peer(nullptr)
 CoreAuthHandler::CoreAuthHandler(QTcpSocket* socket, QObject* parent)
     : AuthHandler(parent)
     , _peer(nullptr)
+    , _metricsServer(Core::instance()->metricsServer())
     , _magicReceived(false)
     , _legacy(false)
     , _clientRegistered(false)
     , _magicReceived(false)
     , _legacy(false)
     , _clientRegistered(false)
@@ -228,7 +231,17 @@ void CoreAuthHandler::handle(const Protocol::Login& msg)
 
     // First attempt local auth using the real username and password.
     // If that fails, move onto the auth provider.
 
     // First attempt local auth using the real username and password.
     // If that fails, move onto the auth provider.
-    UserId uid = Core::validateUser(msg.user, msg.password);
+
+    // Check to see if the user has the "Database" authenticator configured.
+    UserId uid = 0;
+    if (Core::getUserAuthenticator(msg.user) == "Database") {
+        uid = Core::validateUser(msg.user, msg.password);
+    }
+
+    // If they did not, *or* if the database login fails, try to use a different authenticator.
+    // TODO: this logic should likely be moved into Core::authenticateUser in the future.
+    // Right now a core can only have one authenticator configured; this might be something
+    // to change in the future.
     if (uid == 0) {
         uid = Core::authenticateUser(msg.user, msg.password);
     }
     if (uid == 0) {
         uid = Core::authenticateUser(msg.user, msg.password);
     }
@@ -237,9 +250,15 @@ void CoreAuthHandler::handle(const Protocol::Login& msg)
         qInfo() << qPrintable(tr("Invalid login attempt from %1 as \"%2\"").arg(socket()->peerAddress().toString(), msg.user));
         _peer->dispatch(Protocol::LoginFailed(tr(
             "<b>Invalid username or password!</b><br>The username/password combination you supplied could not be found in the database.")));
         qInfo() << qPrintable(tr("Invalid login attempt from %1 as \"%2\"").arg(socket()->peerAddress().toString(), msg.user));
         _peer->dispatch(Protocol::LoginFailed(tr(
             "<b>Invalid username or password!</b><br>The username/password combination you supplied could not be found in the database.")));
+        if (_metricsServer) {
+            _metricsServer->addLoginAttempt(msg.user, false);
+        }
         return;
     }
     _peer->dispatch(Protocol::LoginSuccess());
         return;
     }
     _peer->dispatch(Protocol::LoginSuccess());
+    if (_metricsServer) {
+        _metricsServer->addLoginAttempt(uid, true);
+    }
 
     qInfo() << qPrintable(tr("Client %1 initialized and authenticated successfully as \"%2\" (UserId: %3).")
                           .arg(socket()->peerAddress().toString(), msg.user, QString::number(uid.toInt())));
 
     qInfo() << qPrintable(tr("Client %1 initialized and authenticated successfully as \"%2\" (UserId: %3).")
                           .arg(socket()->peerAddress().toString(), msg.user, QString::number(uid.toInt())));