/// <summary>
        /// Returns the event data for this <paramref name="notificationId"/> from the authenticated source so that we
        /// know that it is valid.
        /// </summary>
        /// <param name="request">The incoming <see cref="HttpRequest">.</see></param>
        /// <param name="routeData">The <see cref="RouteData"/> for the <paramref name="request"/>.</param>
        /// <param name="notificationId">The notification identifier from the <paramref name="request"/> body.</param>
        /// <returns>
        /// A <see cref="Task{JObject}"/> that on completion provides a <see cref="JObject"/> containing event data for
        /// this <paramref name="notificationId"/>.
        /// </returns>
        protected virtual async Task <JObject> GetEventDataAsync(
            HttpRequest request,
            RouteData routeData,
            string notificationId)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (routeData == null)
            {
                throw new ArgumentNullException(nameof(routeData));
            }
            if (notificationId == null)
            {
                throw new ArgumentNullException(nameof(notificationId));
            }

            // Create HTTP request for requesting authoritative event data from Stripe
            var secretKey = await GetReceiverConfig(
                request, routeData,
                ReceiverName,
                StripeConstants.SecretKeyMinLength,
                StripeConstants.SecretKeyMaxLength);

            var address = string.Format(
                CultureInfo.InvariantCulture,
                StripeConstants.EventUriTemplate,
                notificationId);
            var outgoing  = new HttpRequestMessage(HttpMethod.Get, address);
            var challenge = Encoding.UTF8.GetBytes(secretKey + ":");

            outgoing.Headers.Authorization = new AuthenticationHeaderValue(
                "Basic",
                EncodingUtilities.ToBase64(challenge, uriSafe: false));

            using (var response = await _httpClient.SendAsync(outgoing))
            {
                if (!response.IsSuccessStatusCode)
                {
                    Logger.LogError(
                        3,
                        "The notification identifier '{NotificationId}' could not be resolved for an actual event. " +
                        "Callback failed with status code {StatusCode}",
                        notificationId,
                        response.StatusCode);
                    return(null);
                }

                var responseStream = await response.Content.ReadAsStreamAsync();

                var result = await JObject.LoadAsync(new JsonTextReader(new StreamReader(responseStream)));

                return(result);
            }
        }
Exemplo n.º 2
0
        private static void Base64RoundTrip(string input, bool uriSafe)
        {
            byte[] data    = Encoding.UTF8.GetBytes(input);
            string encoded = EncodingUtilities.ToBase64(data, uriSafe);

            byte[] output = EncodingUtilities.FromBase64(encoded);
            string actual = Encoding.UTF8.GetString(output);

            Assert.Equal(input, actual);
        }