Ejemplo n.º 1
0
        public User GetUser()
        {
            if (user == null)
            {
                user = new User()
                {
                    login = HashManager.GenerateToken()
                }.CreateAndGet();
            }

            return(user);
        }
Ejemplo n.º 2
0
        public static RequestResponse Logout(string token)
        {
            var dbToken = AccessToken.FindByToken(
                ValidationManager.OnlyStringsLettersDigitsSpaces(token)
                );

            if (dbToken == null)
            {
                return(new RequestResponse(RequestTypes.LogOut, ReturnCodes.FailedNotFound));
            }

            dbToken.token = HashManager.GenerateToken();
            dbToken.Save();

            return(new RequestResponse(RequestTypes.LogOut, ReturnCodes.Success));
        }
Ejemplo n.º 3
0
        public static RequestResponse Login(string login, string password)
        {
            int tokensLimit = 5;

            login    = ValidationManager.OnlyStringsLettersDigitsSpaces(login);
            password = ValidationManager.OnlyStringsLettersDigitsSpaces(password);

            User user = User.FindByLogin(login);

            if (user == null)
            {
                return(new RequestResponse(RequestTypes.Login, ReturnCodes.FailedUserNotExist));
            }

            if (user.password != HashManager.Encrypt(password))
            {
                return(new RequestResponse(RequestTypes.Login, ReturnCodes.FailedInvalidLoginData));
            }

            int    tokensAmount = AccessToken.UserTokensCount(user.id);
            string resultToken  = HashManager.GenerateToken();

            if (tokensAmount >= tokensLimit)
            {
                var userTokens   = AccessToken.GetListByUserId(user.id);
                var updatedToken = userTokens[new Random().Next(userTokens.Count)];
                updatedToken.token = resultToken;
                updatedToken.Save();
            }
            else
            {
                var newToken = new AccessToken();
                newToken.user_id = user.id;
                newToken.token   = resultToken;
                AccessToken.Create(newToken);
            }

            var data = new JObject();

            data["token"] = resultToken;

            return(new RequestResponse(RequestTypes.Login, ReturnCodes.Success, data));
        }