mirror of
https://github.com/emqx/neuron.git
synced 2026-10-28 22:59:34 +08:00
Add login api
user management and jwt have not yet been implemented
This commit is contained in:
+2
-4
@@ -60,6 +60,7 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
|
||||
endif()
|
||||
|
||||
# define neuron executalbe program
|
||||
aux_source_directory(src/parser NEURON_SRC_PARSE)
|
||||
set(NEURON_SOURCES
|
||||
src/main.c
|
||||
src/core/message.c
|
||||
@@ -69,15 +70,12 @@ set(NEURON_SOURCES
|
||||
src/core/neu_datatag_manager.c
|
||||
src/core/neu_datatag_table.c
|
||||
src/adapter/adapter.c
|
||||
src/parser/neu_json_parser.c
|
||||
src/parser/neu_json_read.c
|
||||
src/parser/neu_json_write.c
|
||||
plugins/restful/handle.c
|
||||
plugins/restful/rest.c)
|
||||
|
||||
set(NEURON_INCLUDE_DIRECTORIES include src plugins)
|
||||
add_executable(neuron)
|
||||
target_sources(neuron PRIVATE ${NEURON_SOURCES})
|
||||
target_sources(neuron PRIVATE ${NEURON_SOURCES} ${NEURON_SRC_PARSE})
|
||||
target_include_directories(neuron PRIVATE ${NEURON_INCLUDE_DIRECTORIES})
|
||||
if(APPLE)
|
||||
target_link_libraries(neuron nng::nng jansson neuron-base)
|
||||
|
||||
@@ -17,16 +17,24 @@
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <nng/nng.h>
|
||||
#include <nng/supplemental/http/http.h>
|
||||
|
||||
#include "parser/neu_json_login.h"
|
||||
#include "parser/neu_json_parser.h"
|
||||
|
||||
#include "handle.h"
|
||||
|
||||
static void bad_request(nng_aio *aio, char *error);
|
||||
static void ping(nng_aio *aio);
|
||||
static void read(nng_aio *aio);
|
||||
static void write(nng_aio *aio);
|
||||
static void login(nng_aio *aio);
|
||||
static void logout(nng_aio *aio);
|
||||
|
||||
struct neu_rest_handler rest_handlers[] = {
|
||||
{
|
||||
@@ -52,6 +60,18 @@ struct neu_rest_handler rest_handlers[] = {
|
||||
.type = NEU_REST_HANDLER_FUNCTION,
|
||||
.url = "/api/v2/write",
|
||||
.value.handler = write,
|
||||
},
|
||||
{
|
||||
.method = NEU_REST_METHOD_POST,
|
||||
.type = NEU_REST_HANDLER_FUNCTION,
|
||||
.url = "/api/v2/login",
|
||||
.value.handler = login,
|
||||
},
|
||||
{
|
||||
.method = NEU_REST_METHOD_POST,
|
||||
.type = NEU_REST_HANDLER_FUNCTION,
|
||||
.url = "/api/v2/logout",
|
||||
.value.handler = logout,
|
||||
}
|
||||
};
|
||||
|
||||
@@ -86,3 +106,72 @@ static void write(nng_aio *aio)
|
||||
{
|
||||
(void) aio;
|
||||
}
|
||||
|
||||
static void login(nng_aio *aio)
|
||||
{
|
||||
int ret = -1;
|
||||
nng_http_res * res = NULL;
|
||||
char * res_data = NULL;
|
||||
char * req_data = NULL;
|
||||
char req_cdata[1024] = { 0 };
|
||||
size_t req_data_size = 0;
|
||||
nng_http_req * req = nng_aio_get_input(aio, 0);
|
||||
struct neu_parse_login_req *login_req = NULL;
|
||||
struct neu_parse_login_res login_res = {
|
||||
.function = NEU_PARSE_OP_LOGIN,
|
||||
.error = 1,
|
||||
.token = "fake token",
|
||||
};
|
||||
|
||||
nng_http_req_get_data(req, (void **) &req_data, &req_data_size);
|
||||
if (req_data_size <= 0) {
|
||||
neu_parse_encode(&login_res, &res_data);
|
||||
bad_request(aio, res_data);
|
||||
free(res_data);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(req_cdata, req_data, req_data_size);
|
||||
ret = neu_parse_decode(req_cdata, (void **) &login_req);
|
||||
if (ret != 0) {
|
||||
neu_parse_encode(&login_res, &res_data);
|
||||
bad_request(aio, res_data);
|
||||
free(res_data);
|
||||
return;
|
||||
}
|
||||
|
||||
login_res.error = 0;
|
||||
login_res.uuid = login_req->uuid;
|
||||
|
||||
neu_parse_encode(&login_res, &res_data);
|
||||
nng_http_res_alloc(&res);
|
||||
|
||||
nng_http_res_copy_data(res, res_data, strlen(res_data));
|
||||
nng_http_res_set_status(res, NNG_HTTP_STATUS_OK);
|
||||
nng_http_res_set_header(res, "Content-Type", "application/json");
|
||||
|
||||
nng_aio_set_output(aio, 0, res);
|
||||
nng_aio_finish(aio, 0);
|
||||
free(res_data);
|
||||
neu_parse_decode_free(login_req);
|
||||
}
|
||||
|
||||
static void logout(nng_aio *aio)
|
||||
{
|
||||
(void) aio;
|
||||
}
|
||||
|
||||
static void bad_request(nng_aio *aio, char *error)
|
||||
{
|
||||
|
||||
nng_http_res *res = NULL;
|
||||
|
||||
nng_http_res_alloc(&res);
|
||||
|
||||
nng_http_res_copy_data(res, error, strlen(error));
|
||||
nng_http_res_set_status(res, NNG_HTTP_STATUS_BAD_REQUEST);
|
||||
nng_http_res_set_header(res, "Content-Type", "application/json");
|
||||
|
||||
nng_aio_set_output(aio, 0, res);
|
||||
nng_aio_finish(aio, 0);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils/json.h"
|
||||
|
||||
#include "neu_json_login.h"
|
||||
|
||||
#define USER "user"
|
||||
#define PASSWORD "password"
|
||||
#define TOKEN "token"
|
||||
|
||||
int neu_parse_decode_login_req(char *buf, struct neu_parse_login_req **result)
|
||||
{
|
||||
struct neu_parse_login_req *req =
|
||||
calloc(1, sizeof(struct neu_parse_login_req));
|
||||
neu_json_elem_t elem[] = { {
|
||||
.name = NEU_PARSE_UUID,
|
||||
.t = NEU_JSON_STR,
|
||||
|
||||
},
|
||||
{
|
||||
.name = USER,
|
||||
.t = NEU_JSON_STR,
|
||||
},
|
||||
{
|
||||
.name = PASSWORD,
|
||||
.t = NEU_JSON_STR,
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem);
|
||||
if (ret != 0) {
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
req->function = NEU_PARSE_OP_LOGIN;
|
||||
req->uuid = elem[0].v.val_str;
|
||||
req->user = elem[1].v.val_str;
|
||||
req->password = elem[2].v.val_str;
|
||||
|
||||
*result = req;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int neu_parse_encode_login_res(struct neu_parse_login_res *res, char **buf)
|
||||
{
|
||||
neu_json_elem_t elems[] = { {
|
||||
.name = NEU_PARSE_FUNCTION,
|
||||
.t = NEU_JSON_INT,
|
||||
.v.val_int = NEU_PARSE_OP_LOGIN,
|
||||
|
||||
},
|
||||
{
|
||||
.name = NEU_PARSE_UUID,
|
||||
.t = NEU_JSON_STR,
|
||||
.v.val_str = res->uuid,
|
||||
},
|
||||
{
|
||||
.name = NEU_PARSE_ERROR,
|
||||
.t = NEU_JSON_INT,
|
||||
.v.val_int = res->error,
|
||||
},
|
||||
{
|
||||
.name = TOKEN,
|
||||
.t = NEU_JSON_STR,
|
||||
.v.val_str = res->token,
|
||||
}
|
||||
|
||||
};
|
||||
return neu_json_encode(elems, NEU_JSON_ELEM_SIZE(elems), buf);
|
||||
}
|
||||
|
||||
int neu_parse_decode_logout_req(char *buf, struct neu_parse_logout_req **result)
|
||||
{
|
||||
(void) buf;
|
||||
(void) result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int neu_parse_encode_logout_res(struct neu_parse_logout_res *res, char **buf)
|
||||
{
|
||||
(void) res;
|
||||
(void) buf;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
#ifndef _NEU_JSON_API_LOGIN_H_
|
||||
#define _NEU_JSON_API_LOGIN_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "neu_json_parser.h"
|
||||
|
||||
struct neu_parse_login_req {
|
||||
enum neu_parse_function function;
|
||||
char * uuid;
|
||||
char * user;
|
||||
char * password;
|
||||
};
|
||||
|
||||
struct neu_parse_login_res {
|
||||
enum neu_parse_function function;
|
||||
char * uuid;
|
||||
int error;
|
||||
char * token;
|
||||
};
|
||||
|
||||
struct neu_parse_logout_req {
|
||||
enum neu_parse_function function;
|
||||
char * uuid;
|
||||
char * user;
|
||||
};
|
||||
|
||||
struct neu_parse_logout_res {
|
||||
enum neu_parse_function function;
|
||||
char * uuid;
|
||||
int error;
|
||||
};
|
||||
|
||||
int neu_parse_decode_login_req(char *buf, struct neu_parse_login_req **req);
|
||||
int neu_parse_encode_login_res(struct neu_parse_login_res *res, char **buf);
|
||||
int neu_parse_decode_logout_req(char *buf, struct neu_parse_logout_req **req);
|
||||
int neu_parse_encode_logout_res(struct neu_parse_logout_res *res, char **buf);
|
||||
|
||||
#endif
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "neu_log.h"
|
||||
#include "utils/json.h"
|
||||
|
||||
#include "neu_json_login.h"
|
||||
#include "neu_json_parser.h"
|
||||
#include "neu_json_read.h"
|
||||
#include "neu_json_write.h"
|
||||
@@ -47,6 +48,12 @@ int neu_parse_decode(char *buf, void **result)
|
||||
ret = neu_parse_decode_write_req(
|
||||
buf, (struct neu_parse_write_req **) result);
|
||||
break;
|
||||
case NEU_PARSE_OP_LOGIN:
|
||||
ret = neu_parse_decode_login_req(
|
||||
buf, (struct neu_parse_login_req **) result);
|
||||
break;
|
||||
case NEU_PARSE_OP_LOGOUT:
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -63,6 +70,11 @@ int neu_parse_encode(void *result, char **buf)
|
||||
case NEU_PARSE_OP_WRITE:
|
||||
neu_parse_encode_write_res((struct neu_parse_write_res *) result, buf);
|
||||
break;
|
||||
case NEU_PARSE_OP_LOGIN:
|
||||
neu_parse_encode_login_res((struct neu_parse_login_res *) result, buf);
|
||||
break;
|
||||
case NEU_PARSE_OP_LOGOUT:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -99,6 +111,18 @@ void neu_parse_decode_free(void *result)
|
||||
free(req->tags);
|
||||
break;
|
||||
}
|
||||
|
||||
case NEU_PARSE_OP_LOGIN: {
|
||||
struct neu_parse_login_req *req = result;
|
||||
|
||||
free(req->uuid);
|
||||
free(req->user);
|
||||
free(req->password);
|
||||
break;
|
||||
}
|
||||
case NEU_PARSE_OP_LOGOUT: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(result);
|
||||
|
||||
@@ -30,7 +30,12 @@ extern "C" {
|
||||
#define NEU_PARSE_UUID "uuid"
|
||||
#define NEU_PARSE_ERROR "error"
|
||||
|
||||
enum neu_parse_function { NEU_PARSE_OP_READ = 50, NEU_PARSE_OP_WRITE = 51 };
|
||||
enum neu_parse_function {
|
||||
NEU_PARSE_OP_READ = 50,
|
||||
NEU_PARSE_OP_WRITE = 51,
|
||||
NEU_PARSE_OP_LOGIN = 10,
|
||||
NEU_PARSE_OP_LOGOUT = 11
|
||||
};
|
||||
|
||||
int neu_parse_decode(char *buf, void **result);
|
||||
void neu_parse_decode_free(void *result);
|
||||
|
||||
Reference in New Issue
Block a user