| 1 | package LWP::UserAgent::msgraph; | ||||||||||||||||||||||||||||
| 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 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use parent 'LWP::UserAgent'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Storable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Data::UUID; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Storable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Net::EmptyPort qw(listen_socket empty_port check_port); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub new($%) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=22 SCAR=39.2
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | my %internals; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | my $class=shift(); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | my %args=@_; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | #This are our lwp-extended options | ||||||||||||||||||||||||||||
| 29 | 1 | for (qw(appid secret grant_type scope persistent sid base store redirect_uri tenant local_port)) { | |||||||||||||||||||||||||||
| 30 | 11 | ▶ | if (exists $args{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 3 | $internals{$_}= $args{$_}; | |||||||||||||||||||||||||||
| 32 | 3 | delete $args{$_}; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | #Some defaults | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | unless (exists $internals{sid} && defined $internals{sid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $internals{"sid"} and defined $internals{"sid"}
MC/DC0%0/2
exists $internals{"sid"} and defined $internals{"sid"}
exists $internals{"sid"}
defined $internals{"sid"}
Truth table33%1/3
A and B
A= exists $internals{"sid"}B= defined $internals{"sid"}
| |||||||||||||||||||||||||||||
| 38 | 1 | my $guid=Data::UUID->new; | |||||||||||||||||||||||||||
| 39 | 1 | $internals{sid}=$guid->create_str(); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my $sid=$internals{sid}; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | $internals{base}='https://graph.microsoft.com/v1.0' unless(exists $internals{base}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | $internals{base} =~ s/\/$//; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | $internals{console}=0 unless (exists $internals{console}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | $internals{expires}=0; | |||||||||||||||||||||||||||
| 50 | 1 | ▶ | $internals{local_port}=8081 unless ($internals{local_port}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | #complain about missing options | ||||||||||||||||||||||||||||
| 53 | 1 | for (qw(appid grant_type tenant)) { | |||||||||||||||||||||||||||
| 54 | 3 | ▶ | croak "Missing mandatory option $_" unless (exists $internals{$_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #Now the persistent thing | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | $internals{persistent}=1 if (exists $internals{store} && ! exists $internals{persistent}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $internals{"store"} and not exists $internals{"persistent"}
MC/DC0%0/2
exists $internals{"store"} and not exists $internals{"persistent"}
exists $internals{"store"}
not exists $internals{"persistent"}
Truth table33%1/3
A and not(B)
A= exists $internals{"store"}B= not exists $internals{"persistent"}
| |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | $internals{persistent}=0 unless (exists $internals{persistent}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | if ($internals{persistent} && ! exists $internals{store}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$internals{"persistent"} and not exists $internals{"store"}
MC/DC0%0/2
$internals{"persistent"} and not exists $internals{"store"}
$internals{"persistent"}
not exists $internals{"store"}
Truth table33%1/3
A and not(B)
A= $internals{"persistent"}B= not exists $internals{"store"}
| |||||||||||||||||||||||||||||
| 62 | 0 | my $tmpdir = File::Spec->tmpdir(); | |||||||||||||||||||||||||||
| 63 | 0 | $internals{store}="$tmpdir/$sid.tmp"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | #We retrieve previous stored runtime data if we persist | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if ($internals{persistent} && -r $internals{store}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$internals{"persistent"} and -r $internals{"store"}
MC/DC0%0/2
$internals{"persistent"} and -r $internals{"store"}
$internals{"persistent"}
-r $internals{"store"}
Truth table33%1/3
A and B
A= $internals{"persistent"}B= -r $internals{"store"}
| |||||||||||||||||||||||||||||
| 68 | 0 | my $stored=retrieve($internals{store}); | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | croak 'Mismatch persistent session' unless ($stored->{sid} eq $sid); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | for (keys %$stored) { | |||||||||||||||||||||||||||
| 71 | 0 | $internals{$_}=$stored->{$_}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | my $self=$class->SUPER::new(%args); | |||||||||||||||||||||||||||
| 76 | 1 | for (keys %internals) { | |||||||||||||||||||||||||||
| 77 | 9 | $self->{$_} = $internals{$_}; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | #We adopt a previous token if there was any | ||||||||||||||||||||||||||||
| 81 | 1 | ▶ | $self->default_header('Authorization' => "Bearer ".$internals{access_token}) if ($internals{persistent} && exists $internals{access_token} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$internals{"persistent"} and exists $internals{"access_token"}
MC/DC0%0/2
$internals{"persistent"} and exists $internals{"access_token"}
$internals{"persistent"}
exists $internals{"access_token"}
Truth table33%1/3
A and B
A= $internals{"persistent"}B= exists $internals{"access_token"}
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | return $self; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub storedata($) { | ||||||||||||||||||||||||||
|
Subroutine
storedata: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 90 | 0 | my $data={}; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | #This is a subset of the runtime data. It's important that the secret is out | ||||||||||||||||||||||||||||
| 93 | 0 | for (qw(access_token expires expires_in refresh_token token_type scope appid sid redirect_uri console)) { | |||||||||||||||||||||||||||
| 94 | 0 | $data->{$_}=$self->{$_}; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | return $data; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub session_dump($) { | ||||||||||||||||||||||||||
|
Subroutine
session_dump: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return to_json($self->storedata()); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub session_restore($$) { | ||||||||||||||||||||||||||
|
Subroutine
session_restore: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 110 | 0 | my $json=shift(); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $data=from_json($json); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | for (qw(access_token expires expires_in refresh_token token_type scope appid sid redirect_uri console)) { | |||||||||||||||||||||||||||
| 115 | 0 | $self->{$_}=$data->{$_}; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | $self->default_header('Authorization' => "Bearer ".$self->{access_token}); | |||||||||||||||||||||||||||
| 119 | 0 | return $self; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub writestore($) { | ||||||||||||||||||||||||||
|
Subroutine
writestore: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | croak 'Wrong writestore call on non-persistant client' unless ($self->{persistent}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | return store $self->storedata(), $self->{store}; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub refreshtoken($) { | ||||||||||||||||||||||||||
|
Subroutine
refreshtoken: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 136 | my $r=$ua->post('https://login.microsoftonline.com/organizations/oauth2/v2.0/token', | ||||||||||||||||||||||||||||
| 137 | [client_id=>$self->{appid}, | ||||||||||||||||||||||||||||
| 138 | scope=>$self->{scope}, | ||||||||||||||||||||||||||||
| 139 | refresh_token=>$self->{refresh_token}, | ||||||||||||||||||||||||||||
| 140 | redirect_uri=> $self->{redirect_uri}, | ||||||||||||||||||||||||||||
| 141 | grant_type=>'refresh_token', | ||||||||||||||||||||||||||||
| 142 | 0 | client_secret=>$self->{secret} ]); | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ($r->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | my $data=decode_json($r->decoded_content); | |||||||||||||||||||||||||||
| 145 | 0 | my $token=$data->{access_token}; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | for (keys %$data) { | |||||||||||||||||||||||||||
| 148 | 0 | $self->{$_}=$data->{$_}; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | $self->{expires}=(time + $data->{expires_in}); | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | $self->writestore() if ($self->{presistent}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $self->default_header('Authorization' => "Bearer ".$token); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | $self->writestore() if ($self->{persistent}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | return $token; | |||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | 0 | croak "Refresh token auth fail"; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub newtoken($) { | ||||||||||||||||||||||||||
|
Subroutine
newtoken: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ($self->{refresh_token}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | return $self->refreshtoken; | |||||||||||||||||||||||||||
| 168 | } else { | ||||||||||||||||||||||||||||
| 169 | 0 | return $self->auth(); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | my ($self,$method, $url, $payload)=@_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | $url =~ s/^\///; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | my $abs_uri=URI->new_abs($url, $self->{base}.'/'); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ($self->{expires} < time()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | $self->newtoken(); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | my $req=HTTP::Request->new($method,"$abs_uri"); | |||||||||||||||||||||||||||
| 186 | 0 | $req->header('Content-Type' => 'application/json'); | |||||||||||||||||||||||||||
| 187 | 0 | $req->header('Accept' => 'application/json'); | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | $req->content(to_json($payload)) if ($payload); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | my $res=LWP::UserAgent::request($self,$req); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | #Response code is a keeper | ||||||||||||||||||||||||||||
| 193 | 0 | $self->{code}=$res->code; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if ($res->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if ($res->header('content-type') =~ /^application\/json/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | my $data=from_json($res->decoded_content); | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | #Here we save the nextLink for further use | ||||||||||||||||||||||||||||
| 201 | 0 | ▶ | if (exists $data->{'@odata.nextLink'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 0 | $self->{nextLink}=$data->{'@odata.nextLink'}; | |||||||||||||||||||||||||||
| 203 | } else { | ||||||||||||||||||||||||||||
| 204 | 0 | $self->{nextLink}=0; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 0 | return $data; | |||||||||||||||||||||||||||
| 207 | } else { | ||||||||||||||||||||||||||||
| 208 | 0 | return 0; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | } else { | ||||||||||||||||||||||||||||
| 211 | 0 | croak $res->decoded_content | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | ▶ | sub code($) { | ||||||||||||||||||||||||||
|
Subroutine
code: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 218 | 0 | return $self->{code}; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | sub next($) { | ||||||||||||||||||||||||||
|
Subroutine
next: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | ▶ | if ($self->{nextLink}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 0 | return $self->request('GET' => $self->{nextLink}); | |||||||||||||||||||||||||||
| 227 | } else { | ||||||||||||||||||||||||||||
| 228 | 0 | return 0; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub authendpoint($) { | ||||||||||||||||||||||||||
|
Subroutine
authendpoint: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | #This is an ugly url. Must be used as a GET or a redirect location, so can't be done as POST | ||||||||||||||||||||||||||||
| 237 | 0 | my $url=URI->new("https://login.microsoftonline.com/".$self->{tenant}."/oauth2/v2.0/authorize"); | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | #query_param_append comes handy, but was introduced in URI 5.16 | ||||||||||||||||||||||||||||
| 240 | 0 | $url->query_param_append('client_id' => $self->{appid}); | |||||||||||||||||||||||||||
| 241 | 0 | $url->query_param_append('response_type' => 'code'); | |||||||||||||||||||||||||||
| 242 | 0 | $url->query_param_append('redirect_uri' => $self->{redirect_uri}); | |||||||||||||||||||||||||||
| 243 | 0 | $url->query_param_append('response_mode' => 'query'); | |||||||||||||||||||||||||||
| 244 | 0 | $url->query_param_append('scope' => $self->{scope}); | |||||||||||||||||||||||||||
| 245 | 0 | $url->query_param_append('state' => $self->{sid}); | |||||||||||||||||||||||||||
| 246 | 0 | return "$url"; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | ▶ | sub tokenendpoint($) { | ||||||||||||||||||||||||||
|
Subroutine
tokenendpoint: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 252 | 0 | return "https://login.microsoftonline.com/".$self->{tenant}."/oauth2/v2.0/token"; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | ▶ | sub sid($) { | ||||||||||||||||||||||||||
|
Subroutine
sid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 256 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 257 | 0 | return $self->{sid}; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 0 | ▶ | sub consolecode($) { | ||||||||||||||||||||||||||
|
Subroutine
consolecode: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | my $port=$self->{local_port}; | |||||||||||||||||||||||||||
| 265 | 0 | my $web=LWP::UserAgent::msgraph::srvauth->new($port); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | #Even if it's local, this redirect_uri must be Azure-registered | ||||||||||||||||||||||||||||
| 268 | 0 | $self->{redirect_uri}="http://localhost:$port/auth"; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | #In order to setup a well-behaved http mini-server, we launch the server as a separate background | ||||||||||||||||||||||||||||
| 271 | #process using the HTTP::Server::Simple module. | ||||||||||||||||||||||||||||
| 272 | #Since this will be a separate process, and we need the authorization code value, we setup a | ||||||||||||||||||||||||||||
| 273 | #private listening socket so the child process can upload the code to us | ||||||||||||||||||||||||||||
| 274 | 0 | my $socket=listen_socket(); | |||||||||||||||||||||||||||
| 275 | 0 | $web->setcaller($self, $socket->sockport); | |||||||||||||||||||||||||||
| 276 | 0 | my $pid=$web->background(); | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | my $client=$socket->accept(); | |||||||||||||||||||||||||||
| 279 | 0 | my $data=""; | |||||||||||||||||||||||||||
| 280 | 0 | $client->recv($data,1024); | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | my ($id,$code)=split /\s/, $data; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | #Our session id is sent as the optional 'state' parameter | ||||||||||||||||||||||||||||
| 285 | #This value comes back to us along with the authorization code | ||||||||||||||||||||||||||||
| 286 | #Here, we honour the state value validation. If the state value | ||||||||||||||||||||||||||||
| 287 | #is not a match, the authorization code is discarded | ||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if ($id && $id eq $self->sid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$id and $id eq $self->sid
MC/DC0%0/2
$id and $id eq $self->sid
$id
$id eq $self->sid
Truth table0%0/3
A and B
A= $idB= $id eq $self->sid
| |||||||||||||||||||||||||||||
| 289 | 0 | print "Authorization code received. You can close the browser now\n"; | |||||||||||||||||||||||||||
| 290 | 0 | return $code; | |||||||||||||||||||||||||||
| 291 | } else { | ||||||||||||||||||||||||||||
| 292 | 0 | return 0; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | sub auth { | ||||||||||||||||||||||||||
|
Subroutine
auth: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 0 | my $post; | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | #Here comes the authentication handshake with the MS Graph platform | ||||||||||||||||||||||||||||
| 303 | #This is all spoken in application/x-www-form-urlencoded, so we use | ||||||||||||||||||||||||||||
| 304 | #the standard simple_request and HTTP::Request approach | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | #Client-credentials for user-less anonymous connection | ||||||||||||||||||||||||||||
| 307 | 0 | ▶ | if ($self->{grant_type} eq 'client_credentials') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | $post=HTTP::Request::Common::POST($self->tokenendpoint(), | ||||||||||||||||||||||||||||
| 310 | [client_id => $self->{appid}, | ||||||||||||||||||||||||||||
| 311 | scope => $self->{scope}, | ||||||||||||||||||||||||||||
| 312 | client_secret=> $self->{secret}, | ||||||||||||||||||||||||||||
| 313 | grant_type => $self->{grant_type} | ||||||||||||||||||||||||||||
| 314 | 0 | ]); | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | #Delegated authorization for user-oriented interaction | ||||||||||||||||||||||||||||
| 317 | } elsif ($self->{grant_type} eq 'authorization_code') { | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 0 | my $code=shift(); | |||||||||||||||||||||||||||
| 320 | 0 | ▶ | $code=$self->consolecode() unless ($code || ! $self->{console}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$code or not $self->{"console"}
MC/DC0%0/2
$code or not $self->{"console"}
$code
not $self->{"console"}
Truth table0%0/3
A or not(B)
A= $codeB= not $self->{"console"}
| |||||||||||||||||||||||||||||
| 321 | 0 | ▶ | croak 'Missing or invalid authorization code' unless ($code); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | #print "Using scope ".$self->{scope}."\n"; | ||||||||||||||||||||||||||||
| 324 | $post=HTTP::Request::Common::POST($self->tokenendpoint(), | ||||||||||||||||||||||||||||
| 325 | [client_id => $self->{appid}, | ||||||||||||||||||||||||||||
| 326 | scope => $self->{scope}, | ||||||||||||||||||||||||||||
| 327 | code => $code, | ||||||||||||||||||||||||||||
| 328 | redirect_uri => $self->{redirect_uri}, | ||||||||||||||||||||||||||||
| 329 | client_secret=> $self->{secret}, | ||||||||||||||||||||||||||||
| 330 | grant_type => $self->{grant_type} | ||||||||||||||||||||||||||||
| 331 | 0 | ]); | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | } else { | ||||||||||||||||||||||||||||
| 334 | 0 | croak 'Missing or unsupported grant_type'; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 0 | ▶ | croak 'Authentication scheme error' unless ($post); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 0 | my $r=$self->simple_request($post); | |||||||||||||||||||||||||||
| 340 | 0 | ▶ | unless ($r->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | 0 | croak "Authentication failure ".$r->decoded_content; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 0 | my $data=from_json($r->decoded_content); | |||||||||||||||||||||||||||
| 345 | 0 | for (keys %$data) { | |||||||||||||||||||||||||||
| 346 | 0 | $self->{$_}=$data->{$_}; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 0 | $self->{expires}=(time + $data->{expires_in}); | |||||||||||||||||||||||||||
| 350 | 0 | ▶ | $self->writestore() if ($self->{presistent}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 0 | $self->default_header('Authorization' => "Bearer ".$self->{access_token}); | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 0 | ▶ | $self->writestore() if ($self->{persistent}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 0 | return $data->{access_token}; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 0 | my ($self,@params)=@_; | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 0 | return $self->request('GET',@params); | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | sub post { | ||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 366 | 0 | my ($self,@params)=@_; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | return $self->request('POST',@params); | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 0 | ▶ | sub head { | ||||||||||||||||||||||||||
|
Subroutine
head: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 373 | 0 | my ($self,@params)=@_; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | return $self->request('HEAD',@params); | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 0 | ▶ | sub patch { | ||||||||||||||||||||||||||
|
Subroutine
patch: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 380 | 0 | my ($self,@params)=@_; | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 0 | return $self->request('PATCH',@params); | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 0 | ▶ | sub put { | ||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 387 | 0 | my ($self,@params)=@_; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 0 | return $self->request('PUT',@params); | |||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 394 | 0 | my ($self,@params)=@_; | |||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 0 | return $self->request('DELETE',@params); | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | package LWP::UserAgent::msgraph::srvauth; | ||||||||||||||||||||||||||||
| 401 | 1 | ▶ | use base 'HTTP::Server::Simple::CGI'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 402 | 1 | ▶ | use HTTP::Server::Simple::CGI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 403 | 1 | ▶ | use IO::Socket qw(AF_INET AF_UNIX SOCK_STREAM SHUT_WR); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | ▶ | sub valid_http_method($$) { | ||||||||||||||||||||||||||
|
Subroutine
valid_http_method: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 0 | my ($self,$method)=@_; | |||||||||||||||||||||||||||
| 408 | 0 | return ($method eq 'GET'); | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | 0 | ▶ | sub setcaller($$$) { | ||||||||||||||||||||||||||
|
Subroutine
setcaller: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 413 | 0 | my $ms=shift(); | |||||||||||||||||||||||||||
| 414 | 0 | my $port=shift(); | |||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 0 | $self->{'code_uri'}=$ms->authendpoint(); | |||||||||||||||||||||||||||
| 417 | 0 | $self->{'callerport'}=$port; | |||||||||||||||||||||||||||
| 418 | 0 | return 1; | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 0 | ▶ | sub sendcode($$$) { | ||||||||||||||||||||||||||
|
Subroutine
sendcode: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 0 | my ($self,$code,$state)=@_; | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | my $client = IO::Socket->new( | ||||||||||||||||||||||||||||
| 426 | Domain => AF_INET, | ||||||||||||||||||||||||||||
| 427 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||
| 428 | proto => 'tcp', | ||||||||||||||||||||||||||||
| 429 | PeerPort => $self->{callerport}, | ||||||||||||||||||||||||||||
| 430 | 0 | ▶ | PeerHost => '127.0.0.1', | ||||||||||||||||||||||||||
|
Condition0%0/2
'IO::Socket'->new('Domain', 2, 'Type', 1, 'proto', 'tcp', 'PeerPort', $self->{'callerport'}, 'PeerHost', '127.0.0.1') || die("Can't open socket: $IO::Socket::errstr")
MC/DC0%0/1
'IO::Socket'->new('Domain', 2, 'Type', 1, 'proto', 'tcp', 'PeerPort', $self->{'callerport'}, 'PeerHost', '127.0.0.1') || die("Can't open socket: $IO::Socket::errstr")
'IO::Socket'->new('Domain', 2, 'Type', 1, 'proto', 'tcp', 'PeerPort', $self->{'callerport'}, 'PeerHost', '127.0.0.1')
Truth table0%0/2
A
A= 'IO::Socket'->new('Domain', 2, 'Type', 1, 'proto', 'tcp', 'PeerPort', $self->{'callerport'}, 'PeerHost', '127.0.0.1')
| |||||||||||||||||||||||||||||
| 431 | ) || die "Can't open socket: $IO::Socket::errstr"; | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 0 | $client->send($state.' '.$code); | |||||||||||||||||||||||||||
| 434 | 0 | $client->shutdown(SHUT_WR); | |||||||||||||||||||||||||||
| 435 | 0 | $client->close(); | |||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | #Here we setup a minimal web server response behavior | ||||||||||||||||||||||||||||
| 439 | #The only verbs allowed are: | ||||||||||||||||||||||||||||
| 440 | # GET /start ==> does a 302 redirect to the MS authorization platform | ||||||||||||||||||||||||||||
| 441 | # GET /auth ==> receives the authorization code in the query string | ||||||||||||||||||||||||||||
| 442 | # | ||||||||||||||||||||||||||||
| 443 | # This two methods performs an MS challenge to the end-user | ||||||||||||||||||||||||||||
| 444 | # | ||||||||||||||||||||||||||||
| 445 | # Note that depending on your particular browser state, there could be | ||||||||||||||||||||||||||||
| 446 | # a valid MS tenant session already logged in with this app previously | ||||||||||||||||||||||||||||
| 447 | # authorized. In that case, the user doesn't get the login challenge | ||||||||||||||||||||||||||||
| 448 | # and the only thing the browser performs is a series of redirects | ||||||||||||||||||||||||||||
| 449 | # In that case, the authorization code get to us in a blink-you-missed-it | ||||||||||||||||||||||||||||
| 450 | # fashion | ||||||||||||||||||||||||||||
| 451 | 0 | ▶ | sub handle_request { | ||||||||||||||||||||||||||
|
Subroutine
handle_request: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 452 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 453 | 0 | my $cgi = shift; | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 0 | my $path = $cgi->request_uri(); | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 0 | ▶ | if ($path =~ "^/auth" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 458 | 0 | print "HTTP/1.0 200 OK\r\n"; | |||||||||||||||||||||||||||
| 459 | 0 | my $msg="Authentication ok. You can close this window now.\n"; | |||||||||||||||||||||||||||
| 460 | 0 | print $cgi->header(-type=>'text/plain', -Content_length => length($msg)); | |||||||||||||||||||||||||||
| 461 | 0 | my $code=$cgi->param('code'); | |||||||||||||||||||||||||||
| 462 | 0 | my $state=$cgi->param('state'); | |||||||||||||||||||||||||||
| 463 | 0 | $self->sendcode($code,$state); | |||||||||||||||||||||||||||
| 464 | 0 | print $msg; | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 0 | exit 0; | |||||||||||||||||||||||||||
| 467 | } elsif ($path =~ "^/start" ) { | ||||||||||||||||||||||||||||
| 468 | 0 | print "HTTP/1.0 302 Redirected\r\n"; | |||||||||||||||||||||||||||
| 469 | 0 | print $cgi->redirect($self->{'code_uri'}); | |||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||
| 471 | else { | ||||||||||||||||||||||||||||
| 472 | 0 | print "HTTP/1.0 404 Not found\r\n"; | |||||||||||||||||||||||||||
| 473 | 0 | print $cgi->header, | |||||||||||||||||||||||||||
| 474 | $cgi->start_html('Not found'), | ||||||||||||||||||||||||||||
| 475 | $cgi->h1('Not found'), | ||||||||||||||||||||||||||||
| 476 | $cgi->end_html; | ||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 0 | ▶ | sub print_banner($) { | ||||||||||||||||||||||||||
|
Subroutine
print_banner: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 481 | 0 | my $self=shift(); | |||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 0 | my $url="http://localhost:".$self->port()."/start"; | |||||||||||||||||||||||||||
| 484 | 0 | print "Authentication required.\nOpen your browser at $url\n"; | |||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 1 | 1; | |||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | =pod | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | =head1 NAME | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | LWP::UserAgent::msgraph | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | =head1 VERSION | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | version 0.12 | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | use LWP::UserAgent::msgraph; | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | #The XXXX, YYYY and ZZZZ are from your Azure App Registration | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | #Application Permission version | ||||||||||||||||||||||||||||
| 511 | $ua = LWP::UserAgent::msgraph->new( | ||||||||||||||||||||||||||||
| 512 | appid => 'XXXX', | ||||||||||||||||||||||||||||
| 513 | secret => 'YYYY', | ||||||||||||||||||||||||||||
| 514 | tenant => 'ZZZZ', | ||||||||||||||||||||||||||||
| 515 | grant_type => 'client_credentials'); | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | #Delegated authentication version | ||||||||||||||||||||||||||||
| 518 | $ua = LWP::UserAgent::msgraph->new( | ||||||||||||||||||||||||||||
| 519 | appid => 'XXXX', | ||||||||||||||||||||||||||||
| 520 | secret => 'YYYY', | ||||||||||||||||||||||||||||
| 521 | tenant => 'ZZZZ', | ||||||||||||||||||||||||||||
| 522 | grant_type=> 'authorization_code', | ||||||||||||||||||||||||||||
| 523 | scope => 'openid user.read'); | ||||||||||||||||||||||||||||
| 524 | $ua->auth($code_obtained_from_challenge); | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | $joe = $ua->request(GET => '/users/jdoe@some.com'); | ||||||||||||||||||||||||||||
| 527 | $dn = $joe->{displayName}; | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | This module allows the interaction between Perl and the MS Graph API service. | ||||||||||||||||||||||||||||
| 532 | Therefore, a MS Graph application can be built using Perl. The application must | ||||||||||||||||||||||||||||
| 533 | be correctly registered within Azure with the proper persmissions. | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | This module has the glue for the needed authentication scheme and the JSON | ||||||||||||||||||||||||||||
| 536 | serialization so a conversation can be established with MS Graph. This is just | ||||||||||||||||||||||||||||
| 537 | middleware. No higher level object abstraction is provided for the MS Graph | ||||||||||||||||||||||||||||
| 538 | object data. | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | my $ua=LWP::UserAgent->new(%options); | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | This method constructs a new L<LWP::UserAgent::msgraph> object. | ||||||||||||||||||||||||||||
| 545 | key/value pairs must be supplied in order to setup the object | ||||||||||||||||||||||||||||
| 546 | properly. Missing mandatory options will result in error | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | KEY MEANING | ||||||||||||||||||||||||||||
| 549 | ------- ----------------------------------- | ||||||||||||||||||||||||||||
| 550 | appid Application (client) ID | ||||||||||||||||||||||||||||
| 551 | secret shared secret needed for handshake | ||||||||||||||||||||||||||||
| 552 | tenant Tenant id | ||||||||||||||||||||||||||||
| 553 | grant_type Authorizations scheme (client_credentials,authorization_code) | ||||||||||||||||||||||||||||
| 554 | scope List of permissions requested as in 'perm1 perm2 perm3...' | ||||||||||||||||||||||||||||
| 555 | console Indicates whether interaction with a user is possible | ||||||||||||||||||||||||||||
| 556 | redirect_uri Redirect URI for delegated auth challenge | ||||||||||||||||||||||||||||
| 557 | local_port tcp port for mini http server. Defaults to 8081 | ||||||||||||||||||||||||||||
| 558 | sid Session id. Defaults to a random UUID | ||||||||||||||||||||||||||||
| 559 | persistent Whether to keep the session data between runs | ||||||||||||||||||||||||||||
| 560 | store Filename for session data. Defaults to a temp file | ||||||||||||||||||||||||||||
| 561 | base Base URL for MS Graph calls. Defaults to https://graph.microsoft.com/v1.0 | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | =head1 auth | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | my $token = $ua->auth; #For app credentiales | ||||||||||||||||||||||||||||
| 566 | my $token = $ua->auth($challenge); #For delegated authentication | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | This method performs the authentication handshake sequence with the MS | ||||||||||||||||||||||||||||
| 569 | Graph platform. The optional parameter is the authorization code obtained | ||||||||||||||||||||||||||||
| 570 | from a challenge with the impersonated user. If this is an application | ||||||||||||||||||||||||||||
| 571 | non-delegated client, then the $challenge is not needed. | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | The challenge code is not kept, but the token is. The token is saved for future requests. | ||||||||||||||||||||||||||||
| 574 | This method returns the token obtained. | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | If used in a web application, you should have redirected the user to the authendpoint() location | ||||||||||||||||||||||||||||
| 577 | and then capture the resulting code listening for the redirect_uri. | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | A special tweak is supplied for console applications with delegated authentication. In that case, | ||||||||||||||||||||||||||||
| 580 | if the code is missing, an http localhost miniserver is launched so the | ||||||||||||||||||||||||||||
| 581 | user can trigger the challenge himself. This behavior is activated via the console constructor option. | ||||||||||||||||||||||||||||
| 582 | The http miniserver is destroyed as soon as the authorization code arrives. | ||||||||||||||||||||||||||||
| 583 | In this case, the redirect_uri is automatically set. The miniserver listens by default on http://localhost:8081. | ||||||||||||||||||||||||||||
| 584 | Please note that MS Graph allows | ||||||||||||||||||||||||||||
| 585 | the use of localhost in the redirect_uri and in that case SSL is not enforced. But still the | ||||||||||||||||||||||||||||
| 586 | localhost URL must be registered in Azure. | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | =head1 request | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | my $object=$ua->request(GET => '/me'); | ||||||||||||||||||||||||||||
| 591 | $ua->request(PATCH => '/me', {officeLocation => $mynewoffice}); | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | The request method makes a call to a MS Graph endpoint url and returns the | ||||||||||||||||||||||||||||
| 594 | corresponding response object as a perl structure. An optional perl structure might be | ||||||||||||||||||||||||||||
| 595 | supplied as the payload (body) for the request. | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | The MS Graph has a rich set of API calls for different operations. Check the | ||||||||||||||||||||||||||||
| 598 | EXAMPLES section for more tips. | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | You should call request() only after a successful auth() call. If a refresh_token | ||||||||||||||||||||||||||||
| 601 | is issued by MS Graph, then request() will handle the token refresh transparently. | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | =head1 get | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | my $me=$ua->get('/me'); | ||||||||||||||||||||||||||||
| 606 | print "Hello $me->{displayName}"; | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | Issues a GET request to the MS Graph endpoint and returns the response as a perl structure. | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | =head1 post | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | my $folder=$ua->post('/me/drive/root/children', {name => 'newfolder', folder => {}}); | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | Issues a POST request to the MS Graph endpoint and returns the response as a perl structure. | ||||||||||||||||||||||||||||
| 615 | The second parameter is the payload for the POST request, as a perl reference. | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =head1 code | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | print "It worked" if ($ua->code == 201); | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | A code() method is supplied as a convenient way of getting the last HTTP response | ||||||||||||||||||||||||||||
| 623 | code. This mitht be important, since the original HTTP::Respone is lost in the | ||||||||||||||||||||||||||||
| 624 | normal operations. You may check L<HTTP::Status> for the meaning and further | ||||||||||||||||||||||||||||
| 625 | processing of the codes. | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | =head1 next | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | $more=$ua->next(); | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | The next() method will request additional response content after a previous | ||||||||||||||||||||||||||||
| 632 | request if a pagination result set happens. | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | =head1 authendpoint | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | $location=$ua->authendpoint() | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | Returns the authentication endpoint as an url string, full with the query part. In a delegated | ||||||||||||||||||||||||||||
| 639 | authentication mode, you should point the user to this url via a browser in order to get the proper | ||||||||||||||||||||||||||||
| 640 | authorization. This is on offline method, the resulting uri is computed from the constructor options | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | =head1 tokenendpoint | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | $location=$ua->tokenendpoint() | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | Returns the oauth 2.0 token endpoint as an url string. This url is used internally to get | ||||||||||||||||||||||||||||
| 647 | the authentication token. This is an offline method. | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | =head1 It's not persistance, it's no cookie. It's Session data | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | MS Graph implements an OAuth 2.0 authentication scheme. This means that the application asks | ||||||||||||||||||||||||||||
| 652 | for an authentication token first and then uses this token for further requests. If you are | ||||||||||||||||||||||||||||
| 653 | building a backend application that offers several services, this means that you must keep the token between runs. | ||||||||||||||||||||||||||||
| 654 | In a backend for a web application, in theory the token could be kept in the browser local storage, but that's | ||||||||||||||||||||||||||||
| 655 | not the approach of LWP::UserAgent::msgraph. | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | The approach is to store the token under the backend application realm. This is done by the persistent | ||||||||||||||||||||||||||||
| 658 | option. | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | =head2 sid | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | $sid=$ua->sid(); | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | Returns the session id. This is a random UUID by default. This is used as a key for the session data. If you are building | ||||||||||||||||||||||||||||
| 665 | a backend application, you may send this back to the client side in order to keep the session data between runs. | ||||||||||||||||||||||||||||
| 666 | Once the sid is created, you can use it in further calls. | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | my $sid=<something from the client side>; | ||||||||||||||||||||||||||||
| 669 | my $ua=LWP::UserAgent->new(sid => $sid, persistent => 1); | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | By default, UUID based sid are provided. You can use your own sid scheme, but you must ensure that it's unique. | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | =head2 store | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | my $sid=<something from the client side> | ||||||||||||||||||||||||||||
| 676 | my $ua=LWP::UserAgent->new(sid => $sid, persistent => 1, store => "some clever app data location/$sid"); | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | The store option is used to set the filename for the session data. This is a Storable file. If the store option is not | ||||||||||||||||||||||||||||
| 679 | set, a temporary file is used. The store option is used in conjunction with the persistent option. The OAuth token | ||||||||||||||||||||||||||||
| 680 | is kept in this store. The Application's shared secret is not. | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | =head1 Changes from the default LWP::UserAgent behavior | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | This class inherits from L<LWP::UserAgent>, but some changes apply. If you are used to | ||||||||||||||||||||||||||||
| 685 | LWP::UserAgent standard tweaks and shortcuts, you should read this. | ||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | The request() method accepts a perl structure which will be sent | ||||||||||||||||||||||||||||
| 688 | as a JSON body to the MS Graph endoint. Instead of an L<HTTP::Response> | ||||||||||||||||||||||||||||
| 689 | object, request() will return whatever object is returned by the | ||||||||||||||||||||||||||||
| 690 | MS Graph method, as a perl structure. The L<JSON> module is used as | ||||||||||||||||||||||||||||
| 691 | a serialization engine. The HTTP::Response object is not kept. | ||||||||||||||||||||||||||||
| 692 | The code() method is provided to check the HTTP response code. | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | request() will use the right Authorization header based on the initial handshake. | ||||||||||||||||||||||||||||
| 695 | The get(), post(), patch(), delete(), put(), delete() methods are setup so | ||||||||||||||||||||||||||||
| 696 | they call the LWP::UserAgent::msgraph version of request(). That is, they would | ||||||||||||||||||||||||||||
| 697 | return a perl structure according to the MS Graph method. | ||||||||||||||||||||||||||||
| 698 | In particular, post() and patch() accepts a perl structure | ||||||||||||||||||||||||||||
| 699 | as the body. All the binding with the L<HTTP::Request::Common> module has been broken. | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | The simple_request() method is kept unchanged, but will use the | ||||||||||||||||||||||||||||
| 702 | right Bearer token authentication. So, if you need more control over the request, you can use | ||||||||||||||||||||||||||||
| 703 | this method. You must add the JSON serialization, though. | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | Also note that in d LWP, an URL starting with a '/' is considered a root-relative URL. In LWP::UserAgent::msgraph, | ||||||||||||||||||||||||||||
| 706 | it's considered relative to the base URL. This is a convenience for MS Graph calls, since MS Graph documentation | ||||||||||||||||||||||||||||
| 707 | often uses this convention. simple_request() retains the original root-relative behavior. This is why $ua->get('/me') works. | ||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||
| 709 | =head1 Requirements | ||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | This module requires the following modules: | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | =over | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | =item L<LWP::UserAgent> for all the base HTTP operations. | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | =item L<JSON> for the JSON serialization/deserialization. | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | =item L<Storable> for the session data storage. | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | =item L<Data::UUID> for the session id generation. | ||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | =item L<HTTP::Server::Simple::CGI> and L<Net::EmptyPort> for the http localhost miniserver feature | ||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | =item L<HTTP::Request::Common> is used for the handshake, since it's still multipart/form-data based | ||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||
| 727 | =back | ||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||
| 730 | =head1 TO-DO | ||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||
| 732 | This module is a work in progress. The following features are planned: | ||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||
| 734 | =over | ||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||
| 736 | =item Certificate based authentication | ||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||
| 738 | =item Allow for a custom storable mechanism, so the session data can be kept in a database or in an encrypted form | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | =item Provide useful samples for the most common MS Graph operations | ||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | =back | ||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||
| 744 | =cut | ||||||||||||||||||||||||||||