cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
credentialpassword.h
1 /*
2  * SPDX-FileCopyrightText: (C) 2013-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef CUTELYSTPLUGIN_CREDENTIALPASSWORD_H
6 #define CUTELYSTPLUGIN_CREDENTIALPASSWORD_H
7 
8 #include <Cutelyst/Plugins/Authentication/authentication.h>
9 #include <Cutelyst/cutelyst_global.h>
10 
11 #include <QtCore/QCryptographicHash>
12 
13 namespace Cutelyst {
14 
15 class CredentialPasswordPrivate;
16 class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT CredentialPassword : public AuthenticationCredential
17 {
18  Q_OBJECT
19  Q_DECLARE_PRIVATE(CredentialPassword)
20 public:
21  enum PasswordType {
22  None,
23  Clear,
24  Hashed,
25  };
26  Q_ENUM(PasswordType)
27 
28 
31  explicit CredentialPassword(QObject *parent = nullptr);
32  virtual ~CredentialPassword() override;
33 
35  authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final;
36 
40  QString passwordField() const;
41 
45  void setPasswordField(const QString &fieldName);
46 
50  PasswordType passwordType() const;
51 
55  void setPasswordType(PasswordType type);
56 
60  QString passwordPreSalt() const;
61 
65  void setPasswordPreSalt(const QString &passwordPreSalt);
66 
70  QString passwordPostSalt() const;
71 
75  void setPasswordPostSalt(const QString &passwordPostSalt);
76 
80  static bool validatePassword(const QByteArray &password, const QByteArray &correctHash);
81 
85  static bool validatePassword(const QString &password, const QString &correctHash);
86 
97  static QByteArray createPassword(const QByteArray &password,
99  int iterations,
100  int saltByteSize,
101  int hashByteSize);
102 
109  static QByteArray createPassword(const QByteArray &password);
110 
117  inline static QString createPassword(const QString &password);
118 
128  static QByteArray pbkdf2(QCryptographicHash::Algorithm method,
129  const QByteArray &password,
130  const QByteArray &salt,
131  int rounds,
132  int keyLength);
133 
137  static QByteArray hmac(QCryptographicHash::Algorithm method,
138  const QByteArray &key,
139  const QByteArray &message);
140 
141 protected:
142  CredentialPasswordPrivate *d_ptr;
143 };
144 
145 inline bool CredentialPassword::validatePassword(const QString &password,
146  const QString &correctHash)
147 {
148  return validatePassword(password.toUtf8(), correctHash.toLatin1());
149 }
150 
152 {
153  return QString::fromLatin1(createPassword(password.toUtf8()));
154 }
155 
156 } // namespace Cutelyst
157 
158 #endif // CUTELYSTPLUGIN_CREDENTIALPASSWORD_H
static QByteArray createPassword(const QByteArray &password, QCryptographicHash::Algorithm method, int iterations, int saltByteSize, int hashByteSize)
Creates a password hash string.
The Cutelyst Context.
Definition: context.h:38
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
QString fromLatin1(QByteArrayView str)
QByteArray toLatin1() const const
static bool validatePassword(const QByteArray &password, const QByteArray &correctHash)
Validates the given password against the correct hash.
QByteArray toUtf8() const const