PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/webserver/website/core/loginhelper.py

https://github.com/hughperkins/springgrid
Python | 198 lines | 170 code | 6 blank | 22 comment | 2 complexity | ffa275bae539930b160e5ed72eb90b71 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. # Copyright Hugh Perkins 2004, 2009
  2. # hughperkins@gmail.com http://manageddreams.com
  3. #
  4. # This program is free software; you can redistribute it and/or modify it
  5. # under the terms of the GNU General Public License as published by the
  6. # Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. # more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program in the file licence.txt; if not, write to the
  16. # Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. # 1307 USA
  18. # You can find the licence also on the web at:
  19. # http://www.opensource.org/licenses/gpl-license.php
  20. #
  21. # functions for login, cookies etc...
  22. import Cookie
  23. import random
  24. import cgi
  25. import string
  26. import os
  27. import os.path
  28. import md5
  29. from sqlalchemy.orm import join
  30. from utils import *
  31. import sqlalchemysetup
  32. import tableclasses
  33. # from tableclasses import *
  34. gusername = "" # first call loginhelper.processCookie(). If the user
  35. # is already logged in after that, then gusername will no
  36. # longer be blank
  37. # testing gusername != '' is sufficient to check if the user
  38. # is logged in
  39. loginhtml = ""
  40. cookiereference = ''
  41. cookie = Cookie.SimpleCookie()
  42. saltlength = 200
  43. def GenerateRef():
  44. return stringhelper.getRandomAlphaNumericString(40)
  45. def hasPassword():
  46. account = sqlalchemysetup.session.query(tableclasses.Account).filter( tableclasses.Account.username == gusername ).first()
  47. if account == None:
  48. return False
  49. if account.passwordinfo == None:
  50. return False
  51. return True
  52. def isLoggedOn():
  53. global gusername
  54. return ( gusername != '')
  55. def getUsername():
  56. global gusername
  57. return gusername
  58. # returns a salt string
  59. def createSalt():
  60. global saltlength
  61. return stringhelper.getRandomPrintableString(saltlength)
  62. # returns True if password correct, otherwise false
  63. def validateUsernamePassword( username, password ):
  64. global authmethod
  65. account = sqlalchemysetup.session.query(tableclasses.Account).filter( tableclasses.Account.username == username ).first()
  66. if account == None:
  67. return False
  68. if account.passwordinfo == None:
  69. return False
  70. result = account.passwordinfo.checkPassword( password )
  71. return result
  72. def logonUserWithAuthenticatedOpenID( openidurl ):
  73. global gusername
  74. global loginhtml
  75. global cookie
  76. global cookiereference
  77. global authmethod
  78. cookiereference = GenerateRef()
  79. cookie = Cookie.SimpleCookie()
  80. cookie["cookiereference"] = cookiereference
  81. account = None
  82. # note: this could be optimized a little...
  83. for thisaccount in sqlalchemysetup.session.query(tableclasses.Account):
  84. for openid in thisaccount.openids:
  85. if openid.openid == openidurl:
  86. account = thisaccount
  87. if account == None:
  88. # create new account
  89. account = tableclasses.Account(openidurl, openidurl )
  90. account.openids.append( tableclasses.OpenID( openidurl ) )
  91. sqlalchemysetup.session.add( account )
  92. cookierow = tableclasses.Cookie( cookiereference, account )
  93. sqlalchemysetup.session.add(cookierow)
  94. sqlalchemysetup.session.commit()
  95. gusername = account.username
  96. loginhtml = "<p>Logged in as: " + gusername + "</p>"
  97. def logonUserWithPassword(username, password):
  98. global gusername
  99. global loginhtml
  100. global cookie
  101. global cookiereference
  102. gusername = ""
  103. if not validateUsernamePassword( username, password ):
  104. loginhtml = "<h4>Logon error: Please check your username and password.</h4>"
  105. return
  106. cookiereference = GenerateRef()
  107. cookie = Cookie.SimpleCookie()
  108. cookie["cookiereference"] = cookiereference
  109. accountrow = sqlalchemysetup.session.query(tableclasses.Account).filter(tableclasses.Account.username == username ).first()
  110. if accountrow == None:
  111. loginhtml = "<h4>Logon error: Please check your username and password.</h4>"
  112. return
  113. cookierow = tableclasses.Cookie( cookiereference, accountrow )
  114. sqlalchemysetup.session.add(cookierow)
  115. sqlalchemysetup.session.commit()
  116. gusername = username
  117. loginhtml = "<p>Logged in as: " + gusername + "</p>"
  118. def changePassword( username, password ):
  119. account = sqlalchemysetup.session.query(tableclasses.Account).filter( tableclasses.Account.username == username ).first()
  120. account.passwordinfo.changePassword( password )
  121. sqlalchemysetup.session.commit()
  122. return True
  123. # can use commandline arguments to login, or just use querystring
  124. # if there is a cookie, uses that
  125. def processCookie():
  126. global cookie, cookiereference, gusername, loginhtml
  127. gusername = ''
  128. cookie = Cookie.SimpleCookie( os.environ.get("HTTP_COOKIE"))
  129. c = cookie.output( "Cookie: " )
  130. if(not c):
  131. # check query string, and if username and password are ok, set gusername from that
  132. if formhelper.getValue('username') != '' and formhelper.getValue('password') != '':
  133. if validateUsernamePassword( formhelper.getValue('username'), formhelper.getValue('password') ):
  134. gusername = formhelper.getValue('username')
  135. return
  136. if not cookie.has_key( "cookiereference" ):
  137. return
  138. cookiereference = str( cookie["cookiereference"].value )
  139. cookierow = sqlalchemysetup.session.query(tableclasses.Cookie).filter(tableclasses.Cookie.cookiereference == cookiereference ).first()
  140. if cookierow == None:
  141. return
  142. # Note: could consider migrating from username string to account object
  143. gusername = cookierow.account.username
  144. if gusername == '':
  145. return
  146. loginhtml = "<p>Logged in as: " + gusername + "</p>"
  147. def logoutUser():
  148. global cookie, cookiereference, gusername, loginhtml
  149. cookierow = sqlalchemysetup.session.query(tableclasses.Cookie).filter(tableclasses.Cookie.cookiereference == cookiereference ).first()
  150. if cookierow != None:
  151. sqlalchemysetup.session.delete(cookierow)
  152. sqlalchemysetup.session.commit()
  153. cookiereference = '0'
  154. cookie = Cookie.SimpleCookie()
  155. gusername = ""
  156. loginhtml = ""