private TcpMessage HandleMultiRelationQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.MultiRelationQuery))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage <DbRelation>(TcpRequestType.List);

            if (request.HasParam("from_id"))
            {
                var fromId = request.GetParamAsNullableInt("from_id");
                var items  = _dataService.GetRelations(fromId);
                response.Add(items);
                return(response);
            }

            return(TcpMessage.Error("Missing or badly formatted query parameters"));
        }
        public void Login(string username, string password)
        {
            var request = new ParamTcpMessage("loginbasic");
            request.SetParam("username", username);
            request.SetParam("password", password);
            var response = _client.SendAndReceive(request);

            if (response.HasType(TcpRequestType.Ok))
            {
                var objResponse = new ObjectTcpMessage<DbItem>(response);
                if (objResponse.Item != null)
                {
                    IsAuthenticated = true;

                    UserItemId = objResponse.Item.Id;

                    UserWorkspaceContainerId = _dataService.GetOrCreateItem(UserItemId, "workspace").Id;
                }
                else
                {
                    throw new AuthException("Login failed. No user-item returned.");
                }
            }
            else
            {
                throw new AuthException("Login failed. Response: " + response.RequestType + " - " + response.Content);
            }
        }
Exemple #3
0
 public override ICollection<DbRelation> GetRelations(int? fromId)
 {
     var request = new ParamTcpMessage(TcpRequestType.MultiRelationQuery);
     request.SetParam("from_id", fromId);
     var response = Client.SendAndReceive<DbRelation>(request);
     return response.Items;
 }
        private TcpMessage HandleUniqueItemQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.UniqueItemQuery))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage <DbItem>(TcpRequestType.List);

            if (request.HasParam("id"))
            {
                var id = request.GetParamAsNullableInt("id");
                if (id != null)
                {
                    var item = _dataService.GetItem(id.Value);
                    response.Add(item);
                    return(response);
                }
            }

            return(TcpMessage.Error("Missing or badly formatted query parameters"));
        }
Exemple #5
0
 public override DbItem GetItem(int id)
 {
     var request = new ParamTcpMessage(TcpRequestType.UniqueItemQuery);
     request.SetParam("id", id);
     var response = Client.SendAndReceive<DbItem>(request);
     return response.Item;
 }
Exemple #6
0
        public static TcpMessage Error(string error)
        {
            var response = new ParamTcpMessage(TcpRequestType.Error);

            response.SetParam("error", error);
            return(response);
        }
Exemple #7
0
        public void Login(string username, string password)
        {
            var request = new ParamTcpMessage("loginbasic");

            request.SetParam("username", username);
            request.SetParam("password", password);
            var response = _client.SendAndReceive(request);

            if (response.HasType(TcpRequestType.Ok))
            {
                var objResponse = new ObjectTcpMessage <DbItem>(response);
                if (objResponse.Item != null)
                {
                    IsAuthenticated = true;

                    UserItemId = objResponse.Item.Id;

                    UserWorkspaceContainerId = _dataService.GetOrCreateItem(UserItemId, "workspace").Id;
                }
                else
                {
                    throw new AuthException("Login failed. No user-item returned.");
                }
            }
            else
            {
                throw new AuthException("Login failed. Response: " + response.RequestType + " - " + response.Content);
            }
        }
Exemple #8
0
 public override DbRelation GetRelation(int? fromId, string identifier)
 {
     var request = new ParamTcpMessage(TcpRequestType.UniqueRelationQuery);
     request.SetParam("from_id", fromId);
     request.SetParam("identifier", identifier);
     var response = Client.SendAndReceive<DbRelation>(request);
     return response.Item;
 }
        private TcpMessage HandleLoginRequest(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType("loginbasic"))
            {
                return(null);
            }

            var request = new ParamTcpMessage(message);

            var username = request.GetParam("username");
            var password = request.GetParam("password");

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                var userItem = _dataService.GetItemByRelation(UsersParentId, username);

                if (userItem == null)
                {
                    if (!AutoRegisterUsers)
                    {
                        return(TcpMessage.Error("User not found. Automatic registering of users is currently disabled."));
                    }

                    // Register
                    userItem = new DbItem(username);
                    _dataService.Insert(userItem);
                    _dataService.Insert(new DbRelation(UsersParentId, username, userItem.Id));

                    var saltItem = new DbItem(HashHelper.CreateSaltString(10));
                    _dataService.Insert(saltItem);
                    _dataService.Insert(new DbRelation(userItem.Id, "salt", saltItem.Id));

                    var passwordItem = new DbItem(HashHelper.GenerateSaltedHash(password, saltItem.Value));
                    _dataService.Insert(passwordItem);
                    _dataService.Insert(new DbRelation(userItem.Id, "password", passwordItem.Id));
                }
                else
                {
                    // Login
                    var saltItem     = _dataService.GetItemByRelation(userItem.Id, "salt");
                    var passwordItem = _dataService.GetItemByRelation(userItem.Id, "password");

                    if (saltItem == null || saltItem.Value == null || passwordItem == null || passwordItem.Value == null || !HashHelper.ConfirmPassword(passwordItem.Value, password, saltItem.Value))
                    {
                        return(TcpMessage.Error("Login failed. Wrong password."));
                    }
                }

                _hostUsers[host] = userItem;

                var response = new ObjectTcpMessage <DbItem>(TcpRequestType.Ok);
                response.Add(userItem);
                return(response);
            }

            return(TcpMessage.Error("Missing or badly formatted login parameters"));
        }
