Cutelyst  1.11.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 
35  : Component(parent)
36  , m_store(store)
37  , m_credential(credential)
38 {
39  m_store->setParent(this);
40  m_credential->setParent(this);
41 }
42 
43 AuthenticationRealm::~AuthenticationRealm()
44 {
45 
46 }
47 
49 {
50  return m_store;
51 }
52 
54 {
55  return m_credential;
56 }
57 
59 {
60  AuthenticationUser ret = m_store->findUser(c, userinfo);
61 
62  if (ret.isNull()) {
63  if (m_store->canAutoCreateUser()) {
64  ret = m_store->autoCreateUser(c, userinfo);
65  }
66  } else if (m_store->canAutoUpdateUser()) {
67  ret = m_store->autoUpdateUser(c, userinfo);
68  }
69 
70  return ret;
71 }
72 
74 {
75  return m_credential->authenticate(c, this, authinfo);
76 }
77 
79 {
80  Session::deleteValues(c, {QStringLiteral(SESSION_AUTHENTICATION_USER), QStringLiteral(SESSION_AUTHENTICATION_USER_REALM)});
81 }
82 
84 {
86  QStringLiteral(SESSION_AUTHENTICATION_USER),
87  m_store->forSession(c, user));
89  QStringLiteral(SESSION_AUTHENTICATION_USER_REALM),
90  objectName());
91 
92  return user;
93 }
94 
96 {
97  AuthenticationUser user;
98  QVariant _frozenUser = frozenUser;
99  if (_frozenUser.isNull()) {
100  _frozenUser = userIsRestorable(c);
101  }
102 
103  if (_frozenUser.isNull()) {
104  return user;
105  }
106 
107  user = m_store->fromSession(c, _frozenUser);
108 
109  if (!user.isNull()) {
110  // Sets the realm the user originated in
111  user.setAuthRealm(this);
112  } else {
113  qCWarning(C_AUTH_REALM) << "Store claimed to have a restorable user, but restoration failed. Did you change the user's id_field?";
114  }
115 
116  return user;
117 }
118 
120 {
121  // No need to check if session is valid
122  // as ::value will do that for us
123  return Session::value(c, QStringLiteral(SESSION_AUTHENTICATION_USER));
124 }
125 
126 #include "moc_authenticationrealm.cpp"
QMap< QString, QString > ParamsMultiMap
AuthenticationUser restoreUser(Context *c, const QVariant &frozenUser)
Retrieves the user from the store TODO move out on Cutelyst2.
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)
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 TODO move out on Cutelyst2.
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
AuthenticationRealm(AuthenticationStore *store, AuthenticationCredential *credential, QObject *parent=nullptr)
Constructs a new AuthenticationRealm object with the given parent.
QVariant userIsRestorable(Context *c)
Checks if user can be retrieved TODO move out on Cutelyst2.
The Cutelyst Context.
Definition: context.h:50
AuthenticationCredential * credential() const
Returns the authentication credential object.
virtual bool canAutoUpdateUser() const
void setAuthRealm(AuthenticationRealm *authRealm)
Sets the authentication realm from which this user was retrieved.
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition: session.cpp:176
virtual AuthenticationUser autoUpdateUser(Context *c, const ParamsMultiMap &userinfo) const
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.
static void deleteValues(Context *c, const QStringList &keys)
Definition: session.cpp:222
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition: session.cpp:161
bool isNull() const
Returns true if the object is null.
virtual AuthenticationUser fromSession(Context *c, const QVariant &frozenUser)
virtual bool canAutoCreateUser() const
void removePersistedUser(Context *c)
Removes the user from the session TODO move out on Cutelyst2.