Cutelyst  3.1.0
credentialpassword.h
1 /*
2  * Copyright (C) 2013-2017 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 #ifndef CUTELYSTPLUGIN_CREDENTIALPASSWORD_H
19 #define CUTELYSTPLUGIN_CREDENTIALPASSWORD_H
20 
21 #include <QtCore/QCryptographicHash>
22 
23 #include <Cutelyst/cutelyst_global.h>
24 #include <Cutelyst/Plugins/Authentication/authentication.h>
25 
26 namespace Cutelyst {
27 
28 class CredentialPasswordPrivate;
29 class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT CredentialPassword : public AuthenticationCredential
30 {
31  Q_OBJECT
32  Q_DECLARE_PRIVATE(CredentialPassword)
33 public:
34  enum PasswordType {
35  None,
36  Clear,
37  Hashed
38  };
39  Q_ENUM(PasswordType)
40 
41 
44  explicit CredentialPassword(QObject *parent = nullptr);
45  virtual ~CredentialPassword() override;
46 
47  AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final;
48 
52  QString passwordField() const;
53 
57  void setPasswordField(const QString &fieldName);
58 
62  PasswordType passwordType() const;
63 
67  void setPasswordType(PasswordType type);
68 
72  QString passwordPreSalt() const;
73 
77  void setPasswordPreSalt(const QString &passwordPreSalt);
78 
82  QString passwordPostSalt() const;
83 
87  void setPasswordPostSalt(const QString &passwordPostSalt);
88 
92  static bool validatePassword(const QByteArray &password, const QByteArray &correctHash);
93 
97  static bool validatePassword(const QString &password, const QString &correctHash);
98 
109  static QByteArray createPassword(const QByteArray &password, QCryptographicHash::Algorithm method, int iterations, int saltByteSize, int hashByteSize);
110 
117  static QByteArray createPassword(const QByteArray &password);
118 
125  inline static QString createPassword(const QString &password);
126 
136  static QByteArray pbkdf2(QCryptographicHash::Algorithm method,
137  const QByteArray &password, const QByteArray &salt,
138  int rounds, int keyLength);
139 
143  static QByteArray hmac(QCryptographicHash::Algorithm method, const QByteArray &key, const QByteArray &message);
144 
145 protected:
146  CredentialPasswordPrivate *d_ptr;
147 };
148 
149 inline bool CredentialPassword::validatePassword(const QString &password, const QString &correctHash)
150 {
151  return validatePassword(password.toUtf8(), correctHash.toLatin1());
152 }
153 
155 {
156  return QString::fromLatin1(createPassword(password.toUtf8()));
157 }
158 
159 } // namespace Plugin
160 
161 #endif // CUTELYSTPLUGIN_CREDENTIALPASSWORD_H
The Cutelyst Context.
Definition: context.h:52
static bool validatePassword(const QByteArray &password, const QByteArray &correctHash)
Validates the given password against the correct hash.
static QByteArray createPassword(const QByteArray &password, QCryptographicHash::Algorithm method, int iterations, int saltByteSize, int hashByteSize)
Creates a password hash string.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QString fromLatin1(const char *str, int size)
QByteArray toLatin1() const const
QByteArray toUtf8() const const