cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
htpasswd.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2014-2022 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#include "htpasswd.h"
6
7#include "common.h"
8
9#include <QFile>
10#include <QLoggingCategory>
11#include <QTemporaryFile>
12
13using namespace Cutelyst;
14
15StoreHtpasswd::StoreHtpasswd(const QString &name)
16 : m_filename(name)
17{
18}
19
20StoreHtpasswd::~StoreHtpasswd()
21{
22}
23
25{
26 const QString username = user.value(QStringLiteral("username"));
27
28 QTemporaryFile tmp(m_filename + QLatin1String("-XXXXXXX"));
29 tmp.setAutoRemove(false); // sort of a backup
30 if (!tmp.open()) {
31 qCWarning(CUTELYST_UTILS_AUTH) << "Failed to open temporary file for writing";
32 return;
33 }
34
35 bool wrote = false;
36 QFile file(m_filename);
37 if (file.exists() && file.open(QFile::ReadWrite | QFile::Text)) {
38 while (!file.atEnd()) {
39 QByteArray line = file.readLine();
40 QByteArrayList parts = line.split(':');
41 if (!wrote && parts.size() >= 2 && parts.first() == username.toLatin1()) {
42 line = username.toLatin1() + ':' +
43 user.value(QStringLiteral("password")).toLatin1().replace(':', ',') + '\n';
44 wrote = true;
45 }
46 tmp.write(line);
47 }
48 file.close();
49 }
50
51 if (!wrote) {
52 QByteArray line = username.toLatin1() + ':' +
53 user.value(QStringLiteral("password")).toLatin1().replace(':', ',') +
54 '\n';
55 tmp.write(line);
56 }
57
58 if (file.exists() && !file.remove()) {
59 qCWarning(CUTELYST_UTILS_AUTH) << "Failed to remove auth file for replacement";
60 return;
61 }
62
63 if (!tmp.rename(m_filename)) {
64 qCWarning(CUTELYST_UTILS_AUTH) << "Failed to rename temporary file";
65 }
66}
67
69{
70 Q_UNUSED(c);
72 const QString username = userInfo.value(QStringLiteral("username"));
73
74 QFile file(m_filename);
75 if (file.open(QFile::ReadOnly | QFile::Text)) {
76 while (!file.atEnd()) {
77 QByteArray line = file.readLine();
78 QByteArrayList parts = line.trimmed().split(':');
79 if (parts.size() >= 2 && !parts.first().startsWith('#') &&
80 parts.first() == username.toLatin1()) {
81 ret.insert(QStringLiteral("username"), username);
82 ret.setId(username);
83 QByteArray password = parts.at(1);
84 ret.insert(QStringLiteral("password"),
85 QString::fromLatin1(password.replace(',', ':')));
86 break;
87 }
88 }
89 }
90 return ret;
91}
92
94{
95 Q_UNUSED(c);
96 return user.id();
97}
98
100{
101 return findUser(c, {{QStringLiteral("username"), frozenUser.toString()}});
102}
void setId(const QVariant &id)
Sets the unique user id restored from the store.
The Cutelyst Context.
Definition: context.h:38
QVariant forSession(Context *c, const AuthenticationUser &user) override final
Definition: htpasswd.cpp:93
AuthenticationUser findUser(Context *c, const ParamsMultiMap &userInfo) override final
Definition: htpasswd.cpp:68
AuthenticationUser fromSession(Context *c, const QVariant &frozenUser) override final
Definition: htpasswd.cpp:99
void addUser(const ParamsMultiMap &user)
Definition: htpasswd.cpp:24
StoreHtpasswd(const QString &name)
Definition: htpasswd.cpp:15
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap