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>();
36Authentication::~Authentication()
44 realm->setParent(
nullptr);
46 d->realmsOrder.append(
realm->objectName());
50 std::shared_ptr<Cutelyst::AuthenticationStore> store,
51 std::shared_ptr<Cutelyst::AuthenticationCredential> credential,
54 addRealm(std::make_shared<AuthenticationRealm>(store, credential, name));
60 return d->realms.value(name);
68 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
72 std::shared_ptr<AuthenticationRealm> realmPtr = auth->d_ptr->
realm(
realm);
76 AuthenticationPrivate::setAuthenticated(c,
user,
realm, realmPtr);
82 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
92 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
98 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
102 ret = realmPtr->findUser(c, userinfo);
109 const QVariant
user = c->
stash(AUTHENTICATION_USER);
111 ret = AuthenticationPrivate::restoreUser(c, QVariant(), QString());
120 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
124 if (AuthenticationPrivate::findRealmForPersistedUser(
125 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
129 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
137 const QVariant
user = c->
stash(AUTHENTICATION_USER);
142 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
146 auto realm = AuthenticationPrivate::findRealmForPersistedUser(
147 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
149 return realm->name() == realmName;
161 auto realm = AuthenticationPrivate::findRealmForPersistedUser(
162 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
164 realm->removePersistedUser(c);
167 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
176std::shared_ptr<AuthenticationRealm> AuthenticationPrivate::realm(
const QString &realmName)
const
178 return realms.value(realmName.isNull() ? defaultRealm : realmName);
182 const QVariant &frozenUser,
183 const QString &realmName)
187 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
191 auto realmPtr = auth->d_ptr->
realm(realmName);
193 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(
194 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
201 ret = realmPtr->restoreUser(c, frozenUser);
203 AuthenticationPrivate::setUser(c, ret);
208std::shared_ptr<AuthenticationRealm> AuthenticationPrivate::findRealmForPersistedUser(
210 const QMap<QString, std::shared_ptr<AuthenticationRealm>> &realms,
211 const QStringList &realmsOrder)
213 const QVariant realmVariant =
Session::value(c, AUTHENTICATION_USER_REALM);
214 if (!realmVariant.isNull()) {
215 std::shared_ptr<AuthenticationRealm> realm = realms.value(realmVariant.toString());
216 if (realm && !realm->userIsRestorable(c).isNull()) {
221 for (
const QString &realmName : realmsOrder) {
222 std::shared_ptr<AuthenticationRealm> realm = realms.value(realmName);
223 if (realm && !realm->userIsRestorable(c).isNull()) {
232void AuthenticationPrivate::setAuthenticated(
Context *c,
234 const QString &realmName,
235 std::shared_ptr<AuthenticationRealm> realm)
237 AuthenticationPrivate::setUser(c, user, realmName);
240 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
243 AuthenticationPrivate::persistUser(c, user, realmName, realm);
246void AuthenticationPrivate::setUser(
Context *c,
248 const QString &realmName)
251 c->
setStash(AUTHENTICATION_USER, QVariant());
252 c->
setStash(AUTHENTICATION_USER_REALM, QVariant());
254 c->
setStash(AUTHENTICATION_USER, QVariant::fromValue(user));
255 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
259void AuthenticationPrivate::persistUser(
Context *c,
261 const QString &realmName,
262 std::shared_ptr<AuthenticationRealm> realm)
268 realm->persistUser(c, user);
278Cutelyst::AuthenticationCredential::~AuthenticationCredential()
282#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.
static char * defaultRealm
default realm name
bool isNull() const
Returns true if the object is null.
void addRealm(std::shared_ptr< AuthenticationRealm > realm)
Adds the realm with name.
static bool userInRealm(Context *c, const QString &realmName=QLatin1String(defaultRealm))
std::shared_ptr< AuthenticationRealm > realm(const QString &name=QLatin1String(defaultRealm)) const
Returns an AuthenticationRealm object that was registered with name.
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...
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