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