<s> from nagare import log <EOL> try : <EOL> import ldap <EOL> except ImportError : <EOL> ldap = None <EOL> import sys <EOL> import types <EOL> def toUTF8 ( v ) : <EOL> if isinstance ( v , unicode ) : <EOL> return v . encode ( '<STR_LIT:utf-8>' ) <EOL> elif isinstance ( v , ( types . TupleType , types . ListType ) ) : <EOL> return [ toUTF8 ( e ) for e in v ] <EOL> elif isinstance ( v , types . DictType ) : <EOL> return dict ( [ ( toUTF8 ( k ) , toUTF8 ( v_ ) ) for k , v_ in v . items ( ) ] ) <EOL> else : <EOL> return v <EOL> class LDAPAuth ( object ) : <EOL> def __init__ ( self , ldap_cfg ) : <EOL> ldap_cfg = toUTF8 ( ldap_cfg ) <EOL> self . server = "<STR_LIT>" % ( ldap_cfg [ '<STR_LIT:host>' ] , ldap_cfg [ '<STR_LIT:port>' ] ) <EOL> self . users_base_dn = ldap_cfg [ '<STR_LIT>' ] <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ldap , '<STR_LIT>' <EOL> return ldap . initialize ( self . server ) <EOL> def get_user_dn ( self , uid ) : <EOL> raise NotImplementedError ( ) <EOL> def check_password ( self , uid , password ) : <EOL> """<STR_LIT>""" <EOL> c = self . connect ( ) <EOL> dn = self . get_user_dn ( uid ) <EOL> try : <EOL> c . simple_bind_s ( dn , password . encode ( '<STR_LIT>' ) ) <EOL> return True <EOL> except ldap . INVALID_CREDENTIALS : <EOL> log . info ( "<STR_LIT>" % dn ) <EOL> except ldap . SERVER_DOWN : <EOL> log . critical ( "<STR_LIT>" ) <EOL> finally : <EOL> c . unbind ( ) <EOL> def get_profile ( self , uid , password ) : <EOL> raise NotImplementedError ( ) <EOL> class NngLDAPAuth ( LDAPAuth ) : <EOL> def get_user_dn ( self , uid ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( ldap . dn . escape_dn_chars ( toUTF8 ( uid ) ) , self . users_base_dn ) <EOL> def get_profile ( self , uid , password ) : <EOL> c = self . connect ( ) <EOL> ldap_result = c . search_s ( self . get_user_dn ( uid ) , ldap . SCOPE_BASE ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> profile = { } <EOL> profile [ '<STR_LIT>' ] = ldap_result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> profile [ '<STR_LIT:name>' ] = ldap_result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . decode ( '<STR_LIT:utf-8>' ) <EOL> profile [ '<STR_LIT:email>' ] = ldap_result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> profile [ '<STR_LIT>' ] = ldap_result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] if '<STR_LIT>' in ldap_result else None <EOL> return profile <EOL> class ADLDAPAuth ( LDAPAuth ) : <EOL> def connect ( self ) : <EOL> conn = super ( ADLDAPAuth , self ) . connect ( ) <EOL> conn . set_option ( ldap . OPT_REFERRALS , <NUM_LIT:0> ) <EOL> conn . protocol_version = <NUM_LIT:3> <EOL> return conn <EOL> def check_password ( self , uid , password ) : <EOL> """<STR_LIT>""" <EOL> c = self . connect ( ) <EOL> try : <EOL> c . simple_bind_s ( uid , password ) <EOL> return True <EOL> except ldap . INVALID_CREDENTIALS : <EOL> log . info ( "<STR_LIT>" % uid ) <EOL> except ldap . SERVER_DOWN : <EOL> log . critical ( "<STR_LIT>" ) <EOL> finally : <EOL> c . unbind ( ) <EOL> def get_profile ( self , uid , password ) : <EOL> c = self . connect ( ) <EOL> c . simple_bind_s ( uid , password ) <EOL> ldap_result = c . search_s ( self . users_base_dn , ldap . SCOPE_SUBTREE , <EOL> '<STR_LIT>' % ldap . dn . escape_dn_chars ( toUTF8 ( uid ) ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> profile = { } <EOL> profile [ '<STR_LIT>' ] = ldap_result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> profile [ '<STR_LIT:name>' ] = ldap_result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . decode ( '<STR_LIT:utf-8>' ) <EOL> profile [ '<STR_LIT:email>' ] = ldap_result . get ( '<STR_LIT>' , [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> profile [ '<STR_LIT>' ] = ldap_result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] if '<STR_LIT>' in ldap_result else None <EOL> c . unbind ( ) <EOL> return profile </s>
