Cutelyst  1.6.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 Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #ifndef AUTHENTICATION_H
20 #define AUTHENTICATION_H
21 
22 #include <Cutelyst/cutelyst_global.h>
23 #include <Cutelyst/plugin.h>
24 #include <Cutelyst/paramsmultimap.h>
25 #include <Cutelyst/Plugins/Authentication/authenticationuser.h>
26 
27 namespace Cutelyst {
28 
29 class Context;
30 class AuthenticationStore;
31 class AuthenticationRealm;
32 class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT AuthenticationCredential : public QObject
33 {
34  Q_OBJECT
35 public:
39  explicit AuthenticationCredential(QObject *parent = nullptr);
40  virtual ~AuthenticationCredential();
41 
46  virtual AuthenticationUser authenticate(Context *c, AuthenticationRealm *realm, const ParamsMultiMap &authinfo) = 0;
47 };
48 
49 class AuthenticationPrivate;
50 class CUTELYST_PLUGIN_AUTHENTICATION_EXPORT Authentication : public Plugin
51 {
52  Q_OBJECT
53  Q_DECLARE_PRIVATE(Authentication)
54 public:
56  static char *defaultRealm;
57 
61  Authentication(Application *parent);
62  virtual ~Authentication();
63 
67  void addRealm(AuthenticationRealm *realm, const QString &name = QLatin1String(defaultRealm));
68 
72  void addRealm(AuthenticationStore *store, AuthenticationCredential *credential, const QString &name = QLatin1String(defaultRealm));
73 
77  AuthenticationRealm *realm(const QString &name = QLatin1String(defaultRealm)) const;
78 
82  static bool authenticate(Context *c, const ParamsMultiMap &userinfo, const QString &realm = QLatin1String(defaultRealm));
83 
87  inline static bool authenticate(Context *c, const QString &realm = QLatin1String(defaultRealm));
88 
92  static AuthenticationUser findUser(Context *c, const ParamsMultiMap &userinfo, const QString &realm = QLatin1String(defaultRealm));
93 
98  static AuthenticationUser user(Context *c);
99 
108  static bool userExists(Context *c);
109 
114  static bool userInRealm(Context *c, const QString &realmName = QLatin1String(defaultRealm));
115 
120  static void logout(Context *c);
121 
122 protected:
123  virtual bool setup(Application *app) override;
124 
125  AuthenticationPrivate *d_ptr;
126 
127 private:
128  Q_PRIVATE_SLOT(d_func(), void _q_postFork(Application*))
129 };
130 
131 inline bool Authentication::authenticate(Context *c, const QString &realm) {
132  return Authentication::authenticate(c, ParamsMultiMap(), realm);
133 }
134 
135 }
136 
137 #endif // AUTHENTICATION_H
QMap< QString, QString > ParamsMultiMap
static char * defaultRealm
default realm name
The Cutelyst Context.
Definition: context.h:52
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
static bool authenticate(Context *c, const ParamsMultiMap &userinfo, const QString &realm=QLatin1String(defaultRealm))
The Cutelyst Application.
Definition: application.h:56