Cutelyst  3.5.0
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 "context.h"
9 #include "common.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  : Component(parent)
22  , m_store(store)
23  , m_credential(credential)
24 {
25  m_store->setParent(this);
26  m_credential->setParent(this);
28  setName(name);
29 }
30 
31 AuthenticationRealm::~AuthenticationRealm()
32 {
33 
34 }
35 
37 {
38  return m_store;
39 }
40 
42 {
43  return m_credential;
44 }
45 
47 {
48  AuthenticationUser ret = m_store->findUser(c, userinfo);
49 
50  if (ret.isNull()) {
51  if (m_store->canAutoCreateUser()) {
52  ret = m_store->autoCreateUser(c, userinfo);
53  }
54  } else {
55  if (m_store->canAutoUpdateUser()) {
56  ret = m_store->autoUpdateUser(c, userinfo);
57  }
58  }
59 
60  if (!ret.isNull() && ret.authRealm() != name()) {
61  ret.setAuthRealm(name());
62  }
63 
64  return ret;
65 }
66 
68 {
69  return m_credential->authenticate(c, this, authinfo);
70 }
71 
73 {
74  Session::deleteValues(c, {QStringLiteral(SESSION_AUTHENTICATION_USER), QStringLiteral(SESSION_AUTHENTICATION_USER_REALM)});
75 }
76 
78 {
80  QStringLiteral(SESSION_AUTHENTICATION_USER),
81  m_store->forSession(c, user));
83  QStringLiteral(SESSION_AUTHENTICATION_USER_REALM),
84  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 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"
void setName(const QString &name)
Definition: component.cpp:37
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:31
void setObjectName(const QString &name)
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition: session.cpp:165
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:211
virtual AuthenticationUser autoCreateUser(Context *c, const ParamsMultiMap &userinfo) const
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition: session.cpp:150
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.