Torc  0.1
torcuser.cpp
Go to the documentation of this file.
1 /* Class TorcUser
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2018
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Qt
24 #include <QRegExp>
25 #include <QMutex>
26 
27 // Torc
28 #include "torclogging.h"
29 #include "torclocalcontext.h"
30 #include "torcadminthread.h"
31 #include "torclanguage.h"
32 #include "torcuser.h"
33 
34 #define TORC_DEFAULT_USERNAME QStringLiteral("admin")
35 #define TORC_DEFAULT_CREDENTIALS QStringLiteral("f0f825afb7ee5ca70ba178463f360d4b")
36 
37 QString TorcUser::gUserName = QStringLiteral();
38 QByteArray TorcUser::gUserCredentials = QByteArray();
39 QMutex TorcUser::gUserCredentialsLock(QMutex::Recursive);
40 
42  : QObject(),
43  TorcHTTPService(this, TORC_USER_SERVICE, QStringLiteral("user"), TorcUser::staticMetaObject, QStringLiteral("")),
44  m_userName(),
45  m_userNameSetting(new TorcSetting(nullptr, QStringLiteral("UserName"), QStringLiteral("User name"), TorcSetting::String, TorcSetting::Persistent, QVariant(TORC_DEFAULT_USERNAME))),
46  m_userCredentials(new TorcSetting(nullptr, QStringLiteral("UserCredentials"), QStringLiteral("Authentication string"), TorcSetting::String, TorcSetting::Persistent, QVariant(TORC_DEFAULT_CREDENTIALS))),
47  m_canRestartTorc(true),
48  m_canStopTorc(true)
49 {
50  {
51  QMutexLocker locker(&gUserCredentialsLock);
52  gUserName = m_userNameSetting->GetValue().toString();
53  gUserCredentials = m_userCredentials->GetValue().toString().toLower().toLatin1();
54  m_userName = gUserName;
55  }
56 
57  connect(m_userNameSetting, static_cast<void (TorcSetting::*)(QString&)>(&TorcSetting::ValueChanged), this, &TorcUser::UpdateUserName);
58  connect(m_userCredentials, static_cast<void (TorcSetting::*)(QString&)>(&TorcSetting::ValueChanged), this, &TorcUser::UpdateCredentials);
59 }
60 
62 {
63  m_userNameSetting->Remove();
64  m_userNameSetting->DownRef();
65  m_userCredentials->Remove();
66  m_userCredentials->DownRef();
67 }
68 
69 QString TorcUser::GetName(void)
70 {
71  QMutexLocker locker(&gUserCredentialsLock);
72  return gUserName;
73 }
74 
75 QString TorcUser::GetUserName(void)
76 {
77  QMutexLocker locker(&gUserCredentialsLock);
78  return m_userName;
79 }
80 
81 QByteArray TorcUser::GetCredentials(void)
82 {
83  QMutexLocker locker(&gUserCredentialsLock);
84  return gUserCredentials;
85 }
86 
88 {
89  QMutexLocker locker(&gUserCredentialsLock);
90  m_userName = Name;
91  gUserName = Name;
92 }
93 
94 void TorcUser::UpdateCredentials(QString &Credentials)
95 {
96  QMutexLocker locker(&gUserCredentialsLock);
97  gUserCredentials = Credentials.toLower().toLatin1();
98 }
99 
100 bool TorcUser::SetUserCredentials(const QString &Name, const QString &Credentials)
101 {
102  QWriteLocker locker(&m_httpServiceLock);
103 
104  if (Name == gUserName && Credentials.toLower() == GetCredentials())
105  {
106  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("New credentials match old - not changing"));
107  return false;
108  }
109 
110  // enforce minimum size (4) and alphanumeric and underscore
111  static QRegExp gReg1("[\\w]{4,}");
112  if (!gReg1.exactMatch(Name))
113  {
114  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Password unacceptable"));
115  return false;
116  }
117 
118  // N.B. MD5 credentials are a hexadecimal representation of a binary. As such they can
119  // contain either upper or lower case digits.
120  static QRegExp gReg2("[0-9a-fA-F]{32}");
121  if (!gReg2.exactMatch(Credentials))
122  {
123  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("User credentials hash unacceptable"));
124  return false;
125  }
126 
127  // but an MD5 hash with upper case letters produces different results when
128  // rehashed... so set to lower
129  m_userNameSetting->SetValue(QString(Name));
130  m_userCredentials->SetValue(QString(Credentials.toLower()));
131 
133  return true;
134 }
135 
136 void TorcUser::SubscriberDeleted(QObject *Subscriber)
137 {
139 }
140 
142 {
144 }
145 
147 {
148  QReadLocker locker(&m_httpServiceLock);
149  return m_canStopTorc;
150 }
151 
153 {
154  // NB could be called from any thread
156 }
157 
159 {
160  QReadLocker locker(&m_httpServiceLock);
161  return m_canRestartTorc;
162 }
163 
165 {
166  Q_DECLARE_TR_FUNCTIONS(TorcUserObject)
167 
168  public:
170  {
171  }
172 
173  ~TorcUserObject() = default;
174 
175  void GetStrings(QVariantMap &Strings)
176  {
177  Strings.insert(QStringLiteral("LoggedInUserTr"), QCoreApplication::translate("TorcUser", "Logged in as %1"));
178  Strings.insert(QStringLiteral("RestartTorcTr"), QCoreApplication::translate("TorcUser", "Restart Torc"));
179  Strings.insert(QStringLiteral("ConfirmRestartTorc"), QCoreApplication::translate("TorcUser", "Are you sure you want to restart Torc?"));
180  Strings.insert(QStringLiteral("StopTorcTr"), QCoreApplication::translate("TorcUser", "Stop Torc"));
181  Strings.insert(QStringLiteral("ConfirmStopTorc"), QCoreApplication::translate("TorcUser", "Are you sure you want to stop Torc?"));
182  Strings.insert(QStringLiteral("ViewConfigTr"), QCoreApplication::translate("TorcUser", "View configuration"));
183  Strings.insert(QStringLiteral("ViewConfigTitleTr"), QCoreApplication::translate("TorcUser", "Current configuration"));
184  Strings.insert(QStringLiteral("ViewDOTTr"), QCoreApplication::translate("TorcUser", "View DOT"));
185  Strings.insert(QStringLiteral("ViewDOTTitleTr"), QCoreApplication::translate("TorcUser", "Stategraph Description"));
186  Strings.insert(QStringLiteral("ViewXSDTr"), QCoreApplication::translate("TorcUser", "View XSD"));
187  Strings.insert(QStringLiteral("ViewXSDTitleTr"), QCoreApplication::translate("TorcUser", "Configuration schema"));
188  Strings.insert(QStringLiteral("ViewAPITr"), QCoreApplication::translate("TorcUser", "View API"));
189  Strings.insert(QStringLiteral("ViewAPITitleTr"), QCoreApplication::translate("TorcUser", "API reference"));
190  Strings.insert(QStringLiteral("ViewLogTr"), QCoreApplication::translate("TorcUser", "View Log"));
191  Strings.insert(QStringLiteral("RefreshTr"), QCoreApplication::translate("TorcUser", "Refresh"));
192  Strings.insert(QStringLiteral("FollowLogTr"), QCoreApplication::translate("TorcUser", "Follow Log"));
193  Strings.insert(QStringLiteral("FollowTr"), QCoreApplication::translate("TorcUser", "Follow"));
194  Strings.insert(QStringLiteral("UnfollowTr"), QCoreApplication::translate("TorcUser", "Unfollow"));
195  Strings.insert(QStringLiteral("Value"), QCoreApplication::translate("TorcUser", "Value"));
196  Strings.insert(QStringLiteral("Valid"), QCoreApplication::translate("TorcUser", "Valid"));
197  Strings.insert(QStringLiteral("CloseTr"), QCoreApplication::translate("TorcUser", "Close"));
198  Strings.insert(QStringLiteral("SettingsTr"), QCoreApplication::translate("TorcUser", "Settings"));
199  Strings.insert(QStringLiteral("ConfirmTr"), QCoreApplication::translate("TorcUser", "Confirm"));
200  Strings.insert(QStringLiteral("CancelTr"), QCoreApplication::translate("TorcUser", "Cancel"));
201  Strings.insert(QStringLiteral("ChangeCredsTr"), QCoreApplication::translate("TorcUser", "Change username and password"));
202  Strings.insert(QStringLiteral("UsernameTr"), QCoreApplication::translate("TorcUser", "Username"));
203  Strings.insert(QStringLiteral("PasswordTr"), QCoreApplication::translate("TorcUser", "Password"));
204  Strings.insert(QStringLiteral("Username2Tr"), QCoreApplication::translate("TorcUser", "Confirm username"));
205  Strings.insert(QStringLiteral("Password2Tr"), QCoreApplication::translate("TorcUser", "Confirm password"));
206  Strings.insert(QStringLiteral("CredentialsHelpTr"), QCoreApplication::translate("TorcUser", "Usernames and passwords are case sensitive, must be at least 4 characters long and can only contain letters, numbers and \'_\' (underscore)."));
207  Strings.insert(QStringLiteral("ServicesTr"), QCoreApplication::translate("TorcUser", "Services"));
208  Strings.insert(QStringLiteral("ReturnformatsTr"), QCoreApplication::translate("TorcUser", "Return formats"));
209  Strings.insert(QStringLiteral("WebSocketsTr"), QCoreApplication::translate("TorcUser", "WebSockets"));
210  Strings.insert(QStringLiteral("AvailableservicesTr"),QCoreApplication::translate("TorcUser", "Available services"));
211  Strings.insert(QStringLiteral("IDTr"), QCoreApplication::translate("TorcUser", "ID"));
212  Strings.insert(QStringLiteral("NameTr"), QCoreApplication::translate("TorcUser", "Name"));
213  Strings.insert(QStringLiteral("PathTr"), QCoreApplication::translate("TorcUser", "Path"));
214  Strings.insert(QStringLiteral("DetailsTr"), QCoreApplication::translate("TorcUser", "Details"));
215  Strings.insert(QStringLiteral("HTTPreturnformatsTr"),QCoreApplication::translate("TorcUser", "Supported HTTP return formats"));
216  Strings.insert(QStringLiteral("ContenttypeTr"), QCoreApplication::translate("TorcUser", "Content type"));
217  Strings.insert(QStringLiteral("WSsubprotocolsTr"), QCoreApplication::translate("TorcUser", "Supported WebSocket subprotocols"));
218  Strings.insert(QStringLiteral("DescriptionTr"), QCoreApplication::translate("TorcUser", "Description"));
219  Strings.insert(QStringLiteral("ParametersTr"), QCoreApplication::translate("TorcUser", "Parameters"));
220  Strings.insert(QStringLiteral("JSreturntypeTr"), QCoreApplication::translate("TorcUser", "Javascript return type"));
221  Strings.insert(QStringLiteral("MethodlistTr"), QCoreApplication::translate("TorcUser", "Method list"));
222  Strings.insert(QStringLiteral("GetterTr"), QCoreApplication::translate("TorcUser", "Getter"));
223  Strings.insert(QStringLiteral("NotificationTr"), QCoreApplication::translate("TorcUser", "Notification"));
224  Strings.insert(QStringLiteral("UpdateTr"), QCoreApplication::translate("TorcUser", "Update"));
225  }
226 
227  void Create(void)
228  {
229  }
230 
231  void Destroy(void)
232  {
233  }
#define TORC_DEFAULT_CREDENTIALS
Definition: torcuser.cpp:35
void StopTorc(void)
Definition: torcuser.cpp:141
QVariant GetValue(void)
A wrapper around a database setting.
Definition: torcsetting.h:15
bool GetCanRestartTorc(void)
Definition: torcuser.cpp:158
A factory class for automatically running objects outside of the main loop.
void RestartTorc(void)
Definition: torcuser.cpp:152
QString GetUserName(void)
Definition: torcuser.cpp:75
void Create(void)
Definition: torcuser.cpp:227
virtual bool DownRef(void)
#define TORC_USER_SERVICE
Definition: torclocaldefs.h:13
static void NotifyEvent(int Event)
#define TORC_DEFAULT_USERNAME
Definition: torcuser.cpp:34
static QMutex gUserCredentialsLock
Definition: torcuser.h:48
void Remove(void)
void Destroy(void)
Definition: torcuser.cpp:231
VERBOSE_PREAMBLE true
#define TORC_ADMIN_LOW_PRIORITY
static QByteArray GetCredentials(void)
Definition: torcuser.cpp:81
bool GetCanStopTorc(void)
Definition: torcuser.cpp:146
QString Name(void) const
void HandleSubscriberDeleted(QObject *Subscriber)
bool SetUserCredentials(const QString &Name, const QString &Credentials)
Definition: torcuser.cpp:100
QReadWriteLock m_httpServiceLock
static QString GetName(void)
Definition: torcuser.cpp:69
static QString gUserName
Definition: torcuser.h:46
TorcUserObject TorcUserObject
bool SetValue(const QVariant &Value)
static QByteArray gUserCredentials
Definition: torcuser.h:47
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
void GetStrings(QVariantMap &Strings)
Definition: torcuser.cpp:175
void UpdateUserName(QString &Name)
Definition: torcuser.cpp:87
A factory class to register translatable strings for use with external interfaces/applications.
Definition: torclanguage.h:66
void UpdateCredentials(QString &Credentials)
Definition: torcuser.cpp:94
void ValueChanged(int Value)
TorcUser()
Definition: torcuser.cpp:41
void SubscriberDeleted(QObject *Subscriber)
Definition: torcuser.cpp:136
~TorcUser()
Definition: torcuser.cpp:61