cutelyst 4.0.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;
15class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT CredentialPassword : public AuthenticationCredential
16{
17 Q_OBJECT
18 Q_DECLARE_PRIVATE(CredentialPassword)
19public:
20 enum PasswordType {
21 None,
22 Clear,
23 Hashed,
24 };
25 Q_ENUM(PasswordType)
26
27
30 explicit CredentialPassword(QObject *parent = nullptr);
31 virtual ~CredentialPassword() override;
32
33 [[nodiscard]] AuthenticationUser
34 authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final;
35
39 [[nodiscard]] QString passwordField() const;
40
44 void setPasswordField(const QString &fieldName);
45
49 [[nodiscard]] PasswordType passwordType() const;
50
54 void setPasswordType(PasswordType type);
55
59 [[nodiscard]] QString passwordPreSalt() const;
60
64 void setPasswordPreSalt(const QString &passwordPreSalt);
65
69 [[nodiscard]] QString passwordPostSalt() const;
70
74 void setPasswordPostSalt(const QString &passwordPostSalt);
75
79 [[nodiscard]] static bool validatePassword(const QByteArray &password,
80 const QByteArray &correctHash);
81
85 [[nodiscard]] static bool validatePassword(const QString &password, const QString &correctHash);
86
97 [[nodiscard]] static QByteArray createPassword(const QByteArray &password,
98 QCryptographicHash::Algorithm method,
99 int iterations,
100 int saltByteSize,
101 int hashByteSize);
102
109 [[nodiscard]] static QByteArray createPassword(const QByteArray &password);
110
117 [[nodiscard]] inline static QString createPassword(const QString &password);
118
128 [[nodiscard]] static QByteArray pbkdf2(QCryptographicHash::Algorithm method,
129 const QByteArray &password,
130 const QByteArray &salt,
131 int rounds,
132 int keyLength);
133
137 [[nodiscard]] static QByteArray hmac(QCryptographicHash::Algorithm method,
138 const QByteArray &key,
139 const QByteArray &message);
140
141protected:
142 CredentialPasswordPrivate *d_ptr;
143};
144
145inline bool CredentialPassword::validatePassword(const QString &password,
146 const QString &correctHash)
147{
148 return validatePassword(password.toUtf8(), correctHash.toLatin1());
149}
150
151QString CredentialPassword::createPassword(const QString &password)
152{
153 return QString::fromLatin1(createPassword(password.toUtf8()));
154}
155
156} // namespace Cutelyst
The Cutelyst Context.
Definition: context.h:38
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
QMultiMap< QString, QString > ParamsMultiMap