// parse request message
        public bool ParseRequest(Stream request, out AmazonSnsMessage message)
        {
            string requestMessage = null;

            try
            {
                using (StreamReader reader = new StreamReader(request))
                {
                    requestMessage = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, null);
            }

            if (requestMessage == null)
            {
                message = null;
                return(false);
            }
            else
            {
                return(ParseRequest(requestMessage, out message));
            }
        }
Exemple #2
0
        //unsubsribe
        public bool Unsubscribe(AmazonSnsMessage amazonSnsMessage)
        {
            string unsubscribeURL = amazonSnsMessage.UnsubscribeURL;

            Uri  confirmUri;
            bool uriIsValid = Uri.TryCreate(unsubscribeURL, UriKind.Absolute, out confirmUri);

            if (!uriIsValid)
            {
                _logger.LogError($"SNS unsubsribe url is not valid: {unsubscribeURL}");
                return(false);
            }

            string response;

            try
            {
                using (WebClient webClient = new WebClient())
                {
                    response = webClient.DownloadString(confirmUri);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, null);
                return(false);
            }

            _logger.LogDebug($"SNS unsubscribe response received: {response}");

            return(CheckUnsubscribeResponse(response));
        }
Exemple #3
0
        //init
        public static bool TryCreate(string jsonMessage, out AmazonSnsMessage amazonSnsMessage)
        {
            amazonSnsMessage = null;
            bool result = false;

            if (string.IsNullOrEmpty(jsonMessage))
            {
                return(false);
            }

            try
            {
                using (TextReader reader = new StringReader(jsonMessage))
                    using (var jsonRreader = new Newtonsoft.Json.JsonTextReader(reader))
                    {
                        var serializer = new Newtonsoft.Json.JsonSerializer();
                        amazonSnsMessage = serializer.Deserialize <AmazonSnsMessage>(jsonRreader);
                    }

                result = true;
            }
            catch (Exception)
            {
            }

            return(result);
        }
        public bool ParseRequest(string request, out AmazonSnsMessage message)
        {
            message = null;
            bool isValid = false;

            // parse json
            AmazonSnsMessage amazonSnsMessage;
            bool             created = AmazonSnsMessage.TryCreate(request, out amazonSnsMessage);

            if (!created)
            {
                _logger.LogError($"SNS message was not successfuly parsed: {request}");
                return(false);
            }

            // verify signature
            bool verified = _signatureVerification.VerifySignature(amazonSnsMessage);

            if (!verified)
            {
                _logger.LogError($"SNS Signature verification failed: {request}");
                return(false);
            }


            // handle depending on type
            if (amazonSnsMessage.AmazonSnsMessageType == AmazonSnsMessageType.Notification)
            {
                message = amazonSnsMessage;
                isValid = message.Message != null;
            }
            // subscribe
            else if (amazonSnsMessage.AmazonSnsMessageType == AmazonSnsMessageType.SubscriptionConfirmation &&
                     ConfirmSubsription)
            {
                isValid = _subscription.ConfirmSubscription(amazonSnsMessage);
            }
            // unknown type
            else
            {
                _logger.LogError($"SNS Unknown message type received: {request}");
                isValid = false;
            }

            return(isValid);
        }
Exemple #5
0
        //проверить
        public bool VerifySignature(AmazonSnsMessage amazonSnsMessage)
        {
            Uri signingCertUri = null;

            // verified message properties
            if (amazonSnsMessage.SignatureVersion != AmazonConstansts.SNS_SUPPORTED_SIGNATURE_VERSION)
            {
                _logger.LogError($"Unknown SNS message type {amazonSnsMessage.SignatureVersion}, when {AmazonConstansts.SNS_SUPPORTED_SIGNATURE_VERSION} wat expected.");
                return(false);
            }

            bool validUri = Uri.TryCreate(amazonSnsMessage.SigningCertURL, UriKind.Absolute, out signingCertUri);

            if (!validUri)
            {
                _logger.LogError($"Unknown SNS certificate address {amazonSnsMessage.SigningCertURL}.");
                return(false);
            }

            if (signingCertUri.Host.EndsWith(AmazonConstansts.SNS_SIGNING_CERTIFICATE_URL_END) == false)
            {
                _logger.LogError($"Invalid SNS certificate address {amazonSnsMessage.SigningCertURL}, when expecting {AmazonConstansts.SNS_SIGNING_CERTIFICATE_URL_END}");
                return(false);
            }

            if (string.IsNullOrEmpty(amazonSnsMessage.Signature))
            {
                _logger.LogError("SNS message signature is not present.");
                return(false);
            }


            // construct message to hash and compare
            string generatedMessage = amazonSnsMessage.GenerateContentString();

            // download certificate
            byte[] pemFileBytes = DownloadCertificate(signingCertUri);

            // verify
            bool verified = CompareSignature(generatedMessage, amazonSnsMessage.Signature, pemFileBytes);

            return(verified);
        }
Exemple #6
0
        //subscribe
        public bool ConfirmSubscription(AmazonSnsMessage amazonSnsMessage)
        {
            string subscribeURL = amazonSnsMessage.SubscribeURL;

            Uri  confirmUri;
            bool uriIsValid = Uri.TryCreate(subscribeURL, UriKind.Absolute, out confirmUri);

            if (!uriIsValid)
            {
                _logger.LogError($"SNS subscription confirmation url is not valid {subscribeURL}");
                return(false);
            }

            string response;

            try
            {
                using (WebClient webClient = new WebClient())
                {
                    response = webClient.DownloadString(confirmUri);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, null);
                return(false);
            }

            _logger.LogDebug($"SNS subscription confirmation response resived: {response}");

            bool confirmed = CheckSubscribeResponse(response);

            _logger.LogDebug($"SNS subscription confirmation finished with result: {confirmed}");

            return(confirmed);
        }