5#include "application.h"
6#include "authentication_p.h"
7#include "authenticationrealm.h"
8#include "authenticationstore.h"
11#include <Cutelyst/Plugins/Session/session.h>
13#include <QLoggingCategory>
15Q_LOGGING_CATEGORY(CUTELYST_UTILS_AUTH,
"cutelyst.utils.auth", QtWarningMsg)
16Q_LOGGING_CATEGORY(C_AUTHENTICATION,
"cutelyst.plugin.authentication", QtWarningMsg)
22 const_cast<char *
>(
"cutelyst_authentication_default_realm");
26#define AUTHENTICATION_USER QStringLiteral("_c_authentication_user")
27#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>();
39Authentication::~Authentication()
47 realm->setParent(
this);
49 d->realmsOrder.append(
realm->objectName());
62 return d->realms.value(name);
70 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
78 AuthenticationPrivate::setAuthenticated(c,
user,
realm, realmPtr);
81 return !
user.isNull();
84 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
94 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
100 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
104 ret = realmPtr->
findUser(c, userinfo);
122 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
126 if (AuthenticationPrivate::findRealmForPersistedUser(
127 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
131 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
140 if (!
user.isNull()) {
144 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
149 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
151 return realm->name() == realmName;
164 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
166 realm->removePersistedUser(c);
169 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
180 return realms.value(realmName.
isNull() ? defaultRealm : realmName);
189 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
195 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(
196 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
205 AuthenticationPrivate::setUser(c, ret);
212 const QMap<QString, AuthenticationRealm *> &realms,
213 const QStringList &realmsOrder)
215 const QVariant realmVariant =
Session::value(c, AUTHENTICATION_USER_REALM);
216 if (!realmVariant.
isNull()) {
223 for (
const QString &realmName : realmsOrder) {
234void AuthenticationPrivate::setAuthenticated(
Context *c,
236 const QString &realmName,
239 AuthenticationPrivate::setUser(c, user, realmName);
242 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
245 AuthenticationPrivate::persistUser(c, user, realmName, realm);
248void AuthenticationPrivate::setUser(
Context *c,
250 const QString &realmName)
253 c->
setStash(AUTHENTICATION_USER, QVariant());
254 c->
setStash(AUTHENTICATION_USER_REALM, QVariant());
257 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
261void AuthenticationPrivate::persistUser(
Context *c,
263 const QString &realmName,
280Cutelyst::AuthenticationCredential::~AuthenticationCredential()
284#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.
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)
Plugin(Application *parent)
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
const T value(const Key &key, const T &defaultValue) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
bool isNull() const const
QVariant fromValue(const T &value)
bool isNull() const const
QString toString() const const