Merge pull request #726 from fengzeroz/main

feat(http): optionally turn off auth
This commit is contained in:
fengzeroz
2022-08-23 10:35:39 +08:00
committed by GitHub
5 changed files with 45 additions and 27 deletions
+4
View File
@@ -23,6 +23,10 @@
extern "C" {
#endif
#include <stdbool.h>
extern bool disable_jwt;
int neu_jwt_init(char *dir_path);
int neu_jwt_new(char **token);
int neu_jwt_validate(char *b_token);
+30 -24
View File
@@ -42,32 +42,38 @@
free(req_data); \
}
#define REST_PROCESS_HTTP_REQUEST_VALIDATE_JWT(aio, req_type, decode_fun, \
func) \
{ \
char *jwt = (char *) http_get_header(aio, (char *) "Authorization"); \
\
NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \
if (error_code.error != NEU_ERR_SUCCESS) { \
http_response(aio, error_code.error, result_error); \
free(result_error); \
return; \
} \
}) \
REST_PROCESS_HTTP_REQUEST(aio, req_type, decode_fun, func); \
#define REST_PROCESS_HTTP_REQUEST_VALIDATE_JWT(aio, req_type, decode_fun, \
func) \
{ \
if (!disable_jwt) { \
char *jwt = \
(char *) http_get_header(aio, (char *) "Authorization"); \
\
NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \
if (error_code.error != NEU_ERR_SUCCESS) { \
http_response(aio, error_code.error, result_error); \
free(result_error); \
return; \
} \
}) \
} \
REST_PROCESS_HTTP_REQUEST(aio, req_type, decode_fun, func); \
}
#define VALIDATE_JWT(aio) \
{ \
char *jwt = (char *) http_get_header(aio, (char *) "Authorization"); \
\
NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \
if (error_code.error != NEU_ERR_SUCCESS) { \
http_response(aio, error_code.error, result_error); \
free(result_error); \
return; \
} \
}); \
#define VALIDATE_JWT(aio) \
{ \
if (!disable_jwt) { \
char *jwt = \
(char *) http_get_header(aio, (char *) "Authorization"); \
\
NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \
if (error_code.error != NEU_ERR_SUCCESS) { \
http_response(aio, error_code.error, result_error); \
free(result_error); \
return; \
} \
}); \
} \
}
enum neu_rest_method {
+5
View File
@@ -60,6 +60,7 @@ const char *usage_text =
" - on-failure, restart only if failure\n"
" - NUMBER, restart max NUMBER of times\n"
" --version print version information\n"
" --disable_auth disable http api auth\n"
"\n";
// clang-format on
@@ -107,6 +108,7 @@ void neu_cli_args_init(neu_cli_args_t *args, int argc, char *argv[])
{ "log", no_argument, NULL, 'l' },
{ "restart", required_argument, NULL, 'r' },
{ "version", no_argument, NULL, 'v' },
{ "disable_auth", no_argument, NULL, 'a' },
{ NULL, 0, NULL, 0 },
};
@@ -141,6 +143,9 @@ void neu_cli_args_init(neu_cli_args_t *args, int argc, char *argv[])
version();
goto quit;
break;
case 'a':
args->disable_auth = true;
break;
case '?':
default:
fprintf(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0],
+1
View File
@@ -38,6 +38,7 @@ extern "C" {
typedef struct {
bool daemonized; // flag indicating whether to run as daemon process
size_t restart; // restart policy
bool disable_auth;
} neu_cli_args_t;
/** Parse command line arguments.
+5 -3
View File
@@ -28,9 +28,10 @@
#include "argparse.h"
#include "daemon.h"
static bool exit_flag = false;
static neu_manager_t *g_manager = NULL;
zlog_category_t * neuron = NULL;
static bool exit_flag = false;
static neu_manager_t *g_manager = NULL;
zlog_category_t * neuron = NULL;
bool disable_jwt = false;
static void sig_handler(int sig)
{
@@ -79,6 +80,7 @@ int main(int argc, char *argv[])
neu_cli_args_init(&args, argc, argv);
disable_jwt = args.disable_auth;
if (args.daemonized) {
// become a daemon, this should be before calling `init`
daemonize();