Plugin/Authentication/Credential/Atom.pm

filter: Statementstmt 36.0% 22 / 61 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 63.6% 7 / 11 Podpod 0.0% 0 / 1 totaltotal 29.2% 29 / 99 CC 12 SCAR 31.6
CC
12
Cov
27%
_extract_credentials
56.0
login_atom
20.0
_validate_credentials
12.0
CRAP 23.5
« Plugin/AtomServer.pm ↑ Summary View/Atom/XML.pm »
1 # $Id: Atom.pm 1222 2006-04-22 04:23:19Z btrott $
2  
3 package Catalyst::Plugin::Authentication::Credential::Atom;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw( Catalyst::Plugin::Authentication::Credential::Password );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use MIME::Base64 qw( encode_base64 decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Digest::SHA1 qw( sha1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::Atom::Util qw( first textValue );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use constant NS_WSSE => 'http://schemas.xmlsoap.org/ws/2002/07/secext';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant NS_WSU => 'http://schemas.xmlsoap.org/ws/2002/07/utility';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub login_atom {
Subroutine login_atom: not called CC=4 SCAR=30.0
Pod undocumented
15 0 my $c = shift;
16 0 my($username, $cred) = $c->_extract_credentials;
17 0 ▶ unless ($username) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($username) 0 0 0
18 0 return $c->_atom_auth_error(401);
19 }
20  
21 0 ▶ if (my $user = $c->get_user($username)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $user = $c->get_user($username)) 0 0 0
22 0 ▶ if ($c->_validate_credentials($user, $cred)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->_validate_credentials($user, $cred)) 0 0 0
23 0 $c->set_authenticated($user);
24 0 return $username;
25 }
26 }
27 0 return $c->_atom_auth_error(403);
28 }
29  
30 0 ▶sub _atom_auth_error {
Subroutine _atom_auth_error: not called CC=1 SCAR=6.9
31 0 my $c = shift;
32 0 my($code) = @_;
33 0 $c->response->status($code);
34 0 $c->response->header('WWW-Authenticate',
35 'WSSE profile="UsernameToken", Basic');
36 0 return 0;
37 }
38  
39 0 ▶sub _extract_credentials {
Subroutine _extract_credentials: not called CC=7 SCAR=40.3
40 0 my $c = shift;
41 0 my $req = $c->request;
42 0 my($tokens, $username, %cred);
43 ## SOAP wrapper only supports WSSE?
44 0 ▶ if ($req->is_soap) {
Branch: 3 branches
BranchTrueFalseTotal
if ($req->is_soap) { } 0 0 0
elsif ($req->header('Authorization') and ($tokens) = $req->header('Authorization') =~ /^Basic (.+)$/) { } 0 0 0
elsif ($req->header('X-WSSE') and ($tokens) = $req->header('X-WSSE') =~ /^UsernameToken (.+)$/) { } 0 0 0
45 0 my $xml = $req->body_parsed;
46 0 my $auth = first($xml, NS_WSSE, 'UsernameToken');
47 0 $username = $cred{Username} = textValue($auth, NS_WSSE, 'Username');
48 0 $cred{PasswordDigest} = textValue($auth, NS_WSSE, 'Password');
49 0 $cred{Nonce} = textValue($auth, NS_WSSE, 'Nonce');
50 0 $cred{Created} = textValue($auth, NS_WSU, 'Created');
51 }
52
53 ## Basic auth.
54 ▶ elsif ($req->header('Authorization') and
Condition0%0/3
$req->header("Authorization") and ($tokens) = $req->header("Authorization") =~ /^Basic (.+)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$req->header("Authorization") and ($tokens) = $req->header("Authorization") =~ /^Basic (.+)$/
$req->header("Authorization") ($tokens) = $req->header("Authorization") =~ /^Basic (.+)$/
Truth table0%0/3
A and B
A= $req->header("Authorization")B= ($tokens) = $req->header("Authorization") =~ /^Basic (.+)$/
ABresult
0X0
100
111
55 ($tokens) = $req->header('Authorization') =~ /^Basic (.+)$/) {
56 0 ($username, $cred{password})= split /:/, decode_base64($tokens);
57 }
58
59 ## WSSE-via-HTTP-headers.
60 ▶ elsif ($req->header('X-WSSE') and
Condition0%0/3
$req->header("X-WSSE") and ($tokens) = $req->header("X-WSSE") =~ /^UsernameToken (.+)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$req->header("X-WSSE") and ($tokens) = $req->header("X-WSSE") =~ /^UsernameToken (.+)$/
$req->header("X-WSSE") ($tokens) = $req->header("X-WSSE") =~ /^UsernameToken (.+)$/
Truth table0%0/3
A and B
A= $req->header("X-WSSE")B= ($tokens) = $req->header("X-WSSE") =~ /^UsernameToken (.+)$/
ABresult
0X0
100
111
61 ($tokens) = $req->header('X-WSSE') =~ /^UsernameToken (.+)$/) {
62 0 for my $pair (split /\s*,\s*/, $tokens) {
63 0 my($k, $v) = split /=/, $pair, 2;
64 0 $v =~ s/^"//;
65 0 $v =~ s/"$//;
66 0 $cred{$k} = $v;
67 }
68 0 $username = delete $cred{Username};
69 }
70 0 ($username, \%cred);
71 }
72  
73 0 ▶sub _validate_credentials {
Subroutine _validate_credentials: not called CC=3 SCAR=24.8
74 0 my $c = shift;
75 0 my($user, $cred) = @_;
76 0 ▶ if ($cred->{password}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cred->{'password'}) { } 0 0 0
elsif ($cred->{'PasswordDigest'}) { } 0 0 0
77 return $c->_check_password($user, $cred->{password})
78 0 } elsif ($cred->{PasswordDigest}) {
79 0 my $pass = $user->password;
80 my $expected = encode_base64(sha1(
81 0 decode_base64($cred->{Nonce}) . $cred->{Created} . $pass
82 ), '');
83 0 return $expected eq $cred->{PasswordDigest};
84 }
85 }
86  
87 1 1;
88 __END__
« Plugin/AtomServer.pm ↑ Summary View/Atom/XML.pm »