/// <summary>
 /// Attribute used to bind a parameter to an Azure Web PubSub, when an request is from Azure Web PubSub service.
 /// </summary>
 /// <param name="hub">Target hub name of the request.</param>
 /// <param name="eventType">Target event name of the request.</param>
 /// <param name="eventName">Target event type of the request.</param>
 /// <param name="connections">Connection strings of allowed upstreams for signature checks.</param>
 public WebPubSubTriggerAttribute(string hub, WebPubSubEventType eventType, string eventName, params string[] connections)
 {
     Hub         = hub;
     EventName   = eventName;
     EventType   = eventType;
     Connections = connections;
 }
 /// <summary>
 /// The client connection context contains the CloudEvents headers under Web PubSub protocol.
 /// </summary>
 /// <param name="eventType">Event type.</param>
 /// <param name="eventName">Event name.</param>
 /// <param name="hub">Hub name.</param>
 /// <param name="connectionId">Connection Id.</param>
 /// <param name="userId">User Id.</param>
 /// <param name="signature">Signature of the connection.</param>
 /// <param name="origin">Origin of the event.</param>
 /// <param name="connectionStates">Connection states.</param>
 /// <param name="headers">Connection request headers.</param>
 public WebPubSubConnectionContext(WebPubSubEventType eventType, string eventName, string hub, string connectionId, string userId = null, string signature = null, string origin = null, IReadOnlyDictionary <string, BinaryData> connectionStates = null, IReadOnlyDictionary <string, string[]> headers = null)
 {
     EventType        = eventType;
     EventName        = eventName;
     Hub              = hub;
     ConnectionId     = connectionId;
     UserId           = userId;
     Signature        = signature;
     Origin           = origin;
     ConnectionStates = connectionStates ?? new Dictionary <string, BinaryData>();
     States           = new StringifiedDictionary(ConnectionStates);
     Headers          = headers;
 }
 public WebPubSubConnectionContext(WebPubSubEventType eventType, string eventName, string hub, string connectionId, string userId, string signature, string origin, IReadOnlyDictionary <string, object> states, IReadOnlyDictionary <string, string[]> headers)
     : this(
         eventType,
         eventName,
         hub,
         connectionId,
         userId,
         signature,
         origin,
         states?.ToDictionary(
             p => p.Key,
             p => p.Value as BinaryData ?? BinaryData.FromObjectAsJson(p.Value)),
         headers)
 {
 }
Exemplo n.º 4
0
        public static HttpRequestMessage CreateHttpRequestMessage(
            string hub,
            WebPubSubEventType type,
            string eventName,
            string connectionId,
            string[] signatures,
            string contentType = Constants.ContentTypes.PlainTextContentType,
            string httpMethod  = "Post",
            string host        = null,
            string userId      = "testuser",
            byte[] payload     = null)
        {
            var context = new HttpRequestMessage()
            {
                Method = new HttpMethod(httpMethod)
            };

            context.Headers.Add(Constants.Headers.CloudEvents.Hub, hub);
            context.Headers.Add(Constants.Headers.CloudEvents.Type, GetFormedType(type, eventName));
            context.Headers.Add(Constants.Headers.CloudEvents.EventName, eventName);
            context.Headers.Add(Constants.Headers.CloudEvents.ConnectionId, connectionId);
            context.Headers.Add(Constants.Headers.CloudEvents.Signature, string.Join(",", signatures));
            if (host != null)
            {
                context.Headers.Add(Constants.Headers.WebHookRequestOrigin, host);
            }
            if (userId != null)
            {
                context.Headers.Add(Constants.Headers.CloudEvents.UserId, userId);
            }

            if (payload != null)
            {
                context.Content = new StreamContent(new MemoryStream(payload));
                context.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            }

            foreach (var header in context.Headers)
            {
                context.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }

            return(context);
        }
