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)
33 using namespace Cutelyst;
35 char *
Authentication::defaultRealm = const_cast<
char *>("cutelyst_authentication_default_realm");
36 char *
AuthenticationRealm::defaultRealm = const_cast<
char *>("cutelyst_authentication_default_realm");
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 qRegisterMetaTypeStreamOperators<AuthenticationUser>();
50 Authentication::~Authentication()
71 return d->realms.value(name);
77 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
85 AuthenticationPrivate::setAuthenticated(c, user, realm, realmPtr);
91 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
99 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
105 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
109 ret = realmPtr->
findUser(c, userinfo);
127 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
131 if (AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
135 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
148 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
152 AuthenticationRealm *
realm = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
154 return realm->
name() == realmName;
166 AuthenticationRealm *
realm = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
171 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
184 return realms.value(realmName.
isNull() ? defaultRealm : realmName);
191 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
197 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
206 AuthenticationPrivate::setUser(c, ret);
214 if (!realmVariant.
isNull()) {
221 for (
const QString &realmName : realmsOrder) {
234 AuthenticationPrivate::setUser(c, user, realmName);
237 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
240 AuthenticationPrivate::persistUser(c, user, realmName, realm);
250 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
270 Cutelyst::AuthenticationCredential::~AuthenticationCredential()
275 #include "moc_authentication.cpp"
AuthenticationUser restoreUser(Context *c, const QVariant &frozenUser)
Retrieves the user from the store.
void postForked(Cutelyst::Application *app)
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)
Tries to find the user with authinfo returning a non null AuthenticationUser on success.
AuthenticationUser persistUser(Context *c, const AuthenticationUser &user)
Stores the user on the session.
virtual AuthenticationUser authenticate(Context *c, const ParamsMultiMap &authinfo)
Tries to authenticate the user with authinfo returning a non null AuthenticationUser on success...
void addRealm(AuthenticationRealm *realm)
Adds the realm with name.
void setStash(const QString &key, const QVariant &value)
static void logout(Context *c)
AuthenticationRealm * realm(const QString &name=QLatin1String(defaultRealm)) const
Returns an AuthenticationRealm object that was registered with name.
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...
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved.
static bool authenticate(Context *c, const ParamsMultiMap &userinfo, const QString &realm=QLatin1String(defaultRealm))
static void setValue(Context *c, const QString &key, const QVariant &value)
virtual bool setup(Application *app) override
void setParent(QObject *parent)
QVariant fromValue(const T &value)
static bool userExists(Context *c)
AuthenticationCredential(QObject *parent=nullptr)
Constructs a new AuthenticationCredential object with the given parent.
static bool userInRealm(Context *c, const QString &realmName=QLatin1String(defaultRealm))
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
bool isNull() const
Returns true if the object is null.
The Cutelyst Application.
static AuthenticationUser user(Context *c)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void stash(const QVariantHash &unite)
const T value(const Key &key, const T &defaultValue) const
void removePersistedUser(Context *c)
Removes the user from the session.