Generate key
FossilOrigin-Name: 1467af84eb41350ef5481cd1389b06da4077531d3d26d4f34a0d1df8cba55b3e
This commit is contained in:
parent
971153bdd4
commit
e250efdd13
9 changed files with 48 additions and 10 deletions
|
@ -25,6 +25,7 @@ add_library(wormhole_lib
|
|||
src/config/config_db.cpp
|
||||
src/config/config_loader.cpp
|
||||
src/config/config_instance.cpp
|
||||
src/user.cpp
|
||||
src/control.cpp
|
||||
src/logger.cpp)
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ RsaFactory::RsaFactory()
|
|||
throw std::runtime_error(ERR_reason_error_string(ERR_get_error()));
|
||||
}
|
||||
|
||||
Rsa RsaFactory::generate_key(const size_t bits /* = 2048 */)
|
||||
Rsa RsaFactory::generate_key(const size_t bits /* = 2048 */) const
|
||||
{
|
||||
int code;
|
||||
RSA* rsa = RSA_new();
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace Crypt
|
|||
RsaFactory();
|
||||
~RsaFactory() = default;
|
||||
|
||||
Rsa generate_key(const size_t bits = 2048);
|
||||
Rsa generate_key(const size_t bits = 2048) const;
|
||||
private:
|
||||
|
||||
std::unique_ptr<BIGNUM, decltype(&BN_free)> e;
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
using namespace DB;
|
||||
|
||||
Database::Database()
|
||||
: rsa_cstor()
|
||||
{}
|
||||
|
||||
bool Database::try_migration()
|
||||
|
@ -42,7 +43,7 @@ User Database::get_user(const std::string& acct)
|
|||
return {};
|
||||
}
|
||||
|
||||
void Database::create_user(const User& props)
|
||||
void Database::create_user(User& props)
|
||||
{
|
||||
std::cerr << "Database::create_user()" << std::endl;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include <string>
|
||||
#include <memory>
|
||||
#include "user.h"
|
||||
#include "crypt/rsa.h"
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
@ -40,7 +41,7 @@ namespace DB
|
|||
virtual User get_user(unsigned long id);
|
||||
virtual User get_user(const std::string& acct);
|
||||
|
||||
virtual void create_user(const User& props);
|
||||
virtual void create_user(User& props);
|
||||
|
||||
/// These are just enums for Configuration and stuff, but Polymorphism
|
||||
/// handles the typing stuff already
|
||||
|
@ -50,7 +51,8 @@ namespace DB
|
|||
PostgreSQL,
|
||||
Unset,
|
||||
};
|
||||
private:
|
||||
protected:
|
||||
Crypt::RsaFactory rsa_cstor;
|
||||
};
|
||||
|
||||
std::shared_ptr<DB::Database> load_db_from_cfg();
|
||||
|
|
|
@ -137,10 +137,11 @@ SQLiteCacheIndex SQLite::sqlite_compile_to_cache(SQLiteCacheIndex idx, std::stri
|
|||
|
||||
User SQLite::get_user(unsigned long id)
|
||||
{
|
||||
User luser{id, {}, false, {}, {}, {}, {}, {}};
|
||||
User luser{};
|
||||
luser.id = id;
|
||||
luser.local = false;
|
||||
|
||||
// Setup function
|
||||
|
||||
sqlite_compile_to_cache(GET_USER_BY_ID, "SELECT acct, display_name, bio, key FROM users WHERE id=?1");
|
||||
|
||||
int code = sqlite_exec(GET_USER_BY_ID, {{id}}, [&luser](sqlite3_stmt* stmt){
|
||||
|
@ -161,16 +162,18 @@ User SQLite::get_user(unsigned long id)
|
|||
return luser;
|
||||
}
|
||||
|
||||
void SQLite::create_user(const User& p)
|
||||
void SQLite::create_user(User& p)
|
||||
{
|
||||
// INSERT INTO users (local, display_name, acct, bio, created_at) VALUES (TRUE, 'Test user', 'test1', 'sneed!!!', unixepoch());
|
||||
|
||||
sqlite_compile_to_cache(CREATE_USER, "INSERT INTO users ( local, email, display_name, acct, bio, created_at, updated_at, birthday, key )"
|
||||
"VALUES ( ?1, ?2, ?3, ?4, ?5, unixepoch(), unixepoch(), ?6, ?7 )");
|
||||
|
||||
p.generate_key(rsa_cstor);
|
||||
|
||||
sqlite_exec(CREATE_USER,
|
||||
{p.local, p.email, p.display_name, p.acct,
|
||||
p.bio, p.birthday, "the key lole"},
|
||||
p.bio, p.birthday, p.key},
|
||||
nullptr);
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ namespace DB
|
|||
virtual bool try_migration() override final;
|
||||
virtual User get_user(unsigned long id) override final;
|
||||
virtual User get_user(const std::string& acct) override final;
|
||||
virtual void create_user(const User& props) override final;
|
||||
virtual void create_user(User& props) override final;
|
||||
private:
|
||||
/**
|
||||
* @brief Executes an SQL statement
|
||||
|
|
24
src/user.cpp
Normal file
24
src/user.cpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Wormhole - Federated social network
|
||||
* Copyright (C) 2022 Nekobit
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "user.h"
|
||||
|
||||
void User::generate_key(const Crypt::RsaFactory& keygen)
|
||||
{
|
||||
|
||||
}
|
|
@ -19,10 +19,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "crypt/rsa.h"
|
||||
|
||||
class User
|
||||
{
|
||||
public:
|
||||
// Values read from databases, so public here
|
||||
unsigned long id;
|
||||
std::string email;
|
||||
bool local;
|
||||
|
@ -31,6 +33,11 @@ public:
|
|||
int birthday;
|
||||
std::string display_name;
|
||||
std::string key;
|
||||
|
||||
inline const std::string& get_key() { return key; }
|
||||
|
||||
/** @brief Generates an RSA key, retrieve with get_key() */
|
||||
void generate_key(const Crypt::RsaFactory& keygen);
|
||||
|
||||
inline operator std::string() const
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue