18 #include "authentication_p.h"
20 #include "authenticationstore.h"
21 #include "authenticationrealm.h"
24 #include "application.h"
26 #include <Cutelyst/Plugins/Session/session.h>
28 #include <QLoggingCategory>
30 Q_LOGGING_CATEGORY(CUTELYST_UTILS_AUTH,
"cutelyst.utils.auth", QtWarningMsg)
31 Q_LOGGING_CATEGORY(C_AUTHENTICATION,
"cutelyst.plugin.authentication", QtWarningMsg)
40 #define AUTHENTICATION_USER QStringLiteral("_c_authentication_user")
41 #define AUTHENTICATION_USER_REALM QStringLiteral("_c_authentication_user_realm")
44 , d_ptr(new AuthenticationPrivate)
46 qRegisterMetaType<AuthenticationUser>();
47 qRegisterMetaTypeStreamOperators<AuthenticationUser>();
50 Authentication::~Authentication()
58 realm->setParent(
this);
60 d->realmsOrder.append(
realm->objectName());
71 return d->realms.value(name);
77 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
91 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
99 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
105 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
109 ret = realmPtr->
findUser(c, userinfo);
116 const QVariant
user = c->
stash(AUTHENTICATION_USER);
118 ret = AuthenticationPrivate::restoreUser(c, QVariant(), QString());
127 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
131 if (AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
135 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
143 const QVariant
user = c->
stash(AUTHENTICATION_USER);
148 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
152 AuthenticationRealm *
realm = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
166 AuthenticationRealm *
realm = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
171 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
184 return realms.value(realmName.isNull() ? defaultRealm : realmName);
191 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
197 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
206 AuthenticationPrivate::setUser(c, ret);
211 AuthenticationRealm *AuthenticationPrivate::findRealmForPersistedUser(
Context *c,
const QMap<QString, AuthenticationRealm *> &realms,
const QStringList &realmsOrder)
213 const QVariant realmVariant =
Session::value(c, AUTHENTICATION_USER_REALM);
214 if (!realmVariant.isNull()) {
221 for (
const QString &realmName : realmsOrder) {
234 AuthenticationPrivate::setUser(c, user);
237 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
240 AuthenticationPrivate::persistUser(c, user, realmName, realm);
246 c->
setStash(AUTHENTICATION_USER, QVariant());
247 c->
setStash(AUTHENTICATION_USER_REALM, QVariant());
249 c->
setStash(AUTHENTICATION_USER, QVariant::fromValue(user));
250 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
272 Cutelyst::AuthenticationCredential::~AuthenticationCredential()
277 #include "moc_authentication.cpp"