예제 #1
0
        /// <summary>
        /// Execute the call to callback url with provided payload
        /// </summary>
        /// <returns>true if the call succeeded, false if the call failed</returns>
        private async Task <string> InitiateCallbackAsync(HttpClient client, string callbackUrl, string callbackToken,
                                                          string callbackEncryption, string payload, int requestTimeout,
                                                          CancellationToken stoppingToken)
        {
            var    hostURI    = new Uri(callbackUrl);
            var    stopwatch  = Stopwatch.StartNew();
            string errMessage = null;

            try
            {
                var restClient          = new RestClient(callbackUrl, callbackToken, client);
                var notificationTimeout = new TimeSpan(0, 0, 0, 0, requestTimeout);
                if (string.IsNullOrEmpty(callbackEncryption))
                {
                    _ = await restClient.PostJsonAsync("", payload, requestTimeout : notificationTimeout, token : stoppingToken);
                }
                else
                {
                    _ = await restClient.PostOctetStream("", MapiEncryption.Encrypt(payload, callbackEncryption), requestTimeout : notificationTimeout, token : stoppingToken);
                }

                logger.LogDebug($"Successfully sent notification to '{callbackUrl}', with execution time of '{stopwatch.ElapsedMilliseconds}'ms");
            }
            catch (Exception ex)
            {
                errMessage = ex.GetBaseException().Message;
                logger.LogError($"Callback failed for host {hostURI.Host}. Error: {ex.GetBaseException().Message}");
            }
            finally
            {
                stopwatch.Stop();
                notificationScheduler.AddExecutionTime(hostURI.Host, stopwatch.ElapsedMilliseconds);
            }
            return(errMessage);
        }
        public void TestEncryption()
        {
            var    recipientKeyPair = PublicKeyBox.GenerateKeyPair();
            var    encryptionKey    = MapiEncryption.GetEncryptionKey(recipientKeyPair);
            string s         = "Test message";
            var    encrypted = MapiEncryption.Encrypt(s, encryptionKey);

            var decrypted = MapiEncryption.Decrypt(encrypted, recipientKeyPair);

            Assert.AreEqual(s, decrypted);

            encrypted[5] ^= 1;

            Assert.ThrowsException <CryptographicException>(() => MapiEncryption.Decrypt(encrypted, recipientKeyPair));
        }
        public static IEnumerable <ValidationResult> IsSupportedEncryption(string s, string memberName)
        {
            if (!string.IsNullOrEmpty(s))
            {
                if (!MapiEncryption.IsEncryptionSupported(s))
                {
                    yield return(new ValidationResult($"{memberName} contains unsupported encryption type"));
                }

                // 1024 is DB limit. It should not happen.
                if (s.Length > 1024)
                {
                    yield return(new ValidationResult($"{memberName} contains encryption token that is too long"));
                }
            }
        }