DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
ControllerBase.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "Common.h"
7#include "System/Object.h"
9#include "System/String.h"
14
15namespace DotNetDupe {
16 namespace WebAppCore {
17 namespace Controllers {
18
28 protected:
31
32 public:
34 ControllerBase() = default;
35
37 ~ControllerBase() override = default;
38
44
51 if (!Request()->GetHeaders().TryGetValue("authorization", sAuthHeader)) {
53 Response()->SetContentType("application/json");
54 Response()->SetBody("{\"error\":\"Unauthorized - Missing Authorization header\"}");
55 return false;
56 }
57
58 if (!sAuthHeader.StartsWith("Bearer ", false)) {
60 Response()->SetContentType("application/json");
61 Response()->SetBody("{\"error\":\"Unauthorized - Invalid authorization scheme\"}");
62 return false;
63 }
64
65 DotNetDupe::System::String sToken = sAuthHeader.Substring(7);
66 try {
68 if (spToken.IsNull() || !spToken->Verify(sSecretKey)) {
70 Response()->SetContentType("application/json");
71 Response()->SetBody("{\"error\":\"Unauthorized - Invalid token signature\"}");
72 return false;
73 }
74
75 // Copy claims
76 auto& payload = spToken->GetPayload();
77 auto keys = payload.GetKeys();
78 for (int i = 0; i < keys.GetLength(); ++i) {
79 claims.Add(keys[i], payload[keys[i]]);
80 }
81 return true;
82 } catch (const DotNetDupe::System::Exception&) {
84 Response()->SetContentType("application/json");
85 Response()->SetBody("{\"error\":\"Unauthorized - Token parsing failed\"}");
86 return false;
87 } catch (const std::exception&) {
89 Response()->SetContentType("application/json");
90 Response()->SetBody("{\"error\":\"Unauthorized - Token parsing failed\"}");
91 return false;
92 }
93 }
94
102 if (!Authorize(sSecretKey, claims)) {
103 return false;
104 }
105
106 DotNetDupe::System::String sClaimValue;
107 if (!claims.TryGetValue(sRequiredClaim, sClaimValue) || sClaimValue != sRequiredValue) {
109 Response()->SetContentType("application/json");
110 Response()->SetBody("{\"error\":\"Forbidden - Missing required claim value\"}");
111 return false;
112 }
113 return true;
114 }
115
121 Response()->SetContentType("application/json");
122 return DotNetDupe::System::String("{\"error\":\"") + error + "\"}";
123 }
124
130
136
141 template <typename U>
144 Response()->SetContentType("application/json");
146 }
147
155
160 template <typename U>
163 Response()->SetContentType("application/json");
165 }
166
174
181
187 Response()->SetContentType("application/json");
188 return DotNetDupe::System::String("{\"error\":\"") + error + "\"}";
189 }
190
196 Response()->SetContentType("application/json");
197 return DotNetDupe::System::String("{\"error\":\"") + error + "\"}";
198 }
199
205 Response()->SetContentType("application/json");
206 return DotNetDupe::System::String("{\"error\":\"") + error + "\"}";
207 }
208 };
209
210 }
211 }
212}
Defines common cross-platform macros, export decorators, and fundamental types.
HTTP request, response, and context abstractions for WebAppCore processing pipelines.
JSON Web Token (JWT) representation and cryptographic operations per RFC 7519.
Provides functionality to serialize objects to JSON strings and deserialize JSON strings into objects...
Base object class for DotNetDupe mirroring .NET System.Object.
Provides reference-counted and weak pointer memory management primitives ensuring zero raw ownership.
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
Represents a collection of keys and values.
Definition Dictionary.h:66
bool TryGetValue(const TKey &key, TValue &value) const
Definition Dictionary.h:285
void Add(const TKey &key, const TValue &value)
Definition Dictionary.h:244
Represents errors that occur during application execution.
Definition Exception.h:19
static SmartPointer< JWTToken > Parse(const String &tokenStr)
Parses a serialized compact JWT string into a JWTToken instance.
Definition JWTToken.cpp:108
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
A unified smart pointer that supports both unique and shared ownership semantics.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String Substring(int iStartIndex) const
Definition String.cpp:659
bool StartsWith(const String &sPrefix) const
Definition String.cpp:610
static String Serialize(const T &value)
Converts the value of a type specified by a generic type parameter into a JSON string.
DotNetDupe::System::String Created(const U &value)
Sets a 201 Created HTTP status and serializes the given resource model as JSON.
DotNetDupe::System::String NotFound(const DotNetDupe::System::String &error="Not Found")
Sets a 404 Not Found HTTP status and returns a JSON error payload.
DotNetDupe::System::SmartPointer< Http::HttpContext > m_httpContext
Active HTTP context encapsulating request and response streams for this execution.
DotNetDupe::System::SmartPointer< Http::HttpResponse > Response() const
Gets the active HTTP response abstraction.
DotNetDupe::System::String Ok(const U &value)
Sets a 200 OK HTTP status and serializes the given object model as JSON.
DotNetDupe::System::String BadRequest(const DotNetDupe::System::String &error="Bad Request")
Sets a 400 Bad Request HTTP status and returns a JSON error payload.
DotNetDupe::System::String NoContent()
Sets a 204 No Content HTTP status and returns an empty body.
DotNetDupe::System::String Forbidden(const DotNetDupe::System::String &error="Forbidden")
Sets a 403 Forbidden HTTP status and returns a JSON error payload.
bool Authorize(const DotNetDupe::System::String &sSecretKey, const DotNetDupe::System::String &sRequiredClaim, const DotNetDupe::System::String &sRequiredValue, DotNetDupe::System::Collections::Generic::Dictionary< DotNetDupe::System::String, DotNetDupe::System::String > &claims)
Validates incoming Bearer JWT authentication and ensures a specific claim and value match.
void Initialize(const DotNetDupe::System::SmartPointer< Http::HttpContext > &context)
Binds the active HTTP context to this controller instance.
DotNetDupe::System::String Unauthorized(const DotNetDupe::System::String &error="Unauthorized")
Sets a 401 Unauthorized HTTP status and returns a JSON error payload.
DotNetDupe::System::String Ok(const DotNetDupe::System::String &body="")
Sets a 200 OK HTTP status and returns raw string response content.
DotNetDupe::System::SmartPointer< Http::HttpRequest > Request() const
Gets the active HTTP request abstraction.
bool Authorize(const DotNetDupe::System::String &sSecretKey, DotNetDupe::System::Collections::Generic::Dictionary< DotNetDupe::System::String, DotNetDupe::System::String > &claims)
Validates incoming Bearer JWT authentication token from the Authorization header against the secret k...
DotNetDupe::System::String Created(const DotNetDupe::System::String &body="")
Sets a 201 Created HTTP status and returns raw string response content.
~ControllerBase() override=default
Virtual destructor.
@ Created
Equivalent to HTTP status 201. Indicates that the request resulted in a new resource created.
@ OK
Equivalent to HTTP status 200. Indicates that the request succeeded.