Catalyst-Authentication-Credential-HTTP-Proxy

 view release on metacpan or  search on metacpan

lib/Catalyst/Authentication/Credential/HTTP/Proxy.pm  view on Meta::CPAN


sub authenticate_basic {
    my ( $self, $c, $realm, $auth_info ) = @_;

    $c->log->debug('Checking http basic authentication.') if $c->debug;

    my $headers = $c->req->headers;

    if ( my ( $user, $password ) = $headers->authorization_basic ) {
        my $ua = Catalyst::Authentication::Credential::HTTP::Proxy::User->new;
        $ua->credentials($user, $password);
        my $resp = $ua->get($self->url);
        if ( $resp->is_success ) {
            # Config username_field TODO
	        my $user_obj = $realm->find_user( { username => $user }, $c);
	        unless ($user_obj) {
                $c->log->debug("User '$user' doesn't exist in the default store")
                    if $c->debug;
                return;
            }
            $c->set_authenticated($user_obj);
            return 1;
        }
        else {
            $c->log->info('Remote authentication failed:'.$resp->message);
            return 0;
        }
    } 
    elsif ( $c->debug ) {
        $c->log->info('No credentials provided for basic auth');
        return 0;
    }
}

1;

__END__

=pod

lib/Catalyst/Authentication/Credential/HTTP/Proxy/User.pm  view on Meta::CPAN

package Catalyst::Authentication::Credential::HTTP::Proxy::User;

use strict;
use warnings;

use base 'LWP::UserAgent';

sub credentials {
   my ($self,$user,$pass)=@_;
   @{$self->{credentials}}=($user,$pass);
}

sub get_basic_credentials {
    my $self = shift;
    return @{$self->{credentials}};
}

1;

=head1 NAME

Catalyst::Authentication::Credential::HTTP::Proxy::User - Wrapper for LWP::UserAgent

=head1 DESCRIPTION

A thin wrapper for L<LWP::UserAgent> to make basic auth simpler.

=head1 METHODS

=head2 credentials

now takes just a username and password

=head2 get_basic_credentials

Returns the set credentials, takes no options.

=head1 AUTHOR

Marcus Ramberg <mramberg@cpan.org>

=head1 LICENSE

This software is licensed under the same terms as perl itself.

=cut

t/mock.t  view on Meta::CPAN

my ($auth_ua, $auth_res, $auth_url);
{
    no warnings qw/redefine once/;
    *Catalyst::Authentication::Credential::HTTP::Proxy::User::get = sub { $auth_ua = shift; $auth_url = shift; $auth_res };
}
$auth_res = HTTP::Response->new;
$auth_res->code(500);
$auth_res->message('FAIL');

ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with auth response !success');
is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
is($auth_url, 'http://some.proxy:8080', 'get http://some.proxy:8080');
throws_ok {
    $cred->authenticate($mock_c, $mock_realm, {});
} qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach with auth response !success';

like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Basic/, "WWW-Authenticate header set: basic");
like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="myrealm"/, "WWW-Authenticate header set: basic realm");

$res_headers->clear;
$auth_res->code(200);
($auth_url, $auth_ua) = (undef, undef);

ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with auth response success but no user from realm');
is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
is($auth_url, 'http://some.proxy:8080', 'get http://some.proxy:8080');
is_deeply($auth_info, { username => 'Mufasa'}, '$realm->find_user({ username => "Mufasa" })');
ok(!$authenticated, 'Not set_authenticated');
throws_ok {
    $cred->authenticate($mock_c, $mock_realm, {});
} qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach with auth response !success';

($auth_url, $auth_ua) = (undef, undef);
$res_headers->clear;
$user = Test::MockObject->new;

ok($cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns true with auth response success and user from realm');
is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
is_deeply($auth_info, { username => 'Mufasa'}, '$realm->find_user({ username => "Mufasa" })');
ok($authenticated, 'Called set_authenticated');
is("$authenticated_user", "$user", 'Called set_authenticated with user object');
lives_ok {
    $cred->authenticate($mock_c, $mock_realm, {});
} '$cred->authenticate does not detach';
ok(!$res_headers->header('WWW-Authenticate'), 'No authenticate header on successful auth');



( run in 0.309 second using v1.01-cache-2.11-cpan-4d50c553e7e )