X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FAuthentication%2FStore%2FLDAP%2FUser.pm;h=3dc1a78ded3ab31dbdf04015a044f116100cb050;hb=dfead577d0091c13ab636431aefca08d0ff7330b;hp=d63c7651ad1bc082d5c3decc61e1435040e9c34f;hpb=821627ad45646d20b53dcb3e8568f77e743265b8;p=catagits%2FCatalyst-Authentication-Store-LDAP.git diff --git a/lib/Catalyst/Authentication/Store/LDAP/User.pm b/lib/Catalyst/Authentication/Store/LDAP/User.pm index d63c765..3dc1a78 100644 --- a/lib/Catalyst/Authentication/Store/LDAP/User.pm +++ b/lib/Catalyst/Authentication/Store/LDAP/User.pm @@ -48,13 +48,17 @@ use base qw( Catalyst::Authentication::User Class::Accessor::Fast ); use strict; use warnings; +use Scalar::Util qw/refaddr/; -our $VERSION = '1.006'; +our $VERSION = '1.015'; -BEGIN { __PACKAGE__->mk_accessors(qw/user store _ldap_connection_password/) } +BEGIN { __PACKAGE__->mk_accessors(qw/user store/) } use overload '""' => sub { shift->stringify }, fallback => 1; +my %_ldap_connection_passwords; # Store inside-out so that they don't show up + # in dumps.. + =head1 METHODS =head2 new($store, $user, $c) @@ -107,8 +111,8 @@ sub stringify { return $string; } else { - my ($string) = $self->$userfield; - return $string; + my $val = $self->$userfield; + return ref($val) eq 'ARRAY' ? $val->[0] : $val; } } @@ -136,20 +140,9 @@ bind, 0 on failure. sub check_password { my ( $self, $password ) = @_; - my $ldap - = $self->store->ldap_bind( undef, $self->ldap_entry->dn, $password, - 'forauth' ); - if ( defined($ldap) ) { - if ($self->store->role_search_as_user) { - # FIXME - This can be removed and made to use the code below.. - # Have to do the role lookup _now_, as this is the only time - # that we have the user's password/ldap bind.. - $self->roles($ldap); - } + if ( $self->store->ldap_auth($self->ldap_entry->dn, $password) ) { # Stash a closure which can be used to retrieve the connection in the users context later. - $self->_ldap_connection_password( sub { $password } ); # Close over - # password to try to ensure it doesn't come out in debug dumps - # or get serialized into sessions etc.. + $_ldap_connection_passwords{refaddr($self)} = $password; return 1; } else { @@ -165,8 +158,7 @@ Returns the results of L's "look sub roles { my $self = shift; - my $ldap = shift; - $self->{_roles} ||= [$self->store->lookup_roles($self, $ldap)]; + $self->{_roles} ||= [$self->store->lookup_roles($self)]; return @{$self->{_roles}}; } @@ -226,6 +218,9 @@ sub has_attribute { if ( $attribute eq "dn" ) { return $self->ldap_entry->dn; } + elsif ( $attribute eq "username" ) { + return $self->user->{'attributes'}->{$self->store->user_field}; + } elsif ( exists( $self->user->{'attributes'}->{$attribute} ) ) { return $self->user->{'attributes'}->{$attribute}; } @@ -234,6 +229,23 @@ sub has_attribute { } } +=head2 get + +A simple wrapper around has_attribute() to satisfy the Catalyst::Authentication::User API. + +=cut + +sub get { return shift->has_attribute(@_) } + +=head2 get_object + +Satisfies the Catalyst::Authentication::User API and returns the contents of the user() +attribute. + +=cut + +sub get_object { return shift->user } + =head2 ldap_connection Re-binds to the auth store with the credentials of the user you logged in @@ -244,7 +256,7 @@ as, and returns a L object which you can use to do further queries. sub ldap_connection { my $self = shift; $self->store->ldap_bind( undef, $self->ldap_entry->dn, - $self->_ldap_connection_password->() ); + $_ldap_connection_passwords{refaddr($self)} ); } =head2 AUTOLOADed methods @@ -286,6 +298,21 @@ value of user_field (uid by default.) =cut +sub DESTROY { + my $self = shift; + # Don't leak passwords.. + delete $_ldap_connection_passwords{refaddr($self)}; +} + +sub can { + my ($self, $method) = @_; + + return $self->SUPER::can($method) || do { + return unless $self->has_attribute($method); + return sub { $_[0]->has_attribute($method) }; + }; +} + sub AUTOLOAD { my $self = shift; @@ -294,20 +321,9 @@ sub AUTOLOAD { if ( $method eq "DESTROY" ) { return; } - if ( exists( $self->user->{'attributes'}->{$method} ) ) { - return $self->user->{'attributes'}->{$method}; - } - elsif ( $method eq "username" ) { - my $userfield = $self->store->user_field; - my $username = $self->has_attribute($userfield); - if ($username) { - return $username; - } - else { - Catalyst::Exception->throw( "User is missing the " - . $userfield - . " attribute, which should not be possible!" ); - } + + if ( my $attribute = $self->has_attribute($method) ) { + return $attribute; } else { Catalyst::Exception->throw(