Cutelyst  3.5.0
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 <QtCore/QCryptographicHash>
9 
10 #include <Cutelyst/cutelyst_global.h>
11 #include <Cutelyst/Plugins/Authentication/authentication.h>
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 
34  AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final;
35 
39  QString passwordField() const;
40 
44  void setPasswordField(const QString &fieldName);
45 
49  PasswordType passwordType() const;
50 
54  void setPasswordType(PasswordType type);
55 
59  QString passwordPreSalt() const;
60 
64  void setPasswordPreSalt(const QString &passwordPreSalt);
65 
69  QString passwordPostSalt() const;
70 
74  void setPasswordPostSalt(const QString &passwordPostSalt);
75 
79  static bool validatePassword(const QByteArray &password, const QByteArray &correctHash);
80 
84  static bool validatePassword(const QString &password, const QString &correctHash);
85 
96  static QByteArray createPassword(const QByteArray &password, QCryptographicHash::Algorithm method, int iterations, int saltByteSize, int hashByteSize);
97 
104  static QByteArray createPassword(const QByteArray &password);
105 
112  inline static QString createPassword(const QString &password);
113 
123  static QByteArray pbkdf2(QCryptographicHash::Algorithm method,
124  const QByteArray &password, const QByteArray &salt,
125  int rounds, int keyLength);
126 
130  static QByteArray hmac(QCryptographicHash::Algorithm method, const QByteArray &key, const QByteArray &message);
131 
132 protected:
133  CredentialPasswordPrivate *d_ptr;
134 };
135 
136 inline bool CredentialPassword::validatePassword(const QString &password, const QString &correctHash)
137 {
138  return validatePassword(password.toUtf8(), correctHash.toLatin1());
139 }
140 
142 {
143  return QString::fromLatin1(createPassword(password.toUtf8()));
144 }
145 
146 } // namespace Plugin
147 
148 #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
QByteArray toLatin1() const const
QString fromLatin1(const char *str, int size)
static bool validatePassword(const QByteArray &password, const QByteArray &correctHash)
Validates the given password against the correct hash.
QByteArray toUtf8() const const