auth.py :  » Email » BoboMail » bobomail » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Email » BoboMail 
BoboMail » bobomail » auth.py
"""Authentication module for BoboMail"""
import os

from session import Session
from user import User
from bobomailrc import DEBUG,true,false
from lib import connections


class Authentication:
  "more or less a abstract base class"
  
  def __init__(self, session):
    self.session = session

  def check(self, host, userid, password):
    return false # dummy-method  -  has to be overwritten

  def relogin(self):
    try:
      self.user = User(self.session.user)
      return true
    except:
      if DEBUG: raise
      else: return false

  def login(self, host, userid, password):
    if self.check(host, userid, password):
      self.user = User(userid, host)
      self.user.host = host
      self.user.id = userid
      self.session.user = self.user.key
      self.session.password = password
      return true
    else:
      if DEBUG: raise
      else: return false

  def logout(self):
    try: 
      self.session.expire()
      if hasattr(self, "con"):
        connections.close(self.con)
    except:
      if DEBUG: raise

  

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 lib.pop3lib import POP3X
class POP3Authentication(Authentication):
  "authenticate against a pop server"

  def check(self, host, userid, password):
    try:
      self.con = connections.get(userid, password, host, "pop3")
      if self.con:
        connections.close(self.con)
      c = POP3X(host)
      self.con = connections.register(
        c, userid, password, host, "pop3", c.quit)
      self.con.query("user", (userid,))
      self.con.query("pass_", (password,))
      return true
    except:
      if self.con:
        connections.close(self.con)
      if DEBUG: raise
      else: return false
      


from imaplib import IMAP4
class IMAP4Authentication(Authentication):
  "authenticate against a imap server"
  
  def check(self, host, userid, password):
    try:
      self.con = connections.get(userid, password, host, "imap4")
      if self.con:
        connections.close(self.con)
      c = IMAP4(host)
      self.con = connections.register(
        c, userid, password, host, "imap4", c.logout)
      self.con.query("login", (userid, password))
      return true
    except: 
        if DEBUG: raise
        else: return false
    
#XXX TODO:
#class APOP3Authentication(Authentication): pass
#class PasswdFileAuthentication(Authentication): pass
#class HtAccessAuthentication(Authentication): pass

if __name__ == "__main__":
  a = IMAP4Authentication()
  print a.login("localhost", "guest", "guest")
  
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.