51 if (!
Request()->GetHeaders().TryGetValue(
"authorization", sAuthHeader)) {
53 Response()->SetContentType(
"application/json");
54 Response()->SetBody(
"{\"error\":\"Unauthorized - Missing Authorization header\"}");
58 if (!sAuthHeader.
StartsWith(
"Bearer ",
false)) {
60 Response()->SetContentType(
"application/json");
61 Response()->SetBody(
"{\"error\":\"Unauthorized - Invalid authorization scheme\"}");
68 if (spToken.IsNull() || !spToken->Verify(sSecretKey)) {
70 Response()->SetContentType(
"application/json");
71 Response()->SetBody(
"{\"error\":\"Unauthorized - Invalid token signature\"}");
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]]);
84 Response()->SetContentType(
"application/json");
85 Response()->SetBody(
"{\"error\":\"Unauthorized - Token parsing failed\"}");
87 }
catch (
const std::exception&) {
89 Response()->SetContentType(
"application/json");
90 Response()->SetBody(
"{\"error\":\"Unauthorized - Token parsing failed\"}");
107 if (!claims.
TryGetValue(sRequiredClaim, sClaimValue) || sClaimValue != sRequiredValue) {
109 Response()->SetContentType(
"application/json");
110 Response()->SetBody(
"{\"error\":\"Forbidden - Missing required claim value\"}");
121 Response()->SetContentType(
"application/json");
141 template <
typename U>
144 Response()->SetContentType(
"application/json");
160 template <
typename U>
163 Response()->SetContentType(
"application/json");
187 Response()->SetContentType(
"application/json");
196 Response()->SetContentType(
"application/json");
205 Response()->SetContentType(
"application/json");
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.
bool TryGetValue(const TKey &key, TValue &value) const
void Add(const TKey &key, const TValue &value)
Represents errors that occur during application execution.
static SmartPointer< JWTToken > Parse(const String &tokenStr)
Parses a serialized compact JWT string into a JWTToken instance.
Supports all classes in the DotNetDupe class hierarchy.
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.
String Substring(int iStartIndex) const
bool StartsWith(const String &sPrefix) const
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.
ControllerBase()=default
Default constructor.
@ 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.