From 0d7fb49187c241775c74014e047feffe725ccf7c Mon Sep 17 00:00:00 2001 From: leancloud-bot Date: Wed, 26 Oct 2022 16:01:21 +0800 Subject: [PATCH] fix: signature check --- .../Realtime.Test/LocalSignatureFactory.cs | 31 +++++++------- Realtime/Realtime.Test/Signature.cs | 6 +-- .../Controller/LCIMConversationController.cs | 40 ++++++++++++------- .../Controller/LCIMSessionController.cs | 2 +- .../Public/Signature/LCIMSignature.cs | 5 +++ Storage/Storage.Test/FileTest.cs | 9 +++-- 6 files changed, 56 insertions(+), 37 deletions(-) diff --git a/Realtime/Realtime.Test/LocalSignatureFactory.cs b/Realtime/Realtime.Test/LocalSignatureFactory.cs index ab0fd2b2..116149be 100644 --- a/Realtime/Realtime.Test/LocalSignatureFactory.cs +++ b/Realtime/Realtime.Test/LocalSignatureFactory.cs @@ -9,7 +9,7 @@ namespace Realtime.Test { public class LocalSignatureFactory : ILCIMSignatureFactory { - const string MasterKey = "VFbnm4iaAdqQPzAdXT2M8KjP"; + const string MasterKey = "8Z0R6fd5eohDAHlgtiRgpkAu"; public Task CreateConnectSignature(string clientId) { long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); @@ -57,20 +57,21 @@ public Task CreateConversationSignature(string conversationId, st } public Task CreateBlacklistSignature(string conversationId, string clientId, IEnumerable memberIds, string action) { - string sortedMemberIds = string.Empty; - if (memberIds != null) { - List sortedMemberList = memberIds.ToList(); - sortedMemberList.Sort(); - sortedMemberIds = string.Join(":", sortedMemberList); - } - long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); - string nonce = NewNonce(); - string signature = GenerateSignature(LCCore.AppId, clientId, conversationId, sortedMemberIds, timestamp.ToString(), nonce, action); - return Task.FromResult(new LCIMSignature { - Signature = signature, - Timestamp = timestamp, - Nonce = nonce - }); + //string sortedMemberIds = string.Empty; + //if (memberIds != null) { + // List sortedMemberList = memberIds.ToList(); + // sortedMemberList.Sort(); + // sortedMemberIds = string.Join(":", sortedMemberList); + //} + //long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); + //string nonce = NewNonce(); + //string signature = GenerateSignature(LCCore.AppId, clientId, conversationId, sortedMemberIds, timestamp.ToString(), nonce, action); + //return Task.FromResult(new LCIMSignature { + // Signature = signature, + // Timestamp = timestamp, + // Nonce = nonce + //}); + return Task.FromResult(null); } private static string SignSHA1(string key, string text) { diff --git a/Realtime/Realtime.Test/Signature.cs b/Realtime/Realtime.Test/Signature.cs index 9620764c..957eb8f2 100644 --- a/Realtime/Realtime.Test/Signature.cs +++ b/Realtime/Realtime.Test/Signature.cs @@ -8,9 +8,9 @@ namespace Realtime.Test { public class Signature { - internal const string AppId = "3zWMOXuO9iSdnjXM942i6DdI-gzGzoHsz"; - internal const string AppKey = "bkwiNq4Tj417eUaHlTWS5sPm"; - internal const string AppServer = "https://3zwmoxuo.lc-cn-n1-shared.com"; + internal const string AppId = "7oDgNicekFVXBMkRKcLpvX5w-gzGzoHsz"; + internal const string AppKey = "tPT17REZjS3DfjTJodw6fJzj"; + internal const string AppServer = "https://7odgnice.lc-cn-n1-shared.com"; private LCIMClient hello; private LCIMClient world; diff --git a/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs b/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs index 5e35e9cd..1e1d272c 100644 --- a/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs +++ b/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs @@ -52,9 +52,11 @@ internal async Task CreateConv( }; if (Client.SignatureFactory != null) { LCIMSignature signature = await Client.SignatureFactory.CreateStartConversationSignature(Client.Id, members); - conv.S = signature.Signature; - conv.T = signature.Timestamp; - conv.N = signature.Nonce; + if (signature != null && signature.IsValid) { + conv.S = signature.Signature; + conv.T = signature.Timestamp; + conv.N = signature.Nonce; + } } request.ConvMessage = conv; GenericCommand response = await Connection.SendRequest(request); @@ -138,9 +140,11 @@ internal async Task AddMembers(string convId, Client.Id, clientIds, LCIMSignatureAction.Invite); - conv.S = signature.Signature; - conv.T = signature.Timestamp; - conv.N = signature.Nonce; + if (signature != null && signature.IsValid) { + conv.S = signature.Signature; + conv.T = signature.Timestamp; + conv.N = signature.Nonce; + } } GenericCommand request = NewCommand(CommandType.Conv, OpType.Add); request.ConvMessage = conv; @@ -163,9 +167,11 @@ internal async Task RemoveMembers(string convId, Client.Id, removeIds, LCIMSignatureAction.Kick); - conv.S = signature.Signature; - conv.T = signature.Timestamp; - conv.N = signature.Nonce; + if (signature != null && signature.IsValid) { + conv.S = signature.Signature; + conv.T = signature.Timestamp; + conv.N = signature.Nonce; + } } GenericCommand request = NewCommand(CommandType.Conv, OpType.Remove); request.ConvMessage = conv; @@ -234,9 +240,11 @@ internal async Task BlockMembers(string convId, Client.Id, clientIds, LCIMSignatureAction.ConversationBlockClients); - blacklist.S = signature.Signature; - blacklist.T = signature.Timestamp; - blacklist.N = signature.Nonce; + if (signature != null && signature.IsValid) { + blacklist.S = signature.Signature; + blacklist.T = signature.Timestamp; + blacklist.N = signature.Nonce; + } } GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Block); request.BlacklistMessage = blacklist; @@ -256,9 +264,11 @@ internal async Task UnblockMembers(string convId, Client.Id, clientIds, LCIMSignatureAction.ConversationUnblockClients); - blacklist.S = signature.Signature; - blacklist.T = signature.Timestamp; - blacklist.N = signature.Nonce; + if (signature != null && signature.IsValid) { + blacklist.S = signature.Signature; + blacklist.T = signature.Timestamp; + blacklist.N = signature.Nonce; + } } GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Unblock); request.BlacklistMessage = blacklist; diff --git a/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs b/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs index fd2ff4c6..c5e83eba 100644 --- a/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs +++ b/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs @@ -94,7 +94,7 @@ private async Task NewSessionCommand() { Nonce = ret["nonce"] as string }; } - if (signature != null) { + if (signature != null && signature.IsValid) { session.S = signature.Signature; session.T = signature.Timestamp; session.N = signature.Nonce; diff --git a/Realtime/Realtime/Public/Signature/LCIMSignature.cs b/Realtime/Realtime/Public/Signature/LCIMSignature.cs index 1207f52d..0ad63ebc 100644 --- a/Realtime/Realtime/Public/Signature/LCIMSignature.cs +++ b/Realtime/Realtime/Public/Signature/LCIMSignature.cs @@ -17,5 +17,10 @@ public long Timestamp { public string Nonce { get; set; } + + public bool IsValid => + !string.IsNullOrEmpty(Signature) && + !string.IsNullOrEmpty(Nonce) && + Timestamp > 0; } } diff --git a/Storage/Storage.Test/FileTest.cs b/Storage/Storage.Test/FileTest.cs index be09e681..ed7e6689 100644 --- a/Storage/Storage.Test/FileTest.cs +++ b/Storage/Storage.Test/FileTest.cs @@ -14,6 +14,10 @@ public class FileTest : BaseTest { static readonly string APKFilePath = "../../../../../assets/test.apk"; static readonly string VideoFilePath = "../../../../../assets/video.mp4"; + static readonly string US_APP_ID = "ldCRr8t23k3ydo7FxmJlKQmn-MdYXbMMI"; + static readonly string US_APP_KEY = "GwQDHkmsQTSF2ZXWegzXio5F"; + static readonly string US_SERVER = "https://ldcrr8t2.api.lncldglobal.com"; + private LCFile video; [Test] @@ -109,7 +113,7 @@ public async Task FileACL() { [Test] [Order(10)] public async Task AWS() { - LCApplication.Initialize("HudJvWWmAuGMifwxByDVLmQi-MdYXbMMI", "YjoQr1X8wHoFIfsSGXzeJaAM"); + LCApplication.Initialize(US_APP_ID, US_APP_KEY); LCFile file = new LCFile("avatar.png", AvatarFilePath); await file.Save((count, total) => { TestContext.WriteLine($"progress: {count}/{total}"); @@ -121,8 +125,7 @@ await file.Save((count, total) => { [Test] [Order(11)] public async Task AWSBigFile() { - LCApplication.Initialize("HudJvWWmAuGMifwxByDVLmQi-MdYXbMMI", "YjoQr1X8wHoFIfsSGXzeJaAM", - "https://hudjvwwm.api.lncldglobal.com"); + LCApplication.Initialize(US_APP_ID, US_APP_KEY, US_SERVER); LCFile file = new LCFile("video.mp4", VideoFilePath); await file.Save((count, total) => { TestContext.WriteLine($"progress: {count}/{total}");