def add_wins(username): db, c = config.start_db() wins = get_wins(username) wins += 1 command = "UPDATE users SET wins = ? WHERE username = ?;" c.execute(command, (wins, username)) config.end_db(db)
def get_losses(username): db, c = config.start_db() command = "SELECT losses FROM users WHERE username = ?;" c.execute(command, (username, )) losses = c.fetchone()[0] config.end_db(db) return losses
def add_losses(username): db, c = config.start_db() losses = get_losses(username) losses += 1 command = "UPDATE users SET losses = ? WHERE username = ?;" c.execute(command, (losses, username)) config.end_db(db)
def auth_user(username, password): db, c = config.start_db() data = c.execute("SELECT * FROM users") for row in data: if row[0] == username and sha256_crypt.verify(password, row[1]): db.close() return True config.end_db(db) return False
def all_users(): db, c = config.start_db() command = "SELECT username, password FROM users;" c.execute(command) all = c.fetchall() config.end_db(db) dict = {} for item in all: dict[item[0]] = item[1] return dict
def add_user(username, password, wins=0, losses=0): db, c = config.start_db() data = c.execute("SELECT * FROM users;") for row in data: if username == row[1]: return False command = "INSERT INTO users(username,password,wins,losses)VALUES(?,?,?,?);" c.execute(command,(username,sha256_crypt.hash(password),wins,losses)) config.end_db(db) return True
def user_exists(username): """Returns whether user `username` exists""" db, c = config.start_db() # Check whether there is a row in 'users' where the column 'username' has # the value of `username` c.execute('SELECT EXISTS(SELECT 1 FROM users WHERE username=? LIMIT 1)', (username, )) result = c.fetchone()[0] # 1 if user exists, else 0 config.end_db(db) return result == 1
def auth_user(username, password): db, c = config.start_db() command = "SELECT password FROM users WHERE username = ?;" c.execute(command, (username, )) pw = c.fetchone()[0] if sha256_crypt.verify(password, pw): config.end_db(db) return True config.end_db(db) return False
def get_wins_losses_user(username): db, c = config.start_db() command = "SELECT username,wins,losses FROM users WHERE username = ?;" c.execute(command, (username, )) all = c.fetchall() config.end_db(db) users = [] for item in all: users.append({'username': item[0], 'wins': item[1], 'losses': item[2]}) return users[0]
def get_wins_losses(): db, c = config.start_db() command = "SELECT username,wins,losses FROM users;" c.execute(command) all = c.fetchall() config.end_db(db) users = [] for item in all: #dict[item[0]] = [item[1],item[2]] users.append({'username': item[0], 'wins': item[1], 'losses': item[2]}) return users
def registered(username): db, c = config.start_db() command = "SELECT * FROM users;" c.execute(command) data = c.fetchall() print(data) for row in data: if username == row[0]: return False config.end_db(db) return True
def create_table(): """Creates the SQLite database 'users'""" db, c = config.start_db() try: c.execute('''CREATE TABLE users ( username TEXT PRIMARY KEY, pass_hash BLOB NOT NULL, salt BLOB NOT NULL, karma INT NOT NULL )''') except sqlite3.OperationalError: # Table already exists pass config.end_db(db)
def add_user(username, password): """Add user `username` with password `passowrd` to database""" db, c = config.start_db() if not valid_username(username): return False if not valid_password(password): return False salt = get_salt() pass_hash = hash_pass(password, salt) c.execute('INSERT INTO users VALUES (?, ?, ?, 0)', (username, pass_hash, salt)) config.end_db(db) return True
def all_users(): ''' returns a dictionary with usernames + hashes passwords ''' db, c = config.start_db() command = "SELECT username, password FROM users;" c.execute(command) all_users = c.fetchall() config.end_db(db) users = {} for item in all_users: users[item[0]] = item[1] return users
def add_user(username, password): ''' was this user added to TABLE users? ''' db, c = config.start_db() data = c.execute("SELECT * FROM users;") for row in data: # is this username available if username == row[1]: config.end_db(db) return False command = "INSERT INTO users (username,password) VALUES (?,?);" c.execute(command, (username, sha256_crypt.hash(password))) config.end_db(db) return True
def auth_user(username, password): """Return wihether user `username` exists with password `password`""" db, c = config.start_db() c.execute('SELECT pass_hash, salt FROM users WHERE username=? LIMIT 1', (username, )) result = c.fetchone() config.end_db(db) if result is None: return False pass_hash, salt = result if hmac.compare_digest(pass_hash, hash_pass(password, salt)): return True else: return False
def remove_user(username): """Remove user `username` from database""" db, c = config.start_db() c.execute('DELETE FROM users WHERE username=?', (username, )) config.end_db(db)