Decentralised Art Server
High-performance C++ backend that exposes HTML interface and a secure REST API for managing Performative Transactions entities
 
Loading...
Searching...
No Matches
auth.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <random>
4#include <string>
5#include <expected>
6#include <format>
7#include <regex>
8
9#include <absl/container/flat_hash_map.h>
10#include <spdlog/spdlog.h>
11#include <secp256k1.h>
12#include <secp256k1_recovery.h>
13#include <jwt-cpp/jwt.h>
14
15#include "async.hpp"
16#include "keccak256.hpp"
17#include "chain.hpp"
18
19namespace dcn::auth
20{
21 struct AuthError
22 {
23 enum class Kind : std::uint8_t
24 {
25 UNKNOWN = 0,
26
29
31
33
35 }
37
38 std::string message = "";
39 };
40
42 {
43 public:
44 AuthManager() = delete;
45 AuthManager(asio::io_context & io_context);
46
47 AuthManager(const AuthManager&) = delete;
49
50 ~AuthManager() = default;
51
52 asio::awaitable<std::string> generateNonce(const chain::Address & address);
53
54 asio::awaitable<bool> verifyNonce(const chain::Address & address, const std::string & nonce);
55
56 asio::awaitable<bool> verifySignature(const chain::Address & address, const std::string& signature, const std::string& message);
57
58 asio::awaitable<std::string> generateAccessToken(const chain::Address & address);
59
60 asio::awaitable<std::expected<chain::Address, AuthError>> verifyAccessToken(std::string token) const;
61
62 asio::awaitable<bool> compareAccessToken(const chain::Address & address, std::string token) const;
63
64 asio::awaitable<void> invalidateAccessToken(const chain::Address & address);
65
66 private:
67 asio::strand<asio::io_context::executor_type> _strand;
68
69 const std::string _SECRET; // !!! TODO !!! use secure secret in production
70
71 static std::random_device _rng;
72
73 std::uniform_int_distribution<int> _dist;
74 absl::flat_hash_map<chain::Address, std::string> _nonces;
75
76 absl::flat_hash_map<chain::Address, std::string> _access_tokens;
77 };
78}
79
80template <>
81struct std::formatter<dcn::auth::AuthError::Kind> : std::formatter<std::string> {
82 auto format(const dcn::auth::AuthError::Kind & err, format_context& ctx) const {
83 switch(err)
84 {
85 case dcn::auth::AuthError::Kind::MISSING_TOKEN : return formatter<string>::format("Missing token", ctx);
86 case dcn::auth::AuthError::Kind::INVALID_TOKEN : return formatter<string>::format("Invalid token", ctx);
87 case dcn::auth::AuthError::Kind::INVALID_SIGNATURE : return formatter<string>::format("Invalid signature", ctx);
88 case dcn::auth::AuthError::Kind::INVALID_NONCE : return formatter<string>::format("Invalid nonce", ctx);
89 case dcn::auth::AuthError::Kind::INVALID_ADDRESS : return formatter<string>::format("Invalid address", ctx);
90
91 default: return formatter<string>::format("Unknown", ctx);
92 }
93 return formatter<string>::format("", ctx);
94 }
95};
Definition auth.hpp:42
AuthManager(const AuthManager &)=delete
asio::awaitable< std::string > generateAccessToken(const chain::Address &address)
Definition auth.cpp:87
asio::awaitable< std::expected< chain::Address, AuthError > > verifyAccessToken(std::string token) const
Definition auth.cpp:104
asio::awaitable< std::string > generateNonce(const chain::Address &address)
Definition auth.cpp:16
asio::awaitable< void > invalidateAccessToken(const chain::Address &address)
Definition auth.cpp:201
asio::awaitable< bool > verifySignature(const chain::Address &address, const std::string &signature, const std::string &message)
Definition auth.cpp:45
AuthManager & operator=(const AuthManager &)=delete
asio::awaitable< bool > verifyNonce(const chain::Address &address, const std::string &nonce)
Definition auth.cpp:26
asio::awaitable< bool > compareAccessToken(const chain::Address &address, std::string token) const
Definition auth.cpp:184
Definition auth.hpp:20
evmc::address Address
Definition address.hpp:18
Definition decentralised_art.hpp:33
Definition auth.hpp:22
std::string message
Definition auth.hpp:38
Kind
Definition auth.hpp:24
enum dcn::auth::AuthError::Kind kind
auto format(const dcn::auth::AuthError::Kind &err, format_context &ctx) const
Definition auth.hpp:82