Example #1
0
        public IActionResult CheckToken(string token)
        {
            JudgePayload payload;

            try
            {
                payload = JWTJudgeProvider.DecodeToken(token);
            }
            catch (Exception)
            {
                return(Ok("Invalid"));
            }

            if (payload.Expires <= DateTime.Now)
            {
                return(Ok("Expired"));
            }

            var tokens = JWTJudgeFactory.GetAllTokensForJudge(payload.JudgeId);

            if (tokens.Any(t => t == token))
            {
                return(Ok("Valid"));
            }
            return(Ok("Invalid"));
        }
        public ActionResult <RROJudge> Judge(string token)
        {
            var payload = JWTJudgeProvider.DecodeToken(token);

            if (payload == null)
            {
                return(BadRequest(TokenState.Invalid));
            }

            var judgeId = payload.JudgeId;
            var tour    = payload.Tour;

            switch (tour)
            {
            case 0:
                var judgeCv = (from tJ in _dbContext.JudgesCv
                               where tJ.JudgeId == judgeId
                               select tJ).FirstOrDefault();

                if (judgeCv == null)
                {
                    return(BadRequest(TokenState.Invalid));
                }
                return(new RROJudge
                {
                    JudgeId = judgeCv.JudgeId,
                    Polygon = judgeCv.Polygon,
                    Status = judgeCv.Status,
                    JudgeName = judgeCv.JudgeName
                });

            case 1:
                var judgeFin = (from tJ in _dbContext.JudgesFin
                                where tJ.JudgeId == judgeId
                                select tJ).FirstOrDefault();

                if (judgeFin == null)
                {
                    return(BadRequest(TokenState.Invalid));
                }
                return(new RROJudge
                {
                    JudgeId = judgeFin.JudgeId,
                    Polygon = judgeFin.Polygon,
                    Status = judgeFin.Status,
                    JudgeName = judgeFin.JudgeName
                });

            default:
                return(BadRequest(TokenState.Invalid));
            }
        }
        public ActionResult <string> Authorize(string judgeId, string pass, string serviceId)
        {
            var currentTour =
                (from ct in _dbContext.CurrentTour
                 select ct.Current).Last();

            var judge = (currentTour == 0
                ? (from tJudge in _dbContext.JudgesCv
                   where tJudge.JudgeId == judgeId
                   select tJudge).OfType <RROJudge>()

                : (from tJudge in _dbContext.JudgesFin
                   where tJudge.JudgeId == judgeId
                   select tJudge).OfType <RROJudge>()).FirstOrDefault();

            if (judge == null)
            {
                return(BadRequest(AuthenticationError.UserNotFound));
            }

            if (currentTour == 0)
            {
                if (!((RROJudgeCv)judge).PassHash.Equals(pass))
                {
                    return(BadRequest(AuthenticationError.IncorrectPassword));
                }
            }
            else
            {
                if (!((RROJudgeFin)judge).PassHash.Equals(pass))
                {
                    return(BadRequest(AuthenticationError.IncorrectPassword));
                }
            }

            var payload = JudgePayload.Create(judge, currentTour, serviceId);
            var service =
                (from tService in _dbContext.Services
                 where tService.ServiceId == payload.Service
                 select tService).FirstOrDefault();

            if (service == null)
            {
                return(BadRequest(AuthenticationError.UnknownService));
            }

            var tokenCv = JWTJudgeProvider.CreateToken(payload);

            return(tokenCv);
        }
