server: broadcast to authenticated only + use references in handler

This commit is contained in:
Stéphane L 2019-01-01 01:18:23 +01:00
parent 5b0410a207
commit c245c24752
4 changed files with 19 additions and 9 deletions

View File

@ -128,7 +128,7 @@ QSet<QString> WSRequestHandler::authNotRequired {
"Authenticate"
};
WSRequestHandler::WSRequestHandler(QVariantHash* connProperties) :
WSRequestHandler::WSRequestHandler(QVariantHash& connProperties) :
_messageId(0),
_requestType(""),
data(nullptr),
@ -163,7 +163,7 @@ std::string WSRequestHandler::processIncomingMessage(std::string& textMessage) {
if (Config::Current()->AuthRequired
&& (!authNotRequired.contains(_requestType))
&& (_connProperties->value(PROP_AUTHENTICATED).toBool() == false))
&& (_connProperties.value(PROP_AUTHENTICATED).toBool() == false))
{
SendErrorResponse("Not Authenticated");
return _response;

View File

@ -33,7 +33,7 @@ class WSRequestHandler : public QObject {
Q_OBJECT
public:
explicit WSRequestHandler(QVariantHash* connProperties);
explicit WSRequestHandler(QVariantHash& connProperties);
~WSRequestHandler();
std::string processIncomingMessage(std::string& textMessage);
bool hasField(QString name);
@ -42,7 +42,7 @@ class WSRequestHandler : public QObject {
const char* _messageId;
const char* _requestType;
std::string _response;
QVariantHash* _connProperties;
QVariantHash& _connProperties;
OBSDataAutoRelease data;
void SendOKResponse(obs_data_t* additionalFields = NULL);

View File

@ -85,7 +85,7 @@ void WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
return;
}
if (req->_connProperties->value(PROP_AUTHENTICATED).toBool() == true) {
if (req->_connProperties.value(PROP_AUTHENTICATED).toBool() == true) {
req->SendErrorResponse("already authenticated");
return;
}
@ -101,7 +101,7 @@ void WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
return;
}
req->_connProperties->insert(PROP_AUTHENTICATED, true);
req->_connProperties.insert(PROP_AUTHENTICATED, true);
req->SendOKResponse();
}

View File

@ -96,6 +96,12 @@ void WSServer::broadcast(QString message)
{
QMutexLocker locker(&_clMutex);
for (connection_hdl hdl : _connections) {
if (Config::Current()->AuthRequired) {
bool authenticated = _connectionProperties[hdl].value(PROP_AUTHENTICATED).toBool();
if (!authenticated) {
continue;
}
}
_server.send(hdl, message.toStdString(), websocketpp::frame::opcode::text);
}
}
@ -118,16 +124,20 @@ void WSServer::onMessage(connection_hdl hdl, server::message_ptr message)
return;
}
QMutexLocker locker(&_clMutex);
QVariantHash connProperties = _connectionProperties[hdl];
locker.unlock();
std::string payload = message->get_payload();
WSRequestHandler handler(&connProperties);
WSRequestHandler handler(connProperties);
std::string response = handler.processIncomingMessage(payload);
_connectionProperties[hdl] = connProperties;
_server.send(hdl, response, websocketpp::frame::opcode::text);
locker.relock();
_connectionProperties[hdl] = connProperties;
locker.unlock();
}
void WSServer::onClose(connection_hdl hdl)