Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def auth_login(H,G,P): #(Headers, Get_DATA, POST_DATA)
- #accounts = [('test','test'),('test2','testy')]
- header = []
- #checkconn = sqlite3.connect('webserve-info.db')
- c = conn.cursor()
- try:
- if P['username'] and P['password']:
- username = P['username'][0]
- password = P['password'][0]
- except Exception, g:
- return 200,[], '<h1> %s not provided </h1>' % (g)
- c.execute("SELECT id FROM users WHERE username=? AND password = ?",(username,password))
- accounts = c.fetchone()
- print (accounts,)
- if accounts:
- hashv = generate_session_id(username)
- c.execute("INSERT INTO sessions (user_id,session_id) VALUES (?,?)",(accounts[0],hashv))
- conn.commit()
- C = Cookie.SimpleCookie()
- C['session'] = hashv
- x = C.output()
- x += '; Path=/'
- header.insert(len(header), x.split(': ',1))
- return 200,header,'<h4>Login Succesful!</h4>'
- return 401,[],'<h1> Unauthorized Access</h1>'
- def auth_logout(H,G,P):
- header = []
- C = Cookie.SimpleCookie()
- for (k, v) in H:
- if k.lower() == 'cookie':
- C.load(v)
- c = conn.cursor()
- c.execute("""DELETE FROM sessions WHERE session_id = ?""", (C['session'].value,))
- C['session'] = ''
- x = C.output()
- x += '; Path=/'
- header.insert(len(header), x.split(': ',1))
- return 200,header,'<h4> Logout Succesful! </h4>'
- def auth_print(H,G,P):
- if H:
- C = Cookie.SimpleCookie()
- for (k,v) in H:
- if k.lower() == 'cookie':
- C.load(v)
- if 'session' in C:
- user = C['session'].value
- c = conn.cursor()
- c.execute("SELECT * FROM users INNER JOIN sessions WHERE users.id = sessions.user_id AND sessions.session_id = ? LIMIT 1", (user,))
- user = c.fetchone()
- if user:
- return 200,H,"you are user %s" % (user[1],)
- return 200,H,'no user specified'
Add Comment
Please, Sign In to add comment