mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
- The first part
- Added CodeDocs config file for customization - Fixing LGTM alerts - LGTM bug fixed again - added token option to hyperion-remote - fix DBManager::getDB() - next bugfix - correct broken signal from SettingManager to Hyperion - Token list is created after the schema is fetched Signed-off-by: Paulchen-Panther <Paulchen-Panter@protonmail.com>
This commit is contained in:
160
include/hyperion/AuthManager.h
Normal file
160
include/hyperion/AuthManager.h
Normal file
@@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
|
||||
#include <utils/Logger.h>
|
||||
#include <utils/settings.h>
|
||||
|
||||
//qt
|
||||
#include <QMap>
|
||||
|
||||
class AuthTable;
|
||||
class QTimer;
|
||||
|
||||
///
|
||||
/// @brief Manage the authorization of user and tokens. This class is created once as part of the HyperionDaemon
|
||||
/// To work with the global instance use AuthManager::getInstance()
|
||||
///
|
||||
class AuthManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
friend class HyperionDaemon;
|
||||
/// constructor is private, can be called from HyperionDaemon
|
||||
AuthManager(const QString& rootPath, QObject* parent = 0);
|
||||
|
||||
public:
|
||||
struct AuthDefinition{
|
||||
QString id;
|
||||
QString comment;
|
||||
QObject* caller;
|
||||
uint64_t timeoutTime;
|
||||
QString token;
|
||||
QString lastUse;
|
||||
};
|
||||
|
||||
///
|
||||
/// @brief Get all available token entries
|
||||
///
|
||||
const QVector<AuthDefinition> getTokenList();
|
||||
|
||||
///
|
||||
/// @brief Check authorization is required according to the user setting
|
||||
/// @return True if authorization required else false
|
||||
///
|
||||
const bool & isAuthRequired();
|
||||
|
||||
///
|
||||
/// @brief Check if authorization is required for local network connections
|
||||
/// @return True if authorization required else false
|
||||
///
|
||||
const bool & isLocalAuthRequired();
|
||||
|
||||
///
|
||||
/// @brief Create a new token and skip the usual chain
|
||||
/// @param comment The comment that should be used for
|
||||
/// @return The new Auth definition
|
||||
///
|
||||
const AuthDefinition createToken(const QString& comment);
|
||||
|
||||
///
|
||||
/// @brief Check if user is authorized
|
||||
/// @param user The username
|
||||
/// @param pw The password
|
||||
/// @return True if authorized else false
|
||||
///
|
||||
const bool isUserAuthorized(const QString& user, const QString& pw);
|
||||
|
||||
///
|
||||
/// @brief Check if token is authorized
|
||||
/// @param token The token
|
||||
/// @return True if authorized else false
|
||||
///
|
||||
const bool isTokenAuthorized(const QString& token);
|
||||
|
||||
///
|
||||
/// @brief Generate a new pending token request with the provided comment and id as identifier helper
|
||||
/// @param caller The QObject of the caller to deliver the reply
|
||||
/// @param comment The comment as ident helper
|
||||
/// @param id The id created by the caller
|
||||
///
|
||||
void setNewTokenRequest(QObject* caller, const QString& comment, const QString& id);
|
||||
|
||||
///
|
||||
/// @brief Accept a token request by id, generate token and inform token caller
|
||||
/// @param id The id of the request
|
||||
/// @return True on success, false if not found
|
||||
///
|
||||
const bool acceptTokenRequest(const QString& id);
|
||||
|
||||
///
|
||||
/// @brief Deny a token request by id, inform the requester
|
||||
/// @param id The id of the request
|
||||
/// @return True on success, false if not found
|
||||
///
|
||||
const bool denyTokenRequest(const QString& id);
|
||||
|
||||
///
|
||||
/// @brief Get pending requests
|
||||
/// @return All pending requests
|
||||
///
|
||||
const QMap<QString, AuthDefinition> getPendingRequests();
|
||||
|
||||
///
|
||||
/// @brief Delete a token by id
|
||||
/// @param id The token id
|
||||
/// @return True on success else false (or not found)
|
||||
///
|
||||
const bool deleteToken(const QString& id);
|
||||
|
||||
/// Pointer of this instance
|
||||
static AuthManager* manager;
|
||||
/// Get Pointer of this instance
|
||||
static AuthManager* getInstance() { return manager; };
|
||||
|
||||
public slots:
|
||||
///
|
||||
/// @brief Handle settings update from Hyperion Settingsmanager emit
|
||||
/// @param type settings type from enum
|
||||
/// @param config configuration object
|
||||
///
|
||||
void handleSettingsUpdate(const settings::type& type, const QJsonDocument& config);
|
||||
|
||||
signals:
|
||||
///
|
||||
/// @brief Emits whenever a new token Request has been created along with the id and comment
|
||||
/// @param id The id of the request
|
||||
/// @param comment The comment of the request
|
||||
///
|
||||
void newPendingTokenRequest(const QString& id, const QString& comment);
|
||||
|
||||
///
|
||||
/// @brief Emits when the user has accepted or denied a token
|
||||
/// @param success If true the request was accepted else false and no token will be available
|
||||
/// @param caller The origin caller instance who requested this token
|
||||
/// @param token The new token that is now valid
|
||||
/// @param comment The comment that was part of the request
|
||||
/// @param id The id that was part of the request
|
||||
///
|
||||
void tokenResponse(const bool& success, QObject* caller, const QString& token, const QString& comment, const QString& id);
|
||||
|
||||
private:
|
||||
/// Database interface for auth table
|
||||
AuthTable* _authTable;
|
||||
|
||||
/// All pending requests
|
||||
QMap<QString,AuthDefinition> _pendingRequests;
|
||||
|
||||
/// Reflect state of global auth
|
||||
bool _authRequired;
|
||||
|
||||
/// Reflect state of local auth
|
||||
bool _localAuthRequired;
|
||||
|
||||
/// Timer for counting against pendingRequest timeouts
|
||||
QTimer* _timer;
|
||||
|
||||
private slots:
|
||||
///
|
||||
/// @brief Check timeout of pending requests
|
||||
///
|
||||
void checkTimeout();
|
||||
};
|
@@ -7,6 +7,7 @@
|
||||
#include <QJsonObject>
|
||||
|
||||
class Hyperion;
|
||||
class SettingsTable;
|
||||
|
||||
///
|
||||
/// @brief Manage the settings read write from/to config file, on settings changed will emit a signal to update components accordingly
|
||||
@@ -17,43 +18,38 @@ class SettingsManager : public QObject
|
||||
public:
|
||||
///
|
||||
/// @brief Construct a settings manager and assign a hyperion instance
|
||||
/// @params hyperion The parent hyperion instance
|
||||
/// @params instance Instance number of Hyperion
|
||||
/// @params instance Instance number of Hyperion
|
||||
/// @params configFile The config file
|
||||
/// @params hyperion The parent hyperion instance
|
||||
///
|
||||
SettingsManager(Hyperion* hyperion, const quint8& instance, const QString& configFile);
|
||||
|
||||
///
|
||||
/// @brief Construct a settings manager for HyperionDaemon
|
||||
///
|
||||
SettingsManager(const quint8& instance, const QString& configFile);
|
||||
~SettingsManager();
|
||||
SettingsManager(const quint8& instance, const QString& configFile, Hyperion* hyperion = nullptr);
|
||||
|
||||
///
|
||||
/// @brief Save a complete json config
|
||||
/// @param config The entire config object
|
||||
/// @param correct If true will correct json against schema before save
|
||||
/// @return True on success else false
|
||||
/// @return True on success else false
|
||||
///
|
||||
bool saveSettings(QJsonObject config, const bool& correct = false);
|
||||
|
||||
///
|
||||
/// @brief get a single setting json from config
|
||||
/// @param type The settings::type from enum
|
||||
/// @return The requested json data as QJsonDocument
|
||||
/// @param type The settings::type from enum
|
||||
/// @return The requested json data as QJsonDocument
|
||||
///
|
||||
const QJsonDocument getSetting(const settings::type& type);
|
||||
|
||||
///
|
||||
/// @brief get the full settings object of this instance (with global settings)
|
||||
/// @return The requested json
|
||||
/// @return The requested json
|
||||
///
|
||||
const QJsonObject & getSettings() { return _qconfig; };
|
||||
|
||||
signals:
|
||||
///
|
||||
/// @brief Emits whenever a config part changed.
|
||||
/// @param type The settings type from enum
|
||||
/// @param data The data as QJsonDocument
|
||||
/// @param type The settings type from enum
|
||||
/// @param data The data as QJsonDocument
|
||||
///
|
||||
void settingsChanged(const settings::type& type, const QJsonDocument& data);
|
||||
|
||||
@@ -64,6 +60,9 @@ private:
|
||||
/// Logger instance
|
||||
Logger* _log;
|
||||
|
||||
/// instance of database table interface
|
||||
SettingsTable* _sTable;
|
||||
|
||||
/// the schema
|
||||
static QJsonObject schemaJson;
|
||||
|
||||
|
Reference in New Issue
Block a user