blib/lib/LWP/Authen/Wsse.pm

filter: Statementstmt 44.0% 22 / 50 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 3 totaltotal 31.1% 29 / 93 CC 10 SCAR 40.7
CC
10
Cov
30%
authenticate
110.0
make_nonce
2.0
now_w3cdtf
2.0
CRAP 58.5
↑ Summary
1 package LWP::Authen::Wsse;
2 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $LWP::Authen::Wsse::VERSION = '0.05';
8  
9 1 ▶use Digest::SHA1 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 LWP::Authen::Wsse - Library for enabling X-WSSE authentication in LWP
15  
16 =head1 VERSION
17  
18 This document describes version 0.05 of LWP::Authen::Wsse, released
19 December 27, 2005.
20  
21 =head1 SYNOPSIS
22  
23 use LWP::UserAgent;
24 use HTTP::Request::Common;
25 my $url = 'http://www.example.org/protected_page.html';
26  
27 # Set up the WSSE client
28 my $ua = LWP::UserAgent->new;
29 $ua->credentials('example.org', '', 'username', 'password');
30  
31 $request = GET $url;
32 print "--Performing request now...-----------\n";
33 $response = $ua->request($request);
34 print "--Done with request-------------------\n";
35  
36 if ($response->is_success) {
37 print "It worked!->", $response->code, "\n";
38 }
39 else {
40 print "It didn't work!->", $response->code, "\n";
41 }
42  
43 =head1 DESCRIPTION
44  
45 C<LWP::Authen::Wsse> allows LWP to authenticate against servers that are using
46 the C<X-WSSE> authentication scheme, as required by the Atom Authentication API.
47  
48 The module is used indirectly through LWP, rather than including it directly in
49 your code. The LWP system will invoke the WSSE authentication when it
50 encounters the authentication scheme while attempting to retrieve a URL from a
51 server.
52  
53 You also need to set the credentials on the UserAgent object like this:
54  
55 $ua->credentials('www.company.com:80', '', "username", "password");
56  
57 Alternatively, you may also subclass B<LWP::UserAgent> and override the
58 C<get_basic_credentials()> method. See L<LWP::UserAgent> for more details.
59  
60 =cut
61  
62 1 ▶use constant WITHOUT_LINEBREAK => q{};
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 0 ▶sub authenticate {
Subroutine authenticate: not called CC=10 SCAR=47.0
Pod undocumented
65 0 my $class = shift;
66 0 my ( $ua, $proxy, $auth_param, $response, $request, $arg, $size ) = @_;
67  
68 my ( $user, $pass ) = $ua->get_basic_credentials(
69 0 $auth_param->{realm}, $request->url, $proxy
70 );
71  
72 0 ▶ ( defined $user and defined $pass ) or return $response;
Branch: 1 branch
BranchTrueFalseTotal
defined $user and defined $pass or return $response 0 0 0
Condition0%0/3
defined $user and defined $pass
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user and defined $pass
defined $user defined $pass
Truth table0%0/3
A and B
A= defined $userB= defined $pass
ABresult
0X0
100
111
73  
74 0 my $now = $class->now_w3cdtf;
75 0 my $nonce = $class->make_nonce;
76 0 my $nonce_enc = MIME::Base64::encode_base64( $nonce, WITHOUT_LINEBREAK );
77 0 my $digest = MIME::Base64::encode_base64(
78 Digest::SHA1::sha1( $nonce . $now . $pass ), WITHOUT_LINEBREAK
79 );
80  
81 0 ▶ my $auth_header = ( $proxy ? 'Proxy-Authorization' : 'Authorization' );
Branch: 1 branch
BranchTrueFalseTotal
$proxy ? : 0 0 0
82 0 my $wsse_value = 'UsernameToken ' . join( ', ',
83 qq(Username="$user"), qq(PasswordDigest="$digest"),
84 qq(Nonce="$nonce_enc"), qq(Created="$now"),
85 );
86  
87 0 my $referral = $request->clone;
88  
89 # Need to check this isn't a repeated fail!
90 0 my $r = $response;
91 0 my $failed;
92 0 ▶ while ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
93 0 my $prev = $r->request->{wsse_user_pass};
94 0 ▶ if ( $r->code == 401
Branch: 1 branch
BranchTrueFalseTotal
if ($r->code == 401 and $prev and $prev->[0] eq $user and $prev->[1] eq $pass and $failed++) 0 0 0
Condition0%0/12
$r->code == 401 and $prev and $prev->[0] eq $user and $prev->[1] eq $pass and $failed++
!ll&&!rl&&r
000
$r->code == 401 and $prev and $prev->[0] eq $user and $prev->[1] eq $pass
!ll&&!rl&&r
000
$r->code == 401 and $prev and $prev->[0] eq $user
!ll&&!rl&&r
000
$r->code == 401 and $prev
!ll&&!rl&&r
000
MC/DC0%0/5
$r->code == 401 and $prev and $prev->[0] eq $user and $prev->[1] eq $pass and $failed++
$r->code == 401 $prev $prev->[0] eq $user $prev->[1] eq $pass $failed++
Truth table0%0/6
A and B and C and D and E
A= $r->code == 401B= $prevC= $prev->[0] eq $userD= $prev->[1] eq $passE= $failed++
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
95 and $prev
96 and $prev->[0] eq $user
97 and $prev->[1] eq $pass
98 and $failed++ )
99 {
100 # here we know this failed before
101 0 $response->header(
102 'Client-Warning' => "Credentials for '$user' failed before" );
103 0 return $response;
104 }
105 0 $r = $r->previous;
106 }
107  
108 0 $referral->header( $auth_header => 'WSSE profile="UsernameToken"' );
109 0 $referral->header( 'X-WSSE' => $wsse_value );
110  
111 0 $referral->{wsse_user_pass} = [ $user, $pass ];
112  
113 0 $ua->request( $referral, $arg, $size, $response );
114 }
115  
116 0 ▶sub make_nonce {
Subroutine make_nonce: not called CC=1 SCAR=6.9
Pod undocumented
117 0 Digest::SHA1::sha1( time() . {} . rand() . $PID );
118 }
119  
120 0 ▶sub now_w3cdtf {
Subroutine now_w3cdtf: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ( $sec, $min, $hour, $mday, $mon, $year ) = gmtime();
122 0 $mon++;
123 0 $year += 1900;
124  
125 0 sprintf(
126 '%04s-%02s-%02sT%02s:%02s:%02sZ',
127 $year, $mon, $mday, $hour, $min, $sec,
128 );
129 }
130  
131 1 1;
132  
133 =head1 SEE ALSO
134  
135 L<LWP>, L<LWP::UserAgent>, L<lwpcook>.
136  
137 =head1 AUTHORS
138  
139 Audrey Tang E<lt>audrey@audrey.orgE<gt>
140  
141 =head1 COPYRIGHT
142  
143 Copyright 2004, 2005 by Audrey Tang E<lt>audrey@audrey.orgE<gt>.
144  
145 This program is free software; you can redistribute it and/or
146 modify it under the same terms as Perl itself.
147  
148 See L<http://www.perl.com/perl/misc/Artistic.html>
149  
150 =cut
151  
↑ Summary