Example #4
0
        public ActionResult <RROJudge> Judge(string token, string judgeId, int?tour)
        {
            var tJudgeId = judgeId;
            var tTour    = tour ?? -1;

            if (!String.IsNullOrWhiteSpace(token))
            {
                JudgePayload payload;
                try
                {
                    payload = JWTJudgeProvider.DecodeToken(token);
                }
                catch (Exception e)
                {
                    return(BadRequest("Can't resolve token: " + e.Message));
                }

                tJudgeId = payload.JudgeId;
                tTour    = payload.Tour;
            }

            switch (tTour)
            {
            case 0:
                var judgeCv = (from tJ in _dbContext.JudgesCv
                               where tJ.JudgeId == tJudgeId
                               select tJ).FirstOrDefault();

                if (judgeCv == null)
                {
                    return(Ok());
                }
                return(judgeCv);

            case 1:
                var judgeFin = (from tJ in _dbContext.JudgesFin
                                where tJ.JudgeId == tJudgeId
                                select tJ).FirstOrDefault();

                if (judgeFin == null)
                {
                    return(Ok());
                }
                return(judgeFin);

            default:
                return(BadRequest("Invalid tour parameter value"));
            }
        }
        public async Task <IActionResult> StartNewRound(string category)
        {
            if (!Request.Headers.ContainsKey("token"))
            {
                return(BadRequest(ScoreBoardPostError.TokenNotPassed));
            }
            var token = Request.Headers["token"][0];

            var payload = JWTJudgeProvider.DecodeToken(token);

            if (payload == null)
            {
                return(BadRequest(ScoreBoardPostError.InvalidToken));
            }

            if (payload.Status != "admin")
            {
                return(BadRequest(ScoreBoardPostError.AccessDenied));
            }

            var roundQuery = from tRound in _dbContext.Rounds
                             where tRound.Category == category
                             orderby tRound.Round
                             select tRound;

            foreach (var round in roundQuery)
            {
                round.Current = 0;
            }

            if (!roundQuery.Any())
            {
                _dbContext.Rounds.Add(new CompetitionRound {
                    Category = category, Round = 1, Current = 1
                });
            }
            else
            {
                var lastRound = roundQuery.Last().Round;
                _dbContext.Rounds.Add(new CompetitionRound {
                    Category = category, Round = lastRound + 1, Current = 1
                });
            }

            await _dbContext.SaveChangesAsync();

            return(Ok());
        }
        public ActionResult <TokenState> CheckToken(string token)
        {
            var payload = JWTJudgeProvider.DecodeToken(token);

            if (payload == null)
            {
                return(TokenState.Invalid);
            }

            if (payload.Expires <= DateTime.Now)
            {
                return(TokenState.Expired);
            }

            return(TokenState.Valid);
        }
        private static string RefreshTokenInternal(string token)
        {
            var payload = JWTJudgeProvider.DecodeToken(token);

            if (payload == null)
            {
                return(null);
            }

            payload.OpenTime = DateTime.Now;
            payload.Expires  = payload.OpenTime.AddMinutes(20);

            var newToken = JWTJudgeProvider.CreateToken(payload);

            return(newToken);
        }
Example #8
0
        public IActionResult Authorize(string judgeId, string pass, string serviceId)
        {
            var currentTour =
                (from ct in _dbContext.CurrentTour
                 select ct.Current).Last();

            switch (currentTour)
            {
            case 0:
                var getJudgeCv = from judge in _dbContext.JudgesCv
                                 where judge.JudgeId == judgeId
                                 select judge;

                if (!getJudgeCv.Any())
                {
                    return(BadRequest("User not found"));
                }

                var judgeCv = getJudgeCv.First();
                if (!judgeCv.PassHash.Equals(pass))
                {
                    return(BadRequest("Incorrect password"));
                }

                var payloadCv = JudgePayload.Create(judgeCv, 0, serviceId);
                var serviceCv =
                    (from service in _dbContext.Services
                     where service.ServiceId == payloadCv.Service
                     select service).FirstOrDefault();
                if (serviceCv == null)
                {
                    return(BadRequest("Unknown service"));
                }

                var tokenCv = JWTJudgeProvider.CreateToken(payloadCv);
                JWTJudgeFactory.AddToken(judgeCv.JudgeId, tokenCv, true);

                return(Ok(tokenCv));


            case 1:
                var getJudgeFin = from judge in _dbContext.JudgesFin
                                  where judge.JudgeId == judgeId
                                  select judge;

                if (!getJudgeFin.Any())
                {
                    return(BadRequest("User not found"));
                }

                var judgeFin = getJudgeFin.First();
                if (!judgeFin.PassHash.Equals(pass))
                {
                    return(BadRequest("Incorrect password"));
                }

                var payloadFin = JudgePayload.Create(judgeFin, 1, serviceId);
                var tokenFin   = JWTJudgeProvider.CreateToken(payloadFin);
                JWTJudgeFactory.AddToken(judgeFin.JudgeId, tokenFin, true);

                return(Ok(tokenFin));


            default:
                return(BadRequest("Server error"));
            }
        }
