Browse Source

+ LDAP->chpass

master
Alex 'AdUser' Z 10 years ago
parent
commit
6a6be059a1
  1. 18
      lib/LDV/LDAP.pm
  2. 8
      t/LDAP.t

18
lib/LDV/LDAP.pm

@ -6,6 +6,7 @@ use utf8;
use Net::LDAP; use Net::LDAP;
use Net::LDAP::Util qw(ldap_error_name); use Net::LDAP::Util qw(ldap_error_name);
use Crypt::SaltedHash;
sub new { sub new {
my ($class, $opts) = @_; my ($class, $opts) = @_;
@ -65,6 +66,15 @@ sub auth {
return 1; return 1;
} }
sub chpass {
my ($self, $uid, $newpass) = @_;
my $csh = Crypt::SaltedHash->new(algorithm => 'SHA-1');
$csh->add($newpass);
return $self->update($uid, {userPassword => $csh->generate()});
}
sub create { sub create {
my ($self, $uid) = @_; my ($self, $uid) = @_;
my $conn = $self->_connect(); my $conn = $self->_connect();
@ -186,6 +196,14 @@ LDV::LDAP -- ldap routines
Check user/pass pair against ldap server. Check user/pass pair against ldap server.
Returns 1 on success or undef on auth failure/error; Returns 1 on success or undef on auth failure/error;
=head2 C<chpass>
my $ldap = LDV::LDAP->new(\%opts);
my $result = $ldap->chpass($username, $password);
Changes user password.
Returns nothing on success or scalar with text on error.
=head2 C<create> =head2 C<create>
my $ldap = LDV::LDAP->new(\%opts); my $ldap = LDV::LDAP->new(\%opts);

8
t/LDAP.t

@ -7,12 +7,12 @@ use utf8;
use LDV::LDAP; use LDV::LDAP;
use File::Slurp; use File::Slurp;
use Data::Dumper; use Data::Dumper;
use Test::More tests => 13; use Test::More tests => 16;
my $hash = read_file('conf/ldv.conf'); my $hash = read_file('conf/ldv.conf');
my $config = eval "$hash"; my $config = eval "$hash";
SKIP: { SKIP: {
skip "Can't load config", 13 unless (ref($config) eq 'HASH'); skip "Can't load config", 16 unless (ref($config) eq 'HASH');
my $ldap = LDV::LDAP->new($config->{ldap}); my $ldap = LDV::LDAP->new($config->{ldap});
isa_ok($ldap, "LDV::LDAP", "LDV::LDAP->new"); isa_ok($ldap, "LDV::LDAP", "LDV::LDAP->new");
@ -36,6 +36,10 @@ SKIP: {
is($ldap->auth("test20", "test1"), undef, "check auth falure (wrong pass)"); is($ldap->auth("test20", "test1"), undef, "check auth falure (wrong pass)");
is($ldap->auth("test20", undef), undef, "check auth falure (empty pass)"); is($ldap->auth("test20", undef), undef, "check auth falure (empty pass)");
is($ldap->chpass("test20", "test2"), undef, "change password");
is($ldap->auth("test20", "test2"), 1, "check auth success (new pass)");
is($ldap->auth("test20", "test"), undef, "check auth falure (old pass)");
is($ldap->delete("test20"), undef, "deleting user"); is($ldap->delete("test20"), undef, "deleting user");
}; };

Loading…
Cancel
Save