Cutelyst  2.14.0
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 
34 AuthenticationRealm::AuthenticationRealm(AuthenticationStore *store, AuthenticationCredential *credential, const QString &name, QObject *parent)
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"
Cutelyst::AuthenticationStore::findUser
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)=0
Cutelyst::AuthenticationRealm::removePersistedUser
void removePersistedUser(Context *c)
Removes the user from the session.
Definition: authenticationrealm.cpp:86
Cutelyst::AuthenticationUser::isNull
bool isNull() const
Returns true if the object is null.
Definition: authenticationuser.cpp:49
Cutelyst::AuthenticationCredential
Definition: authentication.h:31
Cutelyst::AuthenticationRealm::store
AuthenticationStore * store() const
Returns the authentication store object.
Definition: authenticationrealm.cpp:50
Cutelyst::Session::setValue
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition: session.cpp:180
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:51
Cutelyst::AuthenticationUser
Definition: authenticationuser.h:31
Cutelyst::AuthenticationRealm::userIsRestorable
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved.
Definition: authenticationrealm.cpp:127
Cutelyst::AuthenticationRealm::persistUser
AuthenticationUser persistUser(Context *c, const AuthenticationUser &user)
Stores the user on the session.
Definition: authenticationrealm.cpp:91
Cutelyst::AuthenticationUser::setAuthRealm
void setAuthRealm(const QString &authRealm)
Sets the authentication realm from which this user was retrieved.
Definition: authenticationuser.cpp:59
Cutelyst::Component
The Cutelyst Component base class.
Definition: component.h:38
Cutelyst::AuthenticationRealm::AuthenticationRealm
AuthenticationRealm(AuthenticationStore *store, AuthenticationCredential *credential, const QString &name=QLatin1String(defaultRealm), QObject *parent=nullptr)
Constructs a new AuthenticationRealm object with the given parent.
Definition: authenticationrealm.cpp:34
Cutelyst::AuthenticationStore::canAutoCreateUser
virtual bool canAutoCreateUser() const
Definition: authenticationstore.cpp:34
Cutelyst::AuthenticationCredential::authenticate
virtual AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo)=0
Tries to authenticate the authinfo using the give realm.
Cutelyst::AuthenticationStore::fromSession
virtual AuthenticationUser fromSession(Context *c, const QVariant &frozenUser)
Definition: authenticationstore.cpp:64
Cutelyst::AuthenticationStore
Definition: authenticationstore.h:26
QMap
Cutelyst::AuthenticationRealm::credential
AuthenticationCredential * credential() const
Returns the authentication credential object.
Definition: authenticationrealm.cpp:55
Cutelyst::AuthenticationStore::canAutoUpdateUser
virtual bool canAutoUpdateUser() const
Definition: authenticationstore.cpp:46
Cutelyst::AuthenticationRealm::authenticate
virtual AuthenticationUser authenticate(Context *c, const ParamsMultiMap &authinfo)
Tries to authenticate the user with authinfo returning a non null AuthenticationUser on success.
Definition: authenticationrealm.cpp:81
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Component::name
QString name() const
Definition: component.cpp:44
Cutelyst::AuthenticationStore::autoUpdateUser
virtual AuthenticationUser autoUpdateUser(Context *c, const ParamsMultiMap &userinfo) const
Definition: authenticationstore.cpp:51
Cutelyst::Component::setName
void setName(const QString &name)
Definition: component.cpp:50
Cutelyst::AuthenticationRealm::restoreUser
AuthenticationUser restoreUser(Context *c, const QVariant &frozenUser)
Retrieves the user from the store.
Definition: authenticationrealm.cpp:103
Cutelyst::Session::deleteValues
static void deleteValues(Context *c, const QStringList &keys)
Definition: session.cpp:226
Cutelyst::AuthenticationRealm::findUser
virtual AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo)
Tries to find the user with authinfo returning a non null AuthenticationUser on success.
Definition: authenticationrealm.cpp:60
Cutelyst::Session::value
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition: session.cpp:165
Cutelyst::AuthenticationStore::autoCreateUser
virtual AuthenticationUser autoCreateUser(Context *c, const ParamsMultiMap &userinfo) const
Definition: authenticationstore.cpp:39
Cutelyst::AuthenticationStore::forSession
virtual QVariant forSession(Context *c, const AuthenticationUser &user)
Definition: authenticationstore.cpp:58
Cutelyst::AuthenticationUser::authRealm
QString authRealm()
Returns the authentication realm from which this user was retrieved.
Definition: authenticationuser.cpp:54