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