File indexing completed on 2024-04-28 05:19:53

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "fakeauthbrowser.h"
0008 
0009 #include <QDesktopServices>
0010 #include <QHostAddress>
0011 #include <QObject>
0012 #include <QTcpSocket>
0013 #include <QTimer>
0014 
0015 extern Q_DECL_IMPORT uint16_t kgapiTcpAuthServerPort; // defined in authjob.cpp
0016 
0017 class FakeAuthBrowser::Private : public QObject
0018 {
0019     Q_OBJECT
0020 public:
0021     explicit Private()
0022     {
0023         kgapiTcpAuthServerPort = 42413;
0024     }
0025 
0026 public Q_SLOTS:
0027     void openUrl(const QUrl &url)
0028     {
0029         Q_UNUSED(url)
0030 
0031         // don't do anything, don't even try to load Google auth page. Instead
0032         // pretend the user have already authenticated and we've reached the
0033         // part where Google sends us the auth code
0034 
0035         QTimer::singleShot(0, this, [=]() {
0036             QTcpSocket socket;
0037             socket.connectToHost(QHostAddress::LocalHost, kgapiTcpAuthServerPort);
0038             if (!socket.waitForConnected()) {
0039                 qWarning() << "Failed to connect to internal TCP server!";
0040                 return;
0041             }
0042             socket.write("GET http://127.0.0.1:42413?code=TheCakeIsALie HTTP/1.1");
0043             socket.waitForBytesWritten();
0044             socket.close();
0045         });
0046     }
0047 };
0048 
0049 FakeAuthBrowser::FakeAuthBrowser()
0050     : d(new FakeAuthBrowser::Private)
0051 {
0052     QDesktopServices::setUrlHandler(QStringLiteral("https"), d.get(), "openUrl");
0053 }
0054 
0055 FakeAuthBrowser::~FakeAuthBrowser()
0056 {
0057     QDesktopServices::unsetUrlHandler(QStringLiteral("https"));
0058 }
0059 
0060 #include "fakeauthbrowser.moc"