Esempio n. 1
0
        private void DoAuth(string arguments)
        {
            IsAuthenticated = false;
            string username = null;
            string password = null;

            if (string.IsNullOrEmpty(arguments))
            {
                SendError("No authentication details");
            }
            else
            {

                if (arguments.IndexOf(" ") <= -1)
                {
                    SendError("No username and/or password supplied");
                }
                else
                {

                    username = arguments.Substring(0, arguments.IndexOf(" "));
                    password = arguments.Substring(arguments.IndexOf(" ") + 1);

                    user = userRepo.GetByUsername(username);
                    if (user == null || string.IsNullOrEmpty(user.Username))
                    {
                        SendError("Invalid Username");
                    }
                    else
                    {

                        if (password != user.Password)
                        {
                            SendError("Login incorrect");
                        }
                        else
                        {

                            SendOK();
                            IsAuthenticated = true;
                            guiThread.AddLog("User " + username + " authenticated.");
                        }
                    }
                }
            }

            if (!IsAuthenticated)
            {
                failedLogins++;
                if (failedLogins == maxFailedLogins)
                {
                    guiThread.AddLog("Client exceeded maximum failed login attempts.");
                    DoQuit();
                }
            }
            else
            {
                failedLogins = 0;
            }
        }
Esempio n. 2
0
        public string RegisterDevice(string token, string deviceType, string deviceId)
        {
            UnitOfWork unitOfWork = new UnitOfWork(_sessionHelper.GetSessionFactory("APL"));

            Repository<Guid, User> userRepo = new Repository<Guid, User>(unitOfWork.Session);

            JavaScriptSerializer js = new JavaScriptSerializer();
            JsonSerializerSettings j = new JsonSerializerSettings();
            j.TypeNameHandling = TypeNameHandling.Objects;
            j.Formatting = Newtonsoft.Json.Formatting.Indented;

            string responseString = null;
            Response response = new Response();

            user = CheckToken(token);
            if (user.AuthToken == null || user.AuthToken != token)
            {
                user.AuthToken = token;
                userRepo.Update(user);
            }

            if (!string.IsNullOrEmpty(user.AuthToken))
            {
                if (CheckToken(user.AuthToken) == null)
                {
                    user.AuthToken = null;
                }
            }

            if (user != null)
            {
                if (DEVICE_TYPES.Contains(deviceType.ToLower()))
                {
                    UserMobileDevice userMobileDevice = new UserMobileDevice();
                    Repository<Guid, UserMobileDevice> userMobileDevRepo = new Repository<Guid, UserMobileDevice>(unitOfWork.Session);
                    userMobileDevice = userMobileDevRepo.FilterBy(x => x.Token == deviceId).FirstOrDefault();
                    if (userMobileDevice != null && userMobileDevice.Id != Guid.Empty)
                    {
                        if (userMobileDevice.MobileDeviceType.ToLower() == deviceType.ToLower() && userMobileDevice.User.Id == user.Id)
                        {
                            response.ResponseCode = "OK";
                            response.ResponseData = "Device registered.";
                        }
                        else
                        {
                            response.ResponseCode = "ERR";
                            response.ResponseData = "Invalid device registration";
                        }

                    }
                    else
                    {
                        userMobileDevice = new UserMobileDevice();
                        userMobileDevice.MobileDeviceType = deviceType.ToLower();
                        userMobileDevice.Token = deviceId;
                        userMobileDevice.User = user;
                        userMobileDevice.EnablePushNotifications = true;
                        user.MobileDevices.Add(userMobileDevice);
                        userMobileDevRepo.Add(userMobileDevice);
                        response.ResponseCode = "OK";
                        response.ResponseData = "Device registered.";
                    }
                }
                else
                {
                    response.ResponseCode = "ERR";
                    response.ResponseData = "Unknown DeviceType";
                }

            }
            else
            {
                response.ResponseCode = "ERR";
                response.ResponseData = "Authentication required.";
            }
            unitOfWork.Commit();

            js = new JavaScriptSerializer();
            responseString = JsonConvert.SerializeObject(response, j);
            return responseString;
        }
Esempio n. 3
0
        public string Authenticate(string Username, string Password)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            JsonSerializerSettings j = new JsonSerializerSettings();
            j.TypeNameHandling = TypeNameHandling.Objects;
            j.Formatting = Newtonsoft.Json.Formatting.Indented;

            string result = "ERR - Unknown";

            string responseString = null;
            Response response = new Response();
            UnitOfWork unitOfWork = new UnitOfWork(_sessionHelper.GetSessionFactory("APL"));
            Repository<Guid, User> userRepo = new Repository<Guid, User>(unitOfWork.Session);
            Repository<Guid, UserToken> userTokenRepo = new Repository<Guid, UserToken>(unitOfWork.Session);
            user = userRepo.FilterBy(x => x.Username == Username).FirstOrDefault();

            if (user != null && user.Id != Guid.Empty)
            {
                if (Password == user.Password)
                {
                    AuthToken token = new AuthToken();

                    if (!string.IsNullOrEmpty(user.AuthToken))
                    {
                        if (CheckToken(user.AuthToken) == null)
                        {
                            user.AuthToken = null;
                        }
                    }

                    if (string.IsNullOrEmpty(user.AuthToken))
                    {
                        result = GenerateAuthToken();
                        APLBackendDB.UserToken userToken = new UserToken();
                        userToken.CreateDate = DateTime.Now;
                        userToken.ExpiryDate = DateTime.Now.AddHours(4);
                        userToken.LastUsedDate = DateTime.Now;
                        userToken.Token = result;
                        userToken.UserId = user.Id;
                        userTokenRepo.Add(userToken);

                        user.AuthToken = result;
                        userRepo.Update(user);

                        token.Token = result;
                    }
                    else
                    {
                        token.Token = user.AuthToken;
                    }

                    response.ResponseCode = "OK";
                    response.ResponseData = token;
                }
                else
                {
                    response.ResponseCode = "ERR";
                    response.ResponseData = "Authentication failed.";
                }
            }
            else
            {
                response.ResponseCode = "ERR";
                response.ResponseData = "User not found.";
            }

            js = new JavaScriptSerializer();
            responseString = JsonConvert.SerializeObject(response, j);
            unitOfWork.Commit();
            return responseString;
        }
Esempio n. 4
0
        public string Ping(string token)
        {
            string responseString = null;
            Response response = new Response();
            user = CheckToken(token);
            if (!string.IsNullOrEmpty(user.AuthToken))
            {
                response.ResponseCode = "OK";
                response.ResponseData = "Pong";
            }
            else
            {
                response.ResponseCode = "ERR";
                response.ResponseData = "Authentication required.";
            }

            return responseString;
        }