Пример #1
0
        public void Can_match_lowercase_http_method()
        {
            var restPath          = new RestPath(typeof(ComplexType), "/Complex/{Id}/{Name}/Unique/{UniqueId}", "PUT");
            var withPathInfoParts = RestPath.GetPathPartsForMatching("/complex/5/Is Alive/unique/4583B364-BBDC-427F-A289-C2923DEBD547");

            Assert.That(restPath.IsMatch("put", withPathInfoParts));
        }
Пример #2
0
        private static void AssertNoMatch(string definitionPath, string requestPath)
        {
            var restPath        = new RestPath(typeof(SlugRequest), definitionPath);
            var requestTestPath = RestPath.GetPathPartsForMatching(requestPath);

            Assert.That(restPath.IsMatch("GET", requestTestPath), Is.False);
        }
Пример #3
0
        public void Does_handle_ignored_routes()
        {
            var restPath       = new RestPath(typeof(IgnoreRoute3), "/ignore/{ignore}/with/{name}");
            var pathComponents = RestPath.GetPathPartsForMatching("/ignore/AnyThing/with/foo");
            var score          = restPath.MatchScore("GET", pathComponents);

            Assert.That(score, Is.GreaterThan(0));

            var request = (IgnoreRoute3)restPath.CreateRequest("/ignore/AnyThing/with/foo");

            Assert.That(request.Name, Is.EqualTo("foo"));
        }
Пример #4
0
            public static List <SlugRoute> GetOrderedMatchingRules(string withVerb, string forPath)
            {
                var matchingRoutes = new List <SlugRoute>();

                foreach (var definition in Definitions)
                {
                    var pathComponents = RestPath.GetPathPartsForMatching(forPath);
                    definition.Score = definition.RestPath.MatchScore(withVerb, pathComponents);
                    if (definition.Score > 0)
                    {
                        matchingRoutes.Add(definition);
                    }
                }

                var orderedRoutes = matchingRoutes.OrderByDescending(x => x.Score).ToList();

                return(orderedRoutes);
            }
Пример #5
0
        private static void AssertMatch(string definitionPath, string requestPath, string firstMatchHashKey,
                                        SlugRequest expectedRequest, int expectedScore)
        {
            var restPath        = new RestPath(typeof(SlugRequest), definitionPath);
            var requestTestPath = RestPath.GetPathPartsForMatching(requestPath);

            Assert.That(restPath.IsMatch("GET", requestTestPath), Is.True);

            Assert.That(firstMatchHashKey, Is.EqualTo(restPath.FirstMatchHashKey));

            var actualRequest = restPath.CreateRequest(requestPath) as SlugRequest;

            Assert.That(actualRequest, Is.Not.Null);
            Assert.That(actualRequest.Slug, Is.EqualTo(expectedRequest.Slug));
            Assert.That(actualRequest.Version, Is.EqualTo(expectedRequest.Version));
            Assert.That(actualRequest.Options, Is.EqualTo(expectedRequest.Options));
            Assert.That(restPath.MatchScore("GET", requestTestPath), Is.EqualTo(expectedScore));
        }
Пример #6
0
        private static void AssertMatch(string definitionPath, string requestPath,
                                        string firstMatchHashKey, BbcMusicRequest expectedRequest)
        {
            var restPath = new RestPath(typeof(BbcMusicRequest), definitionPath);

            var reqestTestPath = RestPath.GetPathPartsForMatching(requestPath);

            Assert.That(restPath.IsMatch("GET", reqestTestPath), Is.True);

            Assert.That(firstMatchHashKey, Is.EqualTo(restPath.FirstMatchHashKey));

            var actualRequest = restPath.CreateRequest(requestPath) as BbcMusicRequest;

            Assert.That(actualRequest, Is.Not.Null);
            Assert.That(actualRequest.mbz_guid, Is.EqualTo(expectedRequest.mbz_guid));
            Assert.That(actualRequest.release_type, Is.EqualTo(expectedRequest.release_type));
            Assert.That(actualRequest.content_type, Is.EqualTo(expectedRequest.content_type));
        }
