Пример #1
0
        /// <summary>
        /// Is authentication needed.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private static bool NeedAuthentication(Hashtable messageMap, string httpMethod, string contentType, string path)
        {
            if (contentType == null || !contentType.Contains("json"))
            {
                return(false);
            }
            string           method;
            GXRestMethodInfo mi = GXGeneral.GetTypes(messageMap, path.ToLower(), out method);

            if (mi == null)
            {
                return(false);
            }
            if (httpMethod == "POST")
            {
                return(mi.PostAuthentication != null);
            }
            else if (httpMethod == "GET")
            {
                return(mi.GetAuthentication != null);
            }
            else if (httpMethod == "PUT")
            {
                return(mi.PutAuthentication != null);
            }
            else if (httpMethod == "DELETE")
            {
                return(mi.DeleteAuthentication != null);
            }
            throw new Exception("Invalid http method.");
        }
Пример #2
0
        /// <summary>
        /// Get request and response types.
        /// </summary>
        /// <param name="parser"></param>
        /// <param name="data"></param>
        public static GXRestMethodInfo GetTypes(Hashtable messageMap, string data, out string method)
        {
            int pos = data.LastIndexOf('/');

            if (pos == -1)
            {
                method = data;
            }
            else
            {
                method = data.Substring(pos + 1);
            }
            GXRestMethodInfo r = messageMap[method.ToLower()] as GXRestMethodInfo;

            if (r != null)
            {
                return(r);
            }
            return(null);
        }
Пример #3
0
        /// <summary>
        /// Find available Rest messages.
        /// </summary>
        public static void UpdateRestMessageTypes(Hashtable messageMap)
        {
            AuthenticateAttribute[] auths;
            Type tp;

            ParameterInfo[] parameters;
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (GXCommon.IsDefaultAssembly(asm))
                {
                    continue;
                }
                try
                {
                    foreach (Type type in asm.GetExportedTypes())
                    {
                        if (!type.IsAbstract && type.IsClass && typeof(GXRestService).IsAssignableFrom(type))
                        {
                            foreach (MethodInfo method in type.GetMethods())
                            {
                                parameters = method.GetParameters();
                                if (parameters.Length == 1 &&
                                    (method.Name == "Post" || method.Name == "Get" || method.Name == "Put" || method.Name == "Delete"))
                                {
                                    tp = parameters[0].ParameterType;
                                    string           name = tp.Name.ToLower();
                                    RouteAttribute[] ra   = (RouteAttribute[])tp.GetCustomAttributes(typeof(RouteAttribute), true);
                                    if (ra.Length == 1)
                                    {
                                        name = ra[0].Path.ToLower();
                                    }
                                    foreach (var it in tp.GetInterfaces())
                                    {
                                        if (it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IGXRequest <>))
                                        {
                                            GXRestMethodInfo r = messageMap[tp.Name.ToLower()] as GXRestMethodInfo;
                                            if (r == null)
                                            {
                                                r = new GXRestMethodInfo();
                                                r.RestClassType = type;
                                                r.RequestType   = tp;
                                                r.ResponseType  = it.GetGenericArguments()[0];
                                                //If class uses authentication.
                                                auths = (AuthenticateAttribute[])type.GetCustomAttributes(typeof(AuthenticateAttribute), true);
                                                //Check that method is not marked as anonymous.
                                                if (auths.Length == 1)
                                                {
                                                    r.PostAuthentication   = auths[0];
                                                    r.GetAuthentication    = auths[0];
                                                    r.PutAuthentication    = auths[0];
                                                    r.DeleteAuthentication = auths[0];
                                                }
                                                messageMap.Add(name, r);
                                            }
                                            auths = (AuthenticateAttribute[])method.GetCustomAttributes(typeof(AuthenticateAttribute), true);
                                            if (method.Name == "Post")
                                            {
                                                if (r.Post != null)
                                                {
                                                    throw new Exception(string.Format("Message handler for '{0}' already added.", name));
                                                }
                                                r.Post = GXInternal.CreateMethodHandler(tp, method);
                                                //If method uses authentication.
                                                if (auths.Length == 1)
                                                {
                                                    r.PostAuthentication = auths[0];
                                                }
                                                else if (r.PostAuthentication != null)
                                                {
                                                    if (method.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 1)
                                                    {
                                                        r.PostAuthentication = null;
                                                    }
                                                }
                                            }
                                            else if (method.Name == "Get")
                                            {
                                                if (r.Get != null)
                                                {
                                                    throw new Exception(string.Format("Message handler for '{0}' already added.", name));
                                                }
                                                r.Get = GXInternal.CreateMethodHandler(tp, method);
                                                //If method uses authentication.
                                                if (auths.Length == 1)
                                                {
                                                    r.GetAuthentication = auths[0];
                                                }
                                                else if (r.GetAuthentication != null)
                                                {
                                                    if (method.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 1)
                                                    {
                                                        r.GetAuthentication = null;
                                                    }
                                                }
                                            }
                                            else if (method.Name == "Put")
                                            {
                                                if (r.Put != null)
                                                {
                                                    throw new Exception(string.Format("Message handler for '{0}' already added.", name));
                                                }
                                                r.Put = GXInternal.CreateMethodHandler(tp, method);
                                                //If method uses authentication.
                                                if (auths.Length == 1)
                                                {
                                                    r.PutAuthentication = auths[0];
                                                }
                                                else if (r.PutAuthentication != null)
                                                {
                                                    if (method.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 1)
                                                    {
                                                        r.PutAuthentication = null;
                                                    }
                                                }
                                            }
                                            else if (method.Name == "Delete")
                                            {
                                                if (r.Delete != null)
                                                {
                                                    throw new Exception(string.Format("Message handler for '{0}' already added.", name));
                                                }
                                                r.Delete = GXInternal.CreateMethodHandler(tp, method);
                                                //If method uses authentication.
                                                if (auths.Length == 1)
                                                {
                                                    r.DeleteAuthentication = auths[0];
                                                }
                                                else if (r.DeleteAuthentication != null)
                                                {
                                                    if (method.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 1)
                                                    {
                                                        r.DeleteAuthentication = null;
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }
        }