Example #9
0
        public async Task <IActionResult> Index()
        {
            if (!Request.Cookies.ContainsKey("token"))
            {
                return(RedirectToAction("Index", "Authorization"));
            }

            var token   = Request.Cookies["token"];
            var payload = JWTJudgeProvider.DecodeToken(token);

            var response = await new HttpClient().GetAsync($"https://rro.azurewebsites.net/api/judge?token={token}");

            if (!response.IsSuccessStatusCode)
            {
                return(StatusCode(500));
            }

            var judgeJson = await response.Content.ReadAsStringAsync();

            var judge         = JsonConvert.DeserializeObject <RROJudge>(judgeJson);
            var teamsResponse = await new HttpClient()
                                .GetAsync($"https://rro.azurewebsites.net/api/teams?tour={payload.Tour}&polygon={judge.Polygon}");

            if (!teamsResponse.IsSuccessStatusCode)
            {
                return(StatusCode(500));
            }

            var teamsJson = await teamsResponse.Content.ReadAsStringAsync();

            var teams = JsonConvert.DeserializeObject <List <RROTeam> >(teamsJson);

            if (!teams.Any())
            {
                return(NotFound());
            }

            var roundResponse = await new HttpClient().GetAsync("https://rro.azurewebsites.net/api/currentround");

            if (!roundResponse.IsSuccessStatusCode)
            {
                return(StatusCode(500));
            }

            var round    = Int32.Parse(await roundResponse.Content.ReadAsStringAsync());
            var category = teams.First().Category;

            TempData["judge"]    = judgeJson;
            TempData["teams"]    = teamsJson;
            TempData["category"] = category;
            TempData["round"]    = round.ToString();
            TempData["tour"]     = payload.Tour.ToString();


            switch (category)
            {
            case "ОМЛ":
                return(RedirectToAction("Oml"));

            default:
                //TODO a lot of forms and tables
                return(NotFound());
            }
        }
        public async Task <ActionResult <string> > OmlPreResult([FromBody] OmlScore score)
        {
            if (!Request.Headers.ContainsKey("token"))
            {
                return(BadRequest(ScoreBoardPostError.TokenNotPassed));
            }

            var token   = Request.Headers["token"][0];
            var payload = JWTJudgeProvider.DecodeToken(token);

            if (payload == null)
            {
                return(BadRequest(ScoreBoardPostError.InvalidToken));
            }
            if (payload.Expires <= DateTime.Now)
            {
                return(BadRequest(ScoreBoardPostError.TokenExpired));
            }

            var judgeId = payload.JudgeId;

            if (score.JudgeId != judgeId)
            {
                return(BadRequest(ScoreBoardPostError.RecordPayloadMismatch));
            }

            if (score.Saved == 1)
            {
                var hasNulls = score.RedBlockState == null || score.YellowBlockState == null ||
                               score.GreenBlockState == null || score.WhiteBlock1State == null ||
                               score.WhiteBlock2State == null || score.BlueBlockState == null ||
                               score.BattaryBlock1State == null || score.BattaryBlock2State == null ||
                               score.RobotState == null || score.Wall1State == null || score.Wall2State == null ||
                               score.Time1 == null || score.Time2 == null;
                if (hasNulls)
                {
                    return(BadRequest(ScoreBoardPostError.InvalidModelContainsNulls));
                }
            }

            var teamInBase = from it in _dbContext.OMLPreResults
                             where it.Round == score.Round
                             where it.TeamId == score.TeamId
                             select it;

            if (!teamInBase.Any())
            {
                score.JudgeId = judgeId;
                await _dbContext.OMLPreResults.AddAsync(score);

                await _dbContext.SaveChangesAsync();

                var newToken = RefreshTokenInternal(token);
                return(newToken);
            }

            var record = teamInBase.First();

            if (record.Saved == 1)
            {
                return(BadRequest(ScoreBoardPostError.OverwritingForbidden));
            }

            record.JudgeId            = judgeId;
            record.Tour               = score.Tour;
            record.RedBlockState      = score.RedBlockState;
            record.YellowBlockState   = score.YellowBlockState;
            record.GreenBlockState    = score.GreenBlockState;
            record.WhiteBlock1State   = score.WhiteBlock1State;
            record.WhiteBlock2State   = score.WhiteBlock2State;
            record.BlueBlockState     = score.BlueBlockState;
            record.BattaryBlock1State = score.BattaryBlock1State;
            record.BattaryBlock2State = score.BattaryBlock2State;
            record.RobotState         = score.RobotState;
            record.Wall1State         = score.Wall1State;
            record.Wall2State         = score.Wall2State;
            record.Time1              = score.Time1;
            record.Time2              = score.Time2;
            record.Saved              = score.Saved;
            await _dbContext.SaveChangesAsync();

            var newToken2 = RefreshTokenInternal(token);

            return(newToken2);
        }