Пример #7
0
        private static void AssertMatch(string definitionPath, string requestPath,
                                        string firstMatchHashKey, RackSpaceRequest expectedRequest)
        {
            var restPath = new RestPath(typeof(RackSpaceRequest), definitionPath);

            var reqestTestPath = RestPath.GetPathPartsForMatching(requestPath);

            Assert.That(restPath.IsMatch("GET", reqestTestPath), Is.True);

            Assert.That(firstMatchHashKey, Is.EqualTo(restPath.FirstMatchHashKey));

            var actualRequest = restPath.CreateRequest(requestPath) as RackSpaceRequest;

            Assert.That(actualRequest, Is.Not.Null);
            Assert.That(actualRequest.version, Is.EqualTo(expectedRequest.version));
            Assert.That(actualRequest.id, Is.EqualTo(expectedRequest.id));
            Assert.That(actualRequest.resource_type, Is.EqualTo(expectedRequest.resource_type));
            Assert.That(actualRequest.action, Is.EqualTo(expectedRequest.action));
        }
        /// <summary>
        /// Is user allowed to use service.
        /// </summary>
        /// <param name="authService"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
        {
            GXAmiUser user;
            OrmLiteConnectionFactory f = authService.TryResolve <IDbConnectionFactory>() as OrmLiteConnectionFactory;

            //Connection factory is null when we are configure server at first time.
            if (f == null || f.ConnectionString == null)
            {
                return(true);
            }
            try
            {
                using (IDbConnection Db = f.OpenDbConnection())
                {
                    lock (Db)
                    {
                        if (!GuruxAMI.Service.GXManagementService.IsDatabaseCreated(Db))
                        {
#if !SS4
                            string[] items = RestPath.GetPathPartsForMatching(authService.RequestContext.PathInfo);
#else
                            string[] items = RestPath.GetPathPartsForMatching(authService.Request.PathInfo);
#endif
                            string target = items[items.Length - 1];
                            if (string.Compare(target, typeof(GXIsDatabaseCreatedRequest).Name, true) == 0 ||
                                string.Compare(target, typeof(GXCreateTablesRequest).Name, true) == 0 ||
                                string.Compare(target, typeof(GXDropTablesRequest).Name, true) == 0)
                            {
                                user    = new GXAmiUser("gurux", "gurux", UserAccessRights.SuperAdmin);
                                user.Id = 1;
                                InitUser(authService, user);
                                return(true);
                            }
                            return(false);
                        }
                        List <GXAmiUser> users = Db.Select <GXAmiUser>(q => q.Name == userName && q.Password == password);
                        if (users.Count != 1)
                        {
                            //If known DC try to get new tasks, add new task, mark task claimed or add device exception.
                            Guid guid;
#if !SS4
                            string[] items = RestPath.GetPathPartsForMatching(authService.RequestContext.PathInfo);
#else
                            string[] items = RestPath.GetPathPartsForMatching(authService.Request.PathInfo);
#endif
                            string target = items[items.Length - 1];
                            if (items != null && items.Length != 0)
                            {
                                if (string.Compare(target, typeof(GXEventsRequest).Name, true) == 0 ||
                                    string.Compare(target, typeof(GXEventsRegisterRequest).Name, true) == 0 ||
                                    string.Compare(target, typeof(GXEventsUnregisterRequest).Name, true) == 0 ||
                                    string.Compare(target, typeof(GXTaskDeleteRequest).Name, true) == 0 ||
                                    string.Compare(target, typeof(GXTasksRequest).Name, true) == 0 ||
                                    string.Compare(target, typeof(GXTaskUpdateRequest).Name, true) == 0 ||
                                    string.Compare(target, typeof(GXTasksClaimRequest).Name, true) == 0 ||
                                    string.Compare(target, typeof(GXTraceLevelRequest).Name, true) == 0)
                                {
                                    //If DC register first time and starts to listen events.
                                    if (IsGuid(userName, out guid))
                                    {
                                        //If known DC wants to listen events.
                                        List <GXAmiDataCollector> list = Db.Select <GXAmiDataCollector>(p => p.Guid == guid);
                                        if (list.Count == 1)
                                        {
                                            IAuthSession s = authService.GetSession(false);
                                            s.Id              = userName;
                                            s.UserAuthId      = Guid.NewGuid().ToString();
                                            s.UserName        = userName;
                                            s.IsAuthenticated = true;
                                            s.Roles           = new List <string>();
                                            s.Roles.Add("0");
                                            return(true);
                                        }
                                        return(false);
                                    }
                                    return(false);
                                }
                                else if (string.Compare(target, typeof(GXDataCollectorUpdateRequest).Name, true) == 0 &&
                                         IsGuid(userName, out guid))
                                {
                                    if (guid == Guid.Empty)
                                    {
                                        /* TODO:
                                         * IAuthSession s = authService.GetSession(false);
                                         * s.Id = userName;
                                         * s.UserAuthId = Guid.NewGuid().ToString();
                                         * s.UserName = userName;
                                         * s.IsAuthenticated = true;
                                         * s.Roles = new List<string>();
                                         * s.Roles.Add("0");
                                         * */
                                        return(true);
                                    }
                                    //If DC updates itself.
                                    List <GXAmiDataCollector> list = Db.Select <GXAmiDataCollector>(p => p.Guid == guid);
                                    if (list.Count == 1)
                                    {
                                        IAuthSession s = authService.GetSession(false);
                                        s.Id              = userName;
                                        s.UserAuthId      = Guid.NewGuid().ToString();
                                        s.UserName        = userName;
                                        s.IsAuthenticated = true;
                                        s.Roles           = new List <string>();
                                        s.Roles.Add("0");
                                        return(true);
                                    }
                                    return(false);
                                }
                                //If data collector wants to get data from itself by Guid.
                                else if (string.Compare(target, typeof(GXDataCollectorsRequest).Name, true) == 0 &&
                                         IsGuid(userName, out guid))
                                {
                                    List <GXAmiDataCollector> list = Db.Select <GXAmiDataCollector>(p => p.Guid == guid);
                                    if (list.Count == 1)
                                    {
                                        IAuthSession s = authService.GetSession(false);
                                        s.Id              = userName;
                                        s.UserAuthId      = Guid.NewGuid().ToString();
                                        s.UserName        = userName;
                                        s.IsAuthenticated = true;
                                        s.Roles           = new List <string>();
                                        s.Roles.Add("0");
                                        return(true);
                                    }
                                    return(false);
                                }
                                return(false);
                            }
                        }
                        user = users[0];
                        InitUser(authService, user);
                    }
                }
            }
            catch (Exception ex)
            {
                GuruxAMI.Server.AppHost.ReportError(ex);
                try
                {
                    if (!System.Diagnostics.EventLog.SourceExists("GuruxAMI"))
                    {
                        System.Diagnostics.EventLog.CreateEventSource("GuruxAMI", "Application");
                    }
                    System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog();
                    appLog.Source = "GuruxAMI";
                    appLog.WriteEntry(ex.Message);
                }
                catch (System.Security.SecurityException)
                {
                    //Security exception is thrown if GuruxAMI source is not exists and it's try to create without administrator privilege.
                    //Just skip this, but errors are not write to eventlog.
                }
                throw ex;
            }
            return(true);
        }