Exemplo n.º 5
0
 private static string GetFormedType(WebPubSubEventType type, string eventName)
 {
     return(type == WebPubSubEventType.User ?
            $"{Constants.Headers.CloudEvents.TypeUserPrefix}{eventName}" :
            $"{Constants.Headers.CloudEvents.TypeSystemPrefix}{eventName}");
 }
        private static HttpContext PrepareHttpContext(
            WebPubSubEventType type = WebPubSubEventType.System,
            string eventName        = "connect",
            string uriStr           = TestEndpoint,
            string connectionId     = "0f9c97a2f0bf4706afe87a14e0797b11",
            string signatures       = "sha256=7767effcb3946f3e1de039df4b986ef02c110b1469d02c0a06f41b3b727ab561",
            string hub         = "testhub",
            string httpMethod  = "POST",
            string userId      = "testuser",
            string body        = null,
            string contentType = Constants.ContentTypes.PlainTextContentType,
            Dictionary <string, BinaryData> connectionState = null)
        {
            var context  = new DefaultHttpContext();
            var services = new ServiceCollection();
            var sp       = services.BuildServiceProvider();

            context.RequestServices = sp;

            var uri            = new Uri(uriStr);
            var request        = context.Request;
            var requestFeature = request.HttpContext.Features.Get <IHttpRequestFeature>();

            requestFeature.Method      = httpMethod;
            requestFeature.Scheme      = uri.Scheme;
            requestFeature.PathBase    = uri.Host;
            requestFeature.Path        = uri.GetComponents(UriComponents.KeepDelimiter | UriComponents.Path, UriFormat.Unescaped);
            requestFeature.PathBase    = "/";
            requestFeature.QueryString = uri.GetComponents(UriComponents.KeepDelimiter | UriComponents.Query, UriFormat.Unescaped);

            var headers = new HeaderDictionary
            {
                { Constants.Headers.CloudEvents.Hub, hub },
                { Constants.Headers.CloudEvents.Type, GetFormedType(type, eventName) },
                { Constants.Headers.CloudEvents.EventName, eventName },
                { Constants.Headers.CloudEvents.ConnectionId, connectionId },
                { Constants.Headers.CloudEvents.Signature, signatures },
                { Constants.Headers.CloudEvents.WebPubSubVersion, "1.0" },
            };

            if (!string.IsNullOrEmpty(uri.Host))
            {
                headers.Add("Host", uri.Host);
                headers.Add(Constants.Headers.WebHookRequestOrigin, uri.Host);
            }

            if (userId != null)
            {
                headers.Add(Constants.Headers.CloudEvents.UserId, userId);
            }

            if (connectionState != null)
            {
                headers.Add(Constants.Headers.CloudEvents.State, connectionState.EncodeConnectionStates());
            }

            if (body != null)
            {
                requestFeature.Body   = new MemoryStream(Encoding.UTF8.GetBytes(body));
                request.ContentLength = request.Body.Length;
                headers.Add("Content-Length", request.Body.Length.ToString());
                request.ContentType = contentType;
                headers.Add("Content-Type", contentType);
            }

            requestFeature.Headers = headers;
            context.Response.Body  = new MemoryStream();

            return(context);
        }
 /// <summary>
 /// Attribute used to bind a parameter to an Azure Web PubSub, when an request is from Azure Web PubSub service.
 /// </summary>
 /// <param name="eventType">Target event name of the request.</param>
 /// <param name="eventName">Target event type of the request.</param>
 public WebPubSubTriggerAttribute(WebPubSubEventType eventType, string eventName)
     : this("", eventType, eventName)
 {
 }
 /// <summary>
 /// Attribute used to bind a parameter to an Azure Web PubSub, when an request is from Azure Web PubSub service.
 /// </summary>
 /// <param name="eventType">Target event name of the request.</param>
 /// <param name="eventName">Target event type of the request.</param>
 /// <param name="connections">Connection strings of allowed upstreams for signature checks.</param>
 public WebPubSubTriggerAttribute(WebPubSubEventType eventType, string eventName, params string[] connections)
     : this("", eventType, eventName, connections)
 {
 }
 /// <summary>
 /// Attribute used to bind a parameter to an Azure Web PubSub, when an request is from Azure Web PubSub service.
 /// </summary>
 /// <param name="hub">Target hub name of the request.</param>
 /// <param name="eventType">Target event name of the request.</param>
 /// <param name="eventName">Target event type of the request.</param>
 public WebPubSubTriggerAttribute(string hub, WebPubSubEventType eventType, string eventName)
     : this(hub, eventType, eventName, null)
 {
 }
 /// <summary>
 /// Used to map to method name automatically
 /// </summary>
 /// <param name="hub"></param>
 /// <param name="eventName"></param>
 /// <param name="eventType"></param>
 public WebPubSubTriggerAttribute(string hub, WebPubSubEventType eventType, string eventName)
 {
     Hub       = hub;
     EventName = eventName;
     EventType = eventType;
 }