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