SNode.C
Loading...
Searching...
No Matches
BasicAuthentication.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 * Json Middleware 2020, 2021 Marlene Mayr, Anna Moser, Matteo Prock, Eric Thalhammer
6 * Github <MarleneMayr><moseranna><MatteoMatteoMatteo><peregrin-tuk>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "express/middleware/BasicAuthentication.h"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include "utils/base64.h"
27#include "web/http/http_utils.h"
28
29#include <list>
30#include <utility>
31
32#endif /* DOXYGEN_SHOULD_SKIP_THIS */
33
34namespace express::middleware {
35
36 BasicAuthentication::BasicAuthentication(const std::string& userName, const std::string& password, const std::string& realm) {
37 std::string userNamePassword = userName + ":" + password;
38 const std::string credentials =
39 base64::base64_encode(reinterpret_cast<unsigned char*>(userNamePassword.data()), userNamePassword.length());
40
41 use([realm, credentials] MIDDLEWARE(req, res, next) {
42 const std::string authCredentials = httputils::str_split(req->get("Authorization"), ' ').second;
43
44 if (authCredentials == credentials) {
45 next();
46 } else {
47 if (authCredentials.empty()) {
48 res->set("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
49 }
50 res->sendStatus(401);
51 }
52 });
53 }
54
56 BasicAuthentication::instance(const std::string& userName, const std::string& password, const std::string& realm) {
57 // Keep the created authentication middleware alive
58 static std::list<std::shared_ptr<class BasicAuthentication>> basicAuthentications;
59
60 BasicAuthentication* basicAuthentication = new BasicAuthentication(userName, password, realm);
61 basicAuthentications.push_back(std::shared_ptr<BasicAuthentication>(basicAuthentication));
62
63 return *basicAuthentication;
64 }
65
66 class BasicAuthentication& BasicAuthentication(const std::string& userName, const std::string& password, const std::string& realm) {
67 return BasicAuthentication::instance(userName, password, realm);
68 }
69
70} // namespace express::middleware
#define MIDDLEWARE(req, res, next)
Definition Router.h:40
friend class BasicAuthentication & BasicAuthentication(const std::string &userName, const std::string &password, const std::string &realm)
BasicAuthentication(const std::string &userName, const std::string &password, const std::string &realm)