move types to types.hpp, match hash to email
This commit is contained in:
parent
cbd86c7000
commit
cb354e666d
23
src/hash.cpp
23
src/hash.cpp
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
#include "hash.hpp"
|
#include "hash.hpp"
|
||||||
|
|
||||||
|
#include "types.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -30,14 +32,14 @@
|
|||||||
|
|
||||||
namespace libravatarserv::hash {
|
namespace libravatarserv::hash {
|
||||||
|
|
||||||
std::string generate_hash(const algorithm algo, const std::string_view text) {
|
std::string generate_hash(const algorithm_t algo, const std::string_view text) {
|
||||||
using namespace CryptoPP;
|
using namespace CryptoPP;
|
||||||
|
|
||||||
SHA256 sha256;
|
SHA256 sha256;
|
||||||
Weak::MD5 md5;
|
Weak::MD5 md5;
|
||||||
std::unique_ptr<HashTransformation> algo_to_use{&sha256};
|
HashTransformation *algo_to_use{&sha256}; // HashFilter calls free()?
|
||||||
if (algo == algorithm::MD5) {
|
if (algo == algorithm_t::MD5) {
|
||||||
algo_to_use.reset(&md5);
|
algo_to_use = &md5;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string hash;
|
std::string hash;
|
||||||
@ -49,18 +51,23 @@ std::string generate_hash(const algorithm algo, const std::string_view text) {
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
table generate_table(const algorithm algo, const fs::path &directory) {
|
std::string find_email(const request_t &req, const fs::path &directory) {
|
||||||
table hashtable;
|
bool default_present{false};
|
||||||
for (const fs::path &path : fs::recursive_directory_iterator(directory)) {
|
for (const fs::path &path : fs::recursive_directory_iterator(directory)) {
|
||||||
if (fs::is_regular_file(path)) {
|
if (fs::is_regular_file(path)) {
|
||||||
std::string email = path.filename();
|
std::string email = path.filename();
|
||||||
std::transform(email.begin(), email.end(), email.begin(),
|
std::transform(email.begin(), email.end(), email.begin(),
|
||||||
::tolower);
|
::tolower);
|
||||||
hashtable.insert({generate_hash(algo, email), email});
|
if (generate_hash(req.algo, email) == req.hash) {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
if (email == "default") {
|
||||||
|
default_present = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return hashtable;
|
return default_present ? "default" : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_valid(const std::string_view hash) {
|
bool is_valid(const std::string_view hash) {
|
||||||
|
17
src/hash.hpp
17
src/hash.hpp
@ -18,25 +18,20 @@
|
|||||||
#define LIBRAVATARSERV_HASH_HPP
|
#define LIBRAVATARSERV_HASH_HPP
|
||||||
|
|
||||||
#include "fs-compat.hpp"
|
#include "fs-compat.hpp"
|
||||||
|
#include "types.hpp"
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
namespace libravatarserv::hash {
|
namespace libravatarserv::hash {
|
||||||
|
|
||||||
enum class algorithm {
|
|
||||||
SHA256,
|
|
||||||
MD5
|
|
||||||
};
|
|
||||||
|
|
||||||
using table = std::map<std::string, std::string>;
|
|
||||||
|
|
||||||
// generate hash from text
|
// generate hash from text
|
||||||
[[nodiscard]] std::string generate_hash(algorithm algo, std::string_view text);
|
[[nodiscard]] std::string generate_hash(algorithm_t algo,
|
||||||
|
std::string_view text);
|
||||||
|
|
||||||
// generate hash -> email table
|
// Match hash to email
|
||||||
[[nodiscard]] table generate_table(algorithm algo, const fs::path &directory);
|
[[nodiscard]] std::string find_email(const request_t &req,
|
||||||
|
const fs::path &directory);
|
||||||
|
|
||||||
// return false if invalid characters are found in hash
|
// return false if invalid characters are found in hash
|
||||||
[[nodiscard]] bool is_valid(std::string_view hash);
|
[[nodiscard]] bool is_valid(std::string_view hash);
|
||||||
|
@ -43,9 +43,11 @@ int main(int /*argc*/, char * /*argv*/[]) {
|
|||||||
if (req.fallback.empty()) {
|
if (req.fallback.empty()) {
|
||||||
req.fallback = cfg.default_fallback;
|
req.fallback = cfg.default_fallback;
|
||||||
}
|
}
|
||||||
|
const std::string email{hash::find_email(req, cfg.avatar_dir)};
|
||||||
|
|
||||||
std::cout << " HASH: " << req.hash << "\n SIZE: " << req.size
|
std::cout << " HASH: " << req.hash << "\n SIZE: " << req.size
|
||||||
<< "\n FALLBACK: " << req.fallback << '\n';
|
<< "\n FALLBACK: " << req.fallback << "\n EMAIL: " << email
|
||||||
const auto hashtable{hash::generate_table(req.algo, cfg.avatar_dir)};
|
<< '\n';
|
||||||
|
|
||||||
} catch (const std::runtime_error &e) {
|
} catch (const std::runtime_error &e) {
|
||||||
if (std::strlen(e.what()) != 0) {
|
if (std::strlen(e.what()) != 0) {
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
#include "request.hpp"
|
#include "request.hpp"
|
||||||
|
|
||||||
#include "hash.hpp"
|
#include "hash.hpp"
|
||||||
|
#include "types.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@ -27,7 +28,7 @@
|
|||||||
|
|
||||||
namespace libravatarserv::request {
|
namespace libravatarserv::request {
|
||||||
|
|
||||||
request parse(const std::string_view uri) {
|
request_t parse(const std::string_view uri) {
|
||||||
constexpr std::uint8_t offset{8};
|
constexpr std::uint8_t offset{8};
|
||||||
if (uri.substr(0, offset) != "/avatar/"
|
if (uri.substr(0, offset) != "/avatar/"
|
||||||
|| uri.find("..", offset) != std::string_view::npos) {
|
|| uri.find("..", offset) != std::string_view::npos) {
|
||||||
@ -35,7 +36,7 @@ request parse(const std::string_view uri) {
|
|||||||
throw std::runtime_error{"Invalid URL"};
|
throw std::runtime_error{"Invalid URL"};
|
||||||
}
|
}
|
||||||
|
|
||||||
request req;
|
request_t req;
|
||||||
req.size = 80;
|
req.size = 80;
|
||||||
|
|
||||||
size_t pos{uri.find('?')};
|
size_t pos{uri.find('?')};
|
||||||
@ -47,9 +48,9 @@ request parse(const std::string_view uri) {
|
|||||||
::tolower);
|
::tolower);
|
||||||
|
|
||||||
if (req.hash.size() == 64 && hash::is_valid(req.hash)) {
|
if (req.hash.size() == 64 && hash::is_valid(req.hash)) {
|
||||||
req.algo = hash::algorithm::SHA256;
|
req.algo = algorithm_t::SHA256;
|
||||||
} else if (req.hash.size() == 32 && hash::is_valid(req.hash)) {
|
} else if (req.hash.size() == 32 && hash::is_valid(req.hash)) {
|
||||||
req.algo = hash::algorithm::MD5;
|
req.algo = algorithm_t::MD5;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Status: 400 Bad Request\n\n";
|
std::cout << "Status: 400 Bad Request\n\n";
|
||||||
throw std::runtime_error{"Invalid hash"};
|
throw std::runtime_error{"Invalid hash"};
|
||||||
|
@ -17,23 +17,15 @@
|
|||||||
#ifndef LIBRAVATARSERV_REQUEST_HPP
|
#ifndef LIBRAVATARSERV_REQUEST_HPP
|
||||||
#define LIBRAVATARSERV_REQUEST_HPP
|
#define LIBRAVATARSERV_REQUEST_HPP
|
||||||
|
|
||||||
#include "hash.hpp"
|
#include "types.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
namespace libravatarserv::request {
|
namespace libravatarserv::request {
|
||||||
|
|
||||||
struct request {
|
|
||||||
std::string hash;
|
|
||||||
hash::algorithm algo;
|
|
||||||
std::int16_t size;
|
|
||||||
std::string fallback;
|
|
||||||
};
|
|
||||||
|
|
||||||
// parse request and return results
|
// parse request and return results
|
||||||
[[nodiscard]] request parse(std::string_view uri);
|
[[nodiscard]] request_t parse(std::string_view uri);
|
||||||
|
|
||||||
// get one parameter from an URI
|
// get one parameter from an URI
|
||||||
[[nodiscard]] std::string_view get_parameter(std::string_view uri,
|
[[nodiscard]] std::string_view get_parameter(std::string_view uri,
|
||||||
|
38
src/types.hpp
Normal file
38
src/types.hpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/* This file is part of libravatarserv.
|
||||||
|
* Copyright © 2022 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, version 3.
|
||||||
|
*
|
||||||
|
* 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 General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBRAVATARSERV_TYPES_HPP
|
||||||
|
#define LIBRAVATARSERV_TYPES_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
namespace libravatarserv {
|
||||||
|
|
||||||
|
enum class algorithm_t {
|
||||||
|
SHA256,
|
||||||
|
MD5
|
||||||
|
};
|
||||||
|
|
||||||
|
struct request_t {
|
||||||
|
std::string hash;
|
||||||
|
algorithm_t algo;
|
||||||
|
std::int16_t size;
|
||||||
|
std::string fallback;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace libravatarserv
|
||||||
|
|
||||||
|
#endif // LIBRAVATARSERV_TYPES_HPP
|
Loading…
x
Reference in New Issue
Block a user