| 1 | package Mojo::UserAgent::Role::DigestAuth; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base -role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Mojo::Util qw(md5_sum); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use constant DEBUG => $ENV{MOJO_USERAGENT_DIGEST_AUTH_DEBUG} || 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_USERAGENT_DIGEST_AUTH_DEBUG'} || 0
MC/DC0%0/1
$ENV{'MOJO_USERAGENT_DIGEST_AUTH_DEBUG'} || 0
$ENV{'MOJO_USERAGENT_DIGEST_AUTH_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_USERAGENT_DIGEST_AUTH_DEBUG'}
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | my $NC = 0; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | around start => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.1
| |||||||||||||||||||||||||||||
| 10 | 3 | my ($next, $self, $tx, $cb) = @_; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | my %auth; | |||||||||||||||||||||||||||
| 13 | 3 | ▶ | @auth{qw(username password)} = split ':', $tx->req->url->userinfo || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$tx->req->url->userinfo || ''
MC/DC100%1/1
$tx->req->url->userinfo || ''
$tx->req->url->userinfo
Truth table100%2/2
A
A= $tx->req->url->userinfo
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | if (my $client_nonce = $tx->req->headers->header('D-Client-Nonce')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 1 | $auth{client_nonce} = $client_nonce; | |||||||||||||||||||||||||||
| 17 | 1 | $tx->req->headers->remove('D-Client-Nonce'); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | $tx->req->url($tx->req->url->clone)->url->userinfo(undef); | |||||||||||||||||||||||||||
| 21 | 3 | warn "[DigestAuth] url=@{[$tx->req->url]}\n" if DEBUG; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Blocking | ||||||||||||||||||||||||||||
| 24 | 3 | ▶ | unless ($cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 3 | my $next_tx = $self->_digest_auth_build_next_tx($self->$next($tx), \%auth); | |||||||||||||||||||||||||||
| 26 | 3 | ▶ | return $next_tx eq $tx ? $tx : $self->$next($next_tx); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Non-blocking | ||||||||||||||||||||||||||||
| 30 | return $self->$next( | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $tx => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($self, $tx) = @_; | |||||||||||||||||||||||||||
| 33 | 0 | my $next_tx = $self->_digest_auth_build_next_tx($tx, \%auth); | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | return $next_tx eq $tx ? $self->$cb($tx) : $self->$next($next_tx, $cb); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 0 | ); | |||||||||||||||||||||||||||
| 37 | 1 | }; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 3 | ▶ | sub _digest_auth_build_next_tx { | ||||||||||||||||||||||||||
|
Subroutine
_digest_auth_build_next_tx: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 40 | 3 | my ($self, $tx, $auth) = @_; | |||||||||||||||||||||||||||
| 41 | 3 | ▶ | my $code = $tx->res->code || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$tx->res->code || ''
MC/DC0%0/1
$tx->res->code || ''
$tx->res->code
Truth table50%1/2
A
A= $tx->res->code
| |||||||||||||||||||||||||||||
| 42 | 3 | warn "[DigestAuth] code=$code\n" if DEBUG; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Return unless we got a digest auth response | ||||||||||||||||||||||||||||
| 45 | return $tx | ||||||||||||||||||||||||||||
| 46 | 3 | ▶ | unless 3 == grep { defined $_ } @$auth{qw(username password)}, $tx->res->headers->header('WWW-Authenticate'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Build a new transaction | ||||||||||||||||||||||||||||
| 49 | 2 | warn "[DigestAuth] Digest authorization...\n" if DEBUG; | |||||||||||||||||||||||||||
| 50 | 2 | my $next_tx = Mojo::Transaction::HTTP->new(req => $tx->req->clone); | |||||||||||||||||||||||||||
| 51 | 2 | $next_tx->req->headers->authorization(sprintf 'Digest %s', join ', ', $self->_digest_auth_kv($tx, $auth)); | |||||||||||||||||||||||||||
| 52 | 2 | $next_tx->req->headers->accept('*/*'); | |||||||||||||||||||||||||||
| 53 | 2 | $next_tx->req->body($tx->req->body); | |||||||||||||||||||||||||||
| 54 | 2 | return $next_tx; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub _digest_auth_clean_tx { | ||||||||||||||||||||||||||
|
Subroutine
_digest_auth_clean_tx: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 0 | my ($self, $tx) = @_; | |||||||||||||||||||||||||||
| 59 | 0 | return $tx; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub _digest_auth_kv { | ||||||||||||||||||||||||||
|
Subroutine
_digest_auth_kv: called CC=8 SCAR=23.0
| |||||||||||||||||||||||||||||
| 63 | 2 | my ($self, $tx, $args) = @_; | |||||||||||||||||||||||||||
| 64 | 2 | my %auth_param = $tx->res->headers->header('WWW-Authenticate') =~ /(\w+)="?([^",]+)"?/g; | |||||||||||||||||||||||||||
| 65 | 2 | my $nc = sprintf '%08X', ++$NC; | |||||||||||||||||||||||||||
| 66 | 2 | my ($ha1, $ha2, $response); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | ▶ | $auth_param{client_nonce} = $args->{client_nonce} // _generate_nonce(time); | ||||||||||||||||||||||||||
|
Condition66%2/3
$args->{"client_nonce"} // _generate_nonce(time)
MC/DC0%0/2
$args->{"client_nonce"} // _generate_nonce(time)
$args->{"client_nonce"}
_generate_nonce(time)
Truth table66%2/3
A // B
A= $args->{"client_nonce"}B= _generate_nonce(time)
| |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | $auth_param{nonce} //= '__UNDEF__'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$auth_param{"nonce"} //= "__UNDEF__"
MC/DC0%0/1
$auth_param{"nonce"} //= "__UNDEF__"
$auth_param{"nonce"}
Truth table50%1/2
A
A= $auth_param{"nonce"}
| |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | $auth_param{realm} //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$auth_param{"realm"} //= ""
MC/DC0%0/1
$auth_param{"realm"} //= ""
$auth_param{"realm"}
Truth table50%1/2
A
A= $auth_param{"realm"}
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | $ha1 = _generate_ha1(\%auth_param, @$args{qw( username password )}); | |||||||||||||||||||||||||||
| 73 | 2 | $ha2 = _generate_ha2(\%auth_param, $tx->req); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | if ($auth_param{qop} and $auth_param{qop} =~ /^auth/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$auth_param{"qop"} and $auth_param{"qop"} =~ /^auth/u
MC/DC0%0/2
$auth_param{"qop"} and $auth_param{"qop"} =~ /^auth/u
$auth_param{"qop"}
$auth_param{"qop"} =~ /^auth/u
Truth table33%1/3
A and B
A= $auth_param{"qop"}B= $auth_param{"qop"} =~ /^auth/u
| |||||||||||||||||||||||||||||
| 76 | 2 | $response = md5_sum join ':', $ha1, $auth_param{nonce}, $nc, $auth_param{client_nonce}, $auth_param{qop}, $ha2; | |||||||||||||||||||||||||||
| 77 | 2 | warn "RESPONSE: MD5($ha1:$auth_param{nonce}:$nc:$auth_param{client_nonce}:$auth_param{qop}:$ha2) = $response\n" | |||||||||||||||||||||||||||
| 78 | if DEBUG; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||
| 81 | 0 | $response = md5_sum join ':', $ha1, $auth_param{nonce}, $ha2; | |||||||||||||||||||||||||||
| 82 | 0 | warn "RESPONSE: MD5($ha1:$auth_param{nonce}:$ha2) = $response\n" if DEBUG; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | return ( | ||||||||||||||||||||||||||||
| 86 | qq(username="$args->{username}"), qq(realm="$auth_param{realm}"), | ||||||||||||||||||||||||||||
| 87 | 2 | ▶ | qq(nonce="$auth_param{nonce}"), qq(uri="@{[$tx->req->url->path]}"), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | $auth_param{qop} ? ("qop=$auth_param{qop}") : (), "nc=$nc", | ||||||||||||||||||||||||||||
| 89 | qq(cnonce="$auth_param{client_nonce}"), qq(response="$response"), | ||||||||||||||||||||||||||||
| 90 | 2 | $auth_param{opaque} ? (qq(opaque="$auth_param{opaque}")) : (), qq(algorithm="MD5"), | |||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub _generate_nonce { | ||||||||||||||||||||||||||
|
Subroutine
_generate_nonce: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 1 | my $time = shift; | |||||||||||||||||||||||||||
| 96 | 1 | my $nonce = Mojo::Util::b64_encode(join ' ', $time, Mojo::Util::hmac_sha1_sum($time), ''); | |||||||||||||||||||||||||||
| 97 | 1 | chomp $nonce; | |||||||||||||||||||||||||||
| 98 | 1 | $nonce =~ s!=+$!!; | |||||||||||||||||||||||||||
| 99 | 1 | return $nonce; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | sub _generate_ha1 { | ||||||||||||||||||||||||||
|
Subroutine
_generate_ha1: called CC=3 SCAR=12.6
| |||||||||||||||||||||||||||||
| 103 | 2 | my ($auth_param, $username, $password) = @_; | |||||||||||||||||||||||||||
| 104 | 2 | my $res; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | if (!$auth_param->{algorithm} or $auth_param->{algorithm} eq 'MD5') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $auth_param->{"algorithm"} or $auth_param->{"algorithm"} eq "MD5"
MC/DC0%0/2
not $auth_param->{"algorithm"} or $auth_param->{"algorithm"} eq "MD5"
not $auth_param->{"algorithm"}
$auth_param->{"algorithm"} eq "MD5"
Truth table33%1/3
not(A) or B
A= not $auth_param->{"algorithm"}B= $auth_param->{"algorithm"} eq "MD5"
| |||||||||||||||||||||||||||||
| 107 | 2 | $res = md5_sum join ':', $username, $auth_param->{realm}, $password; | |||||||||||||||||||||||||||
| 108 | 2 | warn "HA1: MD5($username:$auth_param->{realm}:$password) = $res\n" if DEBUG; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||
| 111 | $res = md5_sum md5_sum(join ':', $username, $auth_param->{realm}, $password), $auth_param->{nonce}, | ||||||||||||||||||||||||||||
| 112 | 0 | $auth_param->{client_nonce}; | |||||||||||||||||||||||||||
| 113 | 0 | warn | |||||||||||||||||||||||||||
| 114 | "HA1: MD5(MD5($username:$auth_param->{realm}:$password), $auth_param->{nonce}, $auth_param->{client_nonce}) = $res\n" | ||||||||||||||||||||||||||||
| 115 | if DEBUG; | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | return $res; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | ▶ | sub _generate_ha2 { | ||||||||||||||||||||||||||
|
Subroutine
_generate_ha2: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||
| 122 | 2 | my ($auth_param, $req) = @_; | |||||||||||||||||||||||||||
| 123 | 2 | my $method = uc $req->method; | |||||||||||||||||||||||||||
| 124 | 2 | my $res; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | ▶ | if (!$auth_param->{qop} or $auth_param->{qop} eq 'auth') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $auth_param->{"qop"} or $auth_param->{"qop"} eq "auth"
MC/DC0%0/2
not $auth_param->{"qop"} or $auth_param->{"qop"} eq "auth"
not $auth_param->{"qop"}
$auth_param->{"qop"} eq "auth"
Truth table33%1/3
not(A) or B
A= not $auth_param->{"qop"}B= $auth_param->{"qop"} eq "auth"
| |||||||||||||||||||||||||||||
| 127 | 2 | $res = md5_sum join ':', $method, $req->url->path; | |||||||||||||||||||||||||||
| 128 | 2 | warn "HA2: MD5($method:@{[$req->url->path]}) = $res\n" if DEBUG; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||
| 131 | 0 | $res = md5_sum join ':', $method, $req->url->path, md5_sum('entityBody'); # TODO: entityBody | |||||||||||||||||||||||||||
| 132 | 0 | warn "HA2: MD5(TODO) = $res\n" if DEBUG; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | return $res; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | 1; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =encoding utf8 | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 NAME | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Mojo::UserAgent::Role::DigestAuth - Allow Mojo::UserAgent to execute digest auth requests | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | L<Mojo::UserAgent::Role::DigestAuth> is a L<Mojo::UserAgent> role that can | ||||||||||||||||||||||||||||
| 149 | handle 401 digest auth responses from the server. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | See L<http://en.wikipedia.org/wiki/Digest_access_authentication>. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | use Mojo::UserAgent; | ||||||||||||||||||||||||||||
| 156 | my $ua = Mojo::UserAgent->with_roles('+DigestAuth')->new; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # blocking | ||||||||||||||||||||||||||||
| 159 | $tx = $ua->get($url); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # non-blocking | ||||||||||||||||||||||||||||
| 162 | $ua = $ua->start($ua->build_tx($method, $url, $headers, $cb)); | ||||||||||||||||||||||||||||
| 163 | $ua = $ua->post($method, $url, $cb); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # promise based | ||||||||||||||||||||||||||||
| 166 | $p = $ua->post_p($method, $url)->then(sub { ... }); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | A custom client nonce can be specified by using a special "D-Client-Nonce" | ||||||||||||||||||||||||||||
| 169 | header. This is a hack to work around servers which does not understand the | ||||||||||||||||||||||||||||
| 170 | nonce generated by this module. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Note that this feature is EXPERIMENTAL and might be removed once I figure | ||||||||||||||||||||||||||||
| 173 | out why the random nonce L<does not work|https://github.com/jhthorsen/mojo-useragent-digestauth/issues/1> | ||||||||||||||||||||||||||||
| 174 | for all servers. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | $tx = $ua->get('http://example.com', { 'D-Client-Nonce' => '0e163838ccd62299' }); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Copyright (C) 2014-2021, Jan Henning Thorsen | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 183 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||