| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "network-web/oauthhttphandler.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | |
| 8 | #include <cctype> |
| 9 | |
| 10 | #include <QTcpSocket> |
| 11 | #include <QUrlQuery> |
| 12 | |
| 13 | OAuthHttpHandler::OAuthHttpHandler(const QString& success_text, QObject* parent) |
| 14 | : HttpServer(parent), m_successText(success_text) {} |
| 15 | |
| 16 | OAuthHttpHandler::~OAuthHttpHandler() {} |
| 17 | |
| 18 | void OAuthHttpHandler::handleRedirection(const QVariantMap& data) { |
| 19 | if (data.isEmpty()) { |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | const QString error = data.value(QSL("error")).toString(); |
| 24 | const QString code = data.value(QSL("code")).toString(); |
| 25 | const QString received_state = data.value(QSL("state")).toString(); |
| 26 | |
| 27 | if (error.size() != 0) { |
| 28 | const QString uri = data.value(QSL("error_uri")).toString(); |
| 29 | const QString description = data.value(QSL("error_description")).toString(); |
| 30 | |
| 31 | qCriticalNN << LOGSEC_OAUTH << "AuthenticationError: " << error << "(" << uri << "): " << description; |
| 32 | emit authRejected(description, received_state); |
| 33 | } |
| 34 | else if (code.isEmpty()) { |
| 35 | qCriticalNN << LOGSEC_OAUTH << "We did not receive authentication code."; |
| 36 | emit authRejected(QSL("Code not received"), received_state); |
| 37 | } |
| 38 | else if (received_state.isEmpty()) { |
| 39 | qCriticalNN << LOGSEC_OAUTH << "State not received."; |
| 40 | emit authRejected(QSL("State not received"), received_state); |
| 41 | } |
| 42 | else { |
| 43 | emit authGranted(code, received_state); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void OAuthHttpHandler::answerClient(QTcpSocket* socket, const HttpRequest& request) { |
| 48 | if (!request.m_url.path().remove(QL1C('/')).isEmpty()) { |
| 49 | qCriticalNN << LOGSEC_OAUTH << "Invalid request:" << QUOTE_W_SPACE_DOT(request.m_url.toString()); |
| 50 | } |
| 51 | else { |
| 52 | QVariantMap received_data; |
| 53 | const QUrlQuery query(request.m_url.query()); |
| 54 | const auto items = query.queryItems(); |
| 55 | |
| 56 | for (const auto& item : items) { |
| 57 | received_data.insert(item.first, item.second); |
| 58 | } |
| 59 | |
| 60 | handleRedirection(received_data); |
| 61 | |
| 62 | const QString html = QSL("<html><head><title>") + qApp->applicationName() + QSL("</title></head><body>") + |
| 63 | m_successText + QSL("</body></html>"); |
| 64 | const QByteArray html_utf = html.toUtf8(); |
| 65 | const QByteArray reply_message = QSL("HTTP/1.1 200 OK \r\n" |
| 66 | "Content-Type: text/html; charset=utf-8\r\n" |
| 67 | "Content-Length: %1\r\n" |
| 68 | "\r\n") |
| 69 | .arg(QString::number(html_utf.size())) |
| 70 | .toLocal8Bit() |
| 71 | .append(html_utf); |
| 72 | |
| 73 | socket->write(reply_message); |
| 74 | } |
| 75 | |
| 76 | socket->disconnectFromHost(); |
| 77 | } |