summaryrefslogtreecommitdiffstats
path: root/src/web_service
diff options
context:
space:
mode:
authorbunnei <bunneidev@gmail.com>2019-11-09 09:09:39 +0100
committerbunnei <bunneidev@gmail.com>2019-11-09 20:00:44 +0100
commit8dc9f35baf7e0338ae611a95c11b628e8edc00dd (patch)
tree9d8795e81c2c029a475da39758fb8a36ff3dd4f9 /src/web_service
parentyuzu: configure_web: Use Base64 encoded token for simplifying user experience. (diff)
downloadyuzu-8dc9f35baf7e0338ae611a95c11b628e8edc00dd.tar
yuzu-8dc9f35baf7e0338ae611a95c11b628e8edc00dd.tar.gz
yuzu-8dc9f35baf7e0338ae611a95c11b628e8edc00dd.tar.bz2
yuzu-8dc9f35baf7e0338ae611a95c11b628e8edc00dd.tar.lz
yuzu-8dc9f35baf7e0338ae611a95c11b628e8edc00dd.tar.xz
yuzu-8dc9f35baf7e0338ae611a95c11b628e8edc00dd.tar.zst
yuzu-8dc9f35baf7e0338ae611a95c11b628e8edc00dd.zip
Diffstat (limited to '')
-rw-r--r--src/web_service/web_backend.cpp52
-rw-r--r--src/web_service/web_backend.h23
2 files changed, 57 insertions, 18 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index dc149d2ed..6683f459f 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -2,10 +2,12 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <array>
#include <cstdlib>
#include <mutex>
#include <string>
#include <LUrlParser.h>
+#include <fmt/format.h>
#include <httplib.h>
#include "common/common_types.h"
#include "common/logging/log.h"
@@ -16,10 +18,10 @@ namespace WebService {
constexpr std::array<const char, 1> API_VERSION{'1'};
-constexpr u32 HTTP_PORT = 80;
-constexpr u32 HTTPS_PORT = 443;
+constexpr int HTTP_PORT = 80;
+constexpr int HTTPS_PORT = 443;
-constexpr u32 TIMEOUT_SECONDS = 30;
+constexpr std::size_t TIMEOUT_SECONDS = 30;
struct Client::Impl {
Impl(std::string host, std::string username, std::string token)
@@ -31,8 +33,9 @@ struct Client::Impl {
}
/// A generic function handles POST, GET and DELETE request together
- Common::WebResult GenericJson(const std::string& method, const std::string& path,
- const std::string& data, bool allow_anonymous) {
+ Common::WebResult GenericRequest(const std::string& method, const std::string& path,
+ const std::string& data, bool allow_anonymous,
+ const std::string& accept) {
if (jwt.empty()) {
UpdateJWT();
}
@@ -43,11 +46,11 @@ struct Client::Impl {
"Credentials needed"};
}
- auto result = GenericJson(method, path, data, jwt);
+ auto result = GenericRequest(method, path, data, accept, jwt);
if (result.result_string == "401") {
// Try again with new JWT
UpdateJWT();
- result = GenericJson(method, path, data, jwt);
+ result = GenericRequest(method, path, data, accept, jwt);
}
return result;
@@ -56,12 +59,13 @@ struct Client::Impl {
/**
* A generic function with explicit authentication method specified
* JWT is used if the jwt parameter is not empty
- * username + token is used if jwt is empty but username and token are not empty
- * anonymous if all of jwt, username and token are empty
+ * username + token is used if jwt is empty but username and token are
+ * not empty anonymous if all of jwt, username and token are empty
*/
- Common::WebResult GenericJson(const std::string& method, const std::string& path,
- const std::string& data, const std::string& jwt = "",
- const std::string& username = "", const std::string& token = "") {
+ Common::WebResult GenericRequest(const std::string& method, const std::string& path,
+ const std::string& data, const std::string& accept,
+ const std::string& jwt = "", const std::string& username = "",
+ const std::string& token = "") {
if (cli == nullptr) {
auto parsedUrl = LUrlParser::clParseURL::ParseURL(host);
int port;
@@ -132,8 +136,7 @@ struct Client::Impl {
return Common::WebResult{Common::WebResult::Code::WrongContent, ""};
}
- if (content_type->second.find("application/json") == std::string::npos &&
- content_type->second.find("text/html; charset=utf-8") == std::string::npos) {
+ if (content_type->second.find(accept) == std::string::npos) {
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
content_type->second);
return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content"};
@@ -147,7 +150,7 @@ struct Client::Impl {
return;
}
- auto result = GenericJson("POST", "/jwt/internal", "", "", username, token);
+ auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token);
if (result.result_code != Common::WebResult::Code::Success) {
LOG_ERROR(WebService, "UpdateJWT failed");
} else {
@@ -180,16 +183,29 @@ Client::~Client() = default;
Common::WebResult Client::PostJson(const std::string& path, const std::string& data,
bool allow_anonymous) {
- return impl->GenericJson("POST", path, data, allow_anonymous);
+ return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json");
}
Common::WebResult Client::GetJson(const std::string& path, bool allow_anonymous) {
- return impl->GenericJson("GET", path, "", allow_anonymous);
+ return impl->GenericRequest("GET", path, "", allow_anonymous, "application/json");
}
Common::WebResult Client::DeleteJson(const std::string& path, const std::string& data,
bool allow_anonymous) {
- return impl->GenericJson("DELETE", path, data, allow_anonymous);
+ return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json");
+}
+
+Common::WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) {
+ return impl->GenericRequest("GET", path, "", allow_anonymous, "text/plain");
+}
+
+Common::WebResult Client::GetImage(const std::string& path, bool allow_anonymous) {
+ return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png");
+}
+
+Common::WebResult Client::GetExternalJWT(const std::string& audience) {
+ return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false,
+ "text/html");
}
} // namespace WebService
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h
index c637e09df..04121f17e 100644
--- a/src/web_service/web_backend.h
+++ b/src/web_service/web_backend.h
@@ -46,6 +46,29 @@ public:
Common::WebResult DeleteJson(const std::string& path, const std::string& data,
bool allow_anonymous);
+ /**
+ * Gets a plain string from the specified path.
+ * @param path the URL segment after the host address.
+ * @param allow_anonymous If true, allow anonymous unauthenticated requests.
+ * @return the result of the request.
+ */
+ Common::WebResult GetPlain(const std::string& path, bool allow_anonymous);
+
+ /**
+ * Gets an PNG image from the specified path.
+ * @param path the URL segment after the host address.
+ * @param allow_anonymous If true, allow anonymous unauthenticated requests.
+ * @return the result of the request.
+ */
+ Common::WebResult GetImage(const std::string& path, bool allow_anonymous);
+
+ /**
+ * Requests an external JWT for the specific audience provided.
+ * @param audience the audience of the JWT requested.
+ * @return the result of the request.
+ */
+ Common::WebResult GetExternalJWT(const std::string& audience);
+
private:
struct Impl;
std::unique_ptr<Impl> impl;