/// <summary>
        /// Verify if the request is valid, then returns LINE Webhook events from the request
        /// </summary>
        /// <param name="request">HttpRequestMessage</param>
        /// <param name="channelSecret">ChannelSecret</param>
        /// <param name="botUserId">BotUserId</param>
        /// <returns>List of WebhookEvent</returns>
        public static async Task <IEnumerable <WebhookEvent> > GetWebhookEventsAsync(this HttpRequestMessage request, string channelSecret, string botUserId = null)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (channelSecret == null)
            {
                throw new ArgumentNullException(nameof(channelSecret));
            }

            var content = await request.Content.ReadAsStringAsync();

            var xLineSignature = request.Headers.GetValues("X-Line-Signature").FirstOrDefault();

            if (string.IsNullOrEmpty(xLineSignature) || !VerifySignature(channelSecret, xLineSignature, content))
            {
                throw new InvalidSignatureException("Signature validation faild.");
            }

            dynamic json = JsonConvert.DeserializeObject(content);

            if (!string.IsNullOrEmpty(botUserId))
            {
                if (botUserId != (string)json.destination)
                {
                    throw new UserIdMismatchException("Bot user ID does not match.");
                }
            }
            return(WebhookEventParser.ParseEvents(json.events));
        }
Example #2
0
        /// <summary>
        /// Verify if the request is valid, then returns LINE Webhook events from the request
        /// </summary>
        /// <param name="xLineSignature"></param>
        /// <param name="requestBody"></param>
        /// <param name="channelSecret"></param>
        /// <param name="botUserId"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidSignatureException"></exception>
        /// <exception cref="UserIdMismatchException"></exception>
        public static IEnumerable <WebhookEvent> GetWebhookEventsAsync(string xLineSignature, string requestBody, string channelSecret, string botUserId = null)
        {
            if (string.IsNullOrEmpty(xLineSignature))
            {
                throw new ArgumentNullException(nameof(xLineSignature));
            }
            if (string.IsNullOrEmpty(requestBody))
            {
                throw new ArgumentNullException(nameof(requestBody));
            }
            if (string.IsNullOrEmpty(channelSecret))
            {
                throw new ArgumentNullException(nameof(channelSecret));
            }

            if (!VerifySignature(channelSecret, xLineSignature, requestBody))
            {
                throw new InvalidSignatureException("Signature validation faild.");
            }

            dynamic json = JsonConvert.DeserializeObject(requestBody);

            if (!string.IsNullOrEmpty(botUserId))
            {
                if (botUserId != (string)json.destination)
                {
                    throw new UserIdMismatchException("Bot user ID does not match.");
                }
            }

            return(WebhookEventParser.ParseEvents(json.events));
        }
        /// <summary>
        /// Verify if the request is valid, then returns LINE Webhook events from the request
        /// </summary>
        /// <param name="request">HttpRequestMessage</param>
        /// <param name="channelSecret">ChannelSecret</param>
        /// <returns>List of WebhookEvent</returns>
        public static async Task <IEnumerable <WebhookEvent> > GetWebhookEventsAsync(this HttpRequestMessage request, string channelSecret)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (channelSecret == null)
            {
                throw new ArgumentNullException(nameof(channelSecret));
            }

            var content = await request.Content.ReadAsStringAsync();

            var xLineSignature = request.Headers.GetValues("X-Line-Signature").FirstOrDefault();

            if (string.IsNullOrEmpty(xLineSignature) || !VerifySignature(channelSecret, xLineSignature, content))
            {
                throw new InvalidSignatureException("Signature validation faild.");
            }
            return(WebhookEventParser.Parse(content));
        }