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> 15 Q_LOGGING_CATEGORY(CUTELYST_UTILS_AUTH,
"cutelyst.utils.auth", QtWarningMsg)
16 Q_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>();
34 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) 35 qRegisterMetaTypeStreamOperators<AuthenticationUser>();
39 Authentication::~Authentication()
62 return d->realms.value(name);
70 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
78 AuthenticationPrivate::setAuthenticated(c,
user,
realm, realmPtr);
84 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
94 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
100 qCWarning(C_AUTHENTICATION) <<
"Could not find realm" <<
realm;
104 ret = realmPtr->
findUser(c, userinfo);
122 if (!c->
stash(AUTHENTICATION_USER).isNull()) {
126 if (AuthenticationPrivate::findRealmForPersistedUser(
127 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder)) {
131 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
144 qCCritical(C_AUTHENTICATION,
"Authentication plugin not registered!");
149 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
164 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
169 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
180 return realms.value(realmName.
isNull() ? defaultRealm : realmName);
189 qCCritical(C_AUTHENTICATION) <<
"Authentication plugin not registered";
195 realmPtr = AuthenticationPrivate::findRealmForPersistedUser(
196 c, auth->d_ptr->realms, auth->d_ptr->realmsOrder);
205 AuthenticationPrivate::setUser(c, ret);
216 if (!realmVariant.
isNull()) {
223 for (
const QString &realmName : realmsOrder) {
234 void AuthenticationPrivate::setAuthenticated(
Context *c,
239 AuthenticationPrivate::setUser(c, user, realmName);
242 qCWarning(C_AUTHENTICATION) <<
"Called with invalid realm" << realmName;
245 AuthenticationPrivate::persistUser(c, user, realmName, realm);
248 void AuthenticationPrivate::setUser(
Context *c,
257 c->
setStash(AUTHENTICATION_USER_REALM, realmName);
261 void AuthenticationPrivate::persistUser(
Context *c,
280 Cutelyst::AuthenticationCredential::~AuthenticationCredential()
284 #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.
static char * defaultRealm
default realm name
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
AuthenticationUser persistUser(Context *c, const AuthenticationUser &user)
Stores the user on the session.
bool isNull() const
Returns true if the object is null.
QVariant fromValue(T &&value)
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)
bool isNull() const const
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.
AuthenticationRealm * realm(const QString &name=QLatin1String(defaultRealm)) const
Returns an AuthenticationRealm object that was registered with name.
static bool authenticate(Context *c, const ParamsMultiMap &userinfo, const QString &realm=QLatin1String(defaultRealm))
bool isNull() const const
void stash(const QVariantHash &unite)
static void setValue(Context *c, const QString &key, const QVariant &value)
The Cutelyst namespace holds all public Cutelyst API.
virtual bool setup(Application *app) override
Authentication(Application *parent)
Constructs a new Authentication object with the given parent.
void setParent(QObject *parent)
static bool userExists(Context *c)
static char * defaultRealm
default realm name
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())
The Cutelyst Application.
static AuthenticationUser user(Context *c)
QString toString() const const
T value(const Key &key, const T &defaultValue) const const
void removePersistedUser(Context *c)
Removes the user from the session.