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);
144 QString password = authinfo.value(passwordField);
145 const QString storedPassword = user.value(passwordField).toString();
147 if (Q_LIKELY(passwordType == CredentialHttp::Hashed)) {
148 if (!passwordPreSalt.isEmpty()) {
149 password.prepend(password);
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)));
231 if (!realm->
name().isEmpty()) {
232 ret.append(QLatin1String(
"realm=\"") + realm->
name() + QLatin1Char(
'"'));
237 QString CredentialHttpPrivate::joinAuthHeaderParts(
const QString &type,
const QStringList &parts)
const
240 if (!parts.isEmpty()) {
241 ret.append(QLatin1Char(
' ') + parts.join(QStringLiteral(
", ")));
246 #include "moc_credentialhttp.cpp"