This commit is contained in:
lixiumei
2021-11-29 18:16:50 +08:00
committed by Parker Liu
parent 2619100f13
commit dcf9160735
11 changed files with 343 additions and 3 deletions
+1 -1
View File
@@ -139,7 +139,7 @@ target_sources(neuron-base PRIVATE ${NEURON_BASE_SOURCES} ${NEURON_SRC_PARSE} ${
target_include_directories(neuron-base
PRIVATE ${NEURON_BASE_INCLUDE_DIRECTORIES})
target_include_directories(neuron-base PRIVATE ${CMAKE_STAGING_PREFIX})
target_link_libraries(neuron-base vector-static csptr jansson crypto yaml
target_link_libraries(neuron-base vector-static csptr jansson crypto yaml jwt
${CMAKE_THREAD_LIBS_INIT})
#copy file for run and unit test
+52
View File
@@ -0,0 +1,52 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/*
* DO NOT EDIT THIS FILE MANUALLY!
* It was automatically generated by `json-autotype`.
*/
#ifndef _NEU_JSON_API_NEU_JSON_LOGIN_H_
#define _NEU_JSON_API_NEU_JSON_LOGIN_H_
#include "json/json.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
char *pass;
char *name;
} neu_json_login_req_t;
int neu_json_decode_login_req(char *buf, neu_json_login_req_t **result);
void neu_json_decode_login_req_free(neu_json_login_req_t *req);
typedef struct {
char *token;
} neu_json_login_resp_t;
int neu_json_encode_login_resp(void *json_object, void *param);
#ifdef __cplusplus
}
#endif
#endif
+1
View File
@@ -111,6 +111,7 @@ function build_jwt() {
cd build
cmake -G Ninja -DBUILD_EXAMPLES=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_SHARED_LIBS=ON \
-DENABLE_DEBUG_INFO_IN_RELEASE=OFF \
-Djansson_DIR=${install_dir}/jansson \
-DJANSSON_INCLUDE_DIRS=${install_dir}/jansson/include \
+2
View File
@@ -20,6 +20,8 @@ web:
api:
host: 0.0.0.0
port: 7001
public_key: ./.ssh/id_rsa.pub
private_key: ./.ssh/id_rsa
opcua:
default_cert_file: ./opcua-default-cert.der
default_key_file: ./opcua-default-key.der
+44
View File
@@ -19,12 +19,17 @@
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <jwt.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "json/neu_json_fn.h"
#include "json/neu_json_login.h"
#include "json/neu_json_tty.h"
#include "handle.h"
@@ -95,6 +100,45 @@ void handle_ping(nng_aio *aio)
void handle_login(nng_aio *aio)
{
(void) aio;
// REST_PROCESS_HTTP_REQUEST(
// aio, neu_json_login_req_t, neu_json_decode_login_req, {
// neu_json_login_req_t *user = NULL;
// char * name = "admin";
// char * pass = "0000";
// jwt_t * jwt = NULL;
// time_t iat = time(NULL);
// int ret = 0;
// jwt_alg_t opt_alg = JWT_ALG_RS256;
// unsigned char key[10240];
// size_t key_len = 0;
// if (req == NULL) {
// http_not_found(aio, "{\"error\": 1}");
// } else {
// if (req->name == name && req->pass == pass) {
// user->name = req->name;
// user->pass = req->pass;
// ret = jwt_new(&jwt);
// if (ret != 0 || jwt == NULL) {
// http_bad_request(aio, "{\"error\": 400}");
// jwt_free(jwt);
// }
// ret = jwt_add_grant_int(jwt, "iat", iat);
// ret = jwt_set_alg(jwt, opt_alg,
// opt_alg == JWT_ALG_NONE ? NULL : key,
// opt_alg == JWT_ALG_NONE ? 0 : key_len);
// if (ret < 0) {
// http_bad_request(aio, "{\"error\": 400}");
// jwt_free(jwt);
// }
// }
// http_ok(aio, "{\"error\": 0}");
// }
// })
}
void handle_logout(nng_aio *aio)
+86
View File
@@ -0,0 +1,86 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/*
* DO NOT EDIT THIS FILE MANUALLY!
* It was automatically generated by `json-autotype`.
*/
#include <stdlib.h>
#include <string.h>
#include "json/json.h"
#include "json/neu_json_login.h"
int neu_json_decode_login_req(char *buf, neu_json_login_req_t **result)
{
int ret = 0;
neu_json_login_req_t *req = calloc(1, sizeof(neu_json_login_req_t));
neu_json_elem_t req_elems[] = { {
.name = "pass",
.t = NEU_JSON_STR,
},
{
.name = "name",
.t = NEU_JSON_STR,
} };
ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(req_elems), req_elems);
if (ret != 0) {
goto decode_fail;
}
req->pass = req_elems[0].v.val_str;
req->name = req_elems[1].v.val_str;
*result = req;
return ret;
decode_fail:
if (req != NULL) {
free(req);
}
return -1;
}
void neu_json_decode_login_req_free(neu_json_login_req_t *req)
{
free(req->pass);
free(req->name);
free(req);
}
int neu_json_encode_login_resp(void *json_object, void *param)
{
int ret = 0;
neu_json_login_resp_t *resp = (neu_json_login_resp_t *) param;
neu_json_elem_t resp_elems[] = { {
.name = "token",
.t = NEU_JSON_STR,
.v.val_str = resp->token,
} };
ret = neu_json_encode_field(json_object, resp_elems,
NEU_JSON_ELEM_SIZE(resp_elems));
return ret;
}
+96 -1
View File
@@ -17,4 +17,99 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "neu_jwt.h"
#include <errno.h>
#include "config.h"
#include "neu_jwt.h"
#include "neu_log.h"
unsigned char public_key[4096] = { 0 };
unsigned char private_key[4096] = { 0 };
size_t priv_key_len = 0;
size_t pub_key_len = 0;
int neu_jwt_init(char *pub_key_path, char *priv_key_path)
{
FILE * fp_priv_key = NULL;
FILE * fp_pub_key = NULL;
size_t priv_key_len = 0;
size_t pub_key_len = 0;
fp_priv_key = fopen(priv_key_path, "r");
if (fp_priv_key == NULL) {
log_error("Failed to open key file: %s, error: %d", priv_key_path,
errno);
return -1;
}
priv_key_len = fread(private_key, 1, sizeof(private_key), fp_priv_key);
if (priv_key_len <= 0) {
log_error("Failed to read key file: %s, error: %d", priv_key_path,
errno);
return -1;
}
fclose(fp_priv_key);
fp_pub_key = fopen(pub_key_path, "r");
if (fp_pub_key == NULL) {
log_error("Failed to open key file: %s, error: %d", pub_key_path,
errno);
return -1;
}
pub_key_len = fread(public_key, 1, sizeof(public_key), fp_pub_key);
if (pub_key_len <= 0) {
log_error("Failed to read key file: %s, error: %d", pub_key_path,
errno);
return -1;
}
fclose(fp_pub_key);
return 0;
}
int neu_jwt_new(char **token)
{
time_t iat = time(NULL);
jwt_alg_t opt_alg = JWT_ALG_RS256;
jwt_t * jwt = NULL;
char * str_jwt = NULL;
int ret = jwt_new(&jwt);
if (ret != 0 || jwt == NULL) {
log_error("Invalid jwt: %d", ret);
return -1;
}
ret = jwt_add_grant_int(jwt, "iat", iat);
if (ret < 0) {
jwt_free(jwt);
log_error("Failed to add grant: %d", ret);
return -1;
}
ret = jwt_set_alg(jwt, opt_alg, private_key, priv_key_len);
if (ret < 0) {
jwt_free(jwt);
log_error("jwt incorrect algorithm: %d", ret);
return -1;
}
str_jwt = jwt_encode_str(jwt);
if (str_jwt == NULL) {
jwt_free(jwt);
log_error("jwt incorrect algorithm");
return -1;
}
*token = str_jwt;
jwt_free(jwt);
jwt_free_str(str_jwt);
return 0;
}
// int neu_jwt_validate(char *token)
// {
// // jwt_valid_t *jwt_valid = NULL;
// (void) token;
// return 0;
// }
+9 -1
View File
@@ -16,5 +16,13 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#ifndef __NEU_JWT_H__
#define __NEU_JWT_H__
#include <jwt.h>
#include <jwt.h>
int neu_jwt_init(char *pub_key_path, char *priv_key_path);
int neu_jwt_new(char **token);
// int neu_jwt_validate(char *token);
#endif
+8
View File
@@ -132,6 +132,13 @@ target_include_directories(opcua_test PRIVATE
${CMAKE_SOURCE_DIR}/plugins/opcua)
target_link_libraries(opcua_test neuron-base gtest_main gtest yaml jansson ssl crypto)
# add_executable(jwt_test jwt_test.cc)
# target_include_directories(jwt_test PRIVATE
# ${CMAKE_SOURCE_DIR}/src
# ${CMAKE_SOURCE_DIR}/include
# ${CMAKE_SOURCE_DIR}/src/utils)
# target_link_libraries(jwt_test neuron-base gtest_main gtest yaml jansson ssl crypto)
include(GoogleTest)
gtest_discover_tests(data_val_test)
gtest_discover_tests(modbus_point_test)
@@ -148,3 +155,4 @@ gtest_discover_tests(uuid_test)
gtest_discover_tests(schema_valid_test)
gtest_discover_tests(mqtt_test)
gtest_discover_tests(opcua_test)
# gtest_discover_tests(jwt_test)
+10
View File
@@ -40,4 +40,14 @@ TEST(JsonTest, DecodeField)
(char *) "port");
EXPECT_STREQ("7001", result);
free(result);
result = neu_config_get_value((char *) "./neuron.yaml", 2, (char *) "api",
(char *) "public_key");
EXPECT_STREQ("./.ssh/id_rsa.pub", result);
free(result);
result = neu_config_get_value((char *) "./neuron.yaml", 2, (char *) "api",
(char *) "private_key");
EXPECT_STREQ("./.ssh/id_rsa", result);
free(result);
}
+34
View File
@@ -0,0 +1,34 @@
#include <gtest/gtest.h>
#include "config.h"
#include "jwt.h"
#include "neu_jwt.h"
TEST(JsonTest, JwtValidate)
{
char * token = NULL;
unsigned char public_key[4096] = { 0 };
size_t pub_key_len = 0;
jwt_t * jwt = NULL;
char *public_key_path = neu_config_get_value(
(char *) "./neuron.yaml", 2, (char *) "api", (char *) "public_key");
char *private_key_path = neu_config_get_value(
(char *) "./neuron.yaml", 2, (char *) "api", (char *) "private_key");
neu_jwt_init(public_key_path, private_key_path);
neu_jwt_new(&token);
EXPECT_STREQ("abc", token);
jwt_valid_t *jwt_valid = NULL;
jwt_alg_t opt_alg = JWT_ALG_RS256;
jwt_valid_new(&jwt_valid, opt_alg);
jwt_valid_set_headers(jwt_valid, 1);
jwt_valid_set_now(jwt_valid, time(NULL));
jwt_decode(&jwt, token, public_key, pub_key_len);
jwt_valid_free(jwt_valid);
}