cutelyst  4.5.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-2023 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #pragma once
6 
7 #include <Cutelyst/Plugins/Authentication/authentication.h>
8 
9 #include <QtCore/QCryptographicHash>
10 
11 namespace Cutelyst {
12 
13 class CredentialPasswordPrivate;
14 
27 class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT CredentialPassword : public AuthenticationCredential
28 {
29  Q_OBJECT
30  Q_DECLARE_PRIVATE(CredentialPassword)
31 public:
35  enum PasswordType {
36  None,
38  Hashed
40  };
41  Q_ENUM(PasswordType)
42 
43 
46  explicit CredentialPassword(QObject *parent = nullptr);
47 
51  virtual ~CredentialPassword() override;
52 
58  [[nodiscard]] AuthenticationUser
59  authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final;
60 
65  [[nodiscard]] QString passwordField() const;
66 
71  void setPasswordField(const QString &fieldName);
72 
77  [[nodiscard]] PasswordType passwordType() const;
78 
83  void setPasswordType(PasswordType type);
84 
89  [[nodiscard]] QString passwordPreSalt() const;
90 
95  void setPasswordPreSalt(const QString &passwordPreSalt);
96 
101  [[nodiscard]] QString passwordPostSalt() const;
102 
107  void setPasswordPostSalt(const QString &passwordPostSalt);
108 
112  [[nodiscard]] static bool validatePassword(const QByteArray &password,
113  const QByteArray &correctHash);
114 
118  [[nodiscard]] static bool validatePassword(const QString &password, const QString &correctHash);
119 
127  [[nodiscard]] static QByteArray createPassword(const QByteArray &password,
129  int iterations,
130  int saltByteSize,
131  int hashByteSize);
132 
142  [[nodiscard]] static QByteArray createPassword(const QByteArray &password);
143 
153  [[nodiscard]] inline static QString createPassword(const QString &password);
154 
159  [[nodiscard]] static QByteArray pbkdf2(QCryptographicHash::Algorithm method,
160  const QByteArray &password,
161  const QByteArray &salt,
162  int rounds,
163  int keyLength);
164 
168  [[nodiscard]] static QByteArray hmac(QCryptographicHash::Algorithm method,
169  const QByteArray &key,
170  const QByteArray &message);
171 
172 protected:
173  CredentialPasswordPrivate *d_ptr;
174 };
175 
176 inline bool CredentialPassword::validatePassword(const QString &password,
177  const QString &correctHash)
178 {
179  return validatePassword(password.toUtf8(), correctHash.toLatin1());
180 }
181 
183 {
184  return QString::fromLatin1(createPassword(password.toUtf8()));
185 }
186 
187 } // namespace Cutelyst
Abstract class to validate authentication credentials like user name and password.
Combines user store and credential validation into a named realm.
Container for user data retrieved from an AuthenticationStore.
The Cutelyst Context.
Definition: context.h:42
Use password based authentication to authenticate a user.
static bool validatePassword(const QByteArray &password, const QByteArray &correctHash)
static QByteArray createPassword(const QByteArray &password, QCryptographicHash::Algorithm method, int iterations, int saltByteSize, int hashByteSize)
The Cutelyst namespace holds all public Cutelyst API.
QString fromLatin1(QByteArrayView str)
QByteArray toLatin1() const const
QByteArray toUtf8() const const