in reply to RFC: Net::LDAP::Simple
I am uncertain how much simpler you could make Net::LDAP to cope with, with perhaps the exception of the $mesg->code && die $mesg->error dance. I have some existing Net::LDAP code, from a CGI application I am prototyping, which bundles the LDAP access politely away from the main code. I have added more verbose comments to describe it, and it appears thus.
sub authenticate { # instance method, $self is a blessed hash holding various # important details like ldaphost etc. my $self = shift; # $q , a CGI->new query object my $q = shift; # Open an anonymous ldap session (anon reads are allowed) my $ldap = Net::LDAP->new( $ldaphost ) or die "LDAP Connection error" +; $ldap->bind; my $user = $q->param('username'); my $mesg = $ldap->search ( base=>$self->{ldap}{userbase}, filter=>"(&(cn=$user))" ); # one day these dies will be calls to pretty printed # html . mymodule::error->database_error() $mesg->code && die $mesg->error; $ldap->unbind; # Dodgy, I admit : we only expect one account with the uid eq $user my $entry = $mesg->shift_entry; # Bailout if user does not exist in LDAP. return undef unless ($entry); my $ldaphash = $entry->get_value('userPassword'); my $ldapuser = $entry->get_value('uid'); # hash the CGI supplied password to compare with LDAP userPassword my $md5 = Digest::MD5->new; $md5->add( $q->param('phrase') ); my $hash = '{MD5}' . encode_base64($md5->digest, ''); if ( ( $q->param('username') eq $ldapuser) and ($hash eq $ldaphash) ) { my $sessionid = $self->start_session( $q ); return $sessionid; } else { return undef } }
I can see where you're coming from , however rewriting this to use Net::LDAP::Simple , feels more like I'm shuffling the args to different methods rather than simplifying the code. I think search paramaters belong with search methods, not in the constructor.
sub authenticate { my $self = shift; my $q = shift; # Using Net::LDAP::Simple my $ldap = Net::LDAP::Simple->new( host=>$ldaphost , base=>$self->{ldap}{userbase} , searchattrs=>'uid' ) or die "LDAP Connection error"; my $user = $q->param('username'); my $result = $ldap->simplesearch( $user ); die $ldap->error unless $result; $ldap->unbind; my $entry = shift @{$result}; # Bailout if user does not exist in LDAP. return undef unless ($entry); my $ldaphash = $entry->get_value('userPassword'); my $ldapuser = $entry->get_value('uid'); my $md5 = Digest::MD5->new; $md5->add( $q->param('phrase') ); my $hash = '{MD5}' . encode_base64($md5->digest, ''); if ( ( $q->param('username') eq $ldapuser) and ($hash eq $ldaphash) ) { my $sessionid = $self->start_session( $q ); return $sessionid; } else { return undef } }
Please forgive me if I have misunderstood your approach, and for goodness sake keep working on the idea. Collecting peoples ideas RE what would make LDAP simpler to use for them might be a good start. I have considered writing some meta-methods to do commonplace things like move and rename, I reckon your ideas there are spot on. I watch with interest
-toaster
I can't believe it's not psellchecked
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: RFC: Net::LDAP::Simple
by bronto (Priest) on Apr 15, 2003 at 12:51 UTC | |
Re: Re: RFC: Net::LDAP::Simple
by kennethwlangley (Novice) on Apr 15, 2003 at 15:22 UTC | |
by submersible_toaster (Chaplain) on Apr 16, 2003 at 00:46 UTC | |
by kennethwlangley (Novice) on Apr 17, 2003 at 17:31 UTC |