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

Add auth header and upgrade SSL version to sslv23 #541

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dev/Gems/Websockets/Code/Include/Websockets/WebsocketsBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ namespace Websockets
//!avoided in production.
virtual AZStd::unique_ptr<IWebsocketClient> CreateClient(const AZStd::string& websocket,
const OnMessage& messageFunc, WebsocketType type) = 0;

virtual AZStd::unique_ptr<IWebsocketClient> CreateClientWithAuth(const AZStd::string& websocket,
const OnMessage& messageFunc, const AZStd::string& authorization) = 0;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Websockets
}
}

bool SecureWebsocketClient_Default::ConnectWebsocket(const AZStd::string & websocket, const OnMessage & messageFunc)
bool SecureWebsocketClient_Default::ConnectWebsocket(const AZStd::string & websocket, const OnMessage & messageFunc, const AZStd::string& authorization)
{
m_messageFunction = messageFunc;

Expand All @@ -42,7 +42,7 @@ namespace Websockets
//TLS handler (Transport Layer Security, also often referred to as SSL secure socket handler)
m_client.set_tls_init_handler([](websocketpp::connection_hdl hdl) -> websocketpp::lib::shared_ptr<asio::ssl::context>
{
websocketpp::lib::shared_ptr<asio::ssl::context> contextPtr(new asio::ssl::context(asio::ssl::context::tlsv1));
websocketpp::lib::shared_ptr<asio::ssl::context> contextPtr(new asio::ssl::context(asio::ssl::context::sslv23));

websocketpp::lib::error_code errorCode;
contextPtr->set_options(asio::ssl::context::default_workarounds
Expand Down Expand Up @@ -75,6 +75,11 @@ namespace Websockets
return false;
}

if (!authorization.empty())
{
m_connection->append_header("Authorization", authorization.c_str());
}

// Connect only requests a connection. No network messages are
// exchanged until run().
m_client.connect(m_connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Websockets

void Destroy();

bool ConnectWebsocket(const AZStd::string& websocket, const OnMessage& messageFunc);
bool ConnectWebsocket(const AZStd::string& websocket, const OnMessage& messageFunc, const AZStd::string& authorization);

bool IsConnectionOpen() const;
void SendWebsocketMessage(const AZStd::string& msgBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Websockets

}

bool SecureWebsocketClient_Unimplemented::ConnectWebsocket(const AZStd::string & websocket, const OnMessage & messageFunc)
bool SecureWebsocketClient_Unimplemented::ConnectWebsocket(const AZStd::string & websocket, const OnMessage & messageFunc, const AZStd::string& authorization)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Websockets

void Destroy();

bool ConnectWebsocket(const AZStd::string& websocket, const OnMessage& messageFunc);
bool ConnectWebsocket(const AZStd::string& websocket, const OnMessage& messageFunc, const AZStd::string& authorization);

bool IsConnectionOpen() const;
void SendWebsocketMessage(const AZStd::string& msgBody);
Expand Down
5 changes: 5 additions & 0 deletions dev/Gems/Websockets/Code/Source/SecureWebsocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ namespace Websockets
}
}

bool SecureWebsocketClient::ConnectWebsocket(const AZStd::string& websocket, const OnMessage& messageFunc, const AZStd::string& authorization)
{
return Platform::ConnectWebsocket(websocket, messageFunc, authorization);
}

bool SecureWebsocketClient::ConnectWebsocket(const AZStd::string& websocket, const OnMessage& messageFunc)
{
return Platform::ConnectWebsocket(websocket, messageFunc);
Expand Down
2 changes: 1 addition & 1 deletion dev/Gems/Websockets/Code/Source/SecureWebsocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Websockets

static void Reflect(AZ::ReflectContext* context);

bool ConnectWebsocket(const AZStd::string& websocket, const OnMessage& messageFunc);
bool ConnectWebsocket(const AZStd::string& websocket, const OnMessage& messageFunc, const AZStd::string& authorization = "");

//interface functions
bool IsConnectionOpen() const override;
Expand Down
19 changes: 19 additions & 0 deletions dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ namespace Websockets
WebsocketsRequestBus::Handler::BusDisconnect();
}

AZStd::unique_ptr<IWebsocketClient> WebsocketsSystemComponent::CreateClientWithAuth(const AZStd::string& websocket, const OnMessage& messageFunc, const AZStd::string& authorization)
{
AZStd::unique_ptr<SecureWebsocketClient> socket = AZStd::make_unique<SecureWebsocketClient>();

if (!socket)
{
AZ_Error("Websocket Gem", socket, "Socket not created!");
return nullptr;
}

if (!socket->ConnectWebsocket(websocket, messageFunc, authorization))
{
AZ_Error("Websocket Gem", false, "Socket connection unsuccessful");
return nullptr;
}

return socket;
}

AZStd::unique_ptr<IWebsocketClient> WebsocketsSystemComponent::CreateClient(const AZStd::string& websocket, const OnMessage& messageFunc, WebsocketType type)
{
switch (type)
Expand Down
3 changes: 3 additions & 0 deletions dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ namespace Websockets
void Activate() override;
void Deactivate() override;

AZStd::unique_ptr<IWebsocketClient> CreateClientWithAuth(const AZStd::string& websocket,
const OnMessage& messageFunc, const AZStd::string& authorization) override;

AZStd::unique_ptr<IWebsocketClient> CreateClient(const AZStd::string& websocket,
const OnMessage& messageFunc, WebsocketType type = WebsocketType::Secure) override;
};
Expand Down