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 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
48 qRegisterMetaTypeStreamOperators<AuthenticationUser>();
52 Authentication::~Authentication()
73 return d->realms.value(name);
79 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
87 AuthenticationPrivate::setAuthenticated(c,
user,
realm, realmPtr);
93 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
101 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
107 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
111 ret = realmPtr->
findUser(c, userinfo);
129 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
133 if (AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
137 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
150 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
154 AuthenticationRealm *
realm = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
168 AuthenticationRealm *
realm = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
173 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
186 return realms.value(realmName.
isNull() ? defaultRealm : realmName);
193 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
199 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
208 AuthenticationPrivate::setUser(c, ret);
216 if (!realmVariant.
isNull()) {
223 for (
const QString &realmName : realmsOrder) {
236 AuthenticationPrivate::setUser(c, user, realmName);
239 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
242 AuthenticationPrivate::persistUser(c, user, realmName, realm);
252 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
272 Cutelyst::AuthenticationCredential::~AuthenticationCredential()
277 #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.
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)
void setParent(QObject *parent)
bool isNull() const const
QVariant fromValue(const T &value)
bool isNull() const const
QString toString() const const