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 
13 using namespace Cutelyst;
14 
15 Q_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 
21  AuthenticationCredential *credential,
22  const QString &name,
23  QObject *parent)
24  : Component(parent)
25  , m_store(store)
26  , m_credential(credential)
27 {
28  m_store->setParent(this);
29  m_credential->setParent(this);
31  setName(name);
32 }
33 
34 AuthenticationRealm::~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 {
91  AuthenticationUser user;
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"
void setName(const QString &name)
Definition: component.cpp:39
AuthenticationUser restoreUser(Context *c, const QVariant &frozenUser)
Retrieves the user from the store.
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)=0
virtual QVariant forSession(Context *c, const AuthenticationUser &user)
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.
bool isNull() const
Returns true if the object is null.
virtual AuthenticationUser autoUpdateUser(Context *c, const ParamsMultiMap &userinfo) const
virtual AuthenticationUser authenticate(Context *c, const ParamsMultiMap &authinfo)
Tries to authenticate the user with authinfo returning a non null AuthenticationUser on success...
virtual bool canAutoUpdateUser() const
virtual bool canAutoCreateUser() const
The Cutelyst Component base class.
Definition: component.h:25
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved.
The Cutelyst Context.
Definition: context.h:38
AuthenticationCredential * credential() const
Returns the authentication credential object.
bool isNull() const const
QString name() const
Definition: component.cpp:33
QString objectName() const const
void setObjectName(QAnyStringView name)
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition: session.cpp:185
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
void setParent(QObject *parent)
virtual AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo)=0
Tries to authenticate the authinfo using the give realm.
AuthenticationStore * store() const
Returns the authentication store object.
static void deleteValues(Context *c, const QStringList &keys)
Definition: session.cpp:233
virtual AuthenticationUser autoCreateUser(Context *c, const ParamsMultiMap &userinfo) const
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition: session.cpp:170
virtual AuthenticationUser fromSession(Context *c, const QVariant &frozenUser)
AuthenticationRealm(AuthenticationStore *store, AuthenticationCredential *credential, const QString &name=QLatin1String(defaultRealm), QObject *parent=nullptr)
Constructs a new AuthenticationRealm object with the given parent.
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.
void removePersistedUser(Context *c)
Removes the user from the session.