/// <summary>
        /// Adds a token to the waitlist.
        /// </summary>
        /// <param name="clientId">clientId: ID_A</param>
        /// <param name="hashedToken">hashed token</param>
        /// <param name="loginLog">Log-Object</param>
        public static void AddToWaitList(string clientId, string hashedToken, LoginLog loginLog)
        {
            if (String.IsNullOrEmpty(clientId) || String.IsNullOrEmpty(hashedToken) || loginLog == null)
                throw new ArgumentException("No null values or empty strings allowed.");

            hashedToken = hashedToken.ToUpper();

            // sets overridden log to success: false.
            if (Items.ContainsKey(clientId))
            {
                Repository.SetLoginSuccess(Items[clientId].LoginLog, false);
            }

            // will override old entry
            Items[clientId] = new WaitlistItem
                {
                    HashedToken = hashedToken,
                    LoginLog = loginLog
                };

            // Start Timer
            Timer removeTimer = new Timer {Interval = Timeframe};
            removeTimer.Elapsed += (sender, e) => RemoveFromWaitlist(sender, e, clientId);
            removeTimer.Enabled = true;
        }
        /// <summary>
        /// Adds a login attempt to a user's log.
        /// </summary>
        /// <param name="userModel">UserModel with new Data.</param>
        /// <returns>Number of affected rows, should be 1.</returns>
        public LoginLog AddLogEntry(UserModel userModel)
        {
            ClientIdentifier clientIdentifier = Entities.ClientIdentifier.FirstOrDefault(x => x.UserName == userModel.UserName);

            LoginLog loginLog = new LoginLog
                {
                    IpAdress = userModel.IpAdress,
                    TimeStamp = DateTime.Now,
                    UserAgent = userModel.UserAgent,
                    Success = 0

                };

            if (clientIdentifier != null)
            {
                clientIdentifier.LastLogin = DateTime.Now;

                if (clientIdentifier.LoginLog == null)
                           clientIdentifier.LoginLog = new Collection<LoginLog>();

                clientIdentifier.LoginLog.Add(loginLog);
            }

            Entities.SaveChanges();

            return loginLog;
        }
        /// <summary>
        /// Converter.
        /// </summary>
        /// <param name="loginLog"></param>
        /// <returns>LoginLogModel</returns>
        private static LoginLogModel ConvertToLoginLogModel(LoginLog loginLog)
        {
            if (loginLog == null)
                return null;

            return new LoginLogModel
                {
                    IpAdress = loginLog.IpAdress.Trim(),
                    Success = loginLog.Success,
                    TimeStamp = loginLog.TimeStamp,
                    UserAgent = loginLog.UserAgent.Trim(),
                    MobileIpAdress = loginLog.MobileIpAdress == null ? null : loginLog.MobileIpAdress.Trim(),
                    MobileUserAgent = loginLog.MobileUserAgent == null ? null : loginLog.MobileUserAgent.Trim()
                };
        }
        /// <summary>
        /// Sets the success of a login log entry.
        /// </summary>
        /// <param name="model">LoginLog</param>
        /// <param name="success">Boolean if successful</param>
        /// <returns>Number of affected rows. Should be 1.</returns>
        public int SetLoginSuccess(LoginLog model, bool success)
        {
            model.Success = success? 1 : 0;
            model.TimeStamp = DateTime.Now;

            return Entities.SaveChanges();
        }
 /// <summary>
 /// Save a log into db.
 /// </summary>
 /// <param name="loginLog">LoginLog</param>
 /// <returns>Number of affected rows. Should be 1.</returns>
 public int CreateLog(LoginLog loginLog)
 {
     Entities.LoginLog.Add(loginLog);
     return Entities.SaveChanges();
 }
        /// <summary>
        /// Validates a token.
        /// </summary>
        /// <param name="userModel">UserModel</param>
        /// <returns>UserName, if token is valid</returns>
        public static string ValidateToken(UserModel userModel)
        {
            string clientID = userModel.ClientId;
            string token = userModel.Token;

            if (String.IsNullOrEmpty(clientID) || String.IsNullOrEmpty(token))
                return String.Empty;

            // check for DoS to this clientID
            if (!Repository.CheckForDoS(clientID, userModel.IpAdress))
                return String.Empty;

            // gets the old token from the waitlist
            string tokenFromWaitlist = AuthHandler.GetToken(clientID);

            LoginLog loginLog = AuthHandler.GetLoginLog(clientID);

            // if no value in waitlist
            if (String.IsNullOrEmpty(tokenFromWaitlist))
            {
                // make log entry
                var newlog = new LoginLog()
                    {
                        MobileIpAdress = userModel.IpAdress,
                        MobileUserAgent = userModel.UserAgent,
                        Success = 0,
                        TimeStamp = DateTime.Now,
                        ClientIdentifier = null,
                        UserId = null,
                        ClientId = clientID
                    };
                Repository.CreateLog(newlog);

                return String.Empty;
            }

            if (loginLog == null || String.IsNullOrEmpty(tokenFromWaitlist))
                return String.Empty;

            // Add properties to log
            loginLog.MobileIpAdress = userModel.IpAdress;
            loginLog.MobileUserAgent = userModel.UserAgent;

            Repository.SetLoginSuccess(loginLog, false);

            token = token.ToUpper();

            // check token
            if (!token.Equals(tokenFromWaitlist))
                return String.Empty;

            UserModel model = Repository.GetUserByClientId(clientID);

            if (model == null)
                return String.Empty;

            // Set log to success: true
            Repository.SetLoginSuccess(loginLog, true);
            AuthHandler.RemoveToken(clientID);

            return model.UserName;
        }