/lib/fiveserver/py/fiveserver/register.py

https://bitbucket.org/juce/fiveserver · Python · 156 lines · 132 code · 16 blank · 8 comment · 17 complexity · 72b05ec506c28ea2dbbe8e3a5f1428a7 MD5 · raw file

  1. from twisted.application import service, internet
  2. from twisted.web import static, server, resource
  3. from xml.sax.saxutils import escape
  4. from Crypto.Cipher import Blowfish
  5. from fiveserver import log
  6. from fiveserver.model import util
  7. import binascii
  8. XML_HEADER = """<?xml version="1.0" encoding="UTF-8"?>
  9. <?xml-stylesheet type="text/xsl" href="/xsl/style.xsl"?>
  10. """
  11. def getFormContent(webDir):
  12. try:
  13. f = open('%s/form.html' % webDir)
  14. except IOError:
  15. f = open('%s/form-sample.html' % webDir)
  16. return f.read()
  17. def getResultContent(webDir):
  18. try:
  19. f = open('%s/result.html' % webDir)
  20. except IOError:
  21. f = open('%s/result-sample.html' % webDir)
  22. return f.read()
  23. class RegistrationResource(resource.Resource):
  24. isLeaf = True
  25. def __init__(self, config, webDir):
  26. self.xsl = open('%s/style.xsl' % webDir).read()
  27. self.config = config
  28. self.webDir = webDir
  29. self.cipher = Blowfish.new(binascii.a2b_hex(self.config.cipherKey))
  30. def render_GET(self, request):
  31. if request.path == '/xsl/style.xsl':
  32. request.setHeader('Content-Type','text/xml')
  33. return self.xsl
  34. elif request.path.startswith('/modifyUser/'):
  35. def _found(results):
  36. if not results:
  37. username,nonce,serial = '','',''
  38. else:
  39. usr = results[0]
  40. username,nonce,serial = usr.username,usr.nonce,usr.serial
  41. s = getFormContent(self.webDir)
  42. s = s.decode('utf-8') % {'username':username,
  43. 'nonce':nonce,
  44. 'serial':serial}
  45. request.write(s.encode('utf-8'))
  46. request.finish()
  47. request.setHeader('Content-Type','text/html')
  48. nonce = request.path.split('/')[-1]
  49. d = self.config.userData.findByNonce(nonce)
  50. d.addCallback(_found)
  51. return server.NOT_DONE_YET
  52. elif request.path == '/md5.js':
  53. request.setHeader('Content-Type','text/javascript')
  54. return open('%s/md5.js' % self.webDir).read()
  55. else:
  56. request.setHeader('Content-Type','text/html')
  57. s = getFormContent(self.webDir)
  58. s = s.decode('utf-8') % {'username':'','nonce':'','serial':''}
  59. return s.encode('utf-8')
  60. def sendHtmlResponse(self, request, message):
  61. request.setHeader('Content-Type','text/html')
  62. s = getResultContent(self.webDir)
  63. s = s.decode('utf-8') % {'result': message}
  64. return s.encode('utf-8')
  65. def sendXmlResponse(self, request, message):
  66. request.setHeader('Content-Type','text/xml')
  67. s = '%s<result text="%s" />' % (XML_HEADER, message)
  68. return s.encode('utf-8')
  69. def sendResponse(self, fmt, request, message):
  70. if fmt == 'html':
  71. return self.sendHtmlResponse(request, message)
  72. return self.sendXmlResponse(request, message)
  73. def render_POST(self, request):
  74. def _created(usr):
  75. request.write(self.sendResponse(
  76. fmt, request, 'SUCCESS: Registration complete'))
  77. request.finish()
  78. def _failed(error):
  79. log.msg('ERROR: %s' % str(error.value))
  80. request.setResponseCode(500)
  81. request.write(self.sendResponse(
  82. fmt, request, 'ERROR: Unable to register: server errror'))
  83. request.finish()
  84. def _modifyUser(results, serial, username, hash, nonce):
  85. if not results:
  86. request.setResponseCode(404)
  87. request.write(
  88. self.sendResponse(
  89. fmt, request,
  90. 'ERROR: Cannot modify user: invalid nonce'))
  91. request.finish()
  92. return
  93. d = self.config.createUser(username, serial, hash, nonce)
  94. d.addCallback(_created)
  95. d.addErrback(_failed)
  96. return d
  97. def _createNew(results, serial, username, hash):
  98. if results:
  99. request.setResponseCode(409)
  100. request.write(
  101. self.sendResponse(
  102. fmt, request,
  103. 'ERROR: Cannot register: username taken'))
  104. request.finish()
  105. return
  106. d = self.config.createUser(username, serial, hash, None)
  107. d.addCallback(_created)
  108. d.addErrback(_failed)
  109. return d
  110. serial = request.args['serial'][0]
  111. username = request.args['user'][0]
  112. hash = request.args['hash'][0]
  113. nonce = request.args['nonce'][0]
  114. try: fmt = request.args['format'][0]
  115. except: fmt = None
  116. #userKey = '%s-%s' % (
  117. # binascii.b2a_hex(
  118. # self.cipher.encrypt(util.padWithZeros(serial,24))),
  119. # binascii.b2a_hex(
  120. # self.cipher.encrypt(binascii.a2b_hex(hash))))
  121. #print 'userKey: {%s}' % userKey
  122. hash = binascii.b2a_hex(self.cipher.encrypt(binascii.a2b_hex(hash)))
  123. log.msg('userHash: {%s}' % hash)
  124. request.setHeader('Content-Type','text/xml')
  125. if self.config.isBanned(request.getClientIP()):
  126. request.setResponseCode(403)
  127. return self.sendResponse(
  128. fmt, request, 'ERROR: Cannot register: your IP is banned')
  129. elif nonce in [None,'']:
  130. # create new
  131. d = self.config.userData.findByUsername(username)
  132. d.addCallback(_createNew, serial, username, hash)
  133. d.addErrback(_failed)
  134. return server.NOT_DONE_YET
  135. else:
  136. # modify existing
  137. d = self.config.userData.findByNonce(nonce)
  138. d.addCallback(_modifyUser, serial, username, hash, nonce)
  139. d.addErrback(_failed)
  140. return server.NOT_DONE_YET