| 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.0'; | ||||||||||||||||||||||||||||
| 17 | our $CONSENT_BASE_URL = 'https://consent.live.com/'; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | my ( $proto, %options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=20.2
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$secret_key and not ref $secret_key
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
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$options{"agent"} ||= 'WWW::Live::Auth' . "/$WWW::Live::Auth::VERSION"
$options{"agent"}
Truth table50%1/2
A
A= $options{"agent"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | return $self; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub proxy { | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
proxy: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 0 | return $self->{'_ua'}->proxy( 'https', shift ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub consent_url { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
consent_url: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $offers = $args{'offers'} || croak('List of offers is required'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{'offers'} || croak('List of offers is required')
MC/DC0%0/2
$args{'offers'} || croak('List of offers is required')
$args{'offers'}
croak('List of offers is required')
Truth table0%0/3
A || B
A= $args{'offers'}B= croak('List of offers is required')
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $privacy = $args{'privacy_url'} || croak('Privacy policy URL is required'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{'privacy_url'} || croak('Privacy policy URL is required')
MC/DC0%0/2
$args{'privacy_url'} || croak('Privacy policy URL is required')
$args{'privacy_url'}
croak('Privacy policy URL is required')
Truth table0%0/3
A || B
A= $args{'privacy_url'}B= croak('Privacy policy URL is required')
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my $secret = $self->{'secret_key'} || croak('Secret key is required'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'secret_key'} || croak('Secret key is required')
MC/DC0%0/2
$self->{'secret_key'} || croak('Secret key is required')
$self->{'secret_key'}
croak('Secret key is required')
Truth table0%0/3
A || B
A= $self->{'secret_key'}B= croak('Secret key is required')
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $app_id = $self->{'application_id'} || croak('Application ID is required'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'application_id'} || croak('Application ID is required')
MC/DC0%0/2
$self->{'application_id'} || croak('Application ID is required')
$self->{'application_id'}
croak('Application ID is required')
Truth table0%0/3
A || B
A= $self->{'application_id'}B= croak('Application ID is required')
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( ref $offers ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( ref $offers ne 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $offers = [ $offers ]; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | $offers = join ',', map { | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | ref $_ ? $_->offer . '.' . $_->action : $_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 81 | 0 | $url .= '&mkt=' . _escape( $args{'market'} ); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( $args{'context'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | $url .= '&appctx=' . _escape( $args{'context'} ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | return $url; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub refresh_url { | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
refresh_url: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $consent_token = $args{'consent_token'} || croak('Consent token is required to construct a refresh URL'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{'consent_token'} || croak('Consent token is required to construct a refresh URL')
MC/DC0%0/2
$args{'consent_token'} || croak('Consent token is required to construct a refresh URL')
$args{'consent_token'}
croak('Consent token is required to construct a refresh URL')
Truth table0%0/3
A || B
A= $args{'consent_token'}B= croak('Consent token is required to construct a refresh URL')
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $secret = $self->{'secret_key'} || croak('Secret key is required to construct a refresh URL'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'secret_key'} || croak('Secret key is required to construct a refresh URL')
MC/DC0%0/2
$self->{'secret_key'} || croak('Secret key is required to construct a refresh URL')
$self->{'secret_key'}
croak('Secret key is required to construct a refresh URL')
Truth table0%0/3
A || B
A= $self->{'secret_key'}B= croak('Secret key is required to construct a refresh URL')
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my $app_id = $self->{'application_id'} || croak('Application ID is required to construct a refresh URL'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'application_id'} || croak('Application ID is required to construct a refresh URL')
MC/DC0%0/2
$self->{'application_id'} || croak('Application ID is required to construct a refresh URL')
$self->{'application_id'}
croak('Application ID is required to construct a refresh URL')
Truth table0%0/3
A || B
A= $self->{'application_id'}B= croak('Application ID is required to construct a refresh URL')
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( !ref $consent_token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | sub is_delegated_authentication { | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my ( $self, $cgi ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_delegated_authentication: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $cgi ||= CGI->new(); | ||||||||||||||||||||||||||
|
Condition0%0/3
$cgi ||= "CGI"->new
MC/DC0%0/2
$cgi ||= "CGI"->new
$cgi
"CGI"->new
Truth table0%0/3
A ||= B
A= $cgiB= "CGI"->new
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ( !$cgi->param('action') || $cgi->param('action') ne 'delauth' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $cgi->param("action") or $cgi->param("action") ne "delauth"
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"
| |||||||||||||||||||||||||||||
| 131 | 0 | return 0; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | return 1; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub receive_consent { | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my ( $self, $cgi ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
receive_consent: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $cgi ||= CGI->new(); | ||||||||||||||||||||||||||
|
Condition0%0/3
$cgi ||= "CGI"->new
MC/DC0%0/2
$cgi ||= "CGI"->new
$cgi
"CGI"->new
Truth table0%0/3
A ||= B
A= $cgiB= "CGI"->new
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Check we are processing a delegated authentication response | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ( ! $self->is_delegated_authentication( $cgi ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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 | sub refresh_consent { | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
refresh_consent: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 160 | 0 | my $url = $self->refresh_url( @_ ); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if ( $self->{'debug'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||