Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Auth/Interceptor.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Frameworks_Auth_Interceptor_h_
5#define _Stroika_Frameworks_Auth_Interceptor_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#include "Stroika/Frameworks/WebServer/Interceptor.h"
13#include "Stroika/Frameworks/WebServer/Request.h"
14
16
17/**
18 * \file
19 *
20 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
21 */
22
23namespace Stroika::Frameworks::Auth {
24
25 using namespace Stroika::Foundation;
26
29
30 /**
31 * Add Interceptor to ConnectionManager.
32 *
33 * Must be templated on TYPE of object to stick into CurrentIdentity.
34 */
35
36 /**
37 * \brief Interceptor added to WebServer::ConnectionManager to translate Authorization: headers (parse them) into thread_local ID variable accessible by webservice methods
38 *
39 * Caller defines ID_TYPE - the type of record stored to track a user ID; type: typically like this
40 * \par Example Usage
41 * \code
42 * struct MyID_ {
43 * String fBearerToken;
44 * MoreUserInfo GetInfoDerivedFromBearerOrThrow401 (); });
45 * };
46 * \enccode
47 *
48 * \note The Interceptor doesn't generate 401s, populates a thread-local data structure CurrentIdentityAuthInterceptor with the auth-token
49 *
50 * \par Example Usage
51 * \code
52 * ...ConnectionManager fConnectionMgr_{...}
53 * CTOR BODY
54 * {
55 * auto convertAuthHeaderToIDObject = [] (Request& request) -> optional<MyID_> {
56 * if (auto authHeader = request.headers ().authorization (); authHeader and authHeader->StartsWith ("Bearer "sv)) {
57 * return WebServiceIdentity{.fBearerToken = authHeader->SubString (7).Trim ()};
58 * }
59 * return nullopt;
60 * };
61 * fConnectionMgr_.AddInterceptor (CurrentIdentityAuthInterceptor{convertAuthHeaderToIDObject}, ConnectionManager::ePrependsToEarly);
62 * }
63 *
64 * // THEN - inside any web-service method, the caller may check
65 * if (optional<MyID_> oid = CurrentIdentityManager<optional<MyID_>>::Get ()) {
66 * String email = oid->GetInfoDerivedFromBearerOrThrow401 ().fEmail;
67 * }
68 * \endcode
69 */
70 template <IIdentityManagerCompatibleID ID_TYPE>
72 private:
73 using inherited = Interceptor;
74
75 public:
76 /**
77 */
78 CurrentIdentityAuthInterceptor (function<ID_TYPE (Request&)> cb);
79
80 private:
81 struct Rep_;
82 };
83 template <invocable<Request&> FN>
85
86}
87
88/*
89 ********************************************************************************
90 ***************************** Implementation Details ***************************
91 ********************************************************************************
92 */
93#include "Interceptor.inl"
94
95#endif /*_Stroika_Frameworks_Auth_Interceptor_h_*/
Interceptor added to WebServer::ConnectionManager to translate Authorization: headers (parse them) in...
this represents a HTTP request object for the WebServer module