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)
18using namespace Qt::StringLiterals;
22 const_cast<char *
>(
"cutelyst_authentication_default_realm");
26const auto AUTHENTICATION_USER = u
"_c_authentication_user"_s;
27const auto AUTHENTICATION_USER_REALM = u
"_c_authentication_user_realm"_s;
32 , d_ptr(new AuthenticationPrivate)
34 qRegisterMetaType<AuthenticationUser>();
45 realm->setParent(
nullptr);
47 d->realmsOrder.append(
realm->objectName());
51 std::shared_ptr<Cutelyst::AuthenticationStore> store,
52 std::shared_ptr<Cutelyst::AuthenticationCredential> credential,
55 addRealm(std::make_shared<AuthenticationRealm>(store, credential, name));
61 return d->realms.value(name);
69 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
73 std::shared_ptr<AuthenticationRealm> realmPtr = auth->d_ptr->realm(
realm);
77 AuthenticationPrivate::setAuthenticated(c,
user,
realm, realmPtr);
83 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
93 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
97 auto realmPtr = auth->d_ptr->realm(
realm);
99 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
103 ret = realmPtr->findUser(c, userinfo);
112 ret = AuthenticationPrivate::restoreUser(c, {}, {});
121 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
125 if (AuthenticationPrivate::findRealmForPersistedUser(
126 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
130 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
143 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
147 auto realm = AuthenticationPrivate::findRealmForPersistedUser(
148 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
150 return realm->name() == realmName;
162 auto realm = AuthenticationPrivate::findRealmForPersistedUser(
163 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
165 realm->removePersistedUser(c);
168 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
177std::shared_ptr<AuthenticationRealm> AuthenticationPrivate::realm(
const QString &realmName)
const
179 return realms.value(realmName.
isNull() ? defaultRealm : realmName);
188 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
192 auto realmPtr = auth->d_ptr->realm(realmName);
194 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(
195 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
202 ret = realmPtr->restoreUser(c, frozenUser);
204 AuthenticationPrivate::setUser(c, ret);
209std::shared_ptr<AuthenticationRealm> AuthenticationPrivate::findRealmForPersistedUser(
211 const QMap<
QString, std::shared_ptr<AuthenticationRealm>> &realms,
215 if (!realmVariant.
isNull()) {
216 std::shared_ptr<AuthenticationRealm> realm = realms.value(realmVariant.
toString());
217 if (realm && !realm->userIsRestorable(c).isNull()) {
222 for (
const QString &realmName : realmsOrder) {
223 std::shared_ptr<AuthenticationRealm> realm = realms.value(realmName);
224 if (realm && !realm->userIsRestorable(c).isNull()) {
233void AuthenticationPrivate::setAuthenticated(
Context *c,
236 std::shared_ptr<AuthenticationRealm> realm)
238 AuthenticationPrivate::setUser(c, user, realmName);
241 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
244 AuthenticationPrivate::persistUser(c, user, realmName, realm);
247void AuthenticationPrivate::setUser(
Context *c,
256 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
260void AuthenticationPrivate::persistUser(
Context *c,
263 std::shared_ptr<AuthenticationRealm> realm)
269 realm->persistUser(c, user);
283#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(T &&value)
bool isNull() const const
QString toString() const const