Exemple #10
0
        public override DbItem GetItem(int id)
        {
            var request = new ParamTcpMessage(TcpRequestType.UniqueItemQuery);

            request.SetParam("id", id);
            var response = Client.SendAndReceive <DbItem>(request);

            return(response.Item);
        }
Exemple #11
0
        public override ICollection <DbRelation> GetRelations(int?fromId)
        {
            var request = new ParamTcpMessage(TcpRequestType.MultiRelationQuery);

            request.SetParam("from_id", fromId);
            var response = Client.SendAndReceive <DbRelation>(request);

            return(response.Items);
        }
Exemple #12
0
        public override DbRelation GetRelation(int?fromId, string identifier)
        {
            var request = new ParamTcpMessage(TcpRequestType.UniqueRelationQuery);

            request.SetParam("from_id", fromId);
            request.SetParam("identifier", identifier);
            var response = Client.SendAndReceive <DbRelation>(request);

            return(response.Item);
        }
Exemple #13
0
 public static TcpMessage Error(string error)
 {
     var response = new ParamTcpMessage(TcpRequestType.Error);
     response.SetParam("error", error);
     return response;
 }
        private TcpMessage HandleLoginRequest(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType("loginbasic"))
                return null;

            var request = new ParamTcpMessage(message);

            var username = request.GetParam("username");
            var password = request.GetParam("password");

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                var userItem = _dataService.GetItemByRelation(UsersParentId, username);

                if (userItem == null)
                {
                    if (!AutoRegisterUsers)
                        return TcpMessage.Error("User not found. Automatic registering of users is currently disabled.");

                    // Register
                    userItem = new DbItem(username);
                    _dataService.Insert(userItem);
                    _dataService.Insert(new DbRelation(UsersParentId, username, userItem.Id));

                    var saltItem = new DbItem(HashHelper.CreateSaltString(10));
                    _dataService.Insert(saltItem);
                    _dataService.Insert(new DbRelation(userItem.Id, "salt", saltItem.Id));

                    var passwordItem = new DbItem(HashHelper.GenerateSaltedHash(password, saltItem.Value));
                    _dataService.Insert(passwordItem);
                    _dataService.Insert(new DbRelation(userItem.Id, "password", passwordItem.Id));
                }
                else
                {
                    // Login
                    var saltItem = _dataService.GetItemByRelation(userItem.Id, "salt");
                    var passwordItem = _dataService.GetItemByRelation(userItem.Id, "password");

                    if (saltItem == null || saltItem.Value == null || passwordItem == null || passwordItem.Value == null || !HashHelper.ConfirmPassword(passwordItem.Value, password, saltItem.Value))
                        return TcpMessage.Error("Login failed. Wrong password.");
                }

                _hostUsers[host] = userItem;

                var response = new ObjectTcpMessage<DbItem>(TcpRequestType.Ok);
                response.Add(userItem);
                return response;
            }

            return TcpMessage.Error("Missing or badly formatted login parameters");
        }
Exemple #15
0
        private TcpMessage HandleUniqueRelationQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.UniqueRelationQuery))
                return null;

            if (!IsAuthenticated(host))
                throw AuthException.NotLoggedIn();

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage<DbRelation>(TcpRequestType.List);
            if (request.HasParam("from_id") && request.HasParam("identifier"))
            {
                var fromId = request.GetParamAsNullableInt("from_id");
                var identifier = request.GetParam("identifier");
                var relation = _dataService.GetRelation(fromId, identifier);
                response.Add(relation);
                return response;
            }

            return TcpMessage.Error("Missing or badly formatted query parameters");
        }
Exemple #16
0
        private TcpMessage HandleUniqueItemQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.UniqueItemQuery))
                return null;

            if (!IsAuthenticated(host))
                throw AuthException.NotLoggedIn();

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage<DbItem>(TcpRequestType.List);
            if (request.HasParam("id"))
            {
                var id = request.GetParamAsNullableInt("id");
                if (id != null)
                {
                    var item = _dataService.GetItem(id.Value);
                    response.Add(item);
                    return response;
                }
            }

            return TcpMessage.Error("Missing or badly formatted query parameters");
        }