18 #include "credentialhttp_p.h"
19 #include "credentialpassword.h"
21 #include "authenticationrealm.h"
23 #include <Cutelyst/Context>
24 #include <Cutelyst/Response>
27 #include <QLoggingCategory>
29 using namespace Cutelyst;
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.";
173 const std::pair<
QString, QString> userPass = c->req()->headers().authorizationBasicPair();
174 if (userPass.first.isEmpty()) {
179 auth.
insert(usernameField, userPass.first);
182 auth.
insert(passwordField, userPass.second);
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";
200 if (authorizationRequiredMessage.isEmpty()) {
201 res->
setBody(QStringLiteral(
"Authorization required."));
203 res->
setBody(authorizationRequiredMessage);
207 if (isAuthTypeBasic()) {
208 createBasicAuthResponse(c, realm);
214 bool CredentialHttpPrivate::isAuthTypeBasic()
const
216 return type == CredentialHttp::Basic || type == CredentialHttp::Any;
222 buildAuthHeaderCommon(realm)));
246 #include "moc_credentialhttp.cpp"
QString passwordPostSalt() const
Returns the salt string to be appended to the password.
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.
QString & prepend(QChar ch)
void setPasswordType(PasswordType type)
Sets the type of password this class will be dealing with.
QString join(const QString &separator) const
void setStatus(quint16 status)
void setUsernameField(const QString &fieldName)
Sets the field to look for when authenticating the user.
void append(const T &value)
AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) final
Tries to authenticate the authinfo using the give realm.
void setType(CredentialHttp::AuthType type)
void setPasswordPreSalt(const QString &passwordPreSalt)
Sets the salt string to be prepended to the password.
void setRequireSsl(bool require)
QString passwordPreSalt() const
Returns the salt string to be prepended to the password.
void setPasswordField(const QString &fieldName)
Sets the field to look for when authenticating the user.
Response * response() const
PasswordType passwordType() const
Returns the type of password this class will be dealing with.
QString usernameField() const
Returns the field to look for when authenticating the user.
bool isNull() const
Returns true if the object is null.
iterator insert(const Key &key, const T &value)
void setAuthorizationRequiredMessage(const QString &message)
void setBody(QIODevice *body)
QString passwordField() const
Returns the field to look for when authenticating the user.
static bool validatePassword(const QByteArray &password, const QByteArray &correctHash)
Validates the given password against the correct hash.
const T value(const Key &key, const T &defaultValue) const
QByteArray toUtf8() const