From 0bd78e41254a74daccd0a9209e5d4a0589ca20fc Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Wed, 22 Jan 2025 15:26:32 +0100 Subject: jupiter code cleanup (#276) * cleanup jupiter * move jupiter files to separate folder * CloudCache -> Jupiter * split up jupiter files * kill redundant JupiterAccessTokenProvider --- src/zenhttp/httpclientauth.cpp | 76 ++++++++++++++++++++++++++++ src/zenhttp/include/zenhttp/httpclientauth.h | 29 +++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/zenhttp/httpclientauth.cpp create mode 100644 src/zenhttp/include/zenhttp/httpclientauth.h (limited to 'src/zenhttp') diff --git a/src/zenhttp/httpclientauth.cpp b/src/zenhttp/httpclientauth.cpp new file mode 100644 index 000000000..04ac2ad3f --- /dev/null +++ b/src/zenhttp/httpclientauth.cpp @@ -0,0 +1,76 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include + +#include + +ZEN_THIRD_PARTY_INCLUDES_START +#include +#include +#include +ZEN_THIRD_PARTY_INCLUDES_END + +namespace zen { namespace httpclientauth { + + using namespace std::literals; + + std::function CreateFromStaticToken(HttpClientAccessToken Token) + { + return [Token]() { return Token; }; + } + + std::function CreateFromStaticToken(std::string_view Token) + { + return CreateFromStaticToken( + HttpClientAccessToken{.Value = fmt::format("Bearer {}"sv, Token), .ExpireTime = HttpClientAccessToken::TimePoint::max()}); + } + + std::function CreateFromOAuthClientCredentials(const OAuthClientCredentialsParams& Params) + { + OAuthClientCredentialsParams OAuthParams(Params); + return [OAuthParams]() { + using namespace std::chrono; + + std::string Body = fmt::format("client_id={}&scope=cache_access&grant_type=client_credentials&client_secret={}"sv, + OAuthParams.ClientId, + OAuthParams.ClientSecret); + + cpr::Response Response = cpr::Post(cpr::Url{OAuthParams.Url}, + cpr::Header{{"Content-Type", "application/x-www-form-urlencoded"}}, + cpr::Body{std::move(Body)}); + + if (Response.error || Response.status_code != 200) + { + return HttpClientAccessToken{}; + } + + std::string JsonError; + json11::Json Json = json11::Json::parse(Response.text, JsonError); + + if (JsonError.empty() == false) + { + return HttpClientAccessToken{}; + } + + std::string Token = Json["access_token"].string_value(); + int64_t ExpiresInSeconds = static_cast(Json["expires_in"].int_value()); + HttpClientAccessToken::TimePoint ExpireTime = HttpClientAccessToken::Clock::now() + seconds(ExpiresInSeconds); + + return HttpClientAccessToken{.Value = fmt::format("Bearer {}"sv, Token), .ExpireTime = ExpireTime}; + }; + } + + std::function CreateFromOpenIdProvider(AuthMgr& AuthManager, std::string_view OpenIdProvider) + { + return [&AuthManager = AuthManager, OpenIdProvider = std::string(OpenIdProvider)]() { + AuthMgr::OpenIdAccessToken Token = AuthManager.GetOpenIdAccessToken(OpenIdProvider); + return HttpClientAccessToken{.Value = Token.AccessToken, .ExpireTime = Token.ExpireTime}; + }; + } + + std::function CreateFromDefaultOpenIdProvider(AuthMgr& AuthManager) + { + return CreateFromOpenIdProvider(AuthManager, "Default"sv); + } + +}} // namespace zen::httpclientauth diff --git a/src/zenhttp/include/zenhttp/httpclientauth.h b/src/zenhttp/include/zenhttp/httpclientauth.h new file mode 100644 index 000000000..aa07620ca --- /dev/null +++ b/src/zenhttp/include/zenhttp/httpclientauth.h @@ -0,0 +1,29 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include + +namespace zen { + +class AuthMgr; + +namespace httpclientauth { + std::function CreateFromStaticToken(HttpClientAccessToken Token); + + std::function CreateFromStaticToken(std::string_view Token); + + struct OAuthClientCredentialsParams + { + std::string_view Url; + std::string_view ClientId; + std::string_view ClientSecret; + }; + + std::function CreateFromOAuthClientCredentials(const OAuthClientCredentialsParams& Params); + + std::function CreateFromOpenIdProvider(AuthMgr& AuthManager, std::string_view OpenIdProvider); + std::function CreateFromDefaultOpenIdProvider(AuthMgr& AuthManager); +} // namespace httpclientauth + +} // namespace zen -- cgit v1.2.3