Auth.pm

filter: Statementstmt 24.7% 22 / 89 Branchbran 5.8% 2 / 34 Conditioncond 9.6% 3 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 45.4% 5 / 11 Podpod n/a totaltotal 17.4% 32 / 183 CC 31 SCAR 39.2
CC
31
Cov
18%
consent_url
132.0
refresh_url
42.0
refresh_consent
30.0
CRAP 50.4
« Auth/ConsentToken.pm ↑ Summary Auth/Utils.pm »
1 package WWW::Live::Auth;
2  
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  
6 1 ▶use WWW::Live::Auth::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 require WWW::Live::Auth::SecretKey;
10 require WWW::Live::Auth::ApplicationToken;
11 require WWW::Live::Auth::ConsentToken;
12 require LWP::UserAgent;
13 require Crypt::SSLeay; # explicitly require, otherwise you get cryptic https failures with LWP
14 require CGI;
15  
16 our $VERSION = '1.0.1';
17 our $CONSENT_BASE_URL = 'https://consent.live.com/';
18  
19 1 ▶sub new {
Subroutine new: called CC=6 SCAR=20.2
20 1 my ( $proto, %options ) = @_;
21 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
22  
23 1 my $app_id = delete $options{'application_id'};
24 1 my $secret_key = delete $options{'secret_key'};
25 1 my $client_ip = delete $options{'client_ip'};
26 1 ▶ if ( $secret_key && !ref $secret_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secret_key and not ref $secret_key) 0 1 1
Condition33%1/3
$secret_key and not ref $secret_key
!ll&&!rl&&r
100
MC/DC0%0/2
$secret_key and not ref $secret_key
$secret_key not ref $secret_key
Truth table33%1/3
A and not(B)
A= $secret_keyB= not ref $secret_key
ABresult
0X0
100
111
27 0 $secret_key = WWW::Live::Auth::SecretKey->new( $secret_key );
28 }
29  
30 1 ▶ $options{'agent'} ||= __PACKAGE__ . "/$VERSION";
Condition50%1/2
$options{"agent"} ||= 'WWW::Live::Auth' . "/$WWW::Live::Auth::VERSION"
l!l
01
MC/DC0%0/1
$options{"agent"} ||= 'WWW::Live::Auth' . "/$WWW::Live::Auth::VERSION"
$options{"agent"}
Truth table50%1/2
A
A= $options{"agent"}
Aresult
11
00
31 my $self = bless {
32 'secret_key' => $secret_key,
33 'application_id' => $app_id,
34 '_ua' => LWP::UserAgent->new( %options ),
35 1 'debug' => delete $options{'debug'},
36 }, $class;
37 1 ▶ $self->{'client_ip'} = $client_ip if ( $client_ip );
Branch: 1 branch
BranchTrueFalseTotal
if $client_ip 0 1 1
38  
39 1 return $self;
40 }
41  
42 0 ▶sub proxy {
Subroutine proxy: not called CC=1 SCAR=6.9
43 0 my $self = shift;
44 0 return $self->{'_ua'}->proxy( 'https', shift );
45 }
46  
47 0 ▶sub consent_url {
Subroutine consent_url: not called CC=11 SCAR=48.8
48 0 my ( $self, %args ) = @_;
49  
50 0 ▶ my $offers = $args{'offers'} || croak('List of offers is required');
Condition0%0/2
$args{'offers'} || croak('List of offers is required')
l!l
00
MC/DC0%0/1
$args{'offers'} || croak('List of offers is required')
$args{'offers'}
Truth table0%0/2
A
A= $args{'offers'}
Aresult
11
00
51 0 ▶ my $privacy = $args{'privacy_url'} || croak('Privacy policy URL is required');
Condition0%0/2
$args{'privacy_url'} || croak('Privacy policy URL is required')
l!l
00
MC/DC0%0/1
$args{'privacy_url'} || croak('Privacy policy URL is required')
$args{'privacy_url'}
Truth table0%0/2
A
A= $args{'privacy_url'}
Aresult
11
00
52 0 ▶ my $secret = $self->{'secret_key'} || croak('Secret key is required');
Condition0%0/2
$self->{'secret_key'} || croak('Secret key is required')
l!l
00
MC/DC0%0/1
$self->{'secret_key'} || croak('Secret key is required')
$self->{'secret_key'}
Truth table0%0/2
A
A= $self->{'secret_key'}
Aresult
11
00
53 0 ▶ my $app_id = $self->{'application_id'} || croak('Application ID is required');
Condition0%0/2
$self->{'application_id'} || croak('Application ID is required')
l!l
00
MC/DC0%0/1
$self->{'application_id'} || croak('Application ID is required')
$self->{'application_id'}
Truth table0%0/2
A
A= $self->{'application_id'}
Aresult
11
00
54  
55 0 ▶ if ( ref $offers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $offers) 0 0 0
56 0 ▶ if ( ref $offers ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $offers ne "ARRAY") 0 0 0
57 0 $offers = [ $offers ];
58 }
59 $offers = join ',', map {
60 0 ▶ ref $_ ? $_->offer . '.' . $_->action : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
61 0 } @{ $offers };
62 }
63  
64 # https://consent.live.com/Delegation.aspx?RU=...&ps=...&pl=...[&app=...][&mkt=...][&appctx=...]
65 0 my $url = sprintf $CONSENT_BASE_URL.'Delegation.aspx?ps=%s&pl=%s',
66 _escape( $offers ), _escape( $privacy );
67
68 0 ▶ if ( $args{'return_url'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"return_url"}) 0 0 0
69 0 $url .= '&RU=' . _escape( $args{'return_url'} );
70 }
71  
72 # Client IP address is optional
73 my $app_token = WWW::Live::Auth::ApplicationToken->new(
74 $secret->signature_key,
75 $app_id,
76 0 $self->{'client_ip'}
77 )->as_string;
78 0 $url .= sprintf '&app=%s', $app_token;
79
80 0 ▶ if ( $args{'market'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"market"}) 0 0 0
81 0 $url .= '&mkt=' . _escape( $args{'market'} );
82 }
83
84 0 ▶ if ( $args{'context'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"context"}) 0 0 0
85 0 $url .= '&appctx=' . _escape( $args{'context'} );
86 }
87
88 0 return $url;
89 }
90  
91 0 ▶sub refresh_url {
Subroutine refresh_url: not called CC=6 SCAR=37.4
92 0 my ( $self, %args ) = @_;
93 0 ▶ my $consent_token = $args{'consent_token'} || croak('Consent token is required to construct a refresh URL');
Condition0%0/2
$args{'consent_token'} || croak('Consent token is required to construct a refresh URL')
l!l
00
MC/DC0%0/1
$args{'consent_token'} || croak('Consent token is required to construct a refresh URL')
$args{'consent_token'}
Truth table0%0/2
A
A= $args{'consent_token'}
Aresult
11
00
94 0 ▶ my $secret = $self->{'secret_key'} || croak('Secret key is required to construct a refresh URL');
Condition0%0/2
$self->{'secret_key'} || croak('Secret key is required to construct a refresh URL')
l!l
00
MC/DC0%0/1
$self->{'secret_key'} || croak('Secret key is required to construct a refresh URL')
$self->{'secret_key'}
Truth table0%0/2
A
A= $self->{'secret_key'}
Aresult
11
00
95 0 ▶ my $app_id = $self->{'application_id'} || croak('Application ID is required to construct a refresh URL');
Condition0%0/2
$self->{'application_id'} || croak('Application ID is required to construct a refresh URL')
l!l
00
MC/DC0%0/1
$self->{'application_id'} || croak('Application ID is required to construct a refresh URL')
$self->{'application_id'}
Truth table0%0/2
A
A= $self->{'application_id'}
Aresult
11
00
96
97 0 ▶ if ( !ref $consent_token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $consent_token) 0 0 0
98 0 $consent_token = WWW::Live::Auth::ConsentToken->new(
99 'consent_token' => $consent_token,
100 'secret_key' => $secret,
101 );
102 }
103
104 0 my $offers = join ',', map { $_->offer.'.'.$_->action } $consent_token->offers;
105
106 # https://consent.live.com/RefreshToken.aspx?RU=...&ps=...&reft=...
107 0 my $url = sprintf $CONSENT_BASE_URL.'RefreshToken.aspx?ps=%s&reft=%s',
108 _escape( $offers ),
109 $consent_token->refresh_token;
110
111 0 ▶ if ( $args{'return_url'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"return_url"}) 0 0 0
112 0 $url .= '&ru=' . _escape( $args{'return_url'} );
113 }
114  
115 # Client IP address is optional
116 my $app_token = WWW::Live::Auth::ApplicationToken->new(
117 $secret->signature_key,
118 $app_id,
119 0 $self->{'client_ip'}
120 )->as_string;
121 0 $url .= sprintf '&app=%s', $app_token;
122
123 0 return $url;
124 }
125  
126 0 ▶sub is_delegated_authentication {
Subroutine is_delegated_authentication: not called CC=4 SCAR=30.0
127 0 my ( $self, $cgi ) = @_;
128 0 ▶ $cgi ||= CGI->new();
Condition0%0/3
$cgi ||= "CGI"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$cgi ||= "CGI"->new
$cgi "CGI"->new
Truth table0%0/3
A ||= B
A= $cgiB= "CGI"->new
ABresult
1X1
011
000
129  
130 0 ▶ if ( !$cgi->param('action') || $cgi->param('action') ne 'delauth' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cgi->param("action") or $cgi->param("action") ne "delauth") 0 0 0
Condition0%0/3
not $cgi->param("action") or $cgi->param("action") ne "delauth"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $cgi->param("action") or $cgi->param("action") ne "delauth"
not $cgi->param("action") $cgi->param("action") ne "delauth"
Truth table0%0/3
not(A) or B
A= not $cgi->param("action")B= $cgi->param("action") ne "delauth"
ABresult
1X1
011
000
131 0 return 0;
132 }
133  
134 0 return 1;
135 }
136  
137 0 ▶sub receive_consent {
Subroutine receive_consent: not called CC=4 SCAR=30.0
138 0 my ( $self, $cgi ) = @_;
139 0 ▶ $cgi ||= CGI->new();
Condition0%0/3
$cgi ||= "CGI"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$cgi ||= "CGI"->new
$cgi "CGI"->new
Truth table0%0/3
A ||= B
A= $cgiB= "CGI"->new
ABresult
1X1
011
000
140  
141 # Check we are processing a delegated authentication response
142 0 ▶ if ( ! $self->is_delegated_authentication( $cgi ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->is_delegated_authentication($cgi)) { } 0 0 0
elsif ($cgi->param('ResponseCode') ne 'RequestApproved') { } 0 0 0
143 0 croak('Unable to process consent - request is not a delegated authentication');
144 } elsif ( $cgi->param('ResponseCode') ne 'RequestApproved' ) {
145 0 croak('Authentication denied');
146 }
147
148 my $consent_token = WWW::Live::Auth::ConsentToken->new(
149 0 'secret_key' => $self->{'secret_key'},
150 'consent_token' => $cgi->param('ConsentToken'),
151 );
152
153 0 my $app_context = _unescape( $cgi->param('appctx') );
154
155 0 return $consent_token, $app_context;
156 }
157  
158 0 ▶sub refresh_consent {
Subroutine refresh_consent: not called CC=5 SCAR=34.0
159 0 my $self = shift;
160 0 my $url = $self->refresh_url( @_ );
161
162 0 ▶ if ( $self->{'debug'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
163 0 warn "About to GET $url";
164 }
165
166 0 my $request = HTTP::Request->new(GET => $url);
167 0 my $response = $self->{'_ua'}->request( $request );
168 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
169 # {"ConsentToken":"delt%3dEwCoARAn ..."}
170 0 my $raw = $response->content;
171
172 0 my ($error, $msg) = $raw =~ m/"error":"(.+)"}(.+)/mxs;
173 0 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
174 0 croak("Could not refresh consent token: $error - $msg");
175 }
176
177 0 my ($consent_token) = $raw =~ m/"ConsentToken":"(.+)"/mxs;
178 0 ▶ if ( !$consent_token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($consent_token) 0 0 0
179 0 return;
180 }
181 return WWW::Live::Auth::ConsentToken->new(
182 0 'secret_key' => $self->{'secret_key'},
183 'consent_token' => $consent_token,
184 );
185
186 } else {
187 0 croak( 'Could not contact Live service: ' . $response->status_line );
188 }
189 }
190  
191 1;
192 __END__
« Auth/ConsentToken.pm ↑ Summary Auth/Utils.pm »