blob: f89ca91dad4f4839eb906b0d9820a4f422a419b6 (
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
85
86
87
88
89
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(HttpResponseCode::OK);
}
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
|