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