5 #include "authentication_p.h"
7 #include "authenticationstore.h"
8 #include "authenticationrealm.h"
11 #include "application.h"
13 #include <Cutelyst/Plugins/Session/session.h>
15 #include <QLoggingCategory>
17 Q_LOGGING_CATEGORY(CUTELYST_UTILS_AUTH,
"cutelyst.utils.auth", QtWarningMsg)
18 Q_LOGGING_CATEGORY(C_AUTHENTICATION,
"cutelyst.plugin.authentication", QtWarningMsg)
27 #define AUTHENTICATION_USER QStringLiteral("_c_authentication_user")
28 #define AUTHENTICATION_USER_REALM QStringLiteral("_c_authentication_user_realm")
31 , d_ptr(new AuthenticationPrivate)
33 qRegisterMetaType<AuthenticationUser>();
34 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
35 qRegisterMetaTypeStreamOperators<AuthenticationUser>();
39 Authentication::~Authentication()
47 realm->setParent(
this);
49 d->realmsOrder.append(
realm->objectName());
60 return d->realms.value(name);
66 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
74 AuthenticationPrivate::setAuthenticated(c,
user,
realm, realmPtr);
80 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
88 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
94 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
98 ret = realmPtr->
findUser(c, userinfo);
105 const QVariant
user = c->
stash(AUTHENTICATION_USER);
107 ret = AuthenticationPrivate::restoreUser(c, QVariant(), QString());
116 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
120 if (AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
124 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
132 const QVariant
user = c->
stash(AUTHENTICATION_USER);
137 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
141 AuthenticationRealm *
realm = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
155 AuthenticationRealm *
realm = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
160 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
173 return realms.value(realmName.isNull() ? defaultRealm : realmName);
180 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
186 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
195 AuthenticationPrivate::setUser(c, ret);
200 AuthenticationRealm *AuthenticationPrivate::findRealmForPersistedUser(
Context *c,
const QMap<QString, AuthenticationRealm *> &realms,
const QStringList &realmsOrder)
202 const QVariant realmVariant =
Session::value(c, AUTHENTICATION_USER_REALM);
203 if (!realmVariant.isNull()) {
210 for (
const QString &realmName : realmsOrder) {
223 AuthenticationPrivate::setUser(c, user, realmName);
226 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
229 AuthenticationPrivate::persistUser(c, user, realmName, realm);
235 c->
setStash(AUTHENTICATION_USER, QVariant());
236 c->
setStash(AUTHENTICATION_USER_REALM, QVariant());
238 c->
setStash(AUTHENTICATION_USER, QVariant::fromValue(user));
239 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
259 Cutelyst::AuthenticationCredential::~AuthenticationCredential()
264 #include "moc_authentication.cpp"
The Cutelyst Application.
void postForked(Cutelyst::Application *app)
AuthenticationCredential(QObject *parent=nullptr)
Constructs a new AuthenticationCredential object with the given parent.
void removePersistedUser(Context *c)
Removes the user from the session.
virtual AuthenticationUser authenticate(Context *c, const ParamsMultiMap &authinfo)
Tries to authenticate the user with authinfo returning a non null AuthenticationUser on success.
static char * defaultRealm
default realm name
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved.
AuthenticationUser persistUser(Context *c, const AuthenticationUser &user)
Stores the user on the session.
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)
Tries to find the user with authinfo returning a non null AuthenticationUser on success.
AuthenticationUser restoreUser(Context *c, const QVariant &frozenUser)
Retrieves the user from the store.
bool isNull() const
Returns true if the object is null.
void addRealm(AuthenticationRealm *realm)
Adds the realm with name.
static bool userInRealm(Context *c, const QString &realmName=QLatin1String(defaultRealm))
static bool userExists(Context *c)
virtual bool setup(Application *app) override
static void logout(Context *c)
static bool authenticate(Context *c, const ParamsMultiMap &userinfo, const QString &realm=QLatin1String(defaultRealm))
static char * defaultRealm
default realm name
Authentication(Application *parent)
Constructs a new Authentication object with the given parent.
static AuthenticationUser user(Context *c)
static AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo, const QString &realm=QLatin1String(defaultRealm))
Tries to find the user with userinfo using the realm, returning a non null AuthenticationUser on succ...
AuthenticationRealm * realm(const QString &name=QLatin1String(defaultRealm)) const
Returns an AuthenticationRealm object that was registered with name.
void stash(const QVariantHash &unite)
void setStash(const QString &key, const QVariant &value)
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
static void setValue(Context *c, const QString &key, const QVariant &value)
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap