aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp/auth/authservice.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2023-05-02 12:31:53 +0200
committerGitHub <[email protected]>2023-05-02 12:31:53 +0200
commite3086573d2244def22ecbe1e6b4b3da8b47e0f14 (patch)
tree627066debdddf7474783893f6b9b6631bb9a4833 /src/zenhttp/auth/authservice.cpp
parentmoved source directories into `/src` (#264) (diff)
downloadzen-e3086573d2244def22ecbe1e6b4b3da8b47e0f14.tar.xz
zen-e3086573d2244def22ecbe1e6b4b3da8b47e0f14.zip
move auth code from zenserver into zenhttp (#265)
this code should be usable outside of zenserver, so this moves it out into zenhttp where it can be used from lower level components
Diffstat (limited to 'src/zenhttp/auth/authservice.cpp')
-rw-r--r--src/zenhttp/auth/authservice.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/zenhttp/auth/authservice.cpp b/src/zenhttp/auth/authservice.cpp
new file mode 100644
index 000000000..6ed587770
--- /dev/null
+++ b/src/zenhttp/auth/authservice.cpp
@@ -0,0 +1,90 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include "zenhttp/auth/authservice.h"
+
+#include <zencore/compactbinarybuilder.h>
+#include <zencore/string.h>
+#include <zenhttp/auth/authmgr.h>
+
+ZEN_THIRD_PARTY_INCLUDES_START
+#include <json11.hpp>
+ZEN_THIRD_PARTY_INCLUDES_END
+
+namespace zen {
+
+using namespace std::literals;
+
+HttpAuthService::HttpAuthService(AuthMgr& AuthMgr) : m_AuthMgr(AuthMgr)
+{
+ m_Router.RegisterRoute(
+ "oidc/refreshtoken",
+ [this](HttpRouterRequest& RouterRequest) {
+ HttpServerRequest& ServerRequest = RouterRequest.ServerRequest();
+
+ const HttpContentType ContentType = ServerRequest.RequestContentType();
+
+ if ((ContentType == HttpContentType::kUnknownContentType || ContentType == HttpContentType::kJSON) == false)
+ {
+ return ServerRequest.WriteResponse(HttpResponseCode::BadRequest);
+ }
+
+ const IoBuffer Body = ServerRequest.ReadPayload();
+
+ std::string JsonText(reinterpret_cast<const char*>(Body.GetData()), Body.GetSize());
+ std::string JsonError;
+ json11::Json TokenInfo = json11::Json::parse(JsonText, JsonError);
+
+ if (!JsonError.empty())
+ {
+ CbObjectWriter Response;
+ Response << "Result"sv << false;
+ Response << "Error"sv << JsonError;
+
+ return ServerRequest.WriteResponse(HttpResponseCode::BadRequest, Response.Save());
+ }
+
+ const std::string RefreshToken = TokenInfo["RefreshToken"].string_value();
+ std::string ProviderName = TokenInfo["ProviderName"].string_value();
+
+ if (ProviderName.empty())
+ {
+ ProviderName = "Default"sv;
+ }
+
+ const bool Ok =
+ m_AuthMgr.AddOpenIdToken(AuthMgr::AddOpenIdTokenParams{.ProviderName = ProviderName, .RefreshToken = RefreshToken});
+
+ if (Ok)
+ {
+ ServerRequest.WriteResponse(Ok ? HttpResponseCode::OK : HttpResponseCode::BadRequest);
+ }
+ else
+ {
+ CbObjectWriter Response;
+ Response << "Result"sv << false;
+ Response << "Error"sv
+ << "Invalid token"sv;
+
+ ServerRequest.WriteResponse(HttpResponseCode::BadRequest, Response.Save());
+ }
+ },
+ HttpVerb::kPost);
+}
+
+HttpAuthService::~HttpAuthService()
+{
+}
+
+const char*
+HttpAuthService::BaseUri() const
+{
+ return "/auth/";
+}
+
+void
+HttpAuthService::HandleRequest(zen::HttpServerRequest& Request)
+{
+ m_Router.HandleRequest(Request);
+}
+
+} // namespace zen