Attachment 'IMAPAuth.py'

Download

   1 #-*- encoding: utf-8 -*-
   2 
   3 # IMAP auth for MoinMoin, by Cléber Zavadniak
   4 # Based on DummyAuth, by RadomirDopieralski
   5 # (http://moinmo.in/AuthMarket?action=AttachFile&do=view&target=DummyAuth.py)
   6 # (http://moinmo.in/RadomirDopieralski)
   7 #
   8 # Usage: add these lines to your wikiconfig.py:
   9 #
  10 #   from IMAPAuth import IMAPAuth
  11 #   auth = [IMAPAuth(u'stmp.yourserver.com')] 
  12 
  13 import imaplib
  14 
  15 from MoinMoin import user
  16 from MoinMoin.auth import BaseAuth, CancelLogin, ContinueLogin
  17 
  18 class IMAPAuth(BaseAuth):
  19     """Authenticate users if they authenticate at some IMAP server."""
  20 
  21     name = 'imap'
  22     logout_possible = True
  23     login_inputs = ['username', 'password']
  24 
  25     def __init__(self, host, port = 143):
  26         self.host = host
  27         self.port = port
  28 
  29     def login_plain (self, imap, user, password, authuser = None):
  30         def plain_callback (response):
  31             if authuser is None:
  32                 return "%s\x00%s\x00%s" % ( user , user , password )
  33             else:
  34                 return "%s\x00%s\x00%s" % ( user , authuser , password )
  35         
  36         try:
  37             return imap.authenticate ('PLAIN', plain_callback)
  38         except imap.error as e:
  39             print 'imap error:', e
  40             return False
  41 
  42 
  43     def authenticate (self, request, username, password):
  44 
  45         imap = imaplib.IMAP4(self.host, self.port)
  46         #print
  47         #print 'imap capabilities:', imap.capabilities
  48         resposta = self.login_plain(imap, username, password)
  49 
  50         if resposta and resposta[0] == 'OK' and 'Logged in' in resposta[1]:
  51 
  52             # Cuts off the @server part of the e-mail address:
  53             clear_username = username.split('@')[0]
  54 
  55             u = user.User(request, auth_username=clear_username, auth_method=self.name, auth_attribs=('username', 'password',))
  56             u.name = clear_username
  57             u.remember_me = 0 # 0 enforces cookie_lifetime config param
  58 
  59             u.create_or_update(True)
  60 
  61             return u
  62 
  63         else:
  64             return False # Invalid credentials!
  65 
  66     def login(self, request, user_obj, **kw):
  67 
  68         _ = request.getText
  69 
  70         username = kw.get('username')
  71         password = kw.get('password')
  72 
  73         if user_obj:
  74             pass # Do we have anything to do in this case?
  75         else:
  76             u = self.authenticate(request, username, password)
  77             if u == False:
  78                 return CancelLogin(_("Invalid username or password."))
  79             else:
  80                 return ContinueLogin(u)
  81 
  82         return ContinueLogin(user_obj)
  83 
  84     def logout(self, request, user_obj, **kw):
  85         # Faz nada.
  86         user_obj.valid = False
  87         return user_obj, False

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-11-13 11:58:37, 2.6 KB) [[attachment:IMAPAuth.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.