添加用户认证功能的初步实现
This commit is contained in:
parent
9cc9b48c18
commit
a4aed41ff1
@ -6,11 +6,11 @@ extern "C"
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto create_token(const char* user_id, const char* secret) noexcept -> char*;
|
char* create_token(const char* user_id, const char* secret);
|
||||||
|
|
||||||
auto get_payload(const char* token) noexcept -> char*;
|
char* get_payload(const char* token);
|
||||||
|
|
||||||
auto verify_token(const char* token, const char* secret) noexcept -> int;
|
int verify_token(const char* token, const char* secret);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
6
include/server/auth.h
Normal file
6
include/server/auth.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef SERVER_AUTH_H
|
||||||
|
#define SERVER_AUTH_H
|
||||||
|
|
||||||
|
// Your code here
|
||||||
|
|
||||||
|
#endif // SERVER_AUTH_H
|
79
src/db/auth.cpp
Normal file
79
src/db/auth.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include "auth.h"
|
||||||
|
|
||||||
|
#include "hash/hash.hpp"
|
||||||
|
|
||||||
|
#include <print>
|
||||||
|
|
||||||
|
#include <leveldb/db.h>
|
||||||
|
|
||||||
|
using leveldb_ptr = leveldb::DB*;
|
||||||
|
|
||||||
|
auto user_db = leveldb_ptr{nullptr};
|
||||||
|
auto iter_round = 10000;
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
auto open_user_db() -> int
|
||||||
|
{
|
||||||
|
auto opts = leveldb::Options{};
|
||||||
|
|
||||||
|
opts.create_if_missing = true;
|
||||||
|
|
||||||
|
auto status = leveldb::DB::Open(opts, "db/user", &user_db);
|
||||||
|
|
||||||
|
if (!status.ok()) {
|
||||||
|
std::println(stderr, "Failed to open user database: {}", status.ToString());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto close_user_db() -> void
|
||||||
|
{
|
||||||
|
delete user_db;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto check_user_exists(const char* user_id, int* result) -> int
|
||||||
|
{
|
||||||
|
auto value = std::string{};
|
||||||
|
auto status = user_db->Get(leveldb::ReadOptions{}, user_id, &value);
|
||||||
|
if (status.ok()) {
|
||||||
|
*result = 1;
|
||||||
|
} else if (status.IsNotFound()) {
|
||||||
|
*result = 0;
|
||||||
|
} else {
|
||||||
|
std::println(stderr, "Failed to check user existence: {}", status.ToString());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto set_user_password(const char* user_id, const char* password) -> int
|
||||||
|
{
|
||||||
|
auto status = user_db->Put(leveldb::WriteOptions{}, user_id, generate_hash(password, iter_round));
|
||||||
|
if (!status.ok()) {
|
||||||
|
std::println(stderr, "Failed to set user password: {}", status.ToString());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto login(const char* user_id, const char* password, int* result) -> int
|
||||||
|
{
|
||||||
|
auto value = std::string{};
|
||||||
|
auto status = user_db->Get(leveldb::ReadOptions{}, user_id, &value);
|
||||||
|
if (!status.ok()) {
|
||||||
|
std::println(stderr, "Failed to login: {}", status.ToString());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
*result = validate_password(password, value.data());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto logout(const char* user_id) -> int
|
||||||
|
{
|
||||||
|
leveldb::WriteOptions write_options;
|
||||||
|
leveldb::Status status = user_db->Delete(write_options, user_id);
|
||||||
|
return status.ok();
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,7 @@ using namespace std::literals::chrono_literals;
|
|||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
auto create_token(const char* user_id, const char* secret) noexcept -> char*
|
auto create_token(const char* user_id, const char* secret) -> char*
|
||||||
{
|
{
|
||||||
auto token = jwt::create()
|
auto token = jwt::create()
|
||||||
.set_type("JWS")
|
.set_type("JWS")
|
||||||
@ -21,14 +21,14 @@ extern "C"
|
|||||||
return strdup(token.c_str());
|
return strdup(token.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto get_payload(const char* token) noexcept -> char*
|
auto get_payload(const char* token) -> char*
|
||||||
{
|
{
|
||||||
auto decoded_token = jwt::decode(token);
|
auto decoded_token = jwt::decode(token);
|
||||||
auto payload = decoded_token.get_payload_claim("user_id").as_string();
|
auto payload = decoded_token.get_payload_claim("user_id").as_string();
|
||||||
return strdup(payload.c_str());
|
return strdup(payload.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto verify_token(const char* token, const char* secret) noexcept -> int
|
auto verify_token(const char* token, const char* secret) -> int
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
auto decoded_token = jwt::decode(token);
|
auto decoded_token = jwt::decode(token);
|
||||||
|
Loading…
Reference in New Issue
Block a user