예제 #1
0
        public AuthenticateResponseDto Authenticate(string raceId, AuthenticateRequestDto authentication)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }
            if (string.IsNullOrEmpty(authentication.DeviceId))
            {
                throw new ArgumentNullException("Missing DeviceId");
            }

            var judgesRepository = repositorySetProvider.GetRepositorySet(raceId).Judges;
            var judgeDevice      = judgesRepository.FindJudgeDevice(authentication.DeviceId);

            var response = new AuthenticateResponseDto();

            response.DeviceId = authentication.DeviceId;

            if (string.IsNullOrEmpty(authentication.ConnectCode) || judgeDevice == null || !string.Equals(judgeDevice.ConnectCode, authentication.ConnectCode))
            {
                if (judgeDevice == null)
                {
                    judgeDevice          = new JudgeDevice();
                    judgeDevice.DeviceId = authentication.DeviceId;
                }
                bool needsNewConnectCode = true;
                while (needsNewConnectCode)
                {
                    judgeDevice.ConnectCode         = GenerateConnectCode();
                    judgeDevice.AuthenticationToken = null;
                    needsNewConnectCode             = judgesRepository.FindConnectCode(judgeDevice.ConnectCode) != null;
                }
                judgesRepository.SaveJudgeDevice(judgeDevice);

                response.ConnectCode = judgeDevice.ConnectCode;
            }
            else if (judgeDevice.AuthenticationToken == null)
            {
                response.ConnectCode = judgeDevice.ConnectCode;
            }
            else
            {
                response.ConnectCode         = judgeDevice.ConnectCode;
                response.AuthenticationToken = judgeDevice.AuthenticationToken;
                response.JudgeId             = judgeDevice.JudgeId;
                var judge = judgesRepository.FindJudge(judgeDevice.JudgeId);
                if (judge != null)
                {
                    response.JudgeName = judge.Name;
                }
            }
            return(response);
        }
예제 #2
0
        public List <StartingListEntryDto> GetStartingList(string raceId, string startingLaneId)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }

            IRepositorySet repositorySet        = repositorySetProvider.GetRepositorySet(raceId);
            var            rootStartingLanes    = repositorySet.StartingLanes.GetStartingLanes();
            var            allowedStartingLanes = new HashSet <string>(flattener.GetLeaves(rootStartingLanes, startingLaneId).Select(l => l.StartingLaneId));
            var            startingList         = repositorySet.StartingList.GetStartingList();
            var            dtos = startingList.Where(e => allowedStartingLanes.Contains(e.StartingLaneId)).Select(BuildStartingList).ToList();

            return(dtos);
        }
예제 #3
0
        public RaceSetupDto GetSetup(string raceId)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }
            IRepositorySet repositorySet = repositorySetProvider.GetRepositorySet(raceId);

            var raceSetup = new RaceSetupDto();

            raceSetup.Race          = BuildRaceSettings(raceId, repositorySet.RaceSettings.GetRaceSettings());
            raceSetup.StartingLanes = repositorySet.StartingLanes.GetStartingLanes().Select(BuildStartingLane).ToList();
            raceSetup.ResultsLists  = repositorySet.ResultsLists.GetResultsLists().Select(BuildResultsList).ToList();
            raceSetup.Disciplines   = repositorySet.Disciplines.GetDisciplines().Select(BuildDiscipline).ToList();
            return(raceSetup);
        }
예제 #4
0
        public List <AthleteDto> GetAthletes(string raceId, JudgePrincipal principal)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }

            var repositorySet        = repositorySetProvider.GetRepositorySet(raceId);
            var disciplines          = repositorySet.Disciplines.GetDisciplines();
            var isAuthenticated      = principal != null;
            var allowedAnnouncements = new HashSet <string>(disciplines.Where(d => d.AnnouncementsPublic || isAuthenticated).Select(d => d.DisciplineId));
            var publicResults        = new HashSet <string>(disciplines.Where(d => d.ResultsPublic).Select(d => d.DisciplineId));

            var athletes = repositorySet.Athletes.GetAthletes();

            return(athletes.OrderBy(a => a.Surname).ThenBy(a => a.FirstName).Select(athlete => new AthleteDto
            {
                Profile = BuildProfile(athlete),
                Announcements = athlete.Announcements.Select(a => BuildAnnouncement(a, allowedAnnouncements.Contains(a.DisciplineId))).ToList(),
                Results = GetVisibleResults(athlete.ActualResults, principal, publicResults).Select(BuildActualResult).ToList()
            }).ToList());
        }
예제 #5
0
        private void Authenticate(HttpAuthenticationContext context)
        {
            var headers = context.Request.Headers;

            if (!headers.Contains("X-Authentication-Token"))
            {
                return;
            }
            var fullTokenString = headers.GetValues("X-Authentication-Token").FirstOrDefault();
            var token           = AuthenticationToken.Parse(fullTokenString);

            if (token == null || !token.Valid)
            {
                return;
            }

            if (token.Equals(adminToken))
            {
                var judge = new Judge
                {
                    IsAdmin = true,
                    JudgeId = "admin",
                    Name    = "Admin"
                };
                context.Principal = new JudgePrincipal(judge);
            }
            else
            {
                var repository = repositorySetProvider.GetRepositorySet(token.RaceId).Judges;

                var judge = repository.FindJudge(token.JudgeId);
                if (judge == null)
                {
                    return;
                }

                var tokenVerified = repository.FindJudgesDevices(token.JudgeId).Any(d => d.AuthenticationToken == fullTokenString);
                if (!tokenVerified)
                {
                    return;
                }

                context.Principal = new JudgePrincipal(judge);
            }
        }