From 5a7d24e2e7f3efc4f8ac4a25b9a76d9b6881e684 Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Wed, 17 Nov 2021 11:39:10 +0530 Subject: [PATCH 1/9] hmacsha256 refactor --- src/Api/PubnubApi/PubnubApi.csproj | 3 ++ .../PubnubApi/Security/PubnubCryptoBase.cs | 30 +------------------ src/Api/PubnubApiPCL/PubnubApiPCL.csproj | 1 + src/Api/PubnubApiUWP/PubnubApiUWP.csproj | 1 + 4 files changed, 6 insertions(+), 29 deletions(-) diff --git a/src/Api/PubnubApi/PubnubApi.csproj b/src/Api/PubnubApi/PubnubApi.csproj index 9c0eddfaa..1794c7b6a 100644 --- a/src/Api/PubnubApi/PubnubApi.csproj +++ b/src/Api/PubnubApi/PubnubApi.csproj @@ -115,6 +115,9 @@ Removed throw exception on atleast one PAM permission validation None + + 4.3.0 + diff --git a/src/Api/PubnubApi/Security/PubnubCryptoBase.cs b/src/Api/PubnubApi/Security/PubnubCryptoBase.cs index 7dcadeca1..fd2c15772 100644 --- a/src/Api/PubnubApi/Security/PubnubCryptoBase.cs +++ b/src/Api/PubnubApi/Security/PubnubCryptoBase.cs @@ -6,9 +6,7 @@ #if NET35 using System.Security.Cryptography; #else -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Parameters; -using Org.BouncyCastle.Security; +using System.Security.Cryptography; #endif namespace PubnubApi @@ -163,24 +161,11 @@ public string PubnubAccessManagerSign(string key, string data) byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); -#if NET35 using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return Convert.ToBase64String(hashmessage).Replace('+', '-').Replace('/', '_'); } -#else - - //http://mycsharp.de/wbb2/thread.php?postid=3550104 - KeyParameter paramKey = new KeyParameter(keyByte); - IMac mac = MacUtilities.GetMac("HMac-SHA256"); - mac.Init(paramKey); - mac.Reset(); - mac.BlockUpdate(messageBytes, 0, messageBytes.Length); - byte[] hashmessage = new byte[mac.GetMacSize()]; - mac.DoFinal(hashmessage, 0); - return Convert.ToBase64String(hashmessage).Replace('+', '-').Replace('/', '_'); -#endif } public static byte[] PubnubAccessManagerSign(string key, byte[] dataBytes) @@ -191,24 +176,11 @@ public static byte[] PubnubAccessManagerSign(string key, byte[] dataBytes) byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = dataBytes; -#if NET35 using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return hashmessage; } -#else - - //http://mycsharp.de/wbb2/thread.php?postid=3550104 - KeyParameter paramKey = new KeyParameter(keyByte); - IMac mac = MacUtilities.GetMac("HMac-SHA256"); - mac.Init(paramKey); - mac.Reset(); - mac.BlockUpdate(messageBytes, 0, messageBytes.Length); - byte[] hashmessage = new byte[mac.GetMacSize()]; - mac.DoFinal(hashmessage, 0); - return hashmessage; -#endif } public string GetHashRaw(string input) diff --git a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj index f23fb686d..804ba8d62 100644 --- a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj +++ b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj @@ -582,6 +582,7 @@ Removed throw exception on atleast one PAM permission validationNone + diff --git a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj index f83603d10..69ca4a4f0 100644 --- a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj +++ b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj @@ -671,6 +671,7 @@ Removed throw exception on atleast one PAM permission validation 1.8.1.2 + From c275dc1b0bef1a96eb12c4fa5335dee2d48946ae Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Wed, 17 Nov 2021 11:54:27 +0530 Subject: [PATCH 2/9] removed conditional directive --- src/Api/PubnubApi/Security/PubnubCryptoBase.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Api/PubnubApi/Security/PubnubCryptoBase.cs b/src/Api/PubnubApi/Security/PubnubCryptoBase.cs index fd2c15772..dc1d33201 100644 --- a/src/Api/PubnubApi/Security/PubnubCryptoBase.cs +++ b/src/Api/PubnubApi/Security/PubnubCryptoBase.cs @@ -2,12 +2,7 @@ using System.Globalization; using System.Text; using System.Text.RegularExpressions; - -#if NET35 -using System.Security.Cryptography; -#else using System.Security.Cryptography; -#endif namespace PubnubApi { From 74ac9d9c48cf9da845123298f1a477f9fb16bd39 Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Thu, 18 Nov 2021 12:38:17 +0530 Subject: [PATCH 3/9] Removed Portable.BouncyCastle & code refactored for native .net library --- src/Api/PubnubApi/PubnubApi.csproj | 9 ---- src/Api/PubnubApi/Security/PubnubCrypto.cs | 54 ++----------------- src/Api/PubnubApiPCL/PubnubApiPCL.csproj | 3 -- src/Api/PubnubApiUWP/PubnubApiUWP.csproj | 3 -- .../MockServerPubnubApiPCL.csproj | 6 --- 5 files changed, 3 insertions(+), 72 deletions(-) diff --git a/src/Api/PubnubApi/PubnubApi.csproj b/src/Api/PubnubApi/PubnubApi.csproj index bbe6adabb..458c08e8e 100644 --- a/src/Api/PubnubApi/PubnubApi.csproj +++ b/src/Api/PubnubApi/PubnubApi.csproj @@ -85,9 +85,6 @@ - - None - None @@ -99,9 +96,6 @@ - - None - @@ -110,9 +104,6 @@ - - None - 4.3.0 diff --git a/src/Api/PubnubApi/Security/PubnubCrypto.cs b/src/Api/PubnubApi/Security/PubnubCrypto.cs index f05117ea4..30803d616 100644 --- a/src/Api/PubnubApi/Security/PubnubCrypto.cs +++ b/src/Api/PubnubApi/Security/PubnubCrypto.cs @@ -2,17 +2,7 @@ using System.Text; using System.Globalization; -#if !NETSTANDARD10 && !NETSTANDARD11 using System.Security.Cryptography; -#endif - -#if !NET35 -using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Crypto.Engines; -using Org.BouncyCastle.Crypto.Modes; -using Org.BouncyCastle.Crypto.Paddings; -using Org.BouncyCastle.Crypto.Parameters; -#endif namespace PubnubApi { @@ -37,19 +27,10 @@ public PubnubCrypto(string cipher_key) protected override string ComputeHashRaw(string input) { -#if NET35 HashAlgorithm algorithm = SHA256.Create(); Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input); Byte[] hashedBytes = algorithm.ComputeHash(inputBytes); return BitConverter.ToString(hashedBytes); -#else - Sha256Digest algorithm = new Sha256Digest(); - Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input); - Byte[] bufferBytes = new byte[algorithm.GetDigestSize()]; - algorithm.BlockUpdate(inputBytes, 0, inputBytes.Length); - algorithm.DoFinal(bufferBytes, 0); - return BitConverter.ToString(bufferBytes); -#endif } protected override string EncryptOrDecrypt(bool type, string dataStr, bool dynamicIV) @@ -137,7 +118,7 @@ protected override byte[] EncryptOrDecrypt(bool type, byte[] dataBytes, bool dyn { LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} IV = {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), GetDisplayableBytes(ivBytes)), config.LogVerbosity); } -#if NET35 + Aes aesAlg = Aes.Create(); aesAlg.KeySize = 256; aesAlg.BlockSize = 128; @@ -145,34 +126,15 @@ protected override byte[] EncryptOrDecrypt(bool type, byte[] dataBytes, bool dyn aesAlg.Padding = PaddingMode.PKCS7; aesAlg.IV = ivBytes; aesAlg.Key = System.Text.Encoding.UTF8.GetBytes(keyString); -#else - byte[] iv = ivBytes; - byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(keyString); - - //Set up - AesEngine engine = new AesEngine(); - CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC - PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); //Default scheme is PKCS5/PKCS7 - KeyParameter keyParam = new KeyParameter(keyBytes); - ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, iv.Length); -#endif - if (type) { // Encrypt byte[] outputBytes = null; -#if NET35 ICryptoTransform crypto = aesAlg.CreateEncryptor(); byte[] plainBytes = dataBytes; outputBytes = crypto.TransformFinalBlock(plainBytes, 0, plainBytes.Length); -#else - byte[] inputBytes = dataBytes; - cipher.Init(true, keyParamWithIV); - outputBytes = new byte[cipher.GetOutputSize(inputBytes.Length)]; - int length = cipher.ProcessBytes(inputBytes, outputBytes, 0); - cipher.DoFinal(outputBytes, length); //Do the final block -#endif + byte[] newOutputBytes = null; if (dynamicIV) { @@ -193,22 +155,12 @@ protected override byte[] EncryptOrDecrypt(bool type, byte[] dataBytes, bool dyn try { //Decrypt -#if NET35 byte[] decryptedBytes = dataBytes; ICryptoTransform decrypto = aesAlg.CreateDecryptor(); var data = decrypto.TransformFinalBlock(decryptedBytes, 0, decryptedBytes.Length); newOutputBytes = data; -#else - byte[] inputBytes = dataBytes; - cipher.Init(false, keyParamWithIV); - byte[] encryptedBytes = new byte[cipher.GetOutputSize(inputBytes.Length)]; - var encryptLength = cipher.ProcessBytes(inputBytes, encryptedBytes, 0); - var lastBytesLength = cipher.DoFinal(encryptedBytes, encryptLength); //Do the final block - var totalBytesLength = encryptLength + lastBytesLength; - newOutputBytes = new byte[totalBytesLength]; - Array.Copy(encryptedBytes, newOutputBytes, totalBytesLength); -#endif + return newOutputBytes; } catch (Exception ex) diff --git a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj index 5142628cc..b6f4f9b89 100644 --- a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj +++ b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj @@ -596,9 +596,6 @@ - - None - None diff --git a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj index de614a86f..87224b4d7 100644 --- a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj +++ b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj @@ -673,9 +673,6 @@ 9.0.1 - - 1.8.1.2 - diff --git a/src/UnitTests/MockServerPubnubApiPCL/MockServerPubnubApiPCL.csproj b/src/UnitTests/MockServerPubnubApiPCL/MockServerPubnubApiPCL.csproj index 3118e0f63..f58740da6 100644 --- a/src/UnitTests/MockServerPubnubApiPCL/MockServerPubnubApiPCL.csproj +++ b/src/UnitTests/MockServerPubnubApiPCL/MockServerPubnubApiPCL.csproj @@ -42,12 +42,6 @@ - - - None - - - 4.0.0 From 1e7f8a0582f8ba55d559da7620c2848f69014dd3 Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Thu, 18 Nov 2021 12:39:05 +0530 Subject: [PATCH 4/9] minor update to tests --- .../PubnubApi.Tests/EncryptionTests.cs | 16 ++++++++++++---- .../PubnubApi.Tests/PubnubApi.Tests.csproj | 1 + .../PubnubApi.Tests/WhenFileIsRequested.cs | 16 ++++++++-------- .../PubnubApi.Tests/fileupload_enc.txt | Bin 0 -> 64 bytes .../PubnubApiPCL.Tests.csproj | 1 + .../PubnubApiPCL.Tests/fileupload_enc.txt | Bin 0 -> 64 bytes 6 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 src/UnitTests/PubnubApi.Tests/fileupload_enc.txt create mode 100644 src/UnitTests/PubnubApiPCL.Tests/fileupload_enc.txt diff --git a/src/UnitTests/PubnubApi.Tests/EncryptionTests.cs b/src/UnitTests/PubnubApi.Tests/EncryptionTests.cs index f5d96dbe6..b812d6659 100644 --- a/src/UnitTests/PubnubApi.Tests/EncryptionTests.cs +++ b/src/UnitTests/PubnubApi.Tests/EncryptionTests.cs @@ -308,8 +308,12 @@ public void TestLocalFileEncryptionFromPath() { IPubnubUnitTest pubnubUnitTest = new PubnubUnitTest(); pubnubUnitTest.IV = new byte[16] { 133, 126, 158, 123, 43, 95, 96, 90, 215, 178, 17, 73, 166, 130, 79, 156 }; - string sourceFile = @"C:\Pandu\temp\new\input\file_upload_original.txt"; - string destFile = @"C:\Pandu\temp\new\input\file_upload_original_encrypted.txt"; + string sourceFile = "fileupload.txt"; + string destFile = "fileupload_encrypted.txt"; + if (System.IO.File.Exists(destFile)) + { + System.IO.File.Delete(destFile); + } PNConfiguration config = new PNConfiguration(); Pubnub pn = new Pubnub(config); pn.EncryptFile(sourceFile, destFile, "enigma"); @@ -320,8 +324,12 @@ public void TestLocalFileEncryptionFromPath() [Test] public void TestLocalFileDecryptionFromPath() { - string sourceFile = @"C:\Pandu\temp\new\input\file_image_enc.jpg"; - string destFile = @"C:\Pandu\temp\new\input\file_image_enc_decrypted_to_original.jpg"; + string sourceFile = "fileupload_enc.txt"; + string destFile = "fileupload_enc_decrypted_to_original.txt"; + if (System.IO.File.Exists(destFile)) + { + System.IO.File.Delete(destFile); + } PNConfiguration config = new PNConfiguration(); Pubnub pn = new Pubnub(config); pn.DecryptFile(sourceFile, destFile, "enigma"); diff --git a/src/UnitTests/PubnubApi.Tests/PubnubApi.Tests.csproj b/src/UnitTests/PubnubApi.Tests/PubnubApi.Tests.csproj index ede288552..e668d9912 100644 --- a/src/UnitTests/PubnubApi.Tests/PubnubApi.Tests.csproj +++ b/src/UnitTests/PubnubApi.Tests/PubnubApi.Tests.csproj @@ -84,5 +84,6 @@ + diff --git a/src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs index 8683cd66a..8cf4fb9dd 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs @@ -69,7 +69,7 @@ public static void Init() Thread.Sleep(1000); - grantManualEvent.WaitOne(); + grantManualEvent.WaitOne(2000); pubnub.Destroy(); pubnub.PubnubUnitTest = null; @@ -152,7 +152,7 @@ public static void ThenSendFileShouldReturnSuccess() mre.Set(); })); Thread.Sleep(1000); - mre.WaitOne(); + mre.WaitOne(3 * 1000 * 60); if (receivedMessage) { @@ -169,7 +169,7 @@ public static void ThenSendFileShouldReturnSuccess() mre.Set(); })); Thread.Sleep(1000); - mre.WaitOne(); + mre.WaitOne(2 * 1000 * 60); } if (receivedMessage) @@ -186,7 +186,7 @@ public static void ThenSendFileShouldReturnSuccess() } mre.Set(); })); - mre.WaitOne(); + mre.WaitOne(2 * 1000 * 60); } if (receivedMessage) @@ -204,7 +204,7 @@ public static void ThenSendFileShouldReturnSuccess() mre.Set(); })); Thread.Sleep(1000); - mre.WaitOne(); + mre.WaitOne(2 * 1000 * 60); } @@ -298,7 +298,7 @@ public static async Task ThenWithAsyncSendFileShouldReturnSuccess() mre.Set(); })); Thread.Sleep(1000); - mre.WaitOne(); + mre.WaitOne(2 * 1000 * 60); if (receivedMessage) @@ -314,7 +314,7 @@ public static async Task ThenWithAsyncSendFileShouldReturnSuccess() } mre.Set(); })); - mre.WaitOne(); + mre.WaitOne(2 * 1000 * 60); } if (receivedMessage) @@ -332,7 +332,7 @@ public static async Task ThenWithAsyncSendFileShouldReturnSuccess() mre.Set(); })); Thread.Sleep(1000); - mre.WaitOne(); + mre.WaitOne(2 * 1000 * 60); } diff --git a/src/UnitTests/PubnubApi.Tests/fileupload_enc.txt b/src/UnitTests/PubnubApi.Tests/fileupload_enc.txt new file mode 100644 index 0000000000000000000000000000000000000000..0dade66382dd4e24684aadfe3d1950b0bc12d592 GIT binary patch literal 64 zcmV-G0Kfm-i(&$b*jB;IXv3g3tuFO^G9)kh@UmtJd literal 0 HcmV?d00001 diff --git a/src/UnitTests/PubnubApiPCL.Tests/PubnubApiPCL.Tests.csproj b/src/UnitTests/PubnubApiPCL.Tests/PubnubApiPCL.Tests.csproj index 6c4cab259..d763a794a 100644 --- a/src/UnitTests/PubnubApiPCL.Tests/PubnubApiPCL.Tests.csproj +++ b/src/UnitTests/PubnubApiPCL.Tests/PubnubApiPCL.Tests.csproj @@ -107,5 +107,6 @@ + diff --git a/src/UnitTests/PubnubApiPCL.Tests/fileupload_enc.txt b/src/UnitTests/PubnubApiPCL.Tests/fileupload_enc.txt new file mode 100644 index 0000000000000000000000000000000000000000..0dade66382dd4e24684aadfe3d1950b0bc12d592 GIT binary patch literal 64 zcmV-G0Kfm-i(&$b*jB;IXv3g3tuFO^G9)kh@UmtJd literal 0 HcmV?d00001 From 00cb7da4b4bdd20a76bf0ea5423ebd3a597e81cb Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Thu, 18 Nov 2021 15:07:36 +0530 Subject: [PATCH 5/9] minor update to tests --- src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs index 8cf4fb9dd..b03fef58b 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenFileIsRequested.cs @@ -174,6 +174,7 @@ public static void ThenSendFileShouldReturnSuccess() if (receivedMessage) { + System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072; //Need this line for .net 3.5/4.0/4.5 receivedMessage = false; mre = new ManualResetEvent(false); pubnub.DownloadFile().Channel(channelName).FileId(fileId).FileName(fileName).Execute(new PNDownloadFileResultExt((result, status) => @@ -303,6 +304,7 @@ public static async Task ThenWithAsyncSendFileShouldReturnSuccess() if (receivedMessage) { + System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072; //Need this line for .net 3.5/4.0/4.5 receivedMessage = false; mre = new ManualResetEvent(false); pubnub.DownloadFile().Channel(channelName).FileId(fileId).FileName(fileName).Execute(new PNDownloadFileResultExt((result, status) => From 9cd483a99ce71ec6e2410a18bdcec15c56c2f1ff Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Thu, 18 Nov 2021 15:12:01 +0530 Subject: [PATCH 6/9] feature file update --- src/UnitTests/AcceptanceTests/Features/revoke-token.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UnitTests/AcceptanceTests/Features/revoke-token.feature b/src/UnitTests/AcceptanceTests/Features/revoke-token.feature index e7713a3dc..f83efad2d 100644 --- a/src/UnitTests/AcceptanceTests/Features/revoke-token.feature +++ b/src/UnitTests/AcceptanceTests/Features/revoke-token.feature @@ -30,7 +30,7 @@ Feature: Revoke an access token Given a token When I revoke a token Then an error is returned - * the error status code is 403 + * the error status code is 400 * the error message is 'Feature disabled' * the error source is 'revoke' * the error detail message is 'Token revocation is disabled.' From 52bda2f2b15fb19c603a372576693a6d579d6c86 Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Tue, 7 Dec 2021 16:57:05 +0530 Subject: [PATCH 7/9] try catch for publish --- .../EndPoint/PubSub/PublishOperation.cs | 184 ++++++++++-------- 1 file changed, 103 insertions(+), 81 deletions(-) diff --git a/src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs b/src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs index f587b65a3..2d647e7b2 100644 --- a/src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs +++ b/src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs @@ -205,67 +205,80 @@ internal void Publish(string channel, object message, bool storeInHistory, int t return; } - string requestMethodName = (this.httpPost) ? "POST" : "GET"; - IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null && !string.IsNullOrEmpty(PubnubInstance.InstanceId) && PubnubTokenMgrCollection.ContainsKey(PubnubInstance.InstanceId)) ? PubnubTokenMgrCollection[PubnubInstance.InstanceId] : null, (PubnubInstance != null) ? PubnubInstance.InstanceId : ""); - - Uri request = urlBuilder.BuildPublishRequest(requestMethodName, "", channel, message, storeInHistory, ttl, metaData, null, externalQueryParam); - RequestState requestState = new RequestState(); - requestState.Channels = new [] { channel }; - requestState.ResponseType = PNOperationType.PNPublishOperation; - requestState.PubnubCallback = callback; - requestState.Reconnect = false; - requestState.EndPointOperation = this; + try + { + string requestMethodName = (this.httpPost) ? "POST" : "GET"; + IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null && !string.IsNullOrEmpty(PubnubInstance.InstanceId) && PubnubTokenMgrCollection.ContainsKey(PubnubInstance.InstanceId)) ? PubnubTokenMgrCollection[PubnubInstance.InstanceId] : null, (PubnubInstance != null) ? PubnubInstance.InstanceId : ""); - string json = ""; + Uri request = urlBuilder.BuildPublishRequest(requestMethodName, "", channel, message, storeInHistory, ttl, metaData, null, externalQueryParam); - if (this.httpPost) - { - requestState.UsePostMethod = true; - string postMessage = JsonEncodePublishMsg(message); - byte[] postData = Encoding.UTF8.GetBytes(postMessage); - UrlProcessRequest(request, requestState, false, postData).ContinueWith(r => + requestState.Channels = new[] { channel }; + requestState.ResponseType = PNOperationType.PNPublishOperation; + requestState.PubnubCallback = callback; + requestState.Reconnect = false; + requestState.EndPointOperation = this; + + string json = ""; + + if (this.httpPost) { - json = r.Result.Item1; - }, TaskContinuationOptions.ExecuteSynchronously).Wait(); - } - else - { - UrlProcessRequest(request, requestState, false).ContinueWith(r => + requestState.UsePostMethod = true; + string postMessage = JsonEncodePublishMsg(message); + byte[] postData = Encoding.UTF8.GetBytes(postMessage); + UrlProcessRequest(request, requestState, false, postData).ContinueWith(r => + { + json = r.Result.Item1; + }, TaskContinuationOptions.ExecuteSynchronously).Wait(); + } + else { - json = r.Result.Item1; - }, TaskContinuationOptions.ExecuteSynchronously).Wait(); - } - - if (!string.IsNullOrEmpty(json)) - { - List result = ProcessJsonResponse(requestState, json); + UrlProcessRequest(request, requestState, false).ContinueWith(r => + { + json = r.Result.Item1; + }, TaskContinuationOptions.ExecuteSynchronously).Wait(); + } - if (result != null && result.Count >= 3) + if (!string.IsNullOrEmpty(json)) { - int publishStatus; - Int32.TryParse(result[0].ToString(), out publishStatus); - if (publishStatus == 1) + List result = ProcessJsonResponse(requestState, json); + + if (result != null && result.Count >= 3) { - ProcessResponseCallbacks(result, requestState); + int publishStatus; + Int32.TryParse(result[0].ToString(), out publishStatus); + if (publishStatus == 1) + { + ProcessResponseCallbacks(result, requestState); + } + else + { + PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, result[1].ToString()); + PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNPublishOperation, category, requestState, 400, new PNException(json)); + if (requestState.PubnubCallback != null) + { + requestState.PubnubCallback.OnResponse(default(PNPublishResult), status); + } + } } else { - PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, result[1].ToString()); - PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNPublishOperation, category, requestState, 400, new PNException(json)); - if (requestState.PubnubCallback != null) - { - requestState.PubnubCallback.OnResponse(default(PNPublishResult), status); - } + ProcessResponseCallbacks(result, requestState); } } - else + + CleanUp(); + } + catch (Exception ex) + { + PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, ex.ToString()); + PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNPublishOperation, category, requestState, 400, new PNException(ex.ToString())); + if (requestState.PubnubCallback != null) { - ProcessResponseCallbacks(result, requestState); + requestState.PubnubCallback.OnResponse(default(PNPublishResult), status); } - } - CleanUp(); + } } internal async Task> Publish(string channel, object message, bool storeInHistory, int ttl, Dictionary metaData, Dictionary externalQueryParam) @@ -290,56 +303,65 @@ internal async Task> Publish(string channel, object me return ret; } - string requestMethodName = (this.httpPost) ? "POST" : "GET"; - IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null && !string.IsNullOrEmpty(PubnubInstance.InstanceId) && PubnubTokenMgrCollection.ContainsKey(PubnubInstance.InstanceId)) ? PubnubTokenMgrCollection[PubnubInstance.InstanceId] : null, (PubnubInstance != null) ? PubnubInstance.InstanceId : ""); - - Uri request = urlBuilder.BuildPublishRequest(requestMethodName, "", channel, message, storeInHistory, ttl, metaData, null, externalQueryParam); - RequestState requestState = new RequestState(); - requestState.Channels = new[] { channel }; - requestState.ResponseType = PNOperationType.PNPublishOperation; - requestState.Reconnect = false; - requestState.EndPointOperation = this; + try + { + string requestMethodName = (this.httpPost) ? "POST" : "GET"; + IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null && !string.IsNullOrEmpty(PubnubInstance.InstanceId) && PubnubTokenMgrCollection.ContainsKey(PubnubInstance.InstanceId)) ? PubnubTokenMgrCollection[PubnubInstance.InstanceId] : null, (PubnubInstance != null) ? PubnubInstance.InstanceId : ""); - Tuple JsonAndStatusTuple; + Uri request = urlBuilder.BuildPublishRequest(requestMethodName, "", channel, message, storeInHistory, ttl, metaData, null, externalQueryParam); - if (this.httpPost) - { - requestState.UsePostMethod = true; - string postMessage = JsonEncodePublishMsg(message); - byte[] postData = Encoding.UTF8.GetBytes(postMessage); - JsonAndStatusTuple = await UrlProcessRequest(request, requestState, false, postData).ConfigureAwait(false); - } - else - { - JsonAndStatusTuple = await UrlProcessRequest(request, requestState, false).ConfigureAwait(false); - } - ret.Status = JsonAndStatusTuple.Item2; - string json = JsonAndStatusTuple.Item1; + requestState.Channels = new[] { channel }; + requestState.ResponseType = PNOperationType.PNPublishOperation; + requestState.Reconnect = false; + requestState.EndPointOperation = this; - if (!string.IsNullOrEmpty(json)) - { - List result = ProcessJsonResponse(requestState, json); + Tuple JsonAndStatusTuple; + + if (this.httpPost) + { + requestState.UsePostMethod = true; + string postMessage = JsonEncodePublishMsg(message); + byte[] postData = Encoding.UTF8.GetBytes(postMessage); + JsonAndStatusTuple = await UrlProcessRequest(request, requestState, false, postData).ConfigureAwait(false); + } + else + { + JsonAndStatusTuple = await UrlProcessRequest(request, requestState, false).ConfigureAwait(false); + } + ret.Status = JsonAndStatusTuple.Item2; + string json = JsonAndStatusTuple.Item1; - if (result != null && result.Count >= 3) + if (!string.IsNullOrEmpty(json)) { - int publishStatus; - Int32.TryParse(result[0].ToString(), out publishStatus); - if (publishStatus == 1) + List result = ProcessJsonResponse(requestState, json); + + if (result != null && result.Count >= 3) { - List resultList = ProcessJsonResponse(requestState, json); - if (resultList != null && resultList.Count > 0) + int publishStatus; + Int32.TryParse(result[0].ToString(), out publishStatus); + if (publishStatus == 1) { - ResponseBuilder responseBuilder = new ResponseBuilder(config, jsonLibrary, pubnubLog); - PNPublishResult responseResult = responseBuilder.JsonToObject(resultList, true); - if (responseResult != null) + List resultList = ProcessJsonResponse(requestState, json); + if (resultList != null && resultList.Count > 0) { - ret.Result = responseResult; + ResponseBuilder responseBuilder = new ResponseBuilder(config, jsonLibrary, pubnubLog); + PNPublishResult responseResult = responseBuilder.JsonToObject(resultList, true); + if (responseResult != null) + { + ret.Result = responseResult; + } } } } } } + catch (Exception ex) + { + PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, ex.ToString()); + PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNPublishOperation, category, requestState, 400, new PNException(ex.ToString())); + ret.Status = status; + } return ret; } From 04ad6c5020f9467cf8bf26764584d7bfd5282860 Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Thu, 16 Dec 2021 12:03:13 +0530 Subject: [PATCH 8/9] refactor on status code --- src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs b/src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs index 2d647e7b2..4dbf7fd7d 100644 --- a/src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs +++ b/src/Api/PubnubApi/EndPoint/PubSub/PublishOperation.cs @@ -271,8 +271,9 @@ internal void Publish(string channel, object message, bool storeInHistory, int t } catch (Exception ex) { - PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, ex.ToString()); - PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNPublishOperation, category, requestState, 400, new PNException(ex.ToString())); + int statusCode = PNStatusCodeHelper.GetHttpStatusCode(ex.ToString()); + PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(statusCode, ex.ToString()); + PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNPublishOperation, category, requestState, statusCode, new PNException(ex.ToString())); if (requestState.PubnubCallback != null) { requestState.PubnubCallback.OnResponse(default(PNPublishResult), status); @@ -358,8 +359,9 @@ internal async Task> Publish(string channel, object me } catch (Exception ex) { - PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(400, ex.ToString()); - PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNPublishOperation, category, requestState, 400, new PNException(ex.ToString())); + int statusCode = PNStatusCodeHelper.GetHttpStatusCode(ex.ToString()); + PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(statusCode, ex.ToString()); + PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNPublishOperation, category, requestState, statusCode, new PNException(ex.ToString())); ret.Status = status; } From 9cb37bfd8568f342d36785f287b54709c75372f3 Mon Sep 17 00:00:00 2001 From: Client Engineering Bot <60980775+Client Engineering Bot@users.noreply.github.com> Date: Thu, 16 Dec 2021 17:41:50 +0000 Subject: [PATCH 9/9] PubNub SDK v5.4.0.0 release. --- .pubnub.yml | 21 +++++++++++++------- CHANGELOG | 5 +++++ src/Api/PubnubApi/Properties/AssemblyInfo.cs | 4 ++-- src/Api/PubnubApi/PubnubApi.csproj | 5 +++-- src/Api/PubnubApiPCL/PubnubApiPCL.csproj | 5 +++-- src/Api/PubnubApiUWP/PubnubApiUWP.csproj | 5 +++-- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/.pubnub.yml b/.pubnub.yml index 1c9054573..37ae351a4 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,8 +1,15 @@ name: c-sharp -version: "5.3.0" +version: "5.4.0" schema: 1 scm: github.com/pubnub/c-sharp changelog: + - date: 2021-12-16 + version: v5.4.0 + changes: + - type: bug + text: "Replaced BouncyCastle lib with System.Security.Cryptography.Algorithms." + - type: bug + text: "Added try/catch for publish operation to catch exceptions." - date: 2021-11-16 version: v5.3.0 changes: @@ -614,7 +621,7 @@ features: - QUERY-PARAM supported-platforms: - - version: Pubnub 'C#' 5.3.0 + version: Pubnub 'C#' 5.4.0 platforms: - Windows 10 and up - Windows Server 2008 and up @@ -624,7 +631,7 @@ supported-platforms: - .Net Framework 4.5 - .Net Framework 4.6.1+ - - version: PubnubPCL 'C#' 5.3.0 + version: PubnubPCL 'C#' 5.4.0 platforms: - Xamarin.Android - Xamarin.iOS @@ -643,7 +650,7 @@ supported-platforms: - .Net Standard 2.1 - .Net Core - - version: PubnubUWP 'C#' 5.3.0 + version: PubnubUWP 'C#' 5.4.0 platforms: - Windows Phone 10 - Universal Windows Apps @@ -667,7 +674,7 @@ sdks: distribution-type: source distribution-repository: Github package-name: Pubnub - location: https://github.com/pubnub/c-sharp/releases/tag/v5.3.0.0 + location: https://github.com/pubnub/c-sharp/releases/tag/v5.4.0.0 requires: - name: ".Net" @@ -964,7 +971,7 @@ sdks: distribution-type: source distribution-repository: GitHub package-name: PubNubPCL - location: https://github.com/pubnub/c-sharp/releases/tag/v5.3.0.0 + location: https://github.com/pubnub/c-sharp/releases/tag/v5.4.0.0 requires: - name: ".Net Core" @@ -1337,7 +1344,7 @@ sdks: distribution-type: source distribution-repository: Github package-name: PubnubUWP - location: https://github.com/pubnub/c-sharp/releases/tag/v5.3.0.0 + location: https://github.com/pubnub/c-sharp/releases/tag/v5.4.0.0 requires: - name: "Universal Windows Platform Development" diff --git a/CHANGELOG b/CHANGELOG index e19d3c758..515cd9265 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +v5.4.0 - December 16 2021 +----------------------------- +- Fixed: replaced BouncyCastle lib with System.Security.Cryptography.Algorithms. +- Fixed: added try/catch for publish operation to catch exceptions. + v5.3.0 - November 16 2021 ----------------------------- - Added: added RevokeToken feature. diff --git a/src/Api/PubnubApi/Properties/AssemblyInfo.cs b/src/Api/PubnubApi/Properties/AssemblyInfo.cs index db587f8b3..5d637fa29 100644 --- a/src/Api/PubnubApi/Properties/AssemblyInfo.cs +++ b/src/Api/PubnubApi/Properties/AssemblyInfo.cs @@ -11,8 +11,8 @@ [assembly: AssemblyProduct("Pubnub C# SDK")] [assembly: AssemblyCopyright("Copyright © 2021")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("5.3.0.0")] -[assembly: AssemblyFileVersion("5.3.0.0")] +[assembly: AssemblyVersion("5.4.0.0")] +[assembly: AssemblyFileVersion("5.4.0.0")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. diff --git a/src/Api/PubnubApi/PubnubApi.csproj b/src/Api/PubnubApi/PubnubApi.csproj index 458c08e8e..026660ed3 100644 --- a/src/Api/PubnubApi/PubnubApi.csproj +++ b/src/Api/PubnubApi/PubnubApi.csproj @@ -13,7 +13,7 @@ Pubnub - 5.3.0.0 + 5.4.0.0 PubNub C# .NET - Web Data Push API Pandu Masabathula PubNub @@ -21,7 +21,8 @@ http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png true https://github.com/pubnub/c-sharp/ - Added RevokeToken feature. + Replaced BouncyCastle lib with System.Security.Cryptography.Algorithms. +Added try/catch for publish operation to catch exceptions. Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously diff --git a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj index b6f4f9b89..36cd7b363 100644 --- a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj +++ b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj @@ -14,7 +14,7 @@ PubnubPCL - 5.3.0.0 + 5.4.0.0 PubNub C# .NET - Web Data Push API Pandu Masabathula PubNub @@ -22,7 +22,8 @@ http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png true https://github.com/pubnub/c-sharp/ - Added RevokeToken feature. + Replaced BouncyCastle lib with System.Security.Cryptography.Algorithms. +Added try/catch for publish operation to catch exceptions. Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously diff --git a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj index 87224b4d7..92e7f0daf 100644 --- a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj +++ b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj @@ -15,7 +15,7 @@ PubnubUWP - 5.3.0.0 + 5.4.0.0 PubNub C# .NET - Web Data Push API Pandu Masabathula PubNub @@ -23,7 +23,8 @@ http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png true https://github.com/pubnub/c-sharp/ - Added RevokeToken feature. + Replaced BouncyCastle lib with System.Security.Cryptography.Algorithms. +Added try/catch for publish operation to catch exceptions. Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously