diff --git a/include/neuron/utils/neu_jwt.h b/include/neuron/utils/neu_jwt.h index 041233e5..298d3052 100644 --- a/include/neuron/utils/neu_jwt.h +++ b/include/neuron/utils/neu_jwt.h @@ -23,6 +23,10 @@ extern "C" { #endif +#include + +extern bool disable_jwt; + int neu_jwt_init(char *dir_path); int neu_jwt_new(char **token); int neu_jwt_validate(char *b_token); diff --git a/plugins/restful/handle.h b/plugins/restful/handle.h index 0f9e8520..0b8fc438 100644 --- a/plugins/restful/handle.h +++ b/plugins/restful/handle.h @@ -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 { diff --git a/src/argparse.c b/src/argparse.c index 8ec657ed..351316a3 100644 --- a/src/argparse.c +++ b/src/argparse.c @@ -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], diff --git a/src/argparse.h b/src/argparse.h index 8fb60416..4d62997f 100644 --- a/src/argparse.h +++ b/src/argparse.h @@ -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. diff --git a/src/main.c b/src/main.c index e4da0ab1..5a837464 100644 --- a/src/main.c +++ b/src/main.c @@ -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();