Commit changes that were in 1.002
[catagits/Catalyst-Authentication-Credential-HTTP.git] / t / live_app_digest.t
CommitLineData
007935b8 1#!/usr/bin/perl
007935b8 2use strict;
3use warnings;
007935b8 4use Test::More;
007935b8 5BEGIN {
6 eval { require Test::WWW::Mechanize::Catalyst }
7 or plan skip_all =>
8 "Test::WWW::Mechanize::Catalyst is needed for this test";
5b51e987 9 eval { require Catalyst::Plugin::Cache }
007935b8 10 or plan skip_all =>
5b51e987 11 "Catalyst::Plugin::Cache is needed for this test";
513d8ab6 12 eval { require Cache::FileCache }
5b51e987 13 or plan skip_all =>
14 "Cache::FileCache is needed for this test";
007935b8 15 plan tests => 4;
16}
007935b8 17use HTTP::Request;
007935b8 18{
007935b8 19 package AuthTestApp;
20 use Catalyst qw/
21 Authentication
5b51e987 22 Cache
007935b8 23 /;
007935b8 24 use Test::More;
513d8ab6 25 our %users;
007935b8 26 sub moose : Local {
27 my ( $self, $c ) = @_;
513d8ab6 28 #$c->authenticate( { realm => 'testrealm@host.com' } );
29 $c->authenticate();
007935b8 30 $c->res->body( $c->user->id );
31 }
490754a8 32 %users = ( Mufasa => { pass => "Circle Of Life", }, );
5b51e987 33 __PACKAGE__->config->{cache}{backend} = {
34 class => 'Cache::FileCache',
35 };
513d8ab6 36 __PACKAGE__->config( authentication => {
37 default_realm => 'testrealm@host.com',
38 realms => {
39 'testrealm@host.com' => {
40 store => {
41 class => 'Minimal',
42 users => \%users,
43 },
44 credential => {
45 class => 'HTTP',
46 type => 'digest',
490754a8 47 password_type => 'clear',
48 password_field => 'pass'
513d8ab6 49 },
50 },
51 },
52 });
007935b8 53 __PACKAGE__->setup;
54}
007935b8 55use Test::WWW::Mechanize::Catalyst qw/AuthTestApp/;
007935b8 56my $mech = Test::WWW::Mechanize::Catalyst->new;
007935b8 57$mech->get("http://localhost/moose");
58is( $mech->status, 401, "status is 401" );
007935b8 59my $www_auth = $mech->res->headers->header('WWW-Authenticate');
a14203f8 60my %www_auth_params = map {
a14203f8 61 my @key_val = split /=/, $_, 2;
a14203f8 62 $key_val[0] = lc $key_val[0];
a14203f8 63 $key_val[1] =~ s{"}{}g; # remove the quotes
a14203f8 64 @key_val;
007935b8 65} split /, /, substr( $www_auth, 7 ); #7 == length "Digest "
007935b8 66$mech->content_lacks( "foo", "no output" );
007935b8 67my $response = '';
68{
a14203f8 69 my $username = 'Mufasa';
a14203f8 70 my $password = 'Circle Of Life';
a14203f8 71 my $realm = $www_auth_params{realm};
a14203f8 72 my $nonce = $www_auth_params{nonce};
a14203f8 73 my $cnonce = '0a4f113b';
a14203f8 74 my $opaque = $www_auth_params{opaque};
a14203f8 75 my $nc = '00000001';
a14203f8 76 my $method = 'GET';
a14203f8 77 my $qop = 'auth';
007935b8 78 my $uri = '/moose';
a14203f8 79 my $ctx = Digest::MD5->new;
a14203f8 80 $ctx->add( join( ':', $username, $realm, $password ) );
007935b8 81 my $A1_digest = $ctx->hexdigest;
a14203f8 82 $ctx = Digest::MD5->new;
a14203f8 83 $ctx->add( join( ':', $method, $uri ) );
007935b8 84 my $A2_digest = $ctx->hexdigest;
a14203f8 85 my $digest = Digest::MD5::md5_hex(
a14203f8 86 join( ':',
a14203f8 87 $A1_digest, $nonce, $qop ? ( $nc, $cnonce, $qop ) : (), $A2_digest )
a14203f8 88 );
89
007935b8 90 $response = qq{Digest username="$username", realm="$realm", nonce="$nonce", uri="$uri", qop=$qop, nc=$nc, cnonce="$cnonce", response="$digest", opaque="$opaque"};
91}
007935b8 92my $r = HTTP::Request->new( GET => "http://localhost/moose" );
93$mech->request($r);
007935b8 94$r->headers->push_header( Authorization => $response );
95$mech->request($r);
007935b8 96is( $mech->status, 200, "status is 200" );
97$mech->content_contains( "Mufasa", "Mufasa output" );
513d8ab6 98