Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Replaced most raw pointers with std::shared_ptr
Browse files Browse the repository at this point in the history
 - Now classes accept interfaces only of their existing dependencies.
 - Some interfaces and implementations will be refactored to remove access
   to more internal values (e.g. friend classes use-cases).
 - Removed un-used dependencies from WsServer
 - Left raw pointers will be refactored later
 - Updated test init
 - Interface commenting will be done later when they are more stable

Signed-off-by: Miladinovic Bojan <[email protected]>
  • Loading branch information
Miladinovic Bojan committed Oct 29, 2019
1 parent e8aed16 commit 33a4c8a
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 145 deletions.
7 changes: 4 additions & 3 deletions w3c-visserver-api/include/AccessChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
#define __ACCESSCHECKER_H__

#include "IAccessChecker.hpp"
#include "Authenticator.hpp"

class IAuthenticator;


class AccessChecker : public IAccessChecker {
private:
Authenticator *tokenValidator;
std::shared_ptr<IAuthenticator> tokenValidator;

public:
AccessChecker(Authenticator *vdator);
AccessChecker(std::shared_ptr<IAuthenticator> vdator);
bool checkReadAccess(WsChannel &channel, const std::string &path);
bool checkWriteAccess(WsChannel &channel, const std::string &path);
bool checkPathWriteAccess(WsChannel &channel, const jsoncons::json &paths);
Expand Down
11 changes: 7 additions & 4 deletions w3c-visserver-api/include/Authenticator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
#include <memory>
#include <string>

#include "IAuthenticator.hpp"

using namespace std;

class WsChannel;
class VssDatabase;
class IVssDatabase;
class ILogger;

class Authenticator {
class Authenticator : public IAuthenticator {
private:
string pubkey = "secret";
string algorithm = "RS256";
Expand All @@ -34,11 +36,12 @@ class Authenticator {
public:
Authenticator(std::shared_ptr<ILogger> loggerUtil, string secretkey, string algorithm);

int validate(WsChannel &channel, VssDatabase *database,
int validate(WsChannel &channel,
std::shared_ptr<IVssDatabase> dn,
string authToken);

void updatePubKey(string key);
bool isStillValid(WsChannel &channel);
void resolvePermissions(WsChannel &channel, VssDatabase *database);
void resolvePermissions(WsChannel &channel, std::shared_ptr<IVssDatabase> database);
};
#endif
7 changes: 4 additions & 3 deletions w3c-visserver-api/include/IAuthenticator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
using namespace std;

class WsChannel;
class VssDatabase;
class IVssDatabase;
class ILogger;

class IAuthenticator {
public:
virtual ~IAuthenticator() {}

virtual int validate(WsChannel &channel,
VssDatabase *database,
std::shared_ptr<IVssDatabase> database,
string authToken) = 0;
virtual void updatePubKey(string key) = 0;
virtual bool isStillValid(WsChannel &channel) = 0;
virtual void resolvePermissions(WsChannel &channel, VssDatabase *database) = 0;
virtual void resolvePermissions(WsChannel &channel, std::shared_ptr<IVssDatabase> database) = 0;
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include <string>


class IWsServer {
class IServer {
public:
virtual ~IWsServer() {}
virtual ~IServer() {}

virtual void startServer(const std::string &endpointName) = 0;
virtual void sendToConnection(uint32_t connID, const std::string &message) = 0;
Expand Down
8 changes: 5 additions & 3 deletions w3c-visserver-api/include/ISubscriptionHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
#define __ISUBSCRIPTIONHANDLER_H__

#include <string>
#include <memory>
#include <jsoncons/json.hpp>

class VssDatabase;
class WsChannel;
class WsServer;
class ILogger;
class IVssDatabase;
class IServer;

class ISubscriptionHandler {
public:
virtual ~ISubscriptionHandler() {}

virtual uint32_t subscribe(WsChannel& channel,
VssDatabase* db,
std::shared_ptr<IVssDatabase> db,
uint32_t channelID,
const std::string &path) = 0;
virtual int unsubscribe(uint32_t subscribeID) = 0;
virtual int unsubscribeAll(uint32_t connectionID) = 0;
virtual int updateByUUID(const std::string &signalUUID, const jsoncons::json &value) = 0;
virtual int updateByPath(const std::string &path, const jsoncons::json &value) = 0;
virtual WsServer* getServer() = 0;
virtual std::shared_ptr<IServer> getServer() = 0;
virtual int startThread() = 0;
virtual int stopThread() = 0;
virtual bool isThreadRunning() const = 0;
Expand Down
7 changes: 7 additions & 0 deletions w3c-visserver-api/include/IVssDatabase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class IVssDatabase {
const std::string &path,
jsoncons::json value) = 0;
virtual jsoncons::json getSignal(WsChannel& channel, const std::string &path) = 0;

// TODO: temporary added while components are refactored
jsoncons::json data_tree;
jsoncons::json meta_tree;
virtual std::list<std::string> getPathForGet(const std::string &path, bool& isBranch) = 0;
virtual std::string getVSSSpecificPath(const std::string &path, bool& isBranch, jsoncons::json& tree) = 0;
virtual jsoncons::json getPathForSet(const std::string &path, jsoncons::json value) = 0;
};

#endif
23 changes: 14 additions & 9 deletions w3c-visserver-api/include/SubscriptionHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

#include "visconf.hpp"
#include "ISubscriptionHandler.hpp"
#include "IAuthenticator.hpp"
#include "IAccessChecker.hpp"
#include "IServer.hpp"

class AccessChecker;
class Authenticator;
Expand All @@ -43,28 +46,30 @@ class SubscriptionHandler : public ISubscriptionHandler {
private:
std::shared_ptr<ILogger> logger;
std::unordered_map<uuid_t, subscriptions_t> subscribeHandle;
WsServer* server;
Authenticator* validator;
AccessChecker* checkAccess;
std::shared_ptr<IServer> server;
std::shared_ptr<IAuthenticator> validator;
std::shared_ptr<IAccessChecker> checkAccess;
std::mutex subMutex;
std::thread subThread;
bool threadRun;
std::queue<std::pair<uint32_t, jsoncons::json>> buffer;

public:
SubscriptionHandler(std::shared_ptr<ILogger> loggerUtil,
WsServer* wserver,
Authenticator* authenticate,
AccessChecker* checkAccess);
std::shared_ptr<IServer> wserver,
std::shared_ptr<IAuthenticator> authenticate,
std::shared_ptr<IAccessChecker> checkAccess);
~SubscriptionHandler();

uint32_t subscribe(WsChannel& channel, VssDatabase* db,
uint32_t channelID, const std::string &path);
uint32_t subscribe(WsChannel& channel,
std::shared_ptr<IVssDatabase> db,
uint32_t channelID,
const std::string &path);
int unsubscribe(uint32_t subscribeID);
int unsubscribeAll(uint32_t connectionID);
int updateByUUID(const std::string &signalUUID, const jsoncons::json &value);
int updateByPath(const std::string &path, const jsoncons::json &value);
WsServer* getServer();
std::shared_ptr<IServer> getServer();
int startThread();
int stopThread();
bool isThreadRunning() const;
Expand Down
27 changes: 14 additions & 13 deletions w3c-visserver-api/include/VssCommandProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@

#include "IVssCommandProcessor.hpp"

class VssDatabase;
class SubscriptionHandler;
class Authenticator;
class AccessChecker;
class WsChannel;
class IVssDatabase;
class ISubscriptionHandler;
class IAuthenticator;
class IAccessChecker;
class ILogger;
class WsChannel;


class VssCommandProcessor : public IVssCommandProcessor {
private:
std::shared_ptr<ILogger> logger;
VssDatabase* database = NULL;
SubscriptionHandler* subHandler = NULL;
Authenticator* tokenValidator = NULL;
AccessChecker* accessValidator = NULL;
std::shared_ptr<IVssDatabase> database;
std::shared_ptr<ISubscriptionHandler> subHandler;
std::shared_ptr<IAuthenticator> tokenValidator;
std::shared_ptr<IAccessChecker> accessValidator;
#ifdef JSON_SIGNING_ON
SigningHandler* signer = NULL;
std::shared_ptr<SigningHandler> signer;
#endif

std::string processGet(WsChannel& channel, uint32_t request_id, std::string path);
Expand All @@ -54,9 +55,9 @@ class VssCommandProcessor : public IVssCommandProcessor {

public:
VssCommandProcessor(std::shared_ptr<ILogger> loggerUtil,
VssDatabase* database,
Authenticator* vdator,
SubscriptionHandler* subhandler);
std::shared_ptr<IVssDatabase> database,
std::shared_ptr<IAuthenticator> vdator,
std::shared_ptr<ISubscriptionHandler> subhandler);
~VssCommandProcessor();

std::string processQuery(const std::string &req_json, WsChannel& channel);
Expand Down
24 changes: 11 additions & 13 deletions w3c-visserver-api/include/VssDatabase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,30 @@

#include "IVssDatabase.hpp"

class SubscriptionHandler;
class AccessChecker;
class WsChannel;
class IAccessChecker;
class ISubscriptionHandler;
class ILogger;

class VssDatabase : public IVssDatabase {
friend class SubscriptionHandler;
friend class Authenticator;
#ifdef UNIT_TEST
friend class w3cunittest;
#endif

private:
std::shared_ptr<ILogger> logger;
std::mutex rwMutex;
jsoncons::json data_tree;
jsoncons::json meta_tree;
SubscriptionHandler* subHandler;
AccessChecker* accessValidator;
std::string getVSSSpecificPath(std::string path, bool& isBranch, jsoncons::json& tree);

std::shared_ptr<ISubscriptionHandler> subHandler;
std::shared_ptr<IAccessChecker> accessValidator;
std::string getPathForMetadata(std::string path, bool& isBranch);
std::list<std::string> getPathForGet(std::string path, bool& isBranch);
jsoncons::json getPathForSet(std::string path, jsoncons::json value);
std::string getReadablePath(std::string jsonpath);
void checkSetPermission(WsChannel& channel, jsoncons::json valueJson);

public:
VssDatabase(std::shared_ptr<ILogger> loggerUtil,
SubscriptionHandler* subHandle,
AccessChecker* accValidator);
std::shared_ptr<ISubscriptionHandler> subHandle,
std::shared_ptr<IAccessChecker> accValidator);
~VssDatabase();

void initJsonTree(const std::string &fileName);
Expand All @@ -61,5 +55,9 @@ class VssDatabase : public IVssDatabase {
void setSignal(const std::string &path, jsoncons::json value);
jsoncons::json getSignal(WsChannel& channel, const std::string &path);

std::list<std::string> getPathForGet(const std::string &path, bool& isBranch);
std::string getVSSSpecificPath(const std::string &path, bool& isBranch,
jsoncons::json& tree);
jsoncons::json getPathForSet(const std::string &path, jsoncons::json value);
};
#endif
28 changes: 13 additions & 15 deletions w3c-visserver-api/include/WsServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,32 @@
#ifndef __WSSERVER_H__
#define __WSSERVER_H__

#include <memory>

#include "server_wss.hpp"
#include "IWsServer.hpp"

#include "IServer.hpp"


class VssCommandProcessor;
class VssCommandProcessor;
class SubscriptionHandler;
class Authenticator;
class VssDatabase;
class AccessChecker;
class IVssCommandProcessor;
class ILogger;

class WsServer : public IWsServer {
class WsServer : public IServer {
private:
SimpleWeb::SocketServer<SimpleWeb::WSS> *secureServer_;
SimpleWeb::SocketServer<SimpleWeb::WS> *insecureServer_;
std::shared_ptr<ILogger> logger;
bool isSecure_;
std::string configFileName_;
bool isInitialized_;

public:
VssCommandProcessor* cmdProcessor;
SubscriptionHandler* subHandler;
Authenticator* tokenValidator;
VssDatabase* database;
AccessChecker* accessCheck;
std::shared_ptr<IVssCommandProcessor> cmdProcessor;

WsServer(std::shared_ptr<ILogger> loggerUtil, int port, std::string configFileName, bool secure);
WsServer();
bool Initialize(std::shared_ptr<ILogger> loggerUtil,
std::shared_ptr<IVssCommandProcessor> processor,
bool secure,
int port);
~WsServer();
void startServer(const std::string &endpointName);
void sendToConnection(uint32_t connID, const std::string &message);
Expand Down
4 changes: 3 additions & 1 deletion w3c-visserver-api/src/AccessChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

#include <jsoncons/json.hpp>
#include <string>

#include "IAuthenticator.hpp"
#include "WsChannel.hpp"

using namespace std;
using namespace jsoncons;
//using jsoncons::json;

AccessChecker::AccessChecker(Authenticator *vdator) {
AccessChecker::AccessChecker(std::shared_ptr<IAuthenticator> vdator) {
tokenValidator = vdator;
}

Expand Down
6 changes: 3 additions & 3 deletions w3c-visserver-api/src/Authenticator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int Authenticator::validateToken(WsChannel& channel, string authToken) {
json claims;
(void) channel;
for (auto& e : decoded.get_payload_claims()) {
logger->Log(LogLevel::INFO, e.first + " = " + e.second.as_string());
logger->Log(LogLevel::INFO, e.first + " = " + e.second.to_json().to_str());
claims[e.first] = e.second.to_json().to_str();
}

Expand Down Expand Up @@ -78,7 +78,7 @@ Authenticator::Authenticator(std::shared_ptr<ILogger> loggerUtil, string secretk

// validates the token against expiry date/time. should be extended to check
// some other claims.
int Authenticator::validate(WsChannel& channel, VssDatabase* db,
int Authenticator::validate(WsChannel& channel, std::shared_ptr<IVssDatabase> db,
string authToken) {
int ttl = validateToken(channel, authToken);
if (ttl > 0) {
Expand Down Expand Up @@ -107,7 +107,7 @@ bool Authenticator::isStillValid(WsChannel& channel) {
// resolves the permission in the JWT token and store the absolute path to the
// signals in permissions JSON in WsChannel.
void Authenticator::resolvePermissions(WsChannel& channel,
VssDatabase* database) {
std::shared_ptr<IVssDatabase> database) {
string authToken = channel.getAuthToken();
auto decoded = jwt::decode(authToken);
json claims;
Expand Down
Loading

0 comments on commit 33a4c8a

Please sign in to comment.