X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Flive_app_digest.t;h=2755f3c3dc499fdacc51e2eef7b9a1c5c9a33b87;hb=a3abf05a7a306bbc349a82265ba6441db14e9480;hp=c50bac2558b06b6649d510e71b2226baf1a57e65;hpb=ac92fd52e650f58ec34dfb078551854880b73d2e;p=catagits%2FCatalyst-Authentication-Credential-HTTP.git diff --git a/t/live_app_digest.t b/t/live_app_digest.t index c50bac2..2755f3c 100644 --- a/t/live_app_digest.t +++ b/t/live_app_digest.t @@ -1,78 +1,76 @@ #!/usr/bin/perl use strict; use warnings; +use FindBin qw/$Bin/; +use lib "$Bin/lib"; use Test::More; BEGIN { - eval { require Test::WWW::Mechanize::Catalyst } + do { + eval { require Test::WWW::Mechanize::Catalyst } + and + Test::WWW::Mechanize::Catalyst->VERSION('0.51') + } or plan skip_all => "Test::WWW::Mechanize::Catalyst is needed for this test"; - eval { require Catalyst::Plugin::Cache::FileCache } + eval { require Catalyst::Plugin::Cache } + or plan skip_all => + "Catalyst::Plugin::Cache is needed for this test"; + eval { require Cache::FileCache } or plan skip_all => - "Catalyst::Plugin::Cache::FileCache is needed for this test"; - plan tests => 4; + "Cache::FileCache is needed for this test"; + plan tests => 12; } +use Digest::MD5; use HTTP::Request; -{ - package AuthTestApp; - use Catalyst qw/ - Authentication - Authentication::Store::Minimal - Authentication::Credential::HTTP - Cache::FileCache - /; - use Test::More; - our $users; - sub moose : Local { - my ( $self, $c ) = @_; - $c->authorization_required( realm => 'testrealm@host.com' ); - $c->res->body( $c->user->id ); +use Test::More; +use Test::WWW::Mechanize::Catalyst qw/AuthDigestTestApp/; + +sub do_test { + my $username = shift; + my $uri = shift; + my $mech = Test::WWW::Mechanize::Catalyst->new; + $mech->get("http://localhost/moose"); + is( $mech->status, 401, "status is 401" ); + my $www_auth = $mech->res->headers->header('WWW-Authenticate'); + my %www_auth_params = map { + my @key_val = split /=/, $_, 2; + $key_val[0] = lc $key_val[0]; + $key_val[1] =~ s{"}{}g; # remove the quotes + @key_val; + } split /, /, substr( $www_auth, 7 ); #7 == length "Digest " + $mech->content_lacks( "foo", "no output" ); + my $response = ''; + { + my $password = 'Circle Of Life'; + my $realm = $www_auth_params{realm}; + my $nonce = $www_auth_params{nonce}; + my $cnonce = '0a4f113b'; + my $opaque = $www_auth_params{opaque}; + my $nc = '00000001'; + my $method = 'GET'; + my $qop = 'auth'; + $uri ||= '/moose'; + my $ctx = Digest::MD5->new; + $ctx->add( join( ':', $username, $realm, $password ) ); + my $A1_digest = $ctx->hexdigest; + $ctx = Digest::MD5->new; + $ctx->add( join( ':', $method, $uri ) ); + my $A2_digest = $ctx->hexdigest; + my $digest = Digest::MD5::md5_hex( + join( ':', + $A1_digest, $nonce, $qop ? ( $nc, $cnonce, $qop ) : (), $A2_digest ) + ); + + $response = qq{Digest username="$username", realm="$realm", nonce="$nonce", uri="$uri", qop=$qop, nc=$nc, cnonce="$cnonce", response="$digest", opaque="$opaque"}; } - __PACKAGE__->config->{authentication}{http}{type} = 'digest'; - __PACKAGE__->config->{authentication}{users} = $users = { - Mufasa => { password => "Circle Of Life", }, - }; - __PACKAGE__->setup; + my $r = HTTP::Request->new( GET => "http://localhost" . $uri ); + $mech->request($r); + $r->headers->push_header( Authorization => $response ); + $mech->request($r); + is( $mech->status, 200, "status is 200" ); + $mech->content_contains( $username, "Mufasa output" ); } -use Test::WWW::Mechanize::Catalyst qw/AuthTestApp/; -my $mech = Test::WWW::Mechanize::Catalyst->new; -$mech->get("http://localhost/moose"); -is( $mech->status, 401, "status is 401" ); -my $www_auth = $mech->res->headers->header('WWW-Authenticate'); -my %www_auth_params = map { - my @key_val = split /=/, $_, 2; - $key_val[0] = lc $key_val[0]; - $key_val[1] =~ s{"}{}g; # remove the quotes - @key_val; -} split /, /, substr( $www_auth, 7 ); #7 == length "Digest " -$mech->content_lacks( "foo", "no output" ); -my $response = ''; -{ - my $username = 'Mufasa'; - my $password = 'Circle Of Life'; - my $realm = $www_auth_params{realm}; - my $nonce = $www_auth_params{nonce}; - my $cnonce = '0a4f113b'; - my $opaque = $www_auth_params{opaque}; - my $nc = '00000001'; - my $method = 'GET'; - my $qop = 'auth'; - my $uri = '/moose'; - my $ctx = Digest::MD5->new; - $ctx->add( join( ':', $username, $realm, $password ) ); - my $A1_digest = $ctx->hexdigest; - $ctx = Digest::MD5->new; - $ctx->add( join( ':', $method, $uri ) ); - my $A2_digest = $ctx->hexdigest; - my $digest = Digest::MD5::md5_hex( - join( ':', - $A1_digest, $nonce, $qop ? ( $nc, $cnonce, $qop ) : (), $A2_digest ) - ); - $response = qq{Digest username="$username", realm="$realm", nonce="$nonce", uri="$uri", qop=$qop, nc=$nc, cnonce="$cnonce", response="$digest", opaque="$opaque"}; -} -my $r = HTTP::Request->new( GET => "http://localhost/moose" ); -$mech->request($r); -$r->headers->push_header( Authorization => $response ); -$mech->request($r); -is( $mech->status, 200, "status is 200" ); -$mech->content_contains( "Mufasa", "Mufasa output" ); +do_test('Mufasa'); +do_test('Mufasa2'); +do_test('Mufasa', '/moose?moose_id=1'); # Digest auth includes the full URL path, so need to test query strings