cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
authenticationrealm.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2013-2022 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#include "authenticationrealm.h"
6
7#include "authenticationstore.h"
8#include "common.h"
9#include "context.h"
10
11#include <Cutelyst/Plugins/Session/session.h>
12
13using namespace Cutelyst;
14
15Q_LOGGING_CATEGORY(C_AUTH_REALM, "cutelyst.plugin.authentication.realm", QtWarningMsg)
16
17#define SESSION_AUTHENTICATION_USER "__authentication_user"
18#define SESSION_AUTHENTICATION_USER_REALM "__authentication_user_realm" // in authentication.cpp
19
22 const QString &name,
25 , m_store(store)
26 , m_credential(credential)
27{
28 m_store->setParent(this);
29 m_credential->setParent(this);
32}
33
34AuthenticationRealm::~AuthenticationRealm()
35{
36}
37
39{
40 return m_store;
41}
42
44{
45 return m_credential;
46}
47
49{
50 AuthenticationUser ret = m_store->findUser(c, userinfo);
51
52 if (ret.isNull()) {
53 if (m_store->canAutoCreateUser()) {
54 ret = m_store->autoCreateUser(c, userinfo);
55 }
56 } else {
57 if (m_store->canAutoUpdateUser()) {
58 ret = m_store->autoUpdateUser(c, userinfo);
59 }
60 }
61
62 if (!ret.isNull() && ret.authRealm() != name()) {
63 ret.setAuthRealm(name());
64 }
65
66 return ret;
67}
68
70{
71 return m_credential->authenticate(c, this, authinfo);
72}
73
75{
77 {QStringLiteral(SESSION_AUTHENTICATION_USER),
78 QStringLiteral(SESSION_AUTHENTICATION_USER_REALM)});
79}
80
82{
83 Session::setValue(c, QStringLiteral(SESSION_AUTHENTICATION_USER), m_store->forSession(c, user));
84 Session::setValue(c, QStringLiteral(SESSION_AUTHENTICATION_USER_REALM), objectName());
85
86 return user;
87}
88
90{
92 QVariant _frozenUser = frozenUser;
93 if (_frozenUser.isNull()) {
94 _frozenUser = userIsRestorable(c);
95 }
96
97 if (_frozenUser.isNull()) {
98 return user;
99 }
100
101 user = m_store->fromSession(c, _frozenUser);
102
103 if (!user.isNull()) {
104 // Sets the realm the user originated in
105 user.setAuthRealm(objectName());
106 } else {
107 qCWarning(C_AUTH_REALM) << "Store claimed to have a restorable user, but restoration "
108 "failed. Did you change the user's id_field?";
109 }
110
111 return user;
112}
113
115{
116 // No need to check if session is valid
117 // as ::value will do that for us
118 return Session::value(c, QStringLiteral(SESSION_AUTHENTICATION_USER));
119}
120
121#include "moc_authenticationrealm.cpp"
AuthenticationCredential * credential() const
Returns the authentication credential object.
AuthenticationRealm(AuthenticationStore *store, AuthenticationCredential *credential, const QString &name=QLatin1String(defaultRealm), QObject *parent=nullptr)
Constructs a new AuthenticationRealm object with the given parent.
void removePersistedUser(Context *c)
Removes the user from the session.
virtual AuthenticationUser authenticate(Context *c, const ParamsMultiMap &authinfo)
Tries to authenticate the user with authinfo returning a non null AuthenticationUser on success.
AuthenticationStore * store() const
Returns the authentication store object.
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved.
AuthenticationUser persistUser(Context *c, const AuthenticationUser &user)
Stores the user on the session.
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)
Tries to find the user with authinfo returning a non null AuthenticationUser on success.
AuthenticationUser restoreUser(Context *c, const QVariant &frozenUser)
Retrieves the user from the store.
QString authRealm()
Returns the authentication realm from which this user was retrieved.
void setAuthRealm(const QString &authRealm)
Sets the authentication realm from which this user was retrieved.
bool isNull() const
Returns true if the object is null.
Component(QObject *parent=nullptr)
Definition component.cpp:11
QString name() const
Definition component.cpp:33
void setName(const QString &name)
Definition component.cpp:39
The Cutelyst Context.
Definition context.h:39
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition session.cpp:170
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition session.cpp:185
static void deleteValues(Context *c, const QStringList &keys)
Definition session.cpp:233
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
QObject(QObject *parent)
void setObjectName(const QString &name)
QObject * parent() const const
bool isNull() const const