public ChannelMethodContext(HttpListenerRequest request, HttpListenerResponse response, ChannelHttpMethod method, List <object> parameters, bool isAuthenticated)
 {
     Request         = request;
     Response        = response;
     HttpMethod      = method;
     IsAuthenticated = isAuthenticated;
     Parameters      = parameters;
 }
        public void WrongHttpMethod(HttpListenerResponse response, ChannelHttpMethod HttpMethod)
        {
            IChannelMessage msg = new ChannelMessage()
            {
                Message = $"Wrong HTTP Method used. In order to call this endpoint u need to send {HttpMethod.ToString()} request"
            };

            response.StatusCode = (int)HttpStatusCode.BadRequest;
            LogChannel.Write(LogSeverity.Error, "Wrong HTTP Method used");
            string outputString = JsonConvert.SerializeObject(msg, Formatting.Indented);

            using (StreamWriter writer = new StreamWriter(response.OutputStream))
            {
                writer.WriteLine(outputString);
            }
        }
示例#3
0
        public List <ChannelDocument> GetDocumentation(AppDomain domain)
        {
            Debug.Assert(_services.Get <IChannelLocator>() != null);

            List <Type> channels = _services.Get <IChannelLocator>().RegisteredChannels(domain);

            foreach (var channel in channels)
            {
                ChannelDocument  channelDoc         = new ChannelDocument();
                ChannelAttribute channelAttr        = channel.GetCustomAttribute <ChannelAttribute>();
                string           channelDescription = channelAttr.Description == null ? string.Empty : channelAttr.Description.ToString();
                string           channelName        = String.IsNullOrEmpty(channelAttr.Name) ? channel.Name : channelAttr.Name;
                string           channelRoute       = $"~/channels/{channelName}";
                MethodInfo[]     methods            = channel.GetMethods().Where(x => x.GetCustomAttribute <ChannelMethodAttribute>() != null).ToArray();
                ChannelMethodDocs = new List <ChannelMethodDocument>();
                foreach (var method in methods)
                {
                    ChannelMethodDocument  methodDocument         = new ChannelMethodDocument();
                    ChannelMethodInfo      description            = _services.Get <IChannelMethodDescriptor>().GetMethodDescription(method);
                    ChannelMethodAttribute ChannelMethodAttribute = method.GetCustomAttribute <ChannelMethodAttribute>();
                    ChannelHttpMethod      HttpMethod             = ChannelMethodAttribute.HttpMethod;
                    string[] names = description.Parameters.Select(x => x.Name).ToArray();
                    Type[]   types = description.Parameters.Select(x => x.Type).ToArray();
                    methodDocument.HttpMethod          = HttpMethod;
                    methodDocument.InputParameters     = names;
                    methodDocument.InputParameterTypes = types;
                    methodDocument.ReturnTypeName      = method.ReturnType.Name;
                    methodDocument.ReturnType          = method.ReturnType;
                    methodDocument.URL         = $"{channelRoute}/{method.Name}/";
                    methodDocument.AuthSchema  = ChannelMethodAttribute.Schema;
                    methodDocument.Description = ChannelMethodAttribute.Description; //null possibility

                    ChannelMethodDocs.Add(methodDocument);
                }
                channelDoc.Name               = channel.Name;
                channelDoc.Description        = channelDescription; //null possibility
                channelDoc.URL                = channelRoute;
                channelDoc.AvailableEndpoints = ChannelMethodDocs;

                ChannelDocs.Add(channelDoc);
            }


            return(ChannelDocs);
        }
 /// <summary>
 /// Method to be used and Auth Type
 /// </summary>
 /// <param name="Schemes">Specified Auth Type for ChannelMethod</param>
 /// <param name="HttpMethod">Http Method</param>
 /// <param name="Description">Description to be used in documentation tool</param>
 public ChannelMethodAttribute(ChannelAuthenticationSchemes Schemes, ChannelHttpMethod HttpMethod, string Description = null)
 {
     Schema           = Schemes;
     this.HttpMethod  = HttpMethod;
     this.Description = Description;
 }
 /// <summary>
 /// Http Method to be used
 /// </summary>
 /// <param name="HttpMethod">Http Method</param>
 /// <param name="Description">Description to be used in documentation tool</param>
 public ChannelMethodAttribute(ChannelHttpMethod HttpMethod, string Description = null)
 {
     this.HttpMethod = HttpMethod;
     Schema          = ChannelAuthenticationSchemes.Anonymous;
 }
示例#6
0
        public ChannelConfigurationInfo Configure(HttpListener httpChannel, Type channel, MethodInfo method, string baseURL)
        {
            ChannelEndpoint  endpoint    = new ChannelEndpoint();
            ChannelAttribute channelAttr = channel.GetCustomAttribute <ChannelAttribute>();

            if (!String.IsNullOrEmpty(channelAttr.Name))
            {
                endpoint.URL = "/channels/" + channelAttr.Name + "/" + method.Name + "/";
            }
            else
            {
                endpoint.URL = "/channels/" + channel.Name + "/" + method.Name + "/";
            }

            endpoint.Name = channel.Name + "." + method.Name;

            ChannelMethodAttribute       channelMethod = method.GetCustomAttribute <ChannelMethodAttribute>();
            AuthorizeChannelAttribute    authAttr      = channel.GetCustomAttribute <AuthorizeChannelAttribute>();
            ChannelAuthenticationSchemes channelSchema = channelMethod.Schema;
            ChannelHttpMethod            httpMethod    = channelMethod.HttpMethod;

            string methodURL = string.Empty;

            if (baseURL == null)
            {
                methodURL = "http://localhost:4200" + endpoint.URL;
            }
            else
            {
                methodURL = baseURL + endpoint.URL;
            }

            bool httpAuthRequired = false;

            //Start hosting
            httpChannel.Prefixes.Add(methodURL);
            if (authAttr != null)
            {
                if (authAttr.Schema != ChannelAuthenticationSchemes.Anonymous)
                {
                    httpAuthRequired = true;
                }
            }
            //ChannelMethod can override ChannelAttribute Authentication Schemes
            if (channelSchema != ChannelAuthenticationSchemes.Anonymous)
            {
                httpAuthRequired = true;
            }
            else
            {
                if (authAttr != null)
                {
                    channelSchema = authAttr.Schema;
                }
            }

            return(new ChannelConfigurationInfo
            {
                ChannelAttribute = channelAttr,
                MethodAttribute = channelMethod,
                MethodUrl = methodURL,
                HttpMethod = httpMethod,
                AuthScheme = channelSchema,
                AuthorizeAttribute = authAttr,
                AuthenticationRequired = httpAuthRequired,
                Endpoint = endpoint
            });
        }
        private void InitChannelMethodContext(IChannelEndpoint endpoint, HttpListenerRequest request, HttpListenerResponse response, bool isAuthenticated, ChannelHttpMethod method, List <object> channelRequestBody)
        {
            ChannelMethodContext methodContext = new ChannelMethodContext(request, response, method, channelRequestBody, isAuthenticated);

            _contextProvider.SetChannelMethodContext(endpoint, methodContext);
        }