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
13namespace Cutelyst {
14
15class CredentialPasswordPrivate;
16class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT CredentialPassword : public AuthenticationCredential
17{
19 Q_DECLARE_PRIVATE(CredentialPassword)
20public:
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
61
66
71
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
129 const QByteArray &password,
130 const QByteArray &salt,
131 int rounds,
132 int keyLength);
133
138 const QByteArray &key,
139 const QByteArray &message);
140
141protected:
142 CredentialPasswordPrivate *d_ptr;
143};
144
146 const QString &correctHash)
147{
148 return validatePassword(password.toUtf8(), correctHash.toLatin1());
149}
150
155
156} // namespace Cutelyst
157
158#endif // CUTELYSTPLUGIN_CREDENTIALPASSWORD_H
AuthenticationCredential(QObject *parent=nullptr)
Constructs a new AuthenticationCredential object with the given parent.
The Cutelyst Context.
Definition context.h:39
void setPasswordType(PasswordType type)
Sets the type of password this class will be dealing with.
QString passwordField() const
Returns the field to look for when authenticating the user.
void setPasswordPostSalt(const QString &passwordPostSalt)
Sets the salt string to be appended to the password.
CredentialPassword(QObject *parent=nullptr)
Constructs a new CredentialPassword object with the given parent.
AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final
Tries to authenticate the authinfo using the give realm.
static QByteArray pbkdf2(QCryptographicHash::Algorithm method, const QByteArray &password, const QByteArray &salt, int rounds, int keyLength)
Generates a pbkdf2 string for the given password.
static bool validatePassword(const QByteArray &password, const QByteArray &correctHash)
Validates the given password against the correct hash.
QString passwordPreSalt() const
Returns the salt string to be prepended to the password.
PasswordType passwordType() const
Returns the type of password this class will be dealing with.
QString passwordPostSalt() const
Returns the salt string to be appended to the password.
static QByteArray createPassword(const QByteArray &password, QCryptographicHash::Algorithm method, int iterations, int saltByteSize, int hashByteSize)
Creates a password hash string.
void setPasswordField(const QString &fieldName)
Sets the field to look for when authenticating the user.
static QByteArray hmac(QCryptographicHash::Algorithm method, const QByteArray &key, const QByteArray &message)
Generates the Hash-based message authentication code.
void setPasswordPreSalt(const QString &passwordPreSalt)
Sets the salt string to be prepended to the password.
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
QObject(QObject *parent)
Q_ENUM(...)
Q_OBJECTQ_OBJECT
QObject * parent() const const
QString fromLatin1(const char *str, int size)
QByteArray toLatin1() const const
QByteArray toUtf8() const const