Cutelyst  2.13.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 if (m_store->canAutoUpdateUser()) {
69  ret = m_store->autoUpdateUser(c, userinfo);
70  }
71 
72  return ret;
73 }
74 
76 {
77  return m_credential->authenticate(c, this, authinfo);
78 }
79 
81 {
82  Session::deleteValues(c, {QStringLiteral(SESSION_AUTHENTICATION_USER), QStringLiteral(SESSION_AUTHENTICATION_USER_REALM)});
83 }
84 
86 {
88  QStringLiteral(SESSION_AUTHENTICATION_USER),
89  m_store->forSession(c, user));
91  QStringLiteral(SESSION_AUTHENTICATION_USER_REALM),
92  objectName());
93 
94  return user;
95 }
96 
98 {
99  AuthenticationUser user;
100  QVariant _frozenUser = frozenUser;
101  if (_frozenUser.isNull()) {
102  _frozenUser = userIsRestorable(c);
103  }
104 
105  if (_frozenUser.isNull()) {
106  return user;
107  }
108 
109  user = m_store->fromSession(c, _frozenUser);
110 
111  if (!user.isNull()) {
112  // Sets the realm the user originated in
113  user.setAuthRealm(objectName());
114  } else {
115  qCWarning(C_AUTH_REALM) << "Store claimed to have a restorable user, but restoration failed. Did you change the user's id_field?";
116  }
117 
118  return user;
119 }
120 
122 {
123  // No need to check if session is valid
124  // as ::value will do that for us
125  return Session::value(c, QStringLiteral(SESSION_AUTHENTICATION_USER));
126 }
127 
128 #include "moc_authenticationrealm.cpp"
Cutelyst::ParamsMultiMap
QMap< QString, QString > ParamsMultiMap
Definition: paramsmultimap.h:36
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:80
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:50
Cutelyst::AuthenticationUser
Definition: authenticationuser.h:31
Cutelyst::AuthenticationRealm::userIsRestorable
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved.
Definition: authenticationrealm.cpp:121
Cutelyst::AuthenticationRealm::persistUser
AuthenticationUser persistUser(Context *c, const AuthenticationUser &user)
Stores the user on the session.
Definition: authenticationrealm.cpp:85
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:60
Cutelyst::AuthenticationStore
Definition: authenticationstore.h:26
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:44
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:75
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:49
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:97
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:54