cutelyst 4.3.0
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
20AuthenticationRealm::AuthenticationRealm(std::shared_ptr<AuthenticationStore> store,
21 std::shared_ptr<AuthenticationCredential> credential,
22 const QString &name,
23 QObject *parent)
24 : Component(parent)
25 , m_store(store)
26 , m_credential(credential)
27{
28 m_credential->setParent(this);
31}
32
33AuthenticationRealm::~AuthenticationRealm()
34{
35}
36
37std::shared_ptr<AuthenticationStore> AuthenticationRealm::store() const noexcept
38{
39 return m_store;
40}
41
42std::shared_ptr<AuthenticationCredential> AuthenticationRealm::credential() const noexcept
43{
44 return m_credential;
45}
46
48{
49 AuthenticationUser ret = m_store->findUser(c, userinfo);
50
51 if (ret.isNull()) {
52 if (m_store->canAutoCreateUser()) {
53 ret = m_store->autoCreateUser(c, userinfo);
54 }
55 } else {
56 if (m_store->canAutoUpdateUser()) {
57 ret = m_store->autoUpdateUser(c, userinfo);
58 }
59 }
60
61 if (!ret.isNull() && ret.authRealm() != name()) {
62 ret.setAuthRealm(name());
63 }
64
65 return ret;
66}
67
69{
70 return m_credential->authenticate(c, this, authinfo);
71}
72
74{
76 {QStringLiteral(SESSION_AUTHENTICATION_USER),
77 QStringLiteral(SESSION_AUTHENTICATION_USER_REALM)});
78}
79
81{
82 Session::setValue(c, QStringLiteral(SESSION_AUTHENTICATION_USER), m_store->forSession(c, user));
83 Session::setValue(c, QStringLiteral(SESSION_AUTHENTICATION_USER_REALM), objectName());
84
85 return user;
86}
87
89{
91 QVariant _frozenUser = frozenUser;
92 if (_frozenUser.isNull()) {
93 _frozenUser = userIsRestorable(c);
94 }
95
96 if (_frozenUser.isNull()) {
97 return user;
98 }
99
100 user = m_store->fromSession(c, _frozenUser);
101
102 if (!user.isNull()) {
103 // Sets the realm the user originated in
104 user.setAuthRealm(objectName());
105 } else {
106 qCWarning(C_AUTH_REALM) << "Store claimed to have a restorable user, but restoration "
107 "failed. Did you change the user's id_field?";
108 }
109
110 return user;
111}
112
114{
115 // No need to check if session is valid
116 // as ::value will do that for us
117 return Session::value(c, QStringLiteral(SESSION_AUTHENTICATION_USER));
118}
119
120#include "moc_authenticationrealm.cpp"
std::shared_ptr< AuthenticationCredential > credential() const noexcept
std::shared_ptr< AuthenticationStore > store() const noexcept
virtual AuthenticationUser authenticate(Context *c, const ParamsMultiMap &authinfo)
AuthenticationRealm(std::shared_ptr< AuthenticationStore > store, std::shared_ptr< AuthenticationCredential > credential, const QString &name=QLatin1String(defaultRealm), QObject *parent=nullptr)
AuthenticationUser persistUser(Context *c, const AuthenticationUser &user)
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)
AuthenticationUser restoreUser(Context *c, const QVariant &frozenUser)
Container for user data retrieved from an AuthenticationStore.
void setAuthRealm(const QString &authRealm)
The Cutelyst Component base class.
Definition component.h:30
QString name() const noexcept
Definition component.cpp:33
void setName(const QString &name)
Definition component.cpp:39
The Cutelyst Context.
Definition context.h:42
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition session.cpp:168
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition session.cpp:183
static void deleteValues(Context *c, const QStringList &keys)
Definition session.cpp:231
The Cutelyst namespace holds all public Cutelyst API.
void setObjectName(QAnyStringView name)
bool isNull() const const