Guest User

Untitled

a guest
May 26th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. def auth_login(H,G,P): #(Headers, Get_DATA, POST_DATA)
  2.  
  3. #accounts = [('test','test'),('test2','testy')]
  4.  
  5. header = []
  6.  
  7. #checkconn = sqlite3.connect('webserve-info.db')
  8.  
  9. c = conn.cursor()
  10.  
  11.  
  12.  
  13. try:
  14.  
  15. if P['username'] and P['password']:
  16.  
  17. username = P['username'][0]
  18.  
  19. password = P['password'][0]
  20.  
  21. except Exception, g:
  22.  
  23. return 200,[], '<h1> %s not provided </h1>' % (g)
  24.  
  25.  
  26.  
  27. c.execute("SELECT id FROM users WHERE username=? AND password = ?",(username,password))
  28.  
  29. accounts = c.fetchone()
  30.  
  31.  
  32. print (accounts,)
  33.  
  34.  
  35. if accounts:
  36. hashv = generate_session_id(username)
  37. c.execute("INSERT INTO sessions (user_id,session_id) VALUES (?,?)",(accounts[0],hashv))
  38. conn.commit()
  39.  
  40. C = Cookie.SimpleCookie()
  41.  
  42. C['session'] = hashv
  43.  
  44. x = C.output()
  45.  
  46. x += '; Path=/'
  47.  
  48. header.insert(len(header), x.split(': ',1))
  49.  
  50. return 200,header,'<h4>Login Succesful!</h4>'
  51.  
  52.  
  53.  
  54.  
  55.  
  56. return 401,[],'<h1> Unauthorized Access</h1>'
  57.  
  58.  
  59.  
  60.  
  61.  
  62. def auth_logout(H,G,P):
  63.  
  64. header = []
  65.  
  66. C = Cookie.SimpleCookie()
  67. for (k, v) in H:
  68. if k.lower() == 'cookie':
  69. C.load(v)
  70.  
  71. c = conn.cursor()
  72. c.execute("""DELETE FROM sessions WHERE session_id = ?""", (C['session'].value,))
  73.  
  74.  
  75. C['session'] = ''
  76.  
  77. x = C.output()
  78.  
  79. x += '; Path=/'
  80.  
  81.  
  82. header.insert(len(header), x.split(': ',1))
  83.  
  84. return 200,header,'<h4> Logout Succesful! </h4>'
  85.  
  86.  
  87.  
  88.  
  89.  
  90. def auth_print(H,G,P):
  91.  
  92. if H:
  93.  
  94. C = Cookie.SimpleCookie()
  95.  
  96. for (k,v) in H:
  97.  
  98. if k.lower() == 'cookie':
  99.  
  100. C.load(v)
  101.  
  102.  
  103.  
  104. if 'session' in C:
  105.  
  106. user = C['session'].value
  107.  
  108. c = conn.cursor()
  109. c.execute("SELECT * FROM users INNER JOIN sessions WHERE users.id = sessions.user_id AND sessions.session_id = ? LIMIT 1", (user,))
  110. user = c.fetchone()
  111.  
  112.  
  113.  
  114. if user:
  115.  
  116. return 200,H,"you are user %s" % (user[1],)
  117.  
  118.  
  119.  
  120. return 200,H,'no user specified'
Add Comment
Please, Sign In to add comment