core: Allow configuring DB/auth from environment
[quassel.git] / src / core / authenticator.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <QObject>
24 #include <QProcessEnvironment>
25 #include <QString>
26 #include <QStringList>
27 #include <QVariant>
28
29 #include "types.h"
30
31 class Authenticator : public QObject {
32
33     Q_OBJECT
34
35 public:
36     using QObject::QObject;
37     ~Authenticator() override = default;
38
39     enum State {
40         IsReady,      // ready to go
41         NeedsSetup,   // need basic setup (ask the user for input)
42         NotAvailable  // remove the authenticator backend from the list of avaliable authenticators.
43     };
44
45
46 public slots:
47     // General
48
49     //! Check if the authenticator type is available.
50     /** An authenticator subclass should return true if it can be successfully used, i.e. if all
51      *  prerequisites are in place.
52      * \return True if and only if the authenticator class can be successfully used.
53      */
54     virtual bool isAvailable() const = 0;
55
56     //! Returns the identifier of the authenticator backend
57     /** \return A string that can be used by the client to identify the authenticator backend */
58     virtual QString backendId() const = 0;
59
60     //! Returns the display name of the authenticator backend
61     /** \return A string that can be used by the client to name the authenticator backend */
62     virtual QString displayName() const = 0;
63
64     //! Returns a description of this authenticator backend
65     /** \return A string that can be displayed by the client to describe the authenticator */
66     virtual QString description() const = 0;
67
68     //! Returns data required to configure the authenticator backend
69     /**
70      * A list of flattened triples for each field: {key, translated field name, default value}
71      * The default value's type determines the kind of input widget to be shown
72      * (int -> QSpinBox; QString -> QLineEdit)
73      * \return A list of triples defining the data to be shown in the configuration dialog
74      */
75     virtual QVariantList setupData() const = 0;
76
77     //! Checks if the authenticator allows manual password changes from inside quassel.
78     virtual bool canChangePassword() const = 0;
79
80     //! Setup the authenticator provider.
81     /** This prepares the authenticator provider (e.g. create tables, etc.) for use within Quassel.
82      *  \param settings   Hostname, port, username, password, ...
83      *  \return True if and only if the authenticator provider was initialized successfully.
84      */
85     virtual bool setup(const QVariantMap &settings = QVariantMap(),
86                        const QProcessEnvironment &environment = {},
87                        bool loadFromEnvironment = false) = 0;
88
89     //! Initialize the authenticator provider
90     /** \param settings   Hostname, port, username, password, ...
91      *  \return the State the authenticator backend is now in (see authenticator::State)
92      */
93     virtual State init(const QVariantMap &settings = QVariantMap(),
94                        const QProcessEnvironment &environment = {},
95                        bool loadFromEnvironment = false) = 0;
96
97     //! Validate a username with a given password.
98     /** \param user     The username to validate
99      *  \param password The user's alleged password
100      *  \return A valid UserId if the password matches the username; 0 else
101      */
102     virtual UserId validateUser(const QString &user, const QString &password) = 0;
103 };