Cutelyst  1.11.0
authentication.h
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 #ifndef AUTHENTICATION_H
19 #define AUTHENTICATION_H
20 
21 #include <Cutelyst/cutelyst_global.h>
22 #include <Cutelyst/plugin.h>
23 #include <Cutelyst/paramsmultimap.h>
24 #include <Cutelyst/Plugins/Authentication/authenticationuser.h>
25 
26 namespace Cutelyst {
27 
28 class Context;
29 class AuthenticationStore;
30 class AuthenticationRealm;
31 class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT AuthenticationCredential : public QObject
32 {
33  Q_OBJECT
34 public:
38  explicit AuthenticationCredential(QObject *parent = nullptr);
39  virtual ~AuthenticationCredential();
40 
45  virtual AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) = 0;
46 };
47 
48 class AuthenticationPrivate;
49 class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT Authentication : public Plugin
50 {
51  Q_OBJECT
52  Q_DECLARE_PRIVATE(Authentication)
53 public:
55  static char *defaultRealm;
56 
60  Authentication(Application *parent);
61  virtual ~Authentication();
62 
67  void addRealm(AuthenticationRealm *realm, const QString &name = QLatin1String(defaultRealm));
68 
73  void addRealm(AuthenticationStore *store, AuthenticationCredential *credential, const QString &name = QLatin1String(defaultRealm));
74 
78  AuthenticationRealm *realm(const QString &name = QLatin1String(defaultRealm)) const;
79 
83  static bool authenticate(Context *c, const ParamsMultiMap &userinfo, const QString &realm = QLatin1String(defaultRealm));
84 
88  inline static bool authenticate(Context *c, const QString &realm = QLatin1String(defaultRealm));
89 
93  static AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo, const QString &realm = QLatin1String(defaultRealm));
94 
99  static AuthenticationUser user(Context *c);
100 
109  static bool userExists(Context *c);
110 
115  static bool userInRealm(Context *c, const QString &realmName = QLatin1String(defaultRealm));
116 
121  static void logout(Context *c);
122 
123 protected:
124  virtual bool setup(Application *app) override;
125 
126  AuthenticationPrivate *d_ptr;
127 
128 private:
129  Q_PRIVATE_SLOT(d_func(), void _q_postFork(Application*))
130 };
131 
132 inline bool Authentication::authenticate(Context *c, const QString &realm) {
133  return Authentication::authenticate(c, ParamsMultiMap(), realm);
134 }
135 
136 }
137 
138 #endif // AUTHENTICATION_H
QMap< QString, QString > ParamsMultiMap
The Cutelyst Context.
Definition: context.h:50
static bool authenticate(Context *c, const ParamsMultiMap &userinfo, const QString &realm=QLatin1String(defaultRealm))
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
static char * defaultRealm
default realm name
The Cutelyst Application.
Definition: application.h:54