droplet 1.1.0
A multipurpose Discord bot with the hacker in mind
 
Loading...
Searching...
No Matches
AuthUtils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Poco/HexBinaryEncoder.h"
4#include "Poco/JWT/Signer.h"
5#include "Poco/JWT/Token.h"
6#include "Poco/Net/HTTPServerRequest.h"
7#include "Poco/SHA2Engine.h"
8#include "Secrets.hpp"
9
10//-----------------------------------------------------
11//
12//-----------------------------------------------------
13static inline std::string hashPassword(const std::string& password) {
14 Poco::SHA2Engine sha256;
15 sha256.update(password);
16 const Poco::DigestEngine::Digest& digest = sha256.digest();
17
18 std::stringstream ss;
19 Poco::HexBinaryEncoder hexEncoder(ss);
20 hexEncoder.write(reinterpret_cast<const char*>(&digest[0]), static_cast<long>(digest.size()));
21 hexEncoder.close();
22
23 return ss.str();
24}
25
26//-----------------------------------------------------
27//
28//-----------------------------------------------------
29static inline bool verifyPassword(const std::string& password, const std::string& storedHash) {
30 return hashPassword(password) == storedHash;
31}
32
33//-----------------------------------------------------
34//
35//-----------------------------------------------------
36enum AuthClearance { NONE = 0, PUBLIC = 10, PRIVATE = 100, SECRET = 1000, TOP_SECRET = 10000, BLACK = 100000 };
37
38//-----------------------------------------------------
39//
40//-----------------------------------------------------
41struct AuthUtil {
42 // Returns { sub, roles } on success; empty map on failure
43 [[nodiscard]]
44 static inline bool is_authorized(const Poco::Net::HTTPServerRequest& req,
45 AuthClearance minClearance = AuthClearance::SECRET) {
46 if (minClearance == AuthClearance::NONE)
47 return true; // no login required
48 if (not req.has("Authorization"))
49 return false; // not logged in
50
51 const auto auth = req.get("Authorization");
52 const std::string bearer = "Bearer ";
53
54 if (auth.rfind(bearer, 0) != 0)
55 return false;
56
57 // extract the token
58 const std::string jwt = auth.substr(bearer.size());
59
60 try {
61 const auto& sec = Secrets::getInstance();
62 Poco::JWT::Signer signer(sec.getSecret("jwt_secret"));
63 Poco::JWT::Token token;
64 signer.tryVerify(jwt, token);
65
66 const auto& claims{token.payload()};
67 for (const auto& claim : claims) {
68 const auto& key{claim.first};
69 const auto& value{claim.second};
70
71 if (key == "prv") {
72 if (value.convert<int>() >= minClearance) {
73 return true;
74 } else {
75 return false;
76 }
77 }
78 }
79 } catch (...) {
80 return false;
81 }
82 return false;
83 }
84};
AuthClearance
Definition AuthUtils.hpp:36
@ PRIVATE
Definition AuthUtils.hpp:36
@ SECRET
Definition AuthUtils.hpp:36
@ TOP_SECRET
Definition AuthUtils.hpp:36
@ NONE
Definition AuthUtils.hpp:36
@ PUBLIC
Definition AuthUtils.hpp:36
@ BLACK
Definition AuthUtils.hpp:36
static Secrets & getInstance()
Definition Secrets.cpp:19
Definition AuthUtils.hpp:41
static bool is_authorized(const Poco::Net::HTTPServerRequest &req, AuthClearance minClearance=AuthClearance::SECRET)
Definition AuthUtils.hpp:44