mirror of
https://github.com/emqx/neuron.git
synced 2026-10-29 07:06:32 +08:00
feat(restful): update login handler password checking
This commit is contained in:
@@ -28,6 +28,9 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "adapter/adapter_internal.h"
|
||||
#include "persist/persist.h"
|
||||
#include "user.h"
|
||||
#include "utils/asprintf.h"
|
||||
#include "utils/log.h"
|
||||
|
||||
@@ -50,12 +53,18 @@ void handle_login(nng_aio *aio)
|
||||
{
|
||||
REST_PROCESS_HTTP_REQUEST(
|
||||
aio, neu_json_login_req_t, neu_json_decode_login_req, {
|
||||
neu_plugin_t * plugin = neu_rest_get_plugin();
|
||||
neu_plugin_common_t * common = neu_plugin_to_plugin_common(plugin);
|
||||
neu_json_login_resp_t login_resp = { 0 };
|
||||
char * name = "admin";
|
||||
char * password = "0000";
|
||||
neu_user_t * user =
|
||||
neu_load_user(common->adapter->persister, req->name);
|
||||
|
||||
if (strcmp(req->name, name) == 0 &&
|
||||
strcmp(req->pass, password) == 0) {
|
||||
if (NULL == user) {
|
||||
nlog_warn("could not find user `%s`", req->name);
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_INVALID_USER_OR_PASSWORD, {
|
||||
http_response(aio, error_code.error, result_error);
|
||||
});
|
||||
} else if (neu_user_check_password(user, req->pass)) {
|
||||
|
||||
char *token = NULL;
|
||||
char *result = NULL;
|
||||
@@ -76,10 +85,13 @@ void handle_login(nng_aio *aio)
|
||||
jwt_free_str(token);
|
||||
free(result);
|
||||
} else {
|
||||
nlog_warn("user `%s` password check fail", req->name);
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_INVALID_USER_OR_PASSWORD, {
|
||||
http_response(aio, error_code.error, result_error);
|
||||
});
|
||||
}
|
||||
|
||||
neu_user_free(user);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+23
-4
@@ -341,16 +341,15 @@ end:
|
||||
return hash;
|
||||
}
|
||||
|
||||
neu_user_t *neu_user_new(const char *name, const char *password, bool is_admin)
|
||||
neu_user_t *neu_user_new(const char *name, const char *password)
|
||||
{
|
||||
neu_user_t *user = malloc(sizeof(*user));
|
||||
if (NULL == user) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
user->name = strdup(name);
|
||||
user->hash = hash_password(password, NULL);
|
||||
user->is_admin = is_admin;
|
||||
user->name = strdup(name);
|
||||
user->hash = hash_password(password, NULL);
|
||||
|
||||
if (NULL == user->name || NULL == user->hash) {
|
||||
free(user->name);
|
||||
@@ -381,3 +380,23 @@ bool neu_user_check_password(neu_user_t *user, const char *password)
|
||||
free(hash);
|
||||
return pass;
|
||||
}
|
||||
|
||||
neu_user_t *neu_load_user(neu_persister_t *persister, const char *name)
|
||||
{
|
||||
neu_user_t *user = malloc(sizeof(*user));
|
||||
if (NULL == user) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
neu_persist_user_info_t *info = NULL;
|
||||
if (0 != neu_persister_load_user(persister, name, &info)) {
|
||||
free(user);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
user->name = info->name;
|
||||
user->hash = info->hash;
|
||||
|
||||
free(info);
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -26,16 +26,19 @@ extern "C" {
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "persist/persist.h"
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *hash;
|
||||
bool is_admin;
|
||||
} neu_user_t;
|
||||
|
||||
neu_user_t *neu_user_new(const char *name, const char *password, bool is_admin);
|
||||
neu_user_t *neu_user_new(const char *name, const char *password);
|
||||
void neu_user_free(neu_user_t *user);
|
||||
bool neu_user_check_password(neu_user_t *user, const char *password);
|
||||
|
||||
neu_user_t *neu_load_user(neu_persister_t *persister, const char *name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user