auth.py
上传用户:gyjinxi
上传日期:2007-01-04
资源大小:159k
文件大小:3k
- # Authentication module for DogMail
- import md5, shelve, time, os, string
- from bobomailrc import *
- from base64 import encodestring
- from mimetools import choose_boundary
- import dumbdbm #XXX: fix anydbm for some systems
- class AuthDB(shelve.Shelf):
- def __init__(self, fn=auth_db_filename):
- shelve.Shelf.__init__(self, dumbdbm.open(fn))
- #os.chmod(fn, 384)
- self.expire()
-
- def add(self, user):
- self[user["hash"]] = user
- def get(self, auth):
- return self[auth]
- def expire(self):
- for x in self.keys():
- if self[x]["expires"] < time.time(): del self[x]
- authdb = AuthDB()
- class Authentication:
-
- def __init__(self, db=authdb):
- self.db = db
- def relogin(self, auth_key):
- try:
- user = self.db.get(auth_key)
- if user["remote"] == os.environ.get("REMOTE_IP", None):
- #if self.check(user["host"], user["userid"], user["password"]):
- self.acquire(user)
- return true
- except: pass
- return false
- def acquire(self, user):
- for key, value in user.items():
- self.__dict__[key] = value
- def check(self, host, userid, password): return false
- def login(self, host, userid, password):
- if self.check(host, userid, password):
- try:
- hash = md5.new()
- hash.update(userid)
- hash.update(password)
- hash.update(choose_boundary())
- key = map(hex, map(ord, hash.digest()))
- key = string.replace(string.join(key, ""), "0x", "")
- user = {
- "host": host,
- "userid": userid,
- "password": password,
- "hash": key,
- "remote": os.environ.get("REMOTE_IP", None),
- "expires": time.time() + max_session_length
- }
- self.db.add(user)
- self.acquire(user)
- return true
- except: pass
- return false
- def logout(self):
- try: del self.db[self.hash]
- except: pass
- class SimpleAuthentication(Authentication):
- atable = {
- "guest": "guest"
- }
- def check(self, host, userid, password):
- if self.atable.get(userid, None) == password:
- return true
- else:
- return false
- from poplib import POP3
- class POP3Authentication(Authentication):
- def check(self, host, userid, password):
- try:
- self.con = POP3(host)
- self.con.user(userid)
- self.con.pass_(password)
- self.con.quit()
- return true
- except: return false
-
- #XXX TODO:
- #class APOP3Authentication(Authentication): pass
- #class PasswdFileAuthentication(Authentication): pass
- #class HtAccessAthentication(Authentication): pass
- #if __name__ == "__main__":
- # a = POP3Authentication()
- # print a.login("localhost", "guest", "guest")
-