5 #include "authenticationrealm.h" 6 #include "credentialhttp_p.h" 7 #include "credentialpassword.h" 9 #include <Cutelyst/Context> 10 #include <Cutelyst/Response> 12 #include <QLoggingCategory> 17 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;
115 if (d->requireSsl && !c->request()->secure()) {
116 ret = d->authenticationFailed(c, realm, authinfo);
120 if (d->isAuthTypeBasic()) {
121 ret = d->authenticateBasic(c, realm, authinfo);
127 ret = d->authenticationFailed(c, realm, authinfo);
137 if (Q_LIKELY(passwordType == CredentialHttp::Hashed)) {
138 if (!passwordPreSalt.isEmpty()) {
142 if (!passwordPostSalt.isEmpty()) {
143 password.
append(password);
147 }
else if (passwordType == CredentialHttp::Clear) {
148 return storedPassword == password;
149 }
else if (passwordType == CredentialHttp::None) {
150 qCCritical(C_CREDENTIALHTTP) <<
"CredentialPassword is set to ignore password check";
163 qCDebug(C_CREDENTIALHTTP) <<
"Checking http basic authentication.";
166 if (userPass.user.isEmpty()) {
171 auth.
insert(usernameField, userPass.user);
174 auth.
insert(passwordField, userPass.password);
175 if (checkPassword(_user, auth)) {
178 qCDebug(C_CREDENTIALHTTP) <<
"Password didn't match";
181 qCDebug(C_CREDENTIALHTTP) <<
"Unable to locate a user matching user info provided in realm";
195 if (authorizationRequiredMessage.isEmpty()) {
196 res->
setBody(QStringLiteral(
"Authorization required."));
198 res->
setBody(authorizationRequiredMessage);
202 if (isAuthTypeBasic()) {
203 createBasicAuthResponse(c, realm);
209 bool CredentialHttpPrivate::isAuthTypeBasic()
const 211 return type == CredentialHttp::Basic || type == CredentialHttp::Any;
217 joinAuthHeaderParts(QStringLiteral(
"Basic"), buildAuthHeaderCommon(realm)));
227 ret.
append(u
"realm=\"" + realm->
name() + u
'"');
232 QString CredentialHttpPrivate::joinAuthHeaderParts(
const QString &type,
242 #include "moc_credentialhttp.cpp" QString & append(QChar ch)
void setPasswordPostSalt(const QString &passwordPostSalt)
Sets the salt string to be appended to the password.
void setContentType(const QString &type)
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)
Tries to find the user with authinfo returning a non null AuthenticationUser on success.
Headers & headers() noexcept
QString & prepend(QChar ch)
bool isNull() const
Returns true if the object is null.
Response * res() const noexcept
QString passwordPreSalt() const
Returns the salt string to be prepended to the password.
void setPasswordType(PasswordType type)
Sets the type of password this class will be dealing with.
QString join(QChar separator) const const
QString passwordPostSalt() const
Returns the salt string to be appended to the password.
QString usernameField() const
Returns the field to look for when authenticating the user.
void setUsernameField(const QString &fieldName)
Sets the field to look for when authenticating the user.
AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final
Tries to authenticate the authinfo using the give realm.
void setType(CredentialHttp::AuthType type)
Headers headers() const noexcept
QString passwordField() const
Returns the field to look for when authenticating the user.
bool isEmpty() const const
bool isEmpty() const const
void setPasswordPreSalt(const QString &passwordPreSalt)
Sets the salt string to be prepended to the password.
iterator insert(const Key &key, const T &value)
The Cutelyst namespace holds all public Cutelyst API.
void setRequireSsl(bool require)
void setPasswordField(const QString &fieldName)
Sets the field to look for when authenticating the user.
void append(QList< T > &&value)
PasswordType passwordType() const
Returns the type of password this class will be dealing with.
void setAuthorizationRequiredMessage(const QString &message)
void setBody(QIODevice *body)
Response * response() const noexcept
QString toString() const const
void setStatus(quint16 status) noexcept
static bool validatePassword(const QByteArray &password, const QByteArray &correctHash)
Validates the given password against the correct hash.
T value(const Key &key, const T &defaultValue) const const
QByteArray toUtf8() const const