4#include "Stroika/Frameworks/StroikaPreComp.h"
7#include "Stroika/Foundation/Containers/Association.h"
13#include "Stroika/Foundation/IO/Network/Transfer/Connection.h"
26using namespace Stroika::Frameworks;
27using namespace Stroika::Frameworks::Auth::OAuth;
36String TokenRequest::ToString ()
const
42 sb <<
", code: "sv <<
code;
43 sb <<
", grant_type: authorization_code"sv;
47 sb <<
", grant_type: refresh_token"sv;
78TypedBLOB TokenRequest::ToWireFormat ()
const
85 static const auto kExcept_ =
RuntimeErrorException{
"Cannot combine authorization code/refresh_token"sv};
92 BLOB reqBody = [&] () {
96 params.Add ({
"code"sv, *
code});
97 params.Add ({
"grant_type"sv,
"authorization_code"sv});
101 params.Add ({
"grant_type"sv,
"refresh_token"sv});
114 return TypedBLOB{reqBody, InternetMediaTypes::kWWWFormURLEncoded};
120 static const auto kExcept_ =
RuntimeErrorException{
"Expected {}"_f(InternetMediaTypes::kWWWFormURLEncoded)};
134 static const auto kExcept_ =
RuntimeErrorException{
"Cannot combine authorization code/refresh_token"sv};
140 .client_secret = params.
LookupOne (
"client_secret"sv),
141 .redirect_uri = params.
LookupOne (
"redirect_uri"sv)};
149String TokenResponse::ToString ()
const
155 sb <<
", scope: "sv << scope;
160 sb <<
", id_token: "sv << id_token;
163 sb <<
", token_type: "sv << token_type;
182 return VariantValue{(objOfType->AsUTC () - DateTime::NowUTC ()).
As<
int> ()};
186 *into = DateTime::NowUTC ().AddSeconds (d.As<
int> ());
189 {
"scope"sv, &TokenResponse::scope,
192 return objOfType->Join (
" "sv);
199 {
"id_token"sv, &TokenResponse::id_token},
200 {
"token_type"sv, &TokenResponse::token_type},
205TypedBLOB TokenResponse::ToWireFormat ()
const
224String TokenRevocationRequest::ToString ()
const
228 sb <<
"access_token: "sv << access_token;
230 sb <<
", refresh_token: "sv << *refresh_token;
233 sb <<
", client_id: "sv << *client_id;
236 sb <<
", client_secret: "sv << *client_secret;
246 mapper.
AddClass<TokenRevocationRequest> ({
247 {
"access_token"sv, &TokenRevocationRequest::access_token},
248 {
"refresh_token"sv, &TokenRevocationRequest::refresh_token},
249 {
"client_id"sv, &TokenRevocationRequest::client_id},
250 {
"client_secret"sv, &TokenRevocationRequest::client_secret},
255TypedBLOB TokenRevocationRequest::ToWireFormat ()
const
257 if (access_token.empty ()) {
261 BLOB reqBody = [&] () {
264 params.
Add ({
"token_type_hint"sv,
"refresh_token"sv});
265 params.
Add ({
"token"sv, *refresh_token});
268 params.
Add ({
"token_type_hint"sv,
"access_token"sv});
269 params.
Add ({
"token"sv, access_token});
272 params.
Add ({
"client_id"sv, *client_id});
275 params.
Add ({
"client_secret"sv, *client_secret});
279 return TypedBLOB{reqBody, InternetMediaTypes::kWWWFormURLEncoded};
287String UserInfo::ToString ()
const
292 sb <<
", name: "sv << name;
295 sb <<
", given_name: "sv << given_name;
298 sb <<
", family_name: "sv << family_name;
301 sb <<
", email: "sv << email;
304 sb <<
", picture: "sv << picture;
317 {
"name"sv, &UserInfo::name},
318 {
"given_name"sv, &UserInfo::given_name},
319 {
"family_name"sv, &UserInfo::family_name},
320 {
"email"sv, &UserInfo::email},
321 {
"picture"sv, &UserInfo::picture},
326UserInfo UserInfo::FromWireFormat (
const TypedBLOB& src)
341 : fProviderConfiguration_{providerConfiguration}
353 return TokenResponse::FromWireFormat (r.
GetTypedData ());
356 DbgTrace (
"Fetcher::Token: exception={}"_f, current_exception ());
370 DbgTrace (
"Fetcher::RevokeTokens: exception={}"_f, current_exception ());
375 DbgTrace (
"Fetcher::RevokeTokens: skipping due to missing revocation_endpoint"_f);
382 auto authInfo = IO::Network::Transfer::Connection::Options::Authentication{
"Bearer "sv + accessToken};
387 return Auth::OAuth::UserInfo::FromWireFormat (r.
GetTypedData ());
390 DbgTrace (
"Fetcher::UserInfo: exception={}"_f, current_exception ());
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
An Association pairs key values with (possibly multiple or none) mapped_type values....
nonvirtual optional< mapped_type > LookupOne(ArgByValueType< key_type > key) const
Lookup and return the first (maybe arbitrarily chosen which is first) value with this key,...
nonvirtual void Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt)
nonvirtual mapped_type LookupOneChecked(ArgByValueType< key_type > key, const THROW_IF_MISSING &throwIfMissing) const
Lookup and return the first (maybe arbitrarily chosen which is first) value with this key,...
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
ObjectVariantMapper can be used to map C++ types to and from variant-union types, which can be transp...
nonvirtual void AddClass(const Traversal::Iterable< StructFieldInfo > &fieldDescriptions, const ClassMapperOptions< CLASS > &mapperOptions={})
function< VariantValue(const ObjectVariantMapper &mapper, const T *objOfType)> FromObjectMapperType
nonvirtual void AddCommonType(ARGS &&... args)
nonvirtual VariantValue FromObject(const T &from) const
nonvirtual T ToObject(const VariantValue &v) const
function< void(const ObjectVariantMapper &mapper, const VariantValue &d, T *into)> ToObjectMapperType
TypedBLOB is a named tuple<Memory::BLOB, optional<InternetMediaType>> - with friendlier names,...
nonvirtual Memory::BLOB WriteAsBLOB(const VariantValue &v) const
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
nonvirtual RETURNTYPE As() const
nonvirtual TypedBLOB GetTypedData() const
combine the 'body' data with a content-type indicator into a combined structure.
nonvirtual CONTAINER_OF_T As(CONTAINER_OF_T_CONSTRUCTOR_ARGS... args) const
nonvirtual UserInfo GetUserInfo(const String &accessToken) const
nonvirtual TokenResponse GetToken(const TokenRequest &tr) const
nonvirtual void RevokeTokens(const TokenRevocationRequest &tr) const
Track configuration data about stuff that differentiates different OAuth providers - what URLs to use...
optional< URI > token_uri
optional< URI > revocation_endpoint
optional< URI > userinfo_endpoint
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
optional< String > code_verifier
optional< String > refresh_token
optional< String > client_secret
optional< URI > redirect_uri
optional< String > refresh_token