wsgetmail/MS365/Client.pm

filter: Statementstmt 71.8% 69 / 96 Branchbran 28.5% 8 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 16 / 24 Podpod 85.7% 6 / 7 totaltotal 62.5% 100 / 160 CC 16 SCAR 13.8
CC
16
Cov
61%
BUILD
10.4
get_request_by_url
6.0
delete_request
6.0
CRAP 4.0
« wsgetmail.pm ↑ Summary wsgetmail/MS365/Message.pm »
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 2020-2026 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48  
49 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 package App::wsgetmail::MS365::Client;
52  
53 =head1 NAME
54  
55 App::wsgetmail::MS365::Client - Low-level client to the Microsoft Graph API
56  
57 =cut
58  
59 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use Azure::AD::ClientCredentials;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 =head1 DESCRIPTION
67  
68 This class performs the actual REST requests to support
69 L<App::wsgetmail::MS365>.
70  
71 =head1 ATTRIBUTES
72  
73 The following attributes are received from L<App::wsgetmail::MS365> and have
74 the same meaning:
75  
76 =over 4
77  
78 =item * secret
79  
80 =cut
81  
82 2 has secret => (
83 is => 'ro',
84 required => 0,
85 );
86  
87 =item * client_id
88  
89 =cut
90  
91 2 has client_id => (
92 is => 'ro',
93 required => 1,
94 );
95  
96 =item * tenant_id
97  
98 =cut
99  
100 2 has tenant_id => (
101 is => 'ro',
102 required => 1,
103 );
104  
105 =item * username
106  
107 =cut
108  
109 2 has username => (
110 is => 'ro',
111 required => 0
112 );
113  
114 =item * user_password
115  
116 =cut
117  
118 2 has user_password => (
119 is => 'ro',
120 required => 0
121 );
122  
123 =item * global_access
124  
125 =item * debug
126  
127 =cut
128  
129 has global_access => (
130 is => 'ro',
131 0 ▶ default => sub { return 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 2 );
133  
134 =back
135  
136 =head2 resource_url
137  
138 A string with the URL for the overall API endpoint. Defaults to
139 C<https://graph.microsoft.com/>. For Microsoft Government Cloud (GCC High),
140 set this to C<https://graph.microsoft.us/>. For DoD, use
141 C<https://dod-graph.microsoft.us/>.
142  
143 =cut
144  
145 has resource_url => (
146 is => 'ro',
147 1 ▶ default => sub { return 'https://graph.microsoft.com/' }
Subroutine __ANON__: called CC=1 SCAR=0.0
148 2 );
149  
150 =head2 login_base_url
151  
152 A string with the base URL for OAuth authentication. Defaults to
153 C<https://login.windows.net>. For Microsoft Government Cloud
154 (GCC High and DoD), set this to C<https://login.microsoftonline.us>.
155  
156 =cut
157  
158 has login_base_url => (
159 is => 'ro',
160 1 ▶ default => sub { return 'https://login.windows.net' }
Subroutine __ANON__: called CC=1 SCAR=0.0
161 2 );
162  
163 =head2 resource_path
164  
165 A string with the REST API endpoint URL path.
166  
167 =cut
168  
169 has resource_path => (
170 is => 'ro',
171 2 ▶ default => sub { return 'v1.0' }
Subroutine __ANON__: called CC=1 SCAR=0.0
172 2 );
173  
174 has debug => (
175 is => 'rw',
176 0 ▶ default => sub { return 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 2 );
178  
179 2 has _ua => (
180 builder => '_build_authorised_ua',
181 is => 'ro',
182 lazy => 1,
183 );
184  
185 2 has _credentials => (
186 is => 'ro',
187 lazy => 1,
188 builder => '_build__credentials',
189 );
190  
191 2 has _access_token => (
192 is => 'ro',
193 lazy => 1,
194 builder => '_build__access_token',
195 );
196  
197 2 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=23.4
Pod undocumented
198 2 my ($self, $args) = @_;
199  
200 2 ▶ if ($args->{global_access}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'global_access'}) { } 0 2 2
201 0 ▶ unless ($args->{secret}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"secret"}) 0 0 0
202 0 die "secret is required when using global_access";
203 }
204 }
205 else {
206 2 ▶ unless ($args->{username} && $args->{user_password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"username"} and $args->{"user_password"}) 0 2 2
Condition33%1/3
$args->{"username"} and $args->{"user_password"}
!ll&&!rl&&r
002
MC/DC0%0/2
$args->{"username"} and $args->{"user_password"}
$args->{"username"} $args->{"user_password"}
Truth table33%1/3
A and B
A= $args->{"username"}B= $args->{"user_password"}
ABresult
0X0
100
111
207 0 die "username and user_password are required when not using global_access";
208 }
209 }
210 }
211  
212  
213 =head1 METHODS
214  
215 =head2 build_rest_uri(@endpoint_parts)
216  
217 Given a list of URL component strings, returns a complete URL string to
218 reach that endpoint from this object's C<resource_url> and C<resource_path>.
219  
220 =cut
221  
222 4 ▶sub build_rest_uri {
Subroutine build_rest_uri: called CC=1 SCAR=0.0
Pod documented
223 4 my ($self, @endpoint_parts) = @_;
224 4 my $base_url = $self->resource_url . $self->resource_path;
225 4 return join('/', $base_url, @endpoint_parts);
226 }
227  
228 =head2 get_request($parts, $params)
229  
230 Makes a GET request to the API. C<$parts> is an arrayref of URL endpoint
231 strings with the specific endpoint to request. C<$params> is a hashref of
232 query parameters to send with the request.
233  
234 =cut
235  
236 4 ▶sub get_request {
Subroutine get_request: called CC=3 SCAR=11.3
Pod documented
237 4 my ($self, $parts, $params) = @_;
238 # add error handling!
239 4 my $uri = URI->new($self->build_rest_uri(@$parts));
240 4 ▶ warn "making GET request to url $uri" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 4 4
241 4 ▶ $uri->query_form($params) if ($params);
Branch: 1 branch
BranchTrueFalseTotal
if $params 4 0 4
242 4 return $self->_ua->get($uri);
243 }
244  
245 =head2 get_request_by_url($url)
246  
247 Makes a GET request to the URL in the C<$url> string.
248  
249 =cut
250  
251 0 ▶sub get_request_by_url {
Subroutine get_request_by_url: not called CC=2 SCAR=17.9
Pod documented
252 0 my ($self, $url) = @_;
253 0 ▶ warn "making GET request to url $url" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
254 0 return $self->_ua->get($url);
255 }
256  
257 =head2 delete_request($parts, $params)
258  
259 Makes a DELETE request to the API. C<$parts> is an arrayref of URL endpoint
260 strings with the specific endpoint to request. C<$params> is unused.
261  
262 =cut
263  
264 0 ▶sub delete_request {
Subroutine delete_request: not called CC=2 SCAR=17.9
Pod documented
265 0 my ($self, $parts, $params) = @_;
266 0 my $url = $self->build_rest_uri(@$parts);
267 0 ▶ warn "making DELETE request to url $url" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
268 0 return $self->_ua->delete($url);
269 }
270  
271 =head2 post_request($path_parts, $post_data)
272  
273 Makes a POST request to the API. C<$path_parts> is an arrayref of URL
274 endpoint strings with the specific endpoint to request. C<$post_data> is a
275 reference to an array or hash of data to include in the POST request body.
276  
277 =cut
278  
279 0 ▶sub post_request {
Subroutine post_request: not called CC=2 SCAR=17.9
Pod documented
280 0 my ($self, $path_parts, $post_data) = @_;
281 0 my $url = $self->build_rest_uri(@$path_parts);
282 0 ▶ warn "making POST request to url $url" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
283 0 return $self->_ua->post($url,$post_data);
284 }
285  
286 =head2 patch_request($path_parts, $patch_params)
287  
288 Makes a PATCH request to the API. C<$path_parts> is an arrayref of URL
289 endpoint strings with the specific endpoint to request. C<$patch_params> is
290 a hashref of data to include in the PATCH request body.
291  
292 =cut
293  
294 0 ▶sub patch_request {
Subroutine patch_request: not called CC=2 SCAR=17.9
Pod documented
295 0 my ($self, $path_parts, $patch_params) = @_;
296 0 my $url = $self->build_rest_uri(@$path_parts);
297 0 ▶ warn "making PATCH request to url $url" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
298 0 return $self->_ua->patch($url,%$patch_params);
299 }
300  
301 ######
302  
303 2 ▶sub _build_authorised_ua {
Subroutine _build_authorised_ua: called CC=2 SCAR=7.0
304 2 my $self = shift;
305 2 my $ua = $self->_new_useragent;
306 2 ▶ warn "getting system access token" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
307 2 $ua->default_header( Authorization => $self->_access_token() );
308 2 return $ua;
309 }
310  
311 2 ▶sub _build__access_token {
Subroutine _build__access_token: called CC=2 SCAR=7.2
312 2 my $self = shift;
313 2 my $access_token;
314 2 ▶ if ($self->global_access) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->global_access) { } 0 2 2
315 0 $access_token = $self->_credentials->access_token;
316 }
317 else {
318 2 $access_token = $self->_get_user_access_token;
319 }
320 2 return $access_token;
321 }
322  
323 2 ▶sub _get_user_access_token {
Subroutine _get_user_access_token: called CC=4 SCAR=15.0
324 2 my $self = shift;
325 2 my $ua = $self->_new_useragent;
326 2 my $access_token;
327 2 ▶ warn "getting user access token" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
328 2 my $oauth_login_url = sprintf('%s/%s/oauth2/token', $self->login_base_url, $self->tenant_id);
329 2 my $response = $ua->post( $oauth_login_url,
330 {
331 resource=> $self->resource_url,
332 client_id => $self->client_id,
333 grant_type=>'password',
334 username=>$self->username,
335 password=>$self->user_password,
336 scope=>'openid'
337 }
338 );
339 2 my $raw_message = $response->content;
340 # check details
341 2 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 2 0 2
342 2 my $token_details = decode_json( $response->content );
343 2 $access_token = "Bearer " . $token_details->{access_token};
344 }
345 else {
346 # throw error
347 0 ▶ warn "auth response from server : $raw_message" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
348 0 die sprintf('unable to get user access token for user %s request failed with status %s ', $self->username, $response->status_line);
349 }
350 2 return $access_token;
351 }
352  
353 0 ▶sub _build__credentials {
Subroutine _build__credentials: not called CC=1 SCAR=6.9
354 0 my $self = shift;
355 0 my $creds = Azure::AD::ClientCredentials->new(
356 resource_id => $self->resource_url,
357 client_id => $self->client_id,
358 secret_id => $self->secret,
359 tenant_id => $self->tenant_id,
360 ad_url => $self->login_base_url,
361 );
362 0 return $creds;
363 }
364  
365 0 ▶sub _new_useragent {
Subroutine _new_useragent: not called CC=1 SCAR=6.9
366 0 return LWP::UserAgent->new();
367 }
368  
369 =head1 SEE ALSO
370  
371 =over 4
372  
373 =item * L<App::wsgetmail::MS365>
374  
375 =back
376  
377 =head1 AUTHOR
378  
379 Best Practical Solutions, LLC <modules@bestpractical.com>
380  
381 =head1 LICENSE AND COPYRIGHT
382  
383 This software is Copyright (c) 2020 by Best Practical Solutions, LLC
384  
385 This is free software, licensed under:
386  
387 The GNU General Public License, Version 2, June 1991
388  
389 =cut
390  
391 2 1;
« wsgetmail.pm ↑ Summary wsgetmail/MS365/Message.pm »