Random token generation

FossilOrigin-Name: f1690870b29cf0135e60affc2547d6c5538a13efe4f0ea3e1c9687e677ad577f
This commit is contained in:
nekobit 2022-10-22 22:18:07 +00:00
parent b014504119
commit f00029c5d1
7 changed files with 112 additions and 3 deletions

View file

@ -26,6 +26,7 @@ add_library(wormhole_lib
src/config/config_loader.cpp
src/config/config_instance.cpp
src/type/thread_pool.cpp
src/random.cpp
src/user.cpp
src/control.cpp
src/logger.cpp)

View file

@ -17,6 +17,7 @@
*/
#include <cstddef>
#include <stdexcept>
#include <utility>
#include "oauth.h"
@ -27,6 +28,7 @@
#include "http/httpserver.h"
#include "http/error_response.h"
#include "http/request.h"
#include "random.h"
#include "user.h"
#include "logger.h"
#include "jsonhelper.h"
@ -34,14 +36,15 @@
using namespace Protocol;
HTTP::Response Route::oauth_authorize(std::any& args,
const HTTP::Request& req,
const HTTP::RequestArgs_t& arg)
{
using namespace std::string_literals;
static Random rng{};
constexpr size_t TOKEN_SIZE = 32;
//DESTRUCT_WORMHOLE_ARGS(args);
rng.generate_token(TOKEN_SIZE);
return HTTP::Response{ "{}", HTTP::MIME::JSON };
}

40
src/random.cpp Normal file
View file

@ -0,0 +1,40 @@
/*
* 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 <cstring>
#include <algorithm>
#include "random.h"
Random::Random()
: rng()
{
}
std::string Random::generate_token(size_t len)
{
using engine = std::mt19937;
const char* syms = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-!@";
static size_t syms_len = std::strlen(syms);
engine r(rng());
std::uniform_int_distribution<engine::result_type> dist{0, syms_len - 1};
std::string result{};
result.resize(len);
std::generate_n(std::begin(result), len, [&, syms](){ return syms[dist(rng)]; });
return result;
}

39
src/random.h Normal file
View file

@ -0,0 +1,39 @@
/*
* 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/>.
*/
#pragma once
#include <string>
#include <cstddef>
#include <random>
class Random
{
public:
Random();
~Random() = default;
/**
* @brief Generates a random token-like value, mainly for OAuth.
* @param len Length of token
* @return [a-zA-Z0-9_!-@] token of size len
*/
std::string generate_token(size_t len);
private:
std::random_device rng;
};

View file

@ -21,8 +21,8 @@
#include <any>
#include "database/database.h"
// Apologies if the word "DESTRUCT" is confusing here, it simply means to cast into regular variables
#define DESTRUCT_WORMHOLE_ARGS(_arg) DB::Database* db = std::any_cast<RouteArgs>(_arg).db;
struct RouteArgs
{

View file

@ -6,3 +6,7 @@ add_test(NAME request_test COMMAND $<TARGET_FILE:request_test>)
add_executable(rsa_test rsa_gen_test.cpp)
target_link_libraries(rsa_test wormhole_lib ${OPENSSL_CRYPTO_LIBRARIES})
add_test(NAME rsa_test COMMAND $<TARGET_FILE:rsa_test>)
add_executable(random_test random_test.cpp)
target_link_libraries(random_test wormhole_lib)
add_test(NAME random_test COMMAND $<TARGET_FILE:random_test>)

22
test/random_test.cpp Normal file
View file

@ -0,0 +1,22 @@
#include <cassert>
#include <cmath>
#include <iostream>
#include "random.h"
int main()
{
Random rng{};
constexpr int start = 50;
const char sym = rng.generate_token(1)[0];
for (int i = 0; i < start; ++i)
{
std::cout << std::string((start-i), sym);
std::cout << rng.generate_token(i);
std::cout << rng.generate_token(std::abs(i));
std::cout << std::string((start-i), sym) << '\n';
}
std::cout << std::endl;
return 0;
}