Exemple #1
0
        public Task Handle(IHttpContext context, Func <Task> next)
        {
            var request = context.Request;

            if (request.Method == HttpMethods.Post)
            {
                if (request.Post.Parsed.TryGetByName("token", out string tokenString))
                {
                    var validationResult = AuthorizationHandler.VerifyToken(tokenString, false);
                    if (validationResult.valid)
                    {
                        if ((DateTime.Now.Subtract(validationResult.account.LastAction ?? new DateTime()).TotalSeconds) > Account.OnlineTimeoutSeconds - 1)
                        {
                            validationResult.account.LastAction = DateTime.Now;
                            DBHolderSQL.Save(nameof(Account), (nameof(Account.ID), validationResult.account.ID), (nameof(Account.LastAction), validationResult.account.LastAction));
                        }
                        if (request.Post.Parsed.TryGetByName("action", out string action))
                        {
                            context.Response = Actions[action]?.Invoke(request.Post.Parsed, validationResult.account);
                        }
                        else
                        {
                            context.Response = new HttpResponse(HttpResponseCode.MethodNotAllowed, "Эм.. что от меня требуется???", false);
                        }
                    }
                    else
                    {
                        DBHolderSQL.Log($"[ОШИБКА ДОСТУПА] Пользователь с поврежденным или подделанным токеном пытался войти в систему. Экземпляр токена предоставлен в описании.",
                                        $"{tokenString}");
                        context.Response = new HttpResponse(HttpResponseCode.Forbidden, "Доступ запрещен! Ошибка разбора токена!", false);
                    }
                }
                else
                {
                    context.Response = new HttpResponse(HttpResponseCode.Forbidden, "Доступ запрещен! Нужен токен!", false);
                }
            }
            else
            {
                context.Response = new HttpResponse(HttpResponseCode.MethodNotAllowed, "Метод недоступен!", false);
            }

            return(Task.Factory.GetCompleted());
        }
Exemple #2
0
        private static IHttpResponse SignUp(IHttpHeaders query)
        {
            if (query.TryGetByName("UserName", out string userName) &&
                query.TryGetByName("Password", out string password) &&
                query.TryGetByName("AccountType", out byte accountType) &&
                query.TryGetByName("BirthDate", out string birthDateString) &&
                DateTime.TryParseExact(birthDateString, Core.CommonVariables.DateFormatString, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime birthDate) &&

                query.TryGetByName("FullName", out string fullName))
            {
                var validationResult = Account.Validate(userName, password, birthDate, fullName);
                if (validationResult == AccountValidationResult.OK)
                {
                    var rows = DBHolderSQL.GetRange("Account", null, 0, 1, true, false, false, false, ("UserName", userName)).Rows;
                    if (rows.Count == 0)
                    {
                        query.TryGetByName("ProfileImage", out byte[] profileImage);
                        return(new HttpResponse(HttpResponseCode.Ok, CreateToken(DBHolderSQL.Save("Account",
                                                                                                  ("UserName", userName),
                                                                                                  ("Password", password),
                                                                                                  ("AccountType", accountType),
                                                                                                  ("BirthDate", birthDate),
                                                                                                  ("ProfileImage", profileImage),
                                                                                                  ("FullName", fullName),
                                                                                                  ("Approved", false),
                                                                                                  ("IsLocal", true),
                                                                                                  ("ID", -1)),
                                                                                 userName, password), true));
                    }
                    else
                    {
                        return(new HttpResponse(HttpResponseCode.BadRequest, "Ошибка! Регистрация невозможна, т.к. пользователь с этим именем пользователя уже зарегистирован в системе!", false));
                    }
                }
                else
                {
                    return(new HttpResponse(HttpResponseCode.BadRequest, ErrorMessages[validationResult], false));
                }
            }
            return(null);
        }
Exemple #3
0
        public static void Main()
        {
            foreach (var current in Enum.GetValues(typeof(ConsoleColor)))
            {
                PrintLogo((ConsoleColor)current);
                Thread.Sleep(50);
                Console.CursorLeft = Console.CursorTop = 0;
            }
            PrintLogo(ConsoleColor.Green);

            Console.WriteLine("Welcome to InCollege.Server! Don't hesitate, open http://localhost/ to see what we got!");
            Console.WriteLine("Made by [CYBOR] = Muhametshin R.A.");

            Console.WriteLine($"Initializing SQLite DB(thanks Frank A. Krueger and other 53 team members for sqlite-net engine) in \n{CommonVariables.DBLocation}...\n");
            bool createAdmin = !File.Exists(CommonVariables.DBLocation);

            DBHolderSQL.Init(CommonVariables.DBLocation);
            if (createAdmin)
            {
                DBHolderSQL.Save(nameof(Account), new Account
                {
                    FullName    = "Администратор",
                    UserName    = "******",
                    AccountType = AccountType.Admin,
                    Approved    = true
                }.Columns.ToArray());
            }

            Console.WriteLine($"Initializing uHttpSharp server engine(thanks Elad Zelingher and other 6 team members for uHttpSharp engine)...");
            InCollegeServer.Start();

            while (true)
            {
                Thread.Sleep(1000);
            }
        }