Cutelyst  2.3.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")
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"
QMap< QString, QString > ParamsMultiMap
void setName(const QString &name)
Definition: component.cpp:45
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:38
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved.
The Cutelyst Context.
Definition: context.h:50
AuthenticationCredential * credential() const
Returns the authentication credential object.
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition: session.cpp:176
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
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:222
virtual AuthenticationUser autoCreateUser(Context *c, const ParamsMultiMap &userinfo) const
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition: session.cpp:161
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.
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.