Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
CurrentIdentity.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Frameworks_Auth_CurrentIdentity_h_
5#define _Stroika_Frameworks_Auth_CurrentIdentity_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
9#include "Stroika/Foundation/Common/Common.h"
10#include "Stroika/Foundation/Common/Concepts.h"
11
12/**
13 * \file
14 *
15 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
16 */
17
18namespace Stroika::Frameworks::Auth {
19
20 using namespace Stroika::Foundation;
21
22 /**
23 * struct AuthenticatedIdentity {
24 * String fEMail; // for now - lets assume that's our identity - what we extract from JWT
25 * };
26 *
27 * OR
28 *
29 * struct AuthenticatedIdentity {
30 * String fBearerToken;
31 *
32 * optional<MoreInfo> PeekAtDerivedInfo () const;
33 * };
34 * then use optional<AuthenticatedIdentity> as arg to IIdentityManagerCompatibleID/CurrentIdentityManager
35 */
36 template <typename T>
37 concept IIdentityManagerCompatibleID = default_initializable<T> and requires (T t) {
38 { static_cast<bool> (t) } -> Common::Boolean_testable;
39 static_cast<bool> (T{}) == false;
40 };
42
43 /**
44 * \brief static/thread_local storage of the some notion of identity, which can be used to 'pass data' to functions
45 * without explicit parameters.
46 *
47 * Intended use is in webserver capturing info from auth headers in interceptors, and storing for use in actual
48 * Route callbacks.
49 *
50 * \brief manage a 'thread_local' 'current-id' - typically for use in threaded applications where IDs might come from
51 * outside, like web-services
52 *
53 * \par Example Usage:
54 * using ID_OBJ = optional<String>; // just the raw auth-header
55 *
56 * [] (Request& rq, Response& rs) {
57 * CurrentIdentityManager<ID_OBJ>::Establish curID {rq.headers().authorization ()};
58 * fWSImpl.method(decoded_args_from_req); // internally peeks at CurrentIdentityManager<ID_OBJ>::Get ()
59 * ...
60 * }
61 *
62 * \see also Stroika::Frameworks::Auth::CurrentIdentityAuthInterceptor - maybe better way to 'Establish' context auth values
63 *
64 */
65 template <IIdentityManagerCompatibleID ID_OBJ>
67
68 /**
69 */
70 using IDType = ID_OBJ;
71
72 /**
73 * \brief sets the current ID to argument value -
74 * \pre CurrentIdentityManager<ID_OBJ>::Get () == false
75 */
76 struct Establish {
77 /**
78 * \pre CurrentIdentityManager<ID_OBJ>::Get () == false
79 */
80 Establish (const IDType& id);
81
82 /**
83 * \post CurrentIdentityManager<ID_OBJ>::Get () == false
84 */
85 ~Establish ();
86 };
87
88 /**
89 * \brief if no identity set with Establish, and maybe even if it has been set, Get() == false
90 */
91 static IDType Get ();
92
93 /**
94 * \brief if no identity set with Establish, and maybe even if it has been set, Get() == false
95 */
96 static void Set (IDType id);
97
98 /**
99 */
100 static void clear ();
101
102 private:
103#if qCompilerAndStdLib_thread_local_static_inline_twice_Buggy
104 static IDType& sCurrent_BWA_ ()
105 {
106 static thread_local IDType sCurrent_;
107 return sCurrent_;
108 }
109#else
110 static inline thread_local IDType sCurrent_;
111#endif
112 };
113
114}
115
116/*
117 ********************************************************************************
118 ***************************** Implementation Details ***************************
119 ********************************************************************************
120 */
121#include "CurrentIdentity.inl"
122
123#endif /*_Stroika_Frameworks_Auth_CurrentIdentity_h_*/
static/thread_local storage of the some notion of identity, which can be used to 'pass data' to funct...
static IDType Get()
if no identity set with Establish, and maybe even if it has been set, Get() == false
static void Set(IDType id)
if no identity set with Establish, and maybe even if it has been set, Get() == false