| 1 | package Authen::Simple::LDAP; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use base 'Authen::Simple::Adapter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Net::LDAP qw[]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Net::LDAP::Constant qw[LDAP_INVALID_CREDENTIALS]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Params::Validate qw[]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | our $VERSION = 0.3; | |||||||||
| 12 | |||||||||||
| 13 | 1 | __PACKAGE__->options({ | |||||||||
| 14 | host => { | ||||||||||
| 15 | type => Params::Validate::SCALAR | Params::Validate::ARRAYREF, | ||||||||||
| 16 | default => 'localhost', | ||||||||||
| 17 | optional => 1 | ||||||||||
| 18 | }, | ||||||||||
| 19 | port => { | ||||||||||
| 20 | type => Params::Validate::SCALAR, | ||||||||||
| 21 | default => 389, | ||||||||||
| 22 | optional => 1 | ||||||||||
| 23 | }, | ||||||||||
| 24 | timeout => { | ||||||||||
| 25 | type => Params::Validate::SCALAR, | ||||||||||
| 26 | default => 60, | ||||||||||
| 27 | optional => 1 | ||||||||||
| 28 | }, | ||||||||||
| 29 | version => { | ||||||||||
| 30 | type => Params::Validate::SCALAR, | ||||||||||
| 31 | default => 3, | ||||||||||
| 32 | optional => 1 | ||||||||||
| 33 | }, | ||||||||||
| 34 | binddn => { | ||||||||||
| 35 | type => Params::Validate::SCALAR, | ||||||||||
| 36 | depends => [ 'bindpw' ], | ||||||||||
| 37 | optional => 1 | ||||||||||
| 38 | }, | ||||||||||
| 39 | bindpw => { | ||||||||||
| 40 | type => Params::Validate::SCALAR, | ||||||||||
| 41 | depends => [ 'binddn' ], | ||||||||||
| 42 | optional => 1 | ||||||||||
| 43 | }, | ||||||||||
| 44 | basedn => { | ||||||||||
| 45 | type => Params::Validate::SCALAR, | ||||||||||
| 46 | optional => 1 | ||||||||||
| 47 | }, | ||||||||||
| 48 | scope => { | ||||||||||
| 49 | type => Params::Validate::SCALAR, | ||||||||||
| 50 | default => 'sub', | ||||||||||
| 51 | optional => 1 | ||||||||||
| 52 | }, | ||||||||||
| 53 | filter => { | ||||||||||
| 54 | type => Params::Validate::SCALAR, | ||||||||||
| 55 | default => '(uid=%s)', | ||||||||||
| 56 | optional => 1 | ||||||||||
| 57 | } | ||||||||||
| 58 | }); | ||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | sub check { | ||||||||
|
Subroutine
check: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||
| 61 | 0 | my ( $self, $username, $password ) = @_; | |||||||||
| 62 | |||||||||||
| 63 | 0 | my $connection = Net::LDAP->new( $self->host, | |||||||||
| 64 | port => $self->port, | ||||||||||
| 65 | timeout => $self->timeout, | ||||||||||
| 66 | version => $self->version | ||||||||||
| 67 | ); | ||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | unless ( defined $connection ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 0 | my $host = $self->host; | |||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | $self->log->error( qq/Failed to connect to '$host'. Reason: '$@'/ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | if $self->log; | ||||||||||
| 75 | |||||||||||
| 76 | 0 | return 0; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 0 | my ( @credentials, $message, $search, $entry, $filter, $dn ); | |||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | @credentials = $self->binddn ? ( $self->binddn, password => $self->bindpw ) : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | $message = $connection->bind(@credentials); | |||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | if ( $message->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | |||||||||||
| 86 | 0 | my $error = $message->error; | |||||||||
| 87 | 0 | my $binddn = $self->binddn; | |||||||||
| 88 | 0 | ▶ | my $bind = $binddn ? qq/with dn '$binddn'/ : "Anonymously"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | $self->log->error( qq/Failed to bind $bind. Reason: '$error'/ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | if $self->log; | ||||||||||
| 92 | |||||||||||
| 93 | 0 | return 0; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 0 | $filter = sprintf( $self->filter, ($username) x 10 ); | |||||||||
| 97 | 0 | $search = $connection->search( | |||||||||
| 98 | base => $self->basedn, | ||||||||||
| 99 | scope => $self->scope, | ||||||||||
| 100 | filter => $filter, | ||||||||||
| 101 | attrs => ['1.1'] | ||||||||||
| 102 | ); | ||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | if ( $search->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | 0 | my $error = $search->error; | |||||||||
| 107 | 0 | my $basedn = $self->basedn; | |||||||||
| 108 | 0 | my $options = qq/basedn '$basedn' with filter '$filter'/; | |||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | $self->log->error( qq/Failed to search $options. Reason: '$error'/ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | if $self->log; | ||||||||||
| 112 | |||||||||||
| 113 | 0 | return 0; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | if ( $search->count == 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | $self->log->debug( qq/User '$username' was not found with filter '$filter'./ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | if $self->log; | ||||||||||
| 120 | |||||||||||
| 121 | 0 | return 0; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | if ( $search->count > 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | |||||||||||
| 126 | 0 | my $count = $search->count; | |||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | $self->log->warn( qq/Found $count matching entries for '$username' with filter '$filter'./ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | if $self->log; | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 0 | $entry = $search->entry(0); | |||||||||
| 133 | 0 | $message = $connection->bind( $entry->dn, password => $password ); | |||||||||
| 134 | 0 | $dn = $entry->dn; | |||||||||
| 135 | |||||||||||
| 136 | 0 | ▶ | if ( $message->is_error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | |||||||||||
| 138 | 0 | my $error = $message->error; | |||||||||
| 139 | 0 | ▶ | my $level = $message->code == LDAP_INVALID_CREDENTIALS ? 'debug' : 'error'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | |||||||||||
| 141 | 0 | ▶ | $self->log->$level( qq/Failed to authenticate user '$username' with dn '$dn'. Reason: '$error'/ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | if $self->log; | ||||||||||
| 143 | |||||||||||
| 144 | 0 | return 0; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 0 | ▶ | $self->log->debug( qq/Successfully authenticated user '$username' with dn '$dn'./ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | if $self->log; | ||||||||||
| 149 | |||||||||||
| 150 | 0 | return $dn; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 1 | 1; | |||||||||
| 154 | |||||||||||
| 155 | __END__ | ||||||||||