cutelyst 4.3.0
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#include <Cutelyst/cutelyst_global.h>
9
10#include <QtCore/QCryptographicHash>
11
12namespace Cutelyst {
13
14class CredentialPasswordPrivate;
15
28class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT CredentialPassword : public AuthenticationCredential
29{
30 Q_OBJECT
31 Q_DECLARE_PRIVATE(CredentialPassword)
32public:
39 Hashed
41 };
42 Q_ENUM(PasswordType)
43
44
47 explicit CredentialPassword(QObject *parent = nullptr);
48
52 virtual ~CredentialPassword() override;
53
59 [[nodiscard]] AuthenticationUser
60 authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final;
61
66 [[nodiscard]] QString passwordField() const;
67
72 void setPasswordField(const QString &fieldName);
73
78 [[nodiscard]] PasswordType passwordType() const;
79
84 void setPasswordType(PasswordType type);
85
90 [[nodiscard]] QString passwordPreSalt() const;
91
96 void setPasswordPreSalt(const QString &passwordPreSalt);
97
102 [[nodiscard]] QString passwordPostSalt() const;
103
108 void setPasswordPostSalt(const QString &passwordPostSalt);
109
113 [[nodiscard]] static bool validatePassword(const QByteArray &password,
114 const QByteArray &correctHash);
115
119 [[nodiscard]] static bool validatePassword(const QString &password, const QString &correctHash);
120
128 [[nodiscard]] static QByteArray createPassword(const QByteArray &password,
130 int iterations,
131 int saltByteSize,
132 int hashByteSize);
133
143 [[nodiscard]] static QByteArray createPassword(const QByteArray &password);
144
154 [[nodiscard]] inline static QString createPassword(const QString &password);
155
160 [[nodiscard]] static QByteArray pbkdf2(QCryptographicHash::Algorithm method,
161 const QByteArray &password,
162 const QByteArray &salt,
163 int rounds,
164 int keyLength);
165
169 [[nodiscard]] static QByteArray hmac(QCryptographicHash::Algorithm method,
170 const QByteArray &key,
171 const QByteArray &message);
172
173protected:
174 CredentialPasswordPrivate *d_ptr;
175};
176
178 const QString &correctHash)
179{
180 return validatePassword(password.toUtf8(), correctHash.toLatin1());
181}
182
187
188} // 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