18 #include "credentialhttp_p.h"
19 #include "credentialpassword.h"
21 #include "authenticationrealm.h"
23 #include <Cutelyst/Context>
24 #include <Cutelyst/Response>
27 #include <QLoggingCategory>
31 Q_LOGGING_CATEGORY(C_CREDENTIALHTTP,
"cutelyst.plugin.credentialhttp", QtWarningMsg)
34 , d_ptr(new CredentialHttpPrivate)
38 CredentialHttp::~CredentialHttp()
52 d->authorizationRequiredMessage = message;
58 return d->passwordField;
64 d->passwordField = fieldName;
70 return d->passwordType;
76 d->passwordType = type;
82 return d->passwordPreSalt;
94 return d->passwordPostSalt;
106 return d->usernameField;
112 d->usernameField = fieldName;
118 d->requireSsl = require;
126 if (d->requireSsl && !c->request()->secure()) {
127 ret = d->authenticationFailed(c, realm, authinfo);
131 if (d->isAuthTypeBasic()) {
132 ret = d->authenticateBasic(c, realm, authinfo);
138 ret = d->authenticationFailed(c, realm, authinfo);
147 if (Q_LIKELY(passwordType == CredentialHttp::Hashed)) {
148 if (!passwordPreSalt.isEmpty()) {
152 if (!passwordPostSalt.isEmpty()) {
153 password.
append(password);
157 }
else if (passwordType == CredentialHttp::Clear) {
158 return storedPassword == password;
159 }
else if (passwordType == CredentialHttp::None) {
160 qCCritical(C_CREDENTIALHTTP) <<
"CredentialPassword is set to ignore password check";
171 qCDebug(C_CREDENTIALHTTP) <<
"Checking http basic authentication.";
174 if (userPass.user.isEmpty()) {
179 auth.
insert(usernameField, userPass.user);
182 auth.
insert(passwordField, userPass.password);
183 if (checkPassword(_user, auth)) {
186 qCDebug(C_CREDENTIALHTTP) <<
"Password didn't match";
189 qCDebug(C_CREDENTIALHTTP) <<
"Unable to locate a user matching user info provided in realm";
201 if (authorizationRequiredMessage.isEmpty()) {
202 res->
setBody(QStringLiteral(
"Authorization required."));
204 res->
setBody(authorizationRequiredMessage);
208 if (isAuthTypeBasic()) {
209 createBasicAuthResponse(c, realm);
215 bool CredentialHttpPrivate::isAuthTypeBasic()
const
217 return type == CredentialHttp::Basic || type == CredentialHttp::Any;
223 buildAuthHeaderCommon(realm)));
247 #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 * response() const
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.
void setBody(QIODevice *body)
void setStatus(quint16 status)
void setContentType(const QString &type)
The Cutelyst namespace holds all public Cutelyst API.
void append(const T &value)
bool isEmpty() const const
const T value(const Key &key, const T &defaultValue) const const
typename QMap< Key, T >::iterator insert(const Key &key, const T &value)
QString & append(QChar ch)
bool isEmpty() const const
QString & prepend(QChar ch)
QByteArray toUtf8() const const
QString join(const QString &separator) const const
QString toString() const const