5 #include "credentialhttp_p.h"
6 #include "credentialpassword.h"
8 #include "authenticationrealm.h"
10 #include <Cutelyst/Context>
11 #include <Cutelyst/Response>
14 #include <QLoggingCategory>
18 Q_LOGGING_CATEGORY(C_CREDENTIALHTTP,
"cutelyst.plugin.credentialhttp", QtWarningMsg)
21 , d_ptr(new CredentialHttpPrivate)
25 CredentialHttp::~CredentialHttp()
39 d->authorizationRequiredMessage = message;
45 return d->passwordField;
51 d->passwordField = fieldName;
57 return d->passwordType;
63 d->passwordType = type;
69 return d->passwordPreSalt;
81 return d->passwordPostSalt;
93 return d->usernameField;
99 d->usernameField = fieldName;
105 d->requireSsl = require;
113 if (d->requireSsl && !c->request()->secure()) {
114 ret = d->authenticationFailed(c, realm, authinfo);
118 if (d->isAuthTypeBasic()) {
119 ret = d->authenticateBasic(c, realm, authinfo);
125 ret = d->authenticationFailed(c, realm, authinfo);
131 QString password = authinfo.value(passwordField);
132 const QString storedPassword = user.value(passwordField).toString();
134 if (Q_LIKELY(passwordType == CredentialHttp::Hashed)) {
135 if (!passwordPreSalt.isEmpty()) {
136 password.prepend(password);
139 if (!passwordPostSalt.isEmpty()) {
140 password.append(password);
144 }
else if (passwordType == CredentialHttp::Clear) {
145 return storedPassword == password;
146 }
else if (passwordType == CredentialHttp::None) {
147 qCCritical(C_CREDENTIALHTTP) <<
"CredentialPassword is set to ignore password check";
158 qCDebug(C_CREDENTIALHTTP) <<
"Checking http basic authentication.";
161 if (userPass.user.isEmpty()) {
166 auth.insert(usernameField, userPass.user);
169 auth.insert(passwordField, userPass.password);
170 if (checkPassword(_user, auth)) {
173 qCDebug(C_CREDENTIALHTTP) <<
"Password didn't match";
176 qCDebug(C_CREDENTIALHTTP) <<
"Unable to locate a user matching user info provided in realm";
188 if (authorizationRequiredMessage.isEmpty()) {
189 res->
setBody(QStringLiteral(
"Authorization required."));
191 res->
setBody(authorizationRequiredMessage);
195 if (isAuthTypeBasic()) {
196 createBasicAuthResponse(c, realm);
202 bool CredentialHttpPrivate::isAuthTypeBasic()
const
204 return type == CredentialHttp::Basic || type == CredentialHttp::Any;
210 buildAuthHeaderCommon(realm)));
219 if (!realm->
name().isEmpty()) {
220 ret.append(u
"realm=\"" + realm->
name() + u
'"');
225 QString CredentialHttpPrivate::joinAuthHeaderParts(
const QString &type,
const QStringList &parts)
const
228 if (!parts.isEmpty()) {
229 ret.append(u
' ' + parts.join(u
", "));
234 #include "moc_credentialhttp.cpp"
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)
Tries to find the user with authinfo returning a non null AuthenticationUser on success.
bool isNull() const
Returns true if the object is null.
Response * res() const noexcept
Response * response() const noexcept
QString usernameField() const
Returns the field to look for when authenticating the user.
void setPasswordType(PasswordType type)
Sets the type of password this class will be dealing with.
void setUsernameField(const QString &fieldName)
Sets the field to look for when authenticating the user.
QString passwordPreSalt() const
Returns the salt string to be prepended to the password.
QString passwordPostSalt() const
Returns the salt string to be appended to the password.
void setPasswordPreSalt(const QString &passwordPreSalt)
Sets the salt string to be prepended to the password.
QString passwordField() const
Returns the field to look for when authenticating the user.
PasswordType passwordType() const
Returns the type of password this class will be dealing with.
void setType(CredentialHttp::AuthType type)
AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final
Tries to authenticate the authinfo using the give realm.
void setPasswordField(const QString &fieldName)
Sets the field to look for when authenticating the user.
void setPasswordPostSalt(const QString &passwordPostSalt)
Sets the salt string to be appended to the password.
void setRequireSsl(bool require)
void setAuthorizationRequiredMessage(const QString &message)
static bool validatePassword(const QByteArray &password, const QByteArray &correctHash)
Validates the given password against the correct hash.
Headers headers() const noexcept
void setStatus(quint16 status) noexcept
Headers & headers() noexcept
void setBody(QIODevice *body)
void setContentType(const QString &type)
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap