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(C_AUTHENTICATION,
"cutelyst.plugin.authentication", QtWarningMsg)
21 const_cast<char *
>(
"cutelyst_authentication_default_realm");
25#define AUTHENTICATION_USER QStringLiteral("_c_authentication_user")
26#define AUTHENTICATION_USER_REALM QStringLiteral("_c_authentication_user_realm")
30 , d_ptr(new AuthenticationPrivate)
32 qRegisterMetaType<AuthenticationUser>();
43 realm->setParent(
nullptr);
45 d->realmsOrder.append(
realm->objectName());
49 std::shared_ptr<Cutelyst::AuthenticationStore> store,
50 std::shared_ptr<Cutelyst::AuthenticationCredential> credential,
53 addRealm(std::make_shared<AuthenticationRealm>(store, credential, name));
59 return d->realms.value(name);
67 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
71 std::shared_ptr<AuthenticationRealm> realmPtr = auth->d_ptr->
realm(
realm);
75 AuthenticationPrivate::setAuthenticated(c,
user,
realm, realmPtr);
81 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
91 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
97 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
101 ret = realmPtr->findUser(c, userinfo);
119 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
123 if (AuthenticationPrivate::findRealmForPersistedUser(
124 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
128 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
141 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
145 auto realm = AuthenticationPrivate::findRealmForPersistedUser(
146 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
148 return realm->name() == realmName;
160 auto realm = AuthenticationPrivate::findRealmForPersistedUser(
161 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
163 realm->removePersistedUser(c);
166 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
175std::shared_ptr<AuthenticationRealm> AuthenticationPrivate::realm(
const QString &realmName)
const
177 return realms.value(realmName.
isNull() ? defaultRealm : realmName);
186 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
190 auto realmPtr = auth->d_ptr->
realm(realmName);
192 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(
193 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
200 ret = realmPtr->restoreUser(c, frozenUser);
202 AuthenticationPrivate::setUser(c, ret);
207std::shared_ptr<AuthenticationRealm> AuthenticationPrivate::findRealmForPersistedUser(
209 const QMap<
QString, std::shared_ptr<AuthenticationRealm>> &realms,
213 if (!realmVariant.
isNull()) {
214 std::shared_ptr<AuthenticationRealm> realm = realms.value(realmVariant.
toString());
215 if (realm && !realm->userIsRestorable(c).isNull()) {
220 for (
const QString &realmName : realmsOrder) {
221 std::shared_ptr<AuthenticationRealm> realm = realms.value(realmName);
222 if (realm && !realm->userIsRestorable(c).isNull()) {
231void AuthenticationPrivate::setAuthenticated(
Context *c,
234 std::shared_ptr<AuthenticationRealm> realm)
236 AuthenticationPrivate::setUser(c, user, realmName);
239 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
242 AuthenticationPrivate::persistUser(c, user, realmName, realm);
245void AuthenticationPrivate::setUser(
Context *c,
254 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
258void AuthenticationPrivate::persistUser(
Context *c,
261 std::shared_ptr<AuthenticationRealm> realm)
267 realm->persistUser(c, user);
281#include "moc_authentication.cpp"
The Cutelyst application.
void postForked(Cutelyst::Application *app)
virtual ~AuthenticationCredential()
AuthenticationCredential(QObject *parent=nullptr)
static char * defaultRealm
Container for user data retrieved from an AuthenticationStore.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Main class to manage user authentication.
void addRealm(std::shared_ptr< AuthenticationRealm > realm)
static bool userInRealm(Context *c, const QString &realmName=QLatin1String(defaultRealm))
std::shared_ptr< AuthenticationRealm > realm(const QString &name=QLatin1String(defaultRealm)) const
static bool userExists(Context *c)
virtual ~Authentication() override
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
Authentication(Application *parent)
static AuthenticationUser user(Context *c)
static AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo, const QString &realm=QLatin1String(defaultRealm))
void stash(const QVariantHash &unite)
void setStash(const QString &key, const QVariant &value)
Base class for Cutelyst Plugins.
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.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isNull() const const
QVariant fromValue(const T &value)
bool isNull() const const
QString toString() const const