Cutelyst  2.14.2
authenticationrealm.cpp
1 /*
2  * Copyright (C) 2013-2017 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "authenticationrealm.h"
20 
21 #include "authenticationstore.h"
22 #include "context.h"
23 #include "common.h"
24 
25 #include <Cutelyst/Plugins/Session/session.h>
26 
27 using namespace Cutelyst;
28 
29 Q_LOGGING_CATEGORY(C_AUTH_REALM, "cutelyst.plugin.authentication.realm", QtWarningMsg)
30 
31 #define SESSION_AUTHENTICATION_USER "__authentication_user"
32 #define SESSION_AUTHENTICATION_USER_REALM "__authentication_user_realm" // in authentication.cpp
33 
35  : Component(parent)
36  , m_store(store)
37  , m_credential(credential)
38 {
39  m_store->setParent(this);
40  m_credential->setParent(this);
41  setObjectName(name);
42  setName(name);
43 }
44 
45 AuthenticationRealm::~AuthenticationRealm()
46 {
47 
48 }
49 
51 {
52  return m_store;
53 }
54 
56 {
57  return m_credential;
58 }
59 
61 {
62  AuthenticationUser ret = m_store->findUser(c, userinfo);
63 
64  if (ret.isNull()) {
65  if (m_store->canAutoCreateUser()) {
66  ret = m_store->autoCreateUser(c, userinfo);
67  }
68  } else {
69  if (m_store->canAutoUpdateUser()) {
70  ret = m_store->autoUpdateUser(c, userinfo);
71  }
72  }
73 
74  if (!ret.isNull() && ret.authRealm() != name()) {
75  ret.setAuthRealm(name());
76  }
77 
78  return ret;
79 }
80 
82 {
83  return m_credential->authenticate(c, this, authinfo);
84 }
85 
87 {
88  Session::deleteValues(c, {QStringLiteral(SESSION_AUTHENTICATION_USER), QStringLiteral(SESSION_AUTHENTICATION_USER_REALM)});
89 }
90 
92 {
94  QStringLiteral(SESSION_AUTHENTICATION_USER),
95  m_store->forSession(c, user));
97  QStringLiteral(SESSION_AUTHENTICATION_USER_REALM),
98  objectName());
99 
100  return user;
101 }
102 
104 {
105  AuthenticationUser user;
106  QVariant _frozenUser = frozenUser;
107  if (_frozenUser.isNull()) {
108  _frozenUser = userIsRestorable(c);
109  }
110 
111  if (_frozenUser.isNull()) {
112  return user;
113  }
114 
115  user = m_store->fromSession(c, _frozenUser);
116 
117  if (!user.isNull()) {
118  // Sets the realm the user originated in
119  user.setAuthRealm(objectName());
120  } else {
121  qCWarning(C_AUTH_REALM) << "Store claimed to have a restorable user, but restoration failed. Did you change the user's id_field?";
122  }
123 
124  return user;
125 }
126 
128 {
129  // No need to check if session is valid
130  // as ::value will do that for us
131  return Session::value(c, QStringLiteral(SESSION_AUTHENTICATION_USER));
132 }
133 
134 #include "moc_authenticationrealm.cpp"
void setName(const QString &name)
Definition: component.cpp:50
AuthenticationUser restoreUser(Context *c, const QVariant &frozenUser)
Retrieves the user from the store.
AuthenticationStore * store() const
Returns the authentication store object.
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)=0
virtual QVariant forSession(Context *c, const AuthenticationUser &user)
QString name() const
Definition: component.cpp:44
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.
virtual AuthenticationUser authenticate(Context *c, const ParamsMultiMap &authinfo)
Tries to authenticate the user with authinfo returning a non null AuthenticationUser on success...
virtual AuthenticationUser autoCreateUser(Context *c, const ParamsMultiMap &userinfo) const
The Cutelyst Component base class.
Definition: component.h:38
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved.
The Cutelyst Context.
Definition: context.h:51
AuthenticationCredential * credential() const
Returns the authentication credential object.
virtual bool canAutoUpdateUser() const
bool isNull() const
void setObjectName(const QString &name)
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition: session.cpp:180
virtual AuthenticationUser autoUpdateUser(Context *c, const ParamsMultiMap &userinfo) const
void setParent(QObject *parent)
virtual AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo)=0
Tries to authenticate the authinfo using the give realm.
static void deleteValues(Context *c, const QStringList &keys)
Definition: session.cpp:226
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition: session.cpp:165
bool isNull() const
Returns true if the object is null.
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.
virtual bool canAutoCreateUser() const
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.