Esempio n. 1
0
    def CreateUser(self, username, password, email, sessionID):

        # Check for non-complying username
        HashedUsername = hashed(username.lower(), "USERNAME")
        if (HashedUsername == ""):
            return "Invalid username, may only use alphanumeric characters and _ "

        # Check for non-complying password
        HashedPassword = hashed(password, "PASSWORD")
        if (HashedPassword == ""):
            return "Invalid password, may not use white spaces"

        UserDB = sqlite3.connect(UserDBFile)
        cur = UserDB.cursor()

        # Check if already logged in (sessionID exists)
        cur.execute('SELECT COUNT(*) FROM users WHERE sessionid = "%s";' % sessionID)
        c = cur.fetchone()
        if c[0]>0:
            UserDB.commit()
            UserDB.close()
            return "Already Logged In c=" + str(c)

        # Check if account with matching password already exists
        cur.execute('SELECT COUNT(*) FROM users WHERE user = "******" AND pass = "******";' \
                 % (HashedUsername, HashedPassword) )
        c = cur.fetchone()
        if c[0]>0:
            UserDB.commit()
            UserDB.close()
            return "LogMe"

        # Check if username is already taken
        cur.execute('SELECT COUNT(*) FROM users WHERE user = "******";' % HashedUsername)
        c = cur.fetchone()
        if c[0]>0:
            UserDB.commit()
            UserDB.close()
            return "Username already taken, please select another"

        SanitizedEmail = sanitize (email, "EMAIL")
        Key = HashedPassword.decode('hex')
        BLOCK_SIZE = 32
        EncryptedEmail = encryption(SanitizedEmail, Key, BLOCK_SIZE)

        script = '''INSERT OR IGNORE INTO users
                    (user, pass, email, sessionid) VALUES ("%s", "%s", "%s", "%s");''' \
                    % (HashedUsername, HashedPassword, EncryptedEmail, sessionID)

        cur.execute(script)
        UserDB.commit()
        UserDB.close()

        return ""
Esempio n. 2
0
 def FindUser(self, username, password):
     # Will search user database for a user with matching Username and Password
     # and will return the sessionID if found (or "" if not)
     HashedUsername = hashed(username.lower(), "USERNAME")
     HashedPassword = hashed(password, "PASSWORD")
     if (HashedUsername == "") or (HashedPassword == ""):
         return ""
     UserDB = sqlite3.connect(UserDBFile)
     cur = UserDB.cursor()
     cur.execute('SELECT sessionid FROM users WHERE user = "******" AND pass = "******";' \
              % (HashedUsername, HashedPassword) )
     allrows = cur.fetchall()
     UserDB.commit()
     UserDB.close()
     if len(allrows) == 0:
         return ""
     else:
         return allrows[0][0]