diff --git a/dev/Gems/Websockets/Code/Include/Websockets/WebsocketsBus.h b/dev/Gems/Websockets/Code/Include/Websockets/WebsocketsBus.h index d2825e0a9d..7ab4ed6545 100644 --- a/dev/Gems/Websockets/Code/Include/Websockets/WebsocketsBus.h +++ b/dev/Gems/Websockets/Code/Include/Websockets/WebsocketsBus.h @@ -55,6 +55,9 @@ namespace Websockets //!avoided in production. virtual AZStd::unique_ptr CreateClient(const AZStd::string& websocket, const OnMessage& messageFunc, WebsocketType type) = 0; + + virtual AZStd::unique_ptr CreateClientWithAuth(const AZStd::string& websocket, + const OnMessage& messageFunc, const AZStd::string& authorization) = 0; }; diff --git a/dev/Gems/Websockets/Code/Source/Platform/Common/Default/SecureWebsocketClient_Default.cpp b/dev/Gems/Websockets/Code/Source/Platform/Common/Default/SecureWebsocketClient_Default.cpp index 753dccdd75..938568a0d4 100644 --- a/dev/Gems/Websockets/Code/Source/Platform/Common/Default/SecureWebsocketClient_Default.cpp +++ b/dev/Gems/Websockets/Code/Source/Platform/Common/Default/SecureWebsocketClient_Default.cpp @@ -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; @@ -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 { - websocketpp::lib::shared_ptr contextPtr(new asio::ssl::context(asio::ssl::context::tlsv1)); + websocketpp::lib::shared_ptr contextPtr(new asio::ssl::context(asio::ssl::context::sslv23)); websocketpp::lib::error_code errorCode; contextPtr->set_options(asio::ssl::context::default_workarounds @@ -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); diff --git a/dev/Gems/Websockets/Code/Source/Platform/Common/Default/SecureWebsocketClient_Default.h b/dev/Gems/Websockets/Code/Source/Platform/Common/Default/SecureWebsocketClient_Default.h index aa6b1a4958..ad8013ddb2 100644 --- a/dev/Gems/Websockets/Code/Source/Platform/Common/Default/SecureWebsocketClient_Default.h +++ b/dev/Gems/Websockets/Code/Source/Platform/Common/Default/SecureWebsocketClient_Default.h @@ -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); diff --git a/dev/Gems/Websockets/Code/Source/Platform/Common/Unimplemented/SecureWebsocketClient_Unimplemented.cpp b/dev/Gems/Websockets/Code/Source/Platform/Common/Unimplemented/SecureWebsocketClient_Unimplemented.cpp index 1d6c04e3d7..c9c888761b 100644 --- a/dev/Gems/Websockets/Code/Source/Platform/Common/Unimplemented/SecureWebsocketClient_Unimplemented.cpp +++ b/dev/Gems/Websockets/Code/Source/Platform/Common/Unimplemented/SecureWebsocketClient_Unimplemented.cpp @@ -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; } diff --git a/dev/Gems/Websockets/Code/Source/Platform/Common/Unimplemented/SecureWebsocketClient_Unimplemented.h b/dev/Gems/Websockets/Code/Source/Platform/Common/Unimplemented/SecureWebsocketClient_Unimplemented.h index 6f4ed390a6..5523f9c38b 100644 --- a/dev/Gems/Websockets/Code/Source/Platform/Common/Unimplemented/SecureWebsocketClient_Unimplemented.h +++ b/dev/Gems/Websockets/Code/Source/Platform/Common/Unimplemented/SecureWebsocketClient_Unimplemented.h @@ -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); diff --git a/dev/Gems/Websockets/Code/Source/SecureWebsocketClient.cpp b/dev/Gems/Websockets/Code/Source/SecureWebsocketClient.cpp index 2a454022b4..241ecf1f75 100644 --- a/dev/Gems/Websockets/Code/Source/SecureWebsocketClient.cpp +++ b/dev/Gems/Websockets/Code/Source/SecureWebsocketClient.cpp @@ -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); diff --git a/dev/Gems/Websockets/Code/Source/SecureWebsocketClient.h b/dev/Gems/Websockets/Code/Source/SecureWebsocketClient.h index 9360635072..8334984ae7 100644 --- a/dev/Gems/Websockets/Code/Source/SecureWebsocketClient.h +++ b/dev/Gems/Websockets/Code/Source/SecureWebsocketClient.h @@ -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; diff --git a/dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.cpp b/dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.cpp index 7b538373f8..ae2ceb6e5b 100644 --- a/dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.cpp +++ b/dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.cpp @@ -60,6 +60,25 @@ namespace Websockets WebsocketsRequestBus::Handler::BusDisconnect(); } + AZStd::unique_ptr WebsocketsSystemComponent::CreateClientWithAuth(const AZStd::string& websocket, const OnMessage& messageFunc, const AZStd::string& authorization) + { + AZStd::unique_ptr socket = AZStd::make_unique(); + + 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 WebsocketsSystemComponent::CreateClient(const AZStd::string& websocket, const OnMessage& messageFunc, WebsocketType type) { switch (type) diff --git a/dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.h b/dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.h index b170dee577..fe0fbe2df3 100644 --- a/dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.h +++ b/dev/Gems/Websockets/Code/Source/WebsocketsSystemComponent.h @@ -36,6 +36,9 @@ namespace Websockets void Activate() override; void Deactivate() override; + AZStd::unique_ptr CreateClientWithAuth(const AZStd::string& websocket, + const OnMessage& messageFunc, const AZStd::string& authorization) override; + AZStd::unique_ptr CreateClient(const AZStd::string& websocket, const OnMessage& messageFunc, WebsocketType type = WebsocketType::Secure) override; };