auth.py
上传用户:gyjinxi
上传日期:2007-01-04
资源大小:159k
文件大小:3k
源码类别:

WEB邮件程序

开发平台:

Python

  1. # Authentication module for DogMail
  2. import md5, shelve, time, os, string
  3. from bobomailrc import *
  4. from base64 import encodestring
  5. from mimetools import choose_boundary
  6. import dumbdbm #XXX: fix anydbm for some systems
  7. class AuthDB(shelve.Shelf):
  8.     def __init__(self, fn=auth_db_filename):
  9.         shelve.Shelf.__init__(self, dumbdbm.open(fn))
  10. #os.chmod(fn, 384)
  11.         self.expire()
  12.     def add(self, user):
  13. self[user["hash"]] = user
  14.     def get(self, auth):
  15. return self[auth]
  16.     def expire(self):
  17. for x in self.keys():
  18. if self[x]["expires"] < time.time(): del self[x]
  19. authdb = AuthDB()
  20. class Authentication:
  21. def __init__(self, db=authdb):
  22. self.db = db
  23. def relogin(self, auth_key):
  24. try:
  25. user = self.db.get(auth_key)
  26. if user["remote"] == os.environ.get("REMOTE_IP", None):
  27. #if self.check(user["host"], user["userid"], user["password"]):
  28. self.acquire(user)
  29. return true
  30. except: pass
  31. return false
  32. def acquire(self, user):
  33. for key, value in user.items():
  34. self.__dict__[key] = value
  35. def check(self, host, userid, password): return false
  36. def login(self, host, userid, password):
  37. if self.check(host, userid, password):
  38. try:
  39. hash = md5.new()
  40. hash.update(userid)
  41. hash.update(password)
  42. hash.update(choose_boundary())
  43. key = map(hex, map(ord, hash.digest()))
  44. key = string.replace(string.join(key, ""), "0x", "")
  45. user = {
  46.                     "host": host,
  47.                     "userid": userid,
  48.                     "password": password,
  49.                     "hash": key,
  50.                     "remote": os.environ.get("REMOTE_IP", None),
  51.                     "expires": time.time() + max_session_length
  52.                     }
  53. self.db.add(user)
  54. self.acquire(user)
  55. return true
  56. except: pass
  57. return false
  58. def logout(self):
  59. try: del self.db[self.hash]
  60. except: pass
  61. class SimpleAuthentication(Authentication):
  62. atable = {
  63. "guest": "guest"
  64. }
  65. def check(self, host, userid, password):
  66. if self.atable.get(userid, None) == password:
  67. return true
  68. else:
  69. return false
  70. from poplib import POP3
  71. class POP3Authentication(Authentication):
  72. def check(self, host, userid, password):
  73. try:
  74. self.con = POP3(host)
  75. self.con.user(userid)
  76. self.con.pass_(password)
  77. self.con.quit()
  78. return true
  79. except: return false
  80. #XXX TODO:
  81. #class APOP3Authentication(Authentication): pass
  82. #class PasswdFileAuthentication(Authentication): pass
  83. #class HtAccessAthentication(Authentication): pass
  84. #if __name__ == "__main__":
  85. # a = POP3Authentication()
  86. # print a.login("localhost", "guest", "guest")