diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 03941e0..8cbfd28 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,4 +1,5 @@ set(EXAMPLES + auth chatserver fileserver ) diff --git a/examples/auth/CMakeLists.txt b/examples/auth/CMakeLists.txt new file mode 100644 index 0000000..47d1b8e --- /dev/null +++ b/examples/auth/CMakeLists.txt @@ -0,0 +1,7 @@ +# Client +add_executable(authclient client.cpp) +target_link_libraries(authclient QHttpEngine) +target_compile_features(authclient PRIVATE cxx_lambdas) +install(TARGETS authclient + RUNTIME DESTINATION "${EXAMPLE_DIR}" +) diff --git a/examples/auth/client.cpp b/examples/auth/client.cpp new file mode 100644 index 0000000..42d360b --- /dev/null +++ b/examples/auth/client.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2015 Nathan Osman + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +int main(int argc, char * argv[]) +{ + QCoreApplication a(argc, argv); + + // Attempt to open the local file and read from it + QLocalFile file; + if (!file.open()) { + qCritical("Unable to open local file - is server running?"); + return 1; + } + + // Parse the JSON to get the port and token + QJsonObject obj = QJsonDocument::fromJson(file.readAll()).object(); + if (!obj.contains("port") || !obj.contains("token")) { + qCritical("Malformed JSON in local file."); + return 1; + } + + // Create a request to the server, using the provided port and passing the + // auth token as a custom HTTP header + QUrl url(QString("http://127.0.0.1:%1/").arg(obj.value("port").toInt())); + QNetworkRequest request(url); + request.setRawHeader("X-Auth-Token", obj.value("token").toString().toUtf8()); + + // Send the request + QNetworkAccessManager manager; + QNetworkReply *reply = manager.get(request); + + // Check the response + QObject::connect(reply, &QNetworkReply::finished, [&a, reply]() { + if (reply->error() == QNetworkReply::NoError) { + qInfo("Successfully authenticated to server."); + a.exit(); + } else { + qCritical("Error: %s", reply->errorString().toUtf8().constData()); + a.exit(1); + } + }); + + return a.exec(); +}