Cutelyst  2.5.0
htpasswd.cpp
1 /*
2  * Copyright (C) 2014-2018 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "htpasswd.h"
19 
20 #include <QFile>
21 #include <QTemporaryFile>
22 #include <QLoggingCategory>
23 
24 #include "common.h"
25 
26 using namespace Cutelyst;
27 
28 StoreHtpasswd::StoreHtpasswd(const QString &name, QObject *parent) : AuthenticationStore(parent)
29  , m_filename(name)
30 {
31 
32 }
33 
34 StoreHtpasswd::~StoreHtpasswd()
35 {
36 
37 }
38 
40 {
41  const QString username = user.value(QStringLiteral("username"));
42 
43  QTemporaryFile tmp(m_filename + QLatin1String("-XXXXXXX"));
44  tmp.setAutoRemove(false); // sort of a backup
45  if (!tmp.open()) {
46  qCWarning(CUTELYST_UTILS_AUTH) << "Failed to open temporary file for writing";
47  return;
48  }
49 
50  bool wrote = false;
51  QFile file(m_filename);
52  if (file.exists() && file.open(QFile::ReadWrite | QFile::Text)) {
53  while (!file.atEnd()) {
54  QByteArray line = file.readLine();
55  QByteArrayList parts = line.split(':');
56  if (!wrote && parts.size() >= 2 && parts.first() == username.toLatin1()) {
57  line = username.toLatin1() + ':' + user.value(QStringLiteral("password")).toLatin1().replace(':', ',') + '\n';
58  wrote = true;
59  }
60  tmp.write(line);
61  }
62  file.close();
63  }
64 
65  if (!wrote) {
66  QByteArray line = username.toLatin1() + ':' + user.value(QStringLiteral("password")).toLatin1().replace(':', ',') + '\n';
67  tmp.write(line);
68  }
69 
70  if (file.exists() && !file.remove()) {
71  qCWarning(CUTELYST_UTILS_AUTH) << "Failed to remove auth file for replacement";
72  return;
73  }
74 
75  if (!tmp.rename(m_filename)) {
76  qCWarning(CUTELYST_UTILS_AUTH) << "Failed to rename temporary file";
77  }
78 }
79 
81 {
83  const QString username = userInfo.value(QStringLiteral("username"));
84 
85  QFile file(m_filename);
86  if (file.open(QFile::ReadOnly | QFile::Text)) {
87  while (!file.atEnd()) {
88  QByteArray line = file.readLine();
89  QByteArrayList parts = line.trimmed().split(':');
90  if (parts.size() >= 2 && !parts.first().startsWith('#') && parts.first() == username.toLatin1()) {
91  ret.insert(QStringLiteral("username"), username);
92  ret.setId(username);
93  QByteArray password = parts.at(1);
94  ret.insert(QStringLiteral("password"), QString::fromLatin1(password.replace(',', ':')));
95  break;
96  }
97  }
98  }
99  return ret;
100 }
101 
103 {
104  return user.id();
105 }
106 
108 {
109  return findUser(c, {
110  {QStringLiteral("username"), frozenUser.toString()}
111  });
112 }
113 
114 #include "moc_htpasswd.cpp"
QMap< QString, QString > ParamsMultiMap
void setId(const QVariant &id)
Sets the unique user id restored from the store.
void addUser(const ParamsMultiMap &user)
Definition: htpasswd.cpp:39
The Cutelyst Context.
Definition: context.h:50
virtual AuthenticationUser fromSession(Context *c, const QVariant &frozenUser) final
Definition: htpasswd.cpp:107
virtual QVariant forSession(Context *c, const AuthenticationUser &user) final
Definition: htpasswd.cpp:102
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
StoreHtpasswd(const QString &name, QObject *parent=nullptr)
Definition: htpasswd.cpp:28
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userInfo) final
Definition: htpasswd.cpp:80