mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-01-09 20:39:37 +01:00
Cleanup / bindings for Middleware, and CSRFToken.
This commit is contained in:
parent
9ce119a5ab
commit
0b0d5e3657
@ -1,27 +1,28 @@
|
|||||||
#include "csrf_token.h"
|
#include "csrf_token.h"
|
||||||
|
|
||||||
#include "crypto/hash/sha256.h"
|
#include "core/crypto/crypto_core.h"
|
||||||
|
#include "core/os/os.h"
|
||||||
|
#include "http_server_enums.h"
|
||||||
#include "http_session.h"
|
#include "http_session.h"
|
||||||
#include "request.h"
|
#include "web_server_request.h"
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
bool CSRFTokenWebServerMiddleware::on_before_handle_request_main(Request *request) {
|
bool CSRFTokenWebServerMiddleware::_on_before_handle_request_main(Ref<WebServerRequest> request) {
|
||||||
switch (request->get_method()) {
|
switch (request->get_method()) {
|
||||||
case HTTP_METHOD_POST:
|
case HTTPServerEnums::HTTP_METHOD_POST:
|
||||||
case HTTP_METHOD_DELETE:
|
case HTTPServerEnums::HTTP_METHOD_DELETE:
|
||||||
case HTTP_METHOD_PATCH:
|
case HTTPServerEnums::HTTP_METHOD_PATCH:
|
||||||
case HTTP_METHOD_PUT: {
|
case HTTPServerEnums::HTTP_METHOD_PUT: {
|
||||||
if (shold_ignore(request)) {
|
if (shold_ignore(request)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!request->session.is_valid()) {
|
if (!request->session.is_valid()) {
|
||||||
request->send_error(HTTP_STATUS_CODE_401_UNAUTHORIZED);
|
request->send_error(HTTPServerEnums::HTTP_STATUS_CODE_401_UNAUTHORIZED);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!request->validate_csrf_token()) {
|
if (!request->validate_csrf_token()) {
|
||||||
request->send_error(HTTP_STATUS_CODE_401_UNAUTHORIZED);
|
request->send_error(HTTPServerEnums::HTTP_STATUS_CODE_401_UNAUTHORIZED);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,11 +44,11 @@ bool CSRFTokenWebServerMiddleware::on_before_handle_request_main(Request *reques
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CSRFTokenWebServerMiddleware::shold_ignore(Request *request) {
|
bool CSRFTokenWebServerMiddleware::shold_ignore(Ref<WebServerRequest> request) {
|
||||||
const String &path = request->get_path_full();
|
String path = request->get_path_full();
|
||||||
|
|
||||||
for (int i = 0; i < ignored_urls.size(); ++i) {
|
for (int i = 0; i < ignored_urls.size(); ++i) {
|
||||||
if (path.starts_with(ignored_urls[i])) {
|
if (path.begins_with(ignored_urls[i])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,14 +57,15 @@ bool CSRFTokenWebServerMiddleware::shold_ignore(Request *request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String CSRFTokenWebServerMiddleware::create_token() {
|
String CSRFTokenWebServerMiddleware::create_token() {
|
||||||
Ref<SHA256> h = SHA256::get();
|
String s = String::num(OS::get_singleton()->get_unix_time());
|
||||||
|
|
||||||
String s = h->compute(String::num(time(NULL)));
|
return s.sha256_text().substr(0, 10);
|
||||||
|
|
||||||
return s.substr(0, 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CSRFTokenWebServerMiddleware::CSRFTokenWebServerMiddleware() {
|
CSRFTokenWebServerMiddleware::CSRFTokenWebServerMiddleware() {
|
||||||
}
|
}
|
||||||
CSRFTokenWebServerMiddleware::~CSRFTokenWebServerMiddleware() {
|
CSRFTokenWebServerMiddleware::~CSRFTokenWebServerMiddleware() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSRFTokenWebServerMiddleware::_bind_methods() {
|
||||||
|
}
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
#ifndef CSRF_TOKEN_H
|
#ifndef CSRF_TOKEN_MIDDLEWARE_H
|
||||||
#define CSRF_TOKEN_H
|
#define CSRF_TOKEN_MIDDLEWARE_H
|
||||||
|
|
||||||
#include "middleware.h"
|
#include "web_server_middleware.h"
|
||||||
|
|
||||||
#include "core/containers/vector.h"
|
#include "core/ustring.h"
|
||||||
#include "core/string.h"
|
#include "core/vector.h"
|
||||||
|
|
||||||
class Request;
|
class WebServerRequest;
|
||||||
|
|
||||||
class CSRFTokenWebServerMiddleware : public WebServerMiddleware {
|
class CSRFTokenWebServerMiddleware : public WebServerMiddleware {
|
||||||
RCPP_OBJECT(CSRFTokenWebServerMiddleware, WebServerMiddleware);
|
GDCLASS(CSRFTokenWebServerMiddleware, WebServerMiddleware);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//returnring true means handled, false means continue
|
//returnring true means handled, false means continue
|
||||||
bool on_before_handle_request_main(Request *request);
|
bool _on_before_handle_request_main(Ref<WebServerRequest> request);
|
||||||
|
|
||||||
bool shold_ignore(Request *request);
|
bool shold_ignore(Ref<WebServerRequest> request);
|
||||||
|
|
||||||
virtual String create_token();
|
virtual String create_token();
|
||||||
|
|
||||||
@ -23,6 +23,9 @@ public:
|
|||||||
~CSRFTokenWebServerMiddleware();
|
~CSRFTokenWebServerMiddleware();
|
||||||
|
|
||||||
Vector<String> ignored_urls;
|
Vector<String> ignored_urls;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static void _bind_methods();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,15 +1,26 @@
|
|||||||
|
|
||||||
#include "middleware.h"
|
#include "web_server_middleware.h"
|
||||||
|
|
||||||
#include "request.h"
|
#include "web_server_request.h"
|
||||||
|
|
||||||
bool WebServerMiddleware::on_before_handle_request_main(Request *request) {
|
bool WebServerMiddleware::on_before_handle_request_main(Ref<WebServerRequest> request) {
|
||||||
|
return call("_on_before_handle_request_main", request);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WebServerMiddleware::_on_before_handle_request_main(Ref<WebServerRequest> request) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WebServerMiddleware::WebServerMiddleware() :
|
WebServerMiddleware::WebServerMiddleware() {
|
||||||
Reference() {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebServerMiddleware::~WebServerMiddleware() {
|
WebServerMiddleware::~WebServerMiddleware() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebServerMiddleware::_bind_methods() {
|
||||||
|
BIND_VMETHOD(MethodInfo("_on_before_handle_request_main", PropertyInfo(Variant::OBJECT, "request", PROPERTY_HINT_RESOURCE_TYPE, "WebServerRequest")));
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("on_before_handle_request_main", "request"), &WebServerMiddleware::on_before_handle_request_main);
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("_on_before_handle_request_main", "request"), &WebServerMiddleware::_on_before_handle_request_main);
|
||||||
|
}
|
||||||
|
@ -1,21 +1,26 @@
|
|||||||
#ifndef MIDDLEWARE_H
|
#ifndef WEB_SERVRER_MIDDLEWARE_H
|
||||||
#define MIDDLEWARE_H
|
#define WEB_SERVRER_MIDDLEWARE_H
|
||||||
|
|
||||||
#include "core/string.h"
|
#include "core/ustring.h"
|
||||||
|
|
||||||
#include "core/reference.h"
|
#include "core/reference.h"
|
||||||
|
|
||||||
class Request;
|
class WebServerRequest;
|
||||||
|
|
||||||
class WebServerMiddleware : public Reference {
|
class WebServerMiddleware : public Reference {
|
||||||
RCPP_OBJECT(WebServerMiddleware, Reference);
|
GDCLASS(WebServerMiddleware, Reference);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//returnring true means handled, false, means continue
|
//returnring true means handled, false, means continue
|
||||||
virtual bool on_before_handle_request_main(Request *request);
|
bool on_before_handle_request_main(Ref<WebServerRequest> request);
|
||||||
|
|
||||||
|
virtual bool _on_before_handle_request_main(Ref<WebServerRequest> request);
|
||||||
|
|
||||||
WebServerMiddleware();
|
WebServerMiddleware();
|
||||||
~WebServerMiddleware();
|
~WebServerMiddleware();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static void _bind_methods();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user