« blib/lib/OpusVL/AppKit/FormFu/Constraint/AppKitUsername.pm
↑ Summary
blib/lib/OpusVL/AppKit/Plugin/AppKit/FeatureList.pm »
| 1 | package OpusVL::AppKit::LDAPAuth; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | with 'OpusVL::AppKit::RolesFor::Auth'; | ||
| 5 | |||
| 6 | use Net::LDAP; | ||
| 7 | use Net::LDAP::Util qw/escape_dn_value/; | ||
| 8 | use experimental 'smartmatch'; | ||
| 9 | |||
| 10 | has ldap_server => (is => 'rw', isa => 'Str', default => 'ldap'); | ||
| 11 | |||
| 12 | # NOTE: we assume that the dn parts we're given are already correctly escaped if | ||
| 13 | # necessary. | ||
| 14 | has user_base_dn => (is => 'ro', isa => 'Str', default => 'ou=People,dc=opusvl'); | ||
| 15 | has user_field => (is => 'ro', isa => 'Str', default => 'uid'); | ||
| 16 | |||
| 17 | sub server | ||
| 18 | { | ||
| 19 | my $self = shift; | ||
| 20 | return Net::LDAP->new($self->ldap_server) || die $@; | ||
| 21 | } | ||
| 22 | |||
| 23 | |||
| 24 | sub check_password | ||
| 25 | { | ||
| 26 | my ($self, $username, $password) = @_; | ||
| 27 | |||
| 28 | my $query = sprintf("(%s=%s)", $self->user_field, escape_dn_value($username)); | ||
| 29 | my $mesg = $self->server->search(base => $self->user_base_dn, filter => $query); | ||
| 30 | |||
| 31 | foreach my $entry ($mesg->entries) { | ||
| 32 | my $login = $self->server->bind($entry->dn, password => $password); | ||
| 33 | return 1 unless $login->is_error; | ||
| 34 | } | ||
| 35 | |||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | |||
| 39 | 1; | ||
| 40 | |||
| 41 | __END__ |