aboutsummaryrefslogtreecommitdiff
path: root/zenserver/auth/authservice.cpp
blob: ac77b237fc06903ea4bf22abf2152308726d8721 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright Epic Games, Inc. All Rights Reserved.

#include <auth/authmgr.h>
#include <auth/authservice.h>

#include <zencore/compactbinarybuilder.h>
#include <zencore/string.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 IdentityToken = TokenInfo["IdentityToken"].string_value();
			const std::string RefreshToken	= TokenInfo["RefreshToken"].string_value();

			const bool Ok = m_AuthMgr.AddOpenIdToken(AuthMgr::AddOpenIdTokenParams{.ProviderName = "Okta"sv, .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