script/solid_auth.pl untested

filter: Statementstmt 0.0% 0 / 463 Branchbran 0.0% 0 / 226 Conditioncond 0.0% 0 / 100 MC/DCmcdc 0.0% 0 / 63 Subroutinesub 0.0% 0 / 40 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 919 CC 95 SCAR 38.3
CC
95
Cov
0%
cmd_put
156.0
_cmd_list
90.0
cmd_patch
72.0
CRAP 46.2
1 #!/usr/local/bin/perl
2 $|++;
3  
4 use lib qw(./lib);
5 use Getopt::Long qw(:config pass_through);
6 use Web::Solid::Auth;
7 use Web::Solid::Auth::Agent;
8 use Web::Solid::Auth::Util;
9 use HTTP::Date;
10 use File::LibMagic;
11 use File::Basename;
12 use MIME::Base64;
13 use JSON;
14 use Path::Tiny;
15 use String::Escape;
16 use Log::Any::Adapter;
17  
18 Log::Any::Adapter->set('Log4perl');
19  
20 my $webid = $ENV{SOLID_WEBID};
21 my $webbase = $ENV{SOLID_REMOTE_BASE};
22 my $clientid = $ENV{SOLID_CLIENT_ID};
23 my $opt_recursive = undef;
24 my $opt_skip = undef;
25 my $opt_real = undef;
26 my $opt_keep = undef;
27 my $opt_delete = undef;
28 my $opt_force = undef;
29 my $opt_etag = undef;
30 my $opt_log = 'log4perl.conf';
31 my $opt_header = [];
32  
33 GetOptions(
34 "clientid|c=s" => \$clientid ,
35 "webid|w=s" => \$webid ,
36 "base|b=s" => \$webbase ,
37 "skip" => \$opt_skip ,
38 "keep" => \$opt_keep ,
39 "delete" => \$opt_delete ,
40 "etag=s" => \$opt_etag ,
41 "force|f" => \$opt_force ,
42 "r" => \$opt_recursive ,
43 "x" => \$opt_real ,
44 "H=s@" => \$opt_header ,
45 "log=s" => \$opt_log ,
46 );
47  
48 my $cmd = shift;
49  
50 if (-e $opt_log) {
51 Log::Log4perl::init($opt_log);
52 }
53  
54 my $auth = Web::Solid::Auth->new(webid => $webid, client_id => $clientid);
55 my $agent = Web::Solid::Auth::Agent->new(auth => $auth);
56  
57 if ($webbase) {
58 # Remove the trailing slash
59 $webbase =~ s{\/$}{};
60 }
61  
62 my $ret;
63  
64 if (0) {}
65 elsif ($cmd eq 'list') {
66 $ret = cmd_list(@ARGV);
67 }
68 elsif ($cmd eq 'get') {
69 $ret = cmd_get(@ARGV);
70 }
71 elsif ($cmd eq 'put') {
72 $ret = cmd_put(@ARGV);
73 }
74 elsif ($cmd eq 'post') {
75 $ret = cmd_post(@ARGV);
76 }
77 elsif ($cmd eq 'patch') {
78 $ret = cmd_patch(@ARGV);
79 }
80 elsif ($cmd eq 'delete') {
81 $ret = cmd_delete(@ARGV);
82 }
83 elsif ($cmd eq 'head') {
84 $ret = cmd_head(@ARGV);
85 }
86 elsif ($cmd eq 'options') {
87 $ret = cmd_options(@ARGV);
88 }
89 elsif ($cmd eq 'mirror') {
90 $ret = cmd_mirror(@ARGV);
91 }
92 elsif ($cmd eq 'upload') {
93 $ret = cmd_upload(@ARGV);
94 }
95 elsif ($cmd eq 'clean') {
96 $ret = cmd_clean(@ARGV);
97 }
98 elsif ($cmd eq 'authenticate') {
99 $ret = cmd_authenticate(@ARGV);
100 }
101 elsif ($cmd eq 'headers') {
102 $ret = cmd_headers(@ARGV);
103 }
104 elsif ($cmd eq 'curl') {
105 $ret = cmd_curl(@ARGV);
106 }
107 elsif ($cmd eq 'id_token') {
108 $ret = cmd_id_token(@ARGV);
109 }
110 elsif ($cmd eq 'access_token') {
111 $ret = cmd_access_token(@ARGV);
112 }
113 else {
114 usage();
115 }
116  
117 exit($ret);
118  
119 sub usage {
120 print STDERR <<EOF;
121 Usage
122 -=-=-=
123  
124 # Login
125 usage: $0 [options] authenticate
126  
127 # Curl like interaction
128 usage: $0 [options] headers method url
129 usage: $0 [options] curl -- [curl-opts] url
130  
131 # Interpret LDP responses
132 usage: $0 [options] list /path/ | url # folder listing
133 usage: $0 [options] mirror /path directory # mirror a container/resource , use [-r] for recursive mirror
134 usage: $0 [options] upload directory /path # upload the directory contents to a path, use [-r] for recursive upload
135 usage: $0 [options] clean /path # delete resources/containers, use [-r] for recursive clean
136  
137 # Simple HTTP interaction
138 usage: $0 [options] get /path | url
139 usage: $0 [options] put (/path/ | url) # create a folder
140 usage: $0 [options] put (/path | url) file [mimeType]
141 usage: $0 [options] post (/path | url) file [mimeType]
142 usage: $0 [options] patch (/path | url) (file | SPARQL)
143 usage: $0 [options] head /path | url
144 usage: $0 [options] options /path | url
145 usage: $0 [options] delete /path | url
146  
147 # Check the credentials
148 usage: $0 access_token
149 usage: $0 id_token
150  
151 options:
152 --webid|w webid - your webid
153 --clientid|c clientid - optional the client-id
154 --base|b base - optional the base url for all requests
155 --skip - skip files that already exist (mirror)
156 --delete - delete local files that are not at the remote location (mirror)
157 --keep - keep containers (clean)
158 --force|f - force overwriting existing resources (put,patch)
159 -r - recursive (mirror, upload, clean)
160 -x - do it for real (upload, clean)
161 -H "Header" - add a header (get,post,put,head,delete)
162 EOF
163 exit 1
164 }
165  
166 sub cmd_list {
167 my ($url) = @_;
168  
169 my $files = _cmd_list($url);
170  
171 return $files if $files && ref($files) eq '';
172  
173 for my $file (sort keys %$files) {
174 my $type = $files->{$file};
175  
176 printf "%s $file\n" , $type eq 'container' ? "d" : "-";
177 }
178  
179 return 0;
180 }
181  
182 sub _cmd_list {
183 my ($url) = @_;
184  
185 unless ($url) {
186 print STDERR "Need a url\n";
187 return 1;
188 }
189  
190 unless ($url =~ /\/$/) {
191 print STDERR "$url doesn't look like a container\n";
192 return 1;
193 }
194  
195 my $iri = _make_url($url);
196  
197 my $response = $agent->get($iri);
198  
199 unless ($response->is_success) {
200 printf STDERR "%s - failed to $url\n" , $response->code;
201 printf STDERR "%s\n" , $response->message;
202 return 2;
203 }
204  
205 my $util = Web::Solid::Auth::Util->new;
206 my $model = $util->parse_turtle($response->decoded_content);
207  
208 my $sparql =<<EOF;
209 prefix ldp: <http://www.w3.org/ns/ldp#>
210  
211 SELECT ?resource ?type {
212 ?base ldp:contains ?resource .
213 OPTIONAL {
214 ?resource a ?type .
215 FILTER (?type IN (
216 ldp:Resource,
217 ldp:RDFSource,
218 ldp:Container,
219 ldp:BasicContainer,
220 ldp:IndirectContainer,
221 ldp:NonRDFSource
222 )
223 )
224 }
225 }
226 EOF
227  
228 my %FILES = ();
229  
230 $util->sparql($model, $sparql, sub {
231 my $res = shift;
232 my $name = $res->value('resource')->as_string;
233  
234 # clean absolute urls into relative ones ...
235 if ($name =~ /^http/) {
236 $name = substr($name,length($iri));
237 }
238 $name =~ s/^\///;
239  
240 # Read the type from type or guess ..based on the name :P
241 my $type;
242  
243 if ($res->value('type')) {
244 $type = $res->value('type')->as_string;
245 }
246 else {
247 $type = ($name =~ /\/$/) ? 'Container' : 'Resource';
248 }
249  
250 my $key = $url . $name;
251  
252 if (exists $FILES{$key} && $FILES{$key} eq 'container') {
253 # Containers are more interesting than resources
254 }
255 else {
256 $FILES{$key} = $type =~ /Container/ ? "container" : "resource";
257 }
258 });
259  
260 return \%FILES;
261 }
262  
263 sub cmd_get {
264 my ($url) = @_;
265  
266 my %headers = _make_headers();
267  
268 my $response = _cmd_get($url,%headers);
269  
270 return $response if $response && ref($response) eq '';
271  
272 print $response->decoded_content;
273  
274 return 0;
275 }
276  
277 sub _cmd_get {
278 my ($url,%headers) = @_;
279  
280 unless ($url) {
281 print STDERR "Need a url\n";
282 return 1;
283 }
284  
285 my $iri = _make_url($url);
286  
287 my $response = $agent->get($iri,%headers);
288  
289 unless ($response->is_success) {
290 printf STDERR "%s - failed to $url\n" , $response->code;
291 printf STDERR "%s\n" , $response->message;
292 return 2;
293 }
294  
295 $response;
296 }
297  
298 sub cmd_head {
299 my ($url) = @_;
300  
301 unless ($url) {
302 print STDERR "Need a url\n";
303 return 1;
304 }
305  
306 my $iri = _make_url($url);
307 my %headers = _make_headers();
308  
309 my $response = $agent->head($iri,%headers);
310  
311 unless ($response->is_success) {
312 printf STDERR "%s - failed to $url\n" , $response->code;
313 printf STDERR "%s\n" , $response->message;
314 return 2;
315 }
316  
317 for my $header ($response->header_field_names) {
318 printf "%s: %s\n" , $header , $response->header($header);
319 }
320  
321 return 0;
322 }
323  
324 sub cmd_options {
325 my ($url) = @_;
326  
327 unless ($url) {
328 print STDERR "Need a url\n";
329 return 1;
330 }
331  
332 my $iri = _make_url($url);
333 my %headers = _make_headers();
334  
335 my $response = $agent->options($iri,%headers);
336  
337 unless ($response->is_success) {
338 printf STDERR "%s - failed to $url\n" , $response->code;
339 printf STDERR "%s\n" , $response->message;
340 return 2;
341 }
342  
343 for my $header ($response->header_field_names) {
344 printf "%s: %s\n" , $header , $response->header($header);
345 }
346  
347 return 0;
348 }
349  
350 sub cmd_put {
351 my ($url, $file, $mimeType) = @_;
352  
353 $mimeType //= _guess_mimetype($file) if $file;
354  
355 unless ($url) {
356 print STDERR "Need a url\n";
357 return 1;
358 }
359  
360 if ($url =~ /\/$/ && ($file || $mimeType)) {
361 print STDERR "Folder names can't have file uploads\n\n";
362 return 1;
363 }
364 elsif ($url !~ /\/$/ && ! ($file || $mimeType)) {
365 print STDERR "Need url file and mimeType\n";
366 return 1;
367 }
368  
369 my $data;
370
371 if ($file) {
372 $data = path($file)->slurp_raw;
373 }
374  
375 my $iri = _make_url($url);
376 my %headers = _make_headers();
377  
378 if ($mimeType) {
379 $headers{'Content-Type'} = $mimeType unless $headers{'Content-Type'};
380 }
381  
382 # Prevent overwriting exiting resources
383 unless ($opt_force) {
384 $headers{'If-None-Match'} = '*';
385 }
386  
387 # Prevent overwriting changed resources
388 if ($opt_etag) {
389 $headers{'If-Match'} = $opt_etag;
390 }
391  
392 my $response;
393  
394 if ($file) {
395 $response = $agent->put($iri, $data, %headers);
396 }
397 else {
398 %headers = _link_header('<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"', %headers);
399 $response = $agent->put($iri,undef,%headers);
400 }
401  
402 unless ($response->is_success) {
403 printf STDERR "%s - failed to $url\n" , $response->code;
404 printf STDERR "%s\n" , $response->message;
405 return 2;
406 }
407  
408 print STDERR $response->decoded_content , "\n";
409  
410 return 0;
411 }
412  
413 sub cmd_post {
414 my ($url, $file, $mimeType) = @_;
415  
416 $mimeType //= _guess_mimetype($file) if $file;
417  
418 unless ($url && $file && -r $file && $mimeType) {
419 print STDERR "Need url file and mimeType\n";
420 return 1;
421 }
422  
423 my $data = path($file)->slurp_raw;
424  
425 my $iri = _make_url($url);
426 my %headers = _make_headers();
427  
428 $headers{'Content-Type'} = $mimeType unless $headers{'Content-Type'};
429  
430 my $response = $agent->post($iri, $data, %headers);
431  
432 unless ($response->is_success) {
433 printf STDERR "%s - failed to $url\n" , $response->code;
434 printf STDERR "%s\n" , $response->message;
435 return 2;
436 }
437  
438 print STDERR $response->decoded_content , "\n";
439  
440 print $response->header('Location') , "\n";
441  
442 return 0;
443 }
444  
445 sub cmd_patch {
446 my ($url,$file_or_string) = @_;
447  
448 unless ($url && $file_or_string) {
449 print STDERR "Need a url and a file or string\n";
450 return 1;
451 }
452  
453 my $sparql;
454  
455 $file_or_string = '/dev/stdin' if $file_or_string eq '-';
456  
457 if (-r $file_or_string) {
458 $sparql = path($file_or_string)->slurp_utf8;
459 }
460 else {
461 $sparql = $file_or_string;
462 }
463  
464 my $iri = _make_url($url);
465 my %headers = _make_headers();
466 $headers{'Content-Type'} = 'application/sparql-update' unless $headers{'Content-Type'};
467  
468 # Prevent overwriting exiting resources
469 unless ($opt_force) {
470 $headers{'If-None-Match'} = '*';
471 }
472  
473 # Prevent overwriting changed resources
474 if ($opt_etag) {
475 $headers{'If-Match'} = $opt_etag;
476 }
477  
478 my $response = $agent->patch($iri, $sparql, %headers);
479  
480 unless ($response->is_success) {
481 printf STDERR "%s - failed to $url\n" , $response->code;
482 printf STDERR "%s\n" , $response->message;
483 return 2;
484 }
485  
486 print STDERR $response->decoded_content , "\n";
487  
488 return 0;
489 }
490  
491 sub cmd_delete {
492 my ($url) = @_;
493  
494 unless ($url) {
495 print STDERR "Need a url\n";
496 return 1;
497 }
498  
499 my $iri = _make_url($url);
500 my %headers = _make_headers();
501  
502 my $response = $agent->delete($iri, %headers);
503  
504 unless ($response->is_success) {
505 printf STDERR "%s - failed to $url\n" , $response->code;
506 printf STDERR "%s\n" , $response->message;
507 return 2;
508 }
509  
510 print STDERR $response->decoded_content , "\n";
511  
512 return 0;
513 }
514  
515 sub cmd_mirror {
516 my ($url,$directory) = @_;
517  
518 unless ($url) {
519 print STDERR "Need a url\n";
520 return 2;
521 }
522  
523 unless ($directory && -d $directory) {
524 print STDERR "Need a directory\n";
525 return 2;
526 }
527  
528 if ($url =~ /\/$/) {
529 # ok we are a container
530 }
531 else {
532 return _cmd_mirror($url,$directory);
533 }
534  
535 my $files = _cmd_list($url);
536  
537 return $files if $files && ref($files) eq '';
538  
539 for my $file (sort keys %$files) {
540 my $type = $files->{$file};
541 my $base = substr($file,length($url));
542 $base =~ s{\/$}{};
543 if ($type eq 'container') {
544 if ($file ne $url && $base !~ /^\./ && $opt_recursive) {
545 path("$directory/$base")->mkpath;
546 cmd_mirror($file,"$directory/$base");
547 }
548 }
549 else {
550 _cmd_mirror($file,$directory);
551 }
552 }
553  
554 _cmd_mirror_delete($url,$files,$directory) if $opt_delete;
555 }
556  
557 sub _cmd_mirror_delete {
558 my ($base,$files,$directory) = @_;
559 my $path_names;
560 for my $file (sort keys %$files) {
561 my $path = substr($file,length($base));
562 $path =~ s{^\/}{};
563 $path =~ s{\/$}{};
564 next unless length($path);
565 $path_names->{$path} = 1;
566 }
567  
568 for my $path (glob("$directory/*")) {
569 my $basename = basename($path);
570 next if -d $path; # we always keep directories
571 next if $path =~ /^[\.~]/;
572  
573 if ($path_names->{$basename}) {
574 # ok , known path
575 }
576 else {
577 if ($opt_real) {
578 print STDERR "deleting: $path\n";
579 unlink $path;
580 }
581 else {
582 print STDERR "deleting: $path [test : use -x for real delete]\n";
583 }
584 }
585 }
586 }
587  
588 sub _cmd_mirror {
589 my ($url,$directory) = @_;
590  
591 my $path = $url;
592 $path =~ s{.*\/}{};
593  
594 my %headers = ();
595  
596 if ($opt_skip && -e "$directory/$path" ) {
597 print STDERR "skipping $directory/$path - already exists\n";
598 return 0;
599 }
600  
601 if (-e "$directory/$path") {
602 my ($mtime) = ( stat("$directory/$path") )[9];
603 $headers{'If-Modified-Since'} = HTTP::Date::time2str($mtime);
604 }
605  
606 if ($opt_real) {
607 print "$url -> $directory/$path\n";
608 my $response = _cmd_get($url,%headers);
609  
610 return $response unless $response && ref($response) ne '';
611  
612 path("$directory/$path")->spew_raw($response->decoded_content);
613 }
614 else {
615 print "$url -> $directory/$path [test : use -x for real mirror]\n";
616 }
617  
618 return 0;
619 }
620  
621 sub cmd_upload {
622 my ($directory,$url) = @_;
623  
624 unless ($directory && -d $directory) {
625 print STDERR "Need a directory";
626 return 2;
627 }
628  
629 unless ($url =~ /\/$/) {
630 print STDERR "Url doesn't look like a container";
631 return 2;
632 }
633  
634 for my $file (glob("$directory/*")) {
635 my $upload_url;
636 my $upload_file = substr($file,length($directory) + 1);
637  
638 if (-d $file) {
639 $upload_url = "$url$upload_file/";
640 }
641 else {
642 $upload_url = "$url$upload_file";
643 }
644
645 if ($opt_real) {
646 print "$file -> $upload_url\n";
647  
648 if (-d $file) {
649 cmd_put($upload_url);
650 }
651 else {
652 cmd_put($upload_url,$file);
653 }
654 }
655 else {
656 print "$file -> $upload_url [test : use -x for real upload]\n";
657 }
658  
659 if ($opt_recursive && -d $file) {
660 cmd_upload($file,$upload_url);
661 }
662 }
663 }
664  
665 sub _crawl_container_url {
666 my ($url,$result) = @_;
667  
668 $result //= {};
669  
670 unless ($url && $url =~ /^\//) {
671 print STDERR "Need a container url\n";
672 return 2;
673 }
674  
675 my $files = _cmd_list($url);
676  
677 # return on error
678 return $files if $files && ref($files) eq '';
679  
680 for my $file (sort { $b cmp $a } keys %$files) {
681 my $type = $files->{$file};
682  
683 next if $file eq $url;
684  
685 $result->{$file} = $type;
686  
687 if ($type eq 'container') {
688 _crawl_container_url($file,$result);
689 }
690 }
691  
692 return $result;
693 }
694  
695 sub cmd_clean {
696 my ($url) = @_;
697  
698 unless ($url && $url =~ /^\//) {
699 print STDERR "Need a container url\n";
700 return 2;
701 }
702  
703 my $files;
704  
705 if ($opt_recursive) {
706 $files = _crawl_container_url($url);
707 }
708 else {
709 $files = _cmd_list($url);
710  
711 }
712  
713 for my $file (sort { $b cmp $a } keys %$files) {
714 my $type = $files->{$file};
715  
716 next if $file eq $url;
717  
718 if ($opt_keep && $type eq 'container') {
719 print "skipping: $file\n";
720 next;
721 }
722  
723 if ($opt_real) {
724 print "deleting: $file\n";
725 cmd_delete($file);
726 }
727 else {
728 print "deleting: $file [test : use -x for real upload]\n";
729 }
730 }
731 }
732  
733 sub cmd_authenticate {
734  
735 unless ($webid) {
736 print STDERR "Need a WebId or SOLID_WEBID environment variable\n";
737 return 2;
738 }
739  
740 $auth->make_clean;
741  
742 my $auth_url = $auth->make_authorization_request;
743  
744 print "Please visit this URL and login:\n\n$auth_url\n\n";
745  
746 print "Starting callback server...\n";
747  
748 $auth->listen;
749  
750 return 0;
751 }
752  
753 sub cmd_headers {
754 my ($method,$url) = @_;
755  
756 usage() unless $method && $url;
757  
758 my $headers = _authentication_headers($method,$url);
759  
760 print "$headers\n";
761  
762 return 0;
763 }
764  
765 sub cmd_curl {
766 my (@rest) = @_;
767  
768 usage() unless @rest;
769  
770 my $method = 'GET';
771 my $url = $rest[-1];
772  
773 shift @rest if @rest[0] eq '--';
774  
775 if (@rest) {
776 for (my $i = 0 ; $i < @rest ; $i++) {
777 if ($rest[$i] eq '-X') {
778 $method = $rest[$i+1];
779 }
780 }
781 @rest = map { String::Escape::quote($_) } @rest;
782 }
783  
784 my $headers = _authentication_headers($method,$url);
785 my $opts = join(" ",@rest);
786 system("curl $headers $opts") == 0;
787 }
788  
789 sub cmd_access_token {
790 my $access = $auth->get_access_token;
791  
792 unless ($webid) {
793 print STDERR "Need a WebId or SOLID_WEBID environment variable\n";
794 return 2;
795 }
796
797 unless ($access && $access->{access_token}) {
798 print STDERR "No access_token found. You are not logged in yet?\n";
799 return 2;
800 }
801  
802 my $token = $access->{access_token};
803  
804 my ($header,$payload,$signature) = split(/\./,$token,3);
805  
806 unless ($header && $payload, $signature) {
807 printf STDERR "Token is not a jwt token\n";
808 }
809  
810 my $json = JSON->new->pretty;
811  
812 $header = JSON::decode_json(MIME::Base64::decode_base64url($header));
813 $payload = JSON::decode_json(MIME::Base64::decode_base64url($payload));
814  
815 printf "Header: %s\n" , $json->encode($header);
816 printf "Payload: %s\n" , $json->encode($payload);
817 printf "Signature: (binary data)\n", MIME::Base64::decode_base64url($signature);
818  
819 return 0;
820 }
821  
822 sub cmd_id_token {
823 my $access = $auth->get_access_token;
824  
825 unless ($access && $access->{id_token}) {
826 print STDERR "No access_token found. You are not logged in yet?\n";
827 return 2;
828 }
829  
830 my $token = $access->{id_token};
831  
832 my ($header,$payload,$signature) = split(/\./,$token,3);
833  
834 unless ($header && $payload, $signature) {
835 printf STDERR "Token is not a jwt token\n";
836 }
837  
838 my $json = JSON->new->pretty;
839  
840 $header = JSON::decode_json(MIME::Base64::decode_base64url($header));
841 $payload = JSON::decode_json(MIME::Base64::decode_base64url($payload));
842  
843 printf "Header: %s\n" , $json->encode($header);
844 printf "Payload: %s\n" , $json->encode($payload);
845 printf "Signature: (binary data)\n", MIME::Base64::decode_base64url($signature);
846  
847 return 0;
848 }
849  
850 # Parse the optional provided headers into a hash
851 sub _make_headers {
852 my %headers = ();
853 for my $h (@$opt_header) {
854 my ($n,$v) = split(/\s*:\s*/,$h,2);
855 $headers{$n} = $v;
856 }
857 return %headers;
858 }
859  
860 # Add a Link header
861 sub _link_header {
862 my ($link,%headers) = @_;
863  
864 if (exists $headers{Link}) {
865 $headers{Link} .= ", $link";
866 }
867 else {
868 $headers{Link} = $link;
869 }
870  
871 %headers;
872 }
873  
874 # Expand a relative url to a full url with SOLID_REMOTE_BASE
875 sub _make_url {
876 my $url = shift;
877  
878 return $url if $url =~ /^http.*/;
879  
880 return $url unless defined($webbase);
881  
882 return $url unless $url =~ /^\.?(\/.*)?/;
883  
884 return "$webbase$1";
885 }
886  
887 # Expand the CURL header with authentication and DPop headers
888 sub _authentication_headers {
889 my ($method,$url) = @_;
890  
891 $webid //= $url;
892  
893 my $headers = $auth->make_authentication_headers($url,$method);
894  
895 unless ($headers) {
896 print STDERR "No access tokens found for $webid. Maybe you need to authenticate first?\n";
897 }
898  
899 my @headers = ();
900 for (keys %$headers) {
901 push @headers , "-H \"" . $_ . ":" . $headers->{$_} ."\"";
902 }
903  
904 return join(" ",@headers);
905 }
906  
907 # Guess the mime type of a file
908 sub _guess_mimetype {
909 my ($path) = @_;
910
911 my $magic = File::LibMagic->new;
912  
913 # My own MIME magic
914 return "text/turtle" if ($path =~ /\.ttl$/);
915 return "text/turtle" if ($path =~ /\.acl$/);
916 return "application/ld+json" if ($path =~ /\.jsonld$/);
917 return "text/n3" if ($path =~ /\.n3$/);
918 return "application/rdf+xml" if ($path =~ /\.rdf$/);
919  
920 # If the file is empty
921 if (! -e $path || -s $path) {
922 my $info = $magic->info_from_filename($path);
923 return $info->{mime_type};
924 }
925 else {
926 # Open the file and do magic guessing
927 open my $fh, '<', $path or die $!;
928 my $info = $magic->info_from_handle($fh);
929 close($fh);
930 return $info->{mime_type};
931 }
932 }
933  
934 __END__