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