6 Catalyst::Authentication::Store::LDAP::Backend
7 - LDAP authentication storage backend.
11 # you probably just want Store::LDAP under most cases,
12 # but if you insist you can instantiate your own store:
14 use Catalyst::Authentication::Store::LDAP::Backend;
18 Authentication::Credential::Password
22 'ldap_server' => 'ldap1.yourcompany.com',
23 'ldap_server_options' => {
26 'binddn' => 'anonymous',
27 'bindpw' => 'dontcarehow',
29 'start_tls_options' => {
32 'user_basedn' => 'ou=people,dc=yourcompany,dc=com',
33 'user_filter' => '(&(objectClass=posixAccount)(uid=%s))',
34 'user_scope' => 'one',
35 'user_field' => 'uid',
36 'user_search_options' => {
39 'user_results_filter' => sub { return shift->pop_entry },
40 'entry_class' => 'MyApp::LDAP::Entry',
42 'role_basedn' => 'ou=groups,dc=yourcompany,dc=com',
43 'role_filter' => '(&(objectClass=posixGroup)(member=%s))',
44 'role_scope' => 'one',
47 'role_search_options' => {
52 our $users = Catalyst::Authentication::Store::LDAP::Backend->new(\%config);
55 my ( $self, $c ) = @_;
57 $c->login( $users->get_user( $c->req->param("login") ),
58 $c->req->param("password") );
63 You probably want L<Catalyst::Authentication::Store::LDAP>, unless
64 you are mixing several stores in a single app and one of them is LDAP.
66 Otherwise, this lets you create a store manually.
68 See the L<Catalyst::Authentication::Store::LDAP> documentation for
69 an explanation of the configuration options.
75 package Catalyst::Authentication::Store::LDAP::Backend;
76 use base qw( Class::Accessor::Fast );
81 our $VERSION = '0.1002';
83 use Catalyst::Authentication::Store::LDAP::User;
87 __PACKAGE__->mk_accessors(
88 qw( ldap_server ldap_server_options binddn
89 bindpw entry_class user_search_options
90 user_filter user_basedn user_scope
91 user_attrs user_field use_roles role_basedn
92 role_filter role_scope role_field role_value
93 role_search_options start_tls start_tls_options
101 Creates a new L<Catalyst::Authentication::Store::LDAP::Backend> object.
102 $config should be a hashref, which should contain the configuration options
103 listed in L<Catalyst::Authentication::Store::LDAP>'s documentation.
105 Also sets a few sensible defaults.
110 my ( $class, $config ) = @_;
112 unless ( defined($config) && ref($config) eq "HASH" ) {
113 Catalyst::Exception->throw(
114 "Catalyst::Authentication::Store::LDAP::Backend needs to be configured with a hashref."
117 my %config_hash = %{$config};
118 $config_hash{'binddn'} ||= 'anonymous';
119 $config_hash{'user_filter'} ||= '(uid=%s)';
120 $config_hash{'user_scope'} ||= 'sub';
121 $config_hash{'user_field'} ||= 'uid';
122 $config_hash{'role_filter'} ||= '(memberUid=%s)';
123 $config_hash{'role_scope'} ||= 'sub';
124 $config_hash{'role_field'} ||= 'cn';
125 $config_hash{'use_roles'} ||= '1';
126 $config_hash{'start_tls'} ||= '0';
127 $config_hash{'entry_class'} ||= 'Catalyst::Model::LDAP::Entry';
129 my $self = \%config_hash;
130 bless( $self, $class );
134 =head2 find_user( I<authinfo> )
136 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
137 for the given User ID. This is the preferred mechanism for getting a
138 given User out of the Store.
140 I<authinfo> should be a hashref with a key of either C<id> or
141 C<username>. The value will be compared against the LDAP C<user_field> field.
146 my ( $self, $authinfo, $c ) = @_;
147 return $self->get_user( $authinfo->{id} || $authinfo->{username} );
152 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
153 for the given User ID. This is the preferred mechanism for getting a
154 given User out of the Store.
159 my ( $self, $id ) = @_;
160 my $user = Catalyst::Authentication::Store::LDAP::User->new( $self,
161 $self->lookup_user($id) );
167 Returns a L<Net::LDAP> object, connected to your LDAP server. (According
168 to how you configured the Backend, of course)
175 if ( defined( $self->ldap_server_options() ) ) {
177 = Net::LDAP->new( $self->ldap_server,
178 %{ $self->ldap_server_options } )
179 or Catalyst::Exception->throw($@);
182 $ldap = Net::LDAP->new( $self->ldap_server )
183 or Catalyst::Exception->throw($@);
185 if ( defined( $self->start_tls ) && $self->start_tls =~ /(1|true)/i ) {
187 if ( defined( $self->start_tls_options ) ) {
188 $mesg = $ldap->start_tls( %{ $self->start_tls_options } );
191 $mesg = $ldap->start_tls;
193 if ( $mesg->is_error ) {
194 Catalyst::Exception->throw( "TLS Error: " . $mesg->error );
200 =head2 ldap_bind($ldap, $binddn, $bindpw)
202 Bind's to the directory. If $ldap is undef, it will connect to the
203 LDAP server first. $binddn should be the DN of the object you wish
204 to bind as, and $bindpw the password.
206 If $binddn is "anonymous", an anonymous bind will be performed.
211 my ( $self, $ldap, $binddn, $bindpw, $forauth ) = @_;
213 $ldap ||= $self->ldap_connect;
214 if ( !defined($ldap) ) {
215 Catalyst::Exception->throw("LDAP Server undefined!");
217 $binddn ||= $self->binddn;
218 $bindpw ||= $self->bindpw;
219 if ( $binddn eq "anonymous" ) {
220 my $mesg = $ldap->bind;
221 if ( $mesg->is_error ) {
222 Catalyst::Exception->throw( "Error on Bind: " . $mesg->error );
227 my $mesg = $ldap->bind( $binddn, 'password' => $bindpw );
228 if ( $mesg->is_error ) {
230 # If we're not checking this bind for authentication purposes
231 # Go ahead an blow up if we fail.
232 if ( $forauth ne 'forauth' ) {
233 Catalyst::Exception->throw(
234 "Error on Initial Bind: " . $mesg->error );
242 my $mesg = $ldap->bind($binddn);
243 if ( $mesg->is_error ) {
251 =head2 lookup_user($id)
253 Given a User ID, this method will:
255 A) Bind to the directory using the configured binddn and bindpw
256 B) Perform a search for the User Object in the directory, using
257 user_basedn, user_filter, and user_scope.
258 C) Assuming we found the object, we will walk it's attributes
259 using L<Net::LDAP::Entry>'s get_value method. We store the
260 results in a hashref.
261 D) Return a hashref that looks like:
264 'ldap_entry' => $entry, # The Net::LDAP::Entry object
265 'attributes' => $attributes,
268 This method is usually only called by find_user().
273 my ( $self, $id ) = @_;
275 # No sneaking in wildcards!
277 Catalyst::Exception->throw("ID $id contains wildcards!");
279 my $ldap = $self->ldap_bind;
281 if ( defined( $self->user_basedn ) ) {
282 push( @searchopts, 'base' => $self->user_basedn );
285 Catalyst::Exception->throw(
286 "You must set user_basedn before looking up users!");
288 my $filter = $self->_replace_filter( $self->user_filter, $id );
289 push( @searchopts, 'filter' => $filter );
290 push( @searchopts, 'scope' => $self->user_scope );
291 if ( defined( $self->user_search_options ) ) {
292 push( @searchopts, %{ $self->user_search_options } );
294 my $usersearch = $ldap->search(@searchopts);
295 if ( $usersearch->is_error ) {
296 Catalyst::Exception->throw(
297 "LDAP Error while searching for user: " . $usersearch->error );
300 my $user_field = $self->user_field;
301 my $results_filter = $self->user_results_filter;
303 if ( defined($results_filter) ) {
304 $entry = &$results_filter($usersearch);
307 $entry = $usersearch->pop_entry;
309 if ( $usersearch->pop_entry ) {
310 Catalyst::Exception->throw(
311 "More than one entry matches user search.\n"
312 . "Consider defining a user_results_filter sub." );
315 # a little extra sanity check with the 'eq' since LDAP already
317 if ( defined($entry) ) {
318 unless ( $entry->get_value($user_field) eq $id ) {
319 Catalyst::Exception->throw(
320 "LDAP claims '$user_field' equals '$id' but results entry does not match."
328 unless ($userentry) {
332 foreach my $attr ( $userentry->attributes ) {
333 my @attrvalues = $userentry->get_value($attr);
334 if ( scalar(@attrvalues) == 1 ) {
335 $attrhash->{ lc($attr) } = $attrvalues[0];
338 $attrhash->{ lc($attr) } = \@attrvalues;
341 my $load_class = $self->entry_class . ".pm";
342 $load_class =~ s|::|/|g;
344 eval { require $load_class };
346 bless( $userentry, $self->entry_class );
347 $userentry->{_use_unicode}++;
350 'ldap_entry' => $userentry,
351 'attributes' => $attrhash,
356 =head2 lookup_roles($userobj)
358 This method looks up the roles for a given user. It takes a
359 L<Catalyst::Authentication::Store::LDAP::User> object
360 as it's sole argument.
362 It returns an array containing the role_field attribute from all the
363 objects that match it's criteria.
368 my ( $self, $userobj ) = @_;
369 if ( $self->use_roles == 0 || $self->use_roles =~ /^false$/i ) {
372 my $ldap = $self->ldap_bind;
374 if ( defined( $self->role_basedn ) ) {
375 push( @searchopts, 'base' => $self->role_basedn );
378 Catalyst::Exception->throw(
379 "You must set up role_basedn before looking up roles!");
381 my $filter_value = $userobj->has_attribute( $self->role_value );
382 if ( !defined($filter_value) ) {
383 Catalyst::Exception->throw( "User object "
387 . " attribute, so I can't look up it's roles!" );
389 my $filter = $self->_replace_filter( $self->role_filter, $filter_value );
390 push( @searchopts, 'filter' => $filter );
391 push( @searchopts, 'scope' => $self->role_scope );
392 push( @searchopts, 'attrs' => [ $self->role_field ] );
393 if ( defined( $self->role_search_options ) ) {
394 push( @searchopts, %{ $self->role_search_options } );
396 my $rolesearch = $ldap->search(@searchopts);
398 RESULT: while ( my $entry = $rolesearch->pop_entry ) {
399 my ($role) = $entry->get_value( $self->role_field );
401 push( @roles, $role );
410 sub _replace_filter {
414 $filter =~ s/\%s/$replace/g;
421 Catalyst::Authentication::Store::LDAP::User->supports(@_).
428 # this can work as a class method
429 Catalyst::Authentication::Store::LDAP::User->supports(@_);
432 =head2 from_session( I<id> )
434 Returns get_user() for I<id>.
439 my ( $self, $c, $id ) = @_;
440 $self->get_user($id);
449 Adam Jacob <holoway@cpan.org>
451 Some parts stolen shamelessly and entirely from
452 L<Catalyst::Plugin::Authentication::Store::Htpasswd>.
454 Currently maintained by Peter Karman <karman@cpan.org>.
458 To nothingmuch, ghenry, castaway and the rest of #catalyst for the help. :)
462 L<Catalyst::Authentication::Store::LDAP>, L<Catalyst::Authentication::Store::LDAP::User>, L<Catalyst::Plugin::Authentication>, L<Net::LDAP>
464 =head1 COPYRIGHT & LICENSE
466 Copyright (c) 2005 the aforementioned authors. All rights
467 reserved. This program is free software; you can redistribute
468 it and/or modify it under the same terms as Perl itself.