| 1 | #!/usr/bin/env 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_log = 'log4perl.conf'; | ||
| 29 | my $opt_header = []; | ||
| 30 | |||
| 31 | GetOptions( | ||
| 32 | "clientid|c=s" => \$clientid , | ||
| 33 | "webid|w=s" => \$webid , | ||
| 34 | "base|b=s" => \$webbase , | ||
| 35 | "skip" => \$opt_skip , | ||
| 36 | "keep" => \$opt_keep , | ||
| 37 | "delete" => \$opt_delete , | ||
| 38 | "r" => \$opt_recursive , | ||
| 39 | "x" => \$opt_real , | ||
| 40 | "H=s@" => \$opt_header , | ||
| 41 | "log=s" => \$opt_log , | ||
| 42 | ); | ||
| 43 | |||
| 44 | my $cmd = shift; | ||
| 45 | |||
| 46 | if (-e $opt_log) { | ||
| 47 | Log::Log4perl::init($opt_log); | ||
| 48 | } | ||
| 49 | |||
| 50 | my $auth = Web::Solid::Auth->new(webid => $webid, client_id => $clientid); | ||
| 51 | my $agent = Web::Solid::Auth::Agent->new(auth => $auth); | ||
| 52 | |||
| 53 | if ($webbase) { | ||
| 54 | # Remove the trailing slash | ||
| 55 | $webbase =~ s{\/$}{}; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $ret; | ||
| 59 | |||
| 60 | if (0) {} | ||
| 61 | elsif ($cmd eq 'list') { | ||
| 62 | $ret = cmd_list(@ARGV); | ||
| 63 | } | ||
| 64 | elsif ($cmd eq 'get') { | ||
| 65 | $ret = cmd_get(@ARGV); | ||
| 66 | } | ||
| 67 | elsif ($cmd eq 'put') { | ||
| 68 | $ret = cmd_put(@ARGV); | ||
| 69 | } | ||
| 70 | elsif ($cmd eq 'post') { | ||
| 71 | $ret = cmd_post(@ARGV); | ||
| 72 | } | ||
| 73 | elsif ($cmd eq 'delete') { | ||
| 74 | $ret = cmd_delete(@ARGV); | ||
| 75 | } | ||
| 76 | elsif ($cmd eq 'head') { | ||
| 77 | $ret = cmd_head(@ARGV); | ||
| 78 | } | ||
| 79 | elsif ($cmd eq 'options') { | ||
| 80 | $ret = cmd_options(@ARGV); | ||
| 81 | } | ||
| 82 | elsif ($cmd eq 'mirror') { | ||
| 83 | $ret = cmd_mirror(@ARGV); | ||
| 84 | } | ||
| 85 | elsif ($cmd eq 'upload') { | ||
| 86 | $ret = cmd_upload(@ARGV); | ||
| 87 | } | ||
| 88 | elsif ($cmd eq 'clean') { | ||
| 89 | $ret = cmd_clean(@ARGV); | ||
| 90 | } | ||
| 91 | elsif ($cmd eq 'authenticate') { | ||
| 92 | $ret = cmd_authenticate(@ARGV); | ||
| 93 | } | ||
| 94 | elsif ($cmd eq 'headers') { | ||
| 95 | $ret = cmd_headers(@ARGV); | ||
| 96 | } | ||
| 97 | elsif ($cmd eq 'curl') { | ||
| 98 | $ret = cmd_curl(@ARGV); | ||
| 99 | } | ||
| 100 | elsif ($cmd eq 'id_token') { | ||
| 101 | $ret = cmd_id_token(@ARGV); | ||
| 102 | } | ||
| 103 | elsif ($cmd eq 'access_token') { | ||
| 104 | $ret = cmd_access_token(@ARGV); | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | usage(); | ||
| 108 | } | ||
| 109 | |||
| 110 | exit($ret); | ||
| 111 | |||
| 112 | sub usage { | ||
| 113 | print STDERR <<EOF; | ||
| 114 | Usage | ||
| 115 | -=-=-= | ||
| 116 | |||
| 117 | # Login | ||
| 118 | usage: $0 [options] authenticate | ||
| 119 | |||
| 120 | # Curl like interaction | ||
| 121 | usage: $0 [options] headers method url | ||
| 122 | usage: $0 [options] curl -- [curl-opts] url | ||
| 123 | |||
| 124 | # Interpret LDP responses | ||
| 125 | usage: $0 [options] list /path/ | url # folder listing | ||
| 126 | usage: $0 [options] mirror /path directory # mirror a container/resource , use [-r] for recursive mirror | ||
| 127 | usage: $0 [options] upload directory /path # upload the directory contents to a path, use [-r] for recursive upload | ||
| 128 | usage: $0 [options] clean /path # delete resources/containers, use [-r] for recursive clean | ||
| 129 | |||
| 130 | # Simple HTTP interaction | ||
| 131 | usage: $0 [options] get /path | url | ||
| 132 | usage: $0 [options] put (/path/ | url) # create a folder | ||
| 133 | usage: $0 [options] put (/path | url) file [mimeType] | ||
| 134 | usage: $0 [options] post (/path | url) file [mimeType] | ||
| 135 | usage: $0 [options] head /path | url | ||
| 136 | usage: $0 [options] options /path | url | ||
| 137 | usage: $0 [options] delete /path | url | ||
| 138 | |||
| 139 | # Check the credentials | ||
| 140 | usage: $0 access_token | ||
| 141 | usage: $0 id_token | ||
| 142 | |||
| 143 | options: | ||
| 144 | --webid|w webid - your webid | ||
| 145 | --clientid|c clientid - optional the client-id | ||
| 146 | --base|b base - optional the base url for all requests | ||
| 147 | --skip - skip files that already exist (mirror) | ||
| 148 | --delete - delete local files that are not at the remote location (mirror) | ||
| 149 | --keep - keep containers (clean) | ||
| 150 | -r - recursive (mirror, upload, clean) | ||
| 151 | -x - do it for real (upload, clean) | ||
| 152 | -H "Header" - add a header (get,post,put,head,delete) | ||
| 153 | EOF | ||
| 154 | exit 1 | ||
| 155 | } | ||
| 156 | |||
| 157 | sub cmd_list { | ||
| 158 | my ($url) = @_; | ||
| 159 | |||
| 160 | my $files = _cmd_list($url); | ||
| 161 | |||
| 162 | return $files if $files && ref($files) eq ''; | ||
| 163 | |||
| 164 | for my $file (sort keys %$files) { | ||
| 165 | my $type = $files->{$file}; | ||
| 166 | |||
| 167 | printf "%s $file\n" , $type eq 'container' ? "d" : "-"; | ||
| 168 | } | ||
| 169 | |||
| 170 | return 0; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub _cmd_list { | ||
| 174 | my ($url) = @_; | ||
| 175 | |||
| 176 | unless ($url) { | ||
| 177 | print STDERR "Need a url\n"; | ||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | unless ($url =~ /\/$/) { | ||
| 182 | print STDERR "$url doesn't look like a container\n"; | ||
| 183 | return 1; | ||
| 184 | } | ||
| 185 | |||
| 186 | my $iri = _make_url($url); | ||
| 187 | |||
| 188 | my $response = $agent->get($iri); | ||
| 189 | |||
| 190 | unless ($response->is_success) { | ||
| 191 | printf STDERR "%s - failed to $url\n" , $response->code; | ||
| 192 | printf STDERR "%s\n" , $response->message; | ||
| 193 | return 2; | ||
| 194 | } | ||
| 195 | |||
| 196 | my $util = Web::Solid::Auth::Util->new; | ||
| 197 | my $model = $util->parse_turtle($response->decoded_content); | ||
| 198 | |||
| 199 | my $sparql =<<EOF; | ||
| 200 | prefix ldp: <http://www.w3.org/ns/ldp#> | ||
| 201 | |||
| 202 | SELECT ?resource ?type { | ||
| 203 | ?resource a ?type . | ||
| 204 | FILTER (?type IN ( | ||
| 205 | ldp:Resource, | ||
| 206 | ldp:RDFSource, | ||
| 207 | ldp:Container, | ||
| 208 | ldp:BasicContainer, | ||
| 209 | ldp:IndirectContainer, | ||
| 210 | ldp:NonRDFSource | ||
| 211 | ) | ||
| 212 | ) | ||
| 213 | } | ||
| 214 | EOF | ||
| 215 | |||
| 216 | my %FILES = (); | ||
| 217 | |||
| 218 | $util->sparql($model, $sparql, sub { | ||
| 219 | my $res = shift; | ||
| 220 | my $name = $res->value('resource')->as_string; | ||
| 221 | $name = substr($name,length($webbase)); | ||
| 222 | $name =~ s/^\///; | ||
| 223 | my $type = $res->value('type')->as_string; | ||
| 224 | |||
| 225 | if ($FILES{$url . $name} && $FILES{$url . $name} eq 'container') { | ||
| 226 | # Containers are more interesting than resources | ||
| 227 | } | ||
| 228 | else { | ||
| 229 | $FILES{$url . $name} = $type =~ /Container/ ? "container" : "resource"; | ||
| 230 | } | ||
| 231 | }); | ||
| 232 | |||
| 233 | return \%FILES; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub cmd_get { | ||
| 237 | my ($url) = @_; | ||
| 238 | |||
| 239 | my %headers = _make_headers(); | ||
| 240 | |||
| 241 | my $response = _cmd_get($url,%headers); | ||
| 242 | |||
| 243 | return $response if $response && ref($response) eq ''; | ||
| 244 | |||
| 245 | print $response->decoded_content; | ||
| 246 | |||
| 247 | return 0; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub _cmd_get { | ||
| 251 | my ($url,%headers) = @_; | ||
| 252 | |||
| 253 | unless ($url) { | ||
| 254 | print STDERR "Need a url\n"; | ||
| 255 | return 1; | ||
| 256 | } | ||
| 257 | |||
| 258 | my $iri = _make_url($url); | ||
| 259 | |||
| 260 | my $response = $agent->get($iri,%headers); | ||
| 261 | |||
| 262 | unless ($response->is_success) { | ||
| 263 | printf STDERR "%s - failed to $url\n" , $response->code; | ||
| 264 | printf STDERR "%s\n" , $response->message; | ||
| 265 | return 2; | ||
| 266 | } | ||
| 267 | |||
| 268 | $response; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub cmd_head { | ||
| 272 | my ($url) = @_; | ||
| 273 | |||
| 274 | unless ($url) { | ||
| 275 | print STDERR "Need a url\n"; | ||
| 276 | return 1; | ||
| 277 | } | ||
| 278 | |||
| 279 | my $iri = _make_url($url); | ||
| 280 | my %headers = _make_headers(); | ||
| 281 | |||
| 282 | my $response = $agent->head($iri,%headers); | ||
| 283 | |||
| 284 | unless ($response->is_success) { | ||
| 285 | printf STDERR "%s - failed to $url\n" , $response->code; | ||
| 286 | printf STDERR "%s\n" , $response->message; | ||
| 287 | return 2; | ||
| 288 | } | ||
| 289 | |||
| 290 | for my $header ($response->header_field_names) { | ||
| 291 | printf "%s: %s\n" , $header , $response->header($header); | ||
| 292 | } | ||
| 293 | |||
| 294 | return 0; | ||
| 295 | } | ||
| 296 | |||
| 297 | sub cmd_options { | ||
| 298 | my ($url) = @_; | ||
| 299 | |||
| 300 | unless ($url) { | ||
| 301 | print STDERR "Need a url\n"; | ||
| 302 | return 1; | ||
| 303 | } | ||
| 304 | |||
| 305 | my $iri = _make_url($url); | ||
| 306 | my %headers = _make_headers(); | ||
| 307 | |||
| 308 | my $response = $agent->options($iri,%headers); | ||
| 309 | |||
| 310 | unless ($response->is_success) { | ||
| 311 | printf STDERR "%s - failed to $url\n" , $response->code; | ||
| 312 | printf STDERR "%s\n" , $response->message; | ||
| 313 | return 2; | ||
| 314 | } | ||
| 315 | |||
| 316 | for my $header ($response->header_field_names) { | ||
| 317 | printf "%s: %s\n" , $header , $response->header($header); | ||
| 318 | } | ||
| 319 | |||
| 320 | return 0; | ||
| 321 | } | ||
| 322 | |||
| 323 | sub cmd_put { | ||
| 324 | my ($url, $file, $mimeType) = @_; | ||
| 325 | |||
| 326 | $mimeType //= _guess_mimetype($file) if $file; | ||
| 327 | |||
| 328 | unless ($url) { | ||
| 329 | print STDERR "Need a url\n"; | ||
| 330 | return 1; | ||
| 331 | } | ||
| 332 | |||
| 333 | if ($url =~ /\/$/ && ($file || $mimeType)) { | ||
| 334 | print STDERR "Folder names can't have file uploads\n\n"; | ||
| 335 | return 1; | ||
| 336 | } | ||
| 337 | elsif ($url !~ /\/$/ && ! ($file || $mimeType)) { | ||
| 338 | print STDERR "Need url file and mimeType\n"; | ||
| 339 | return 1; | ||
| 340 | } | ||
| 341 | |||
| 342 | my $data; | ||
| 343 | |||
| 344 | if ($file) { | ||
| 345 | $data = path($file)->slurp_raw; | ||
| 346 | } | ||
| 347 | |||
| 348 | my $iri = _make_url($url); | ||
| 349 | my %headers = _make_headers(); | ||
| 350 | $headers{'Content-Type'} = $mimeType unless $headers{'Content-Type'}; | ||
| 351 | |||
| 352 | my $response; | ||
| 353 | |||
| 354 | if ($file) { | ||
| 355 | $response = $agent->put($iri, $data, %headers); | ||
| 356 | } | ||
| 357 | else { | ||
| 358 | $response = $agent->put($iri,undef,%headers); | ||
| 359 | } | ||
| 360 | |||
| 361 | unless ($response->is_success) { | ||
| 362 | printf STDERR "%s - failed to $url\n" , $response->code; | ||
| 363 | printf STDERR "%s\n" , $response->message; | ||
| 364 | return 2; | ||
| 365 | } | ||
| 366 | |||
| 367 | print STDERR $response->decoded_content , "\n"; | ||
| 368 | |||
| 369 | return 0; | ||
| 370 | } | ||
| 371 | |||
| 372 | sub cmd_post { | ||
| 373 | my ($url, $file, $mimeType) = @_; | ||
| 374 | |||
| 375 | $mimeType //= _guess_mimetype($file) if $file; | ||
| 376 | |||
| 377 | unless ($url && $file && -r $file && $mimeType) { | ||
| 378 | print STDERR "Need url file and mimeType\n"; | ||
| 379 | return 1; | ||
| 380 | } | ||
| 381 | |||
| 382 | my $data = path($file)->slurp_raw; | ||
| 383 | |||
| 384 | my $iri = _make_url($url); | ||
| 385 | my %headers = _make_headers(); | ||
| 386 | $headers{'Content-Type'} = $mimeType unless $headers{'Content-Type'}; | ||
| 387 | |||
| 388 | my $response = $agent->post($iri, $data, %headers); | ||
| 389 | |||
| 390 | unless ($response->is_success) { | ||
| 391 | printf STDERR "%s - failed to $url\n" , $response->code; | ||
| 392 | printf STDERR "%s\n" , $response->message; | ||
| 393 | return 2; | ||
| 394 | } | ||
| 395 | |||
| 396 | print STDERR $response->decoded_content , "\n"; | ||
| 397 | |||
| 398 | print $response->header('Location') , "\n"; | ||
| 399 | |||
| 400 | return 0; | ||
| 401 | } | ||
| 402 | |||
| 403 | sub cmd_delete { | ||
| 404 | my ($url) = @_; | ||
| 405 | |||
| 406 | unless ($url) { | ||
| 407 | print STDERR "Need a url\n"; | ||
| 408 | return 1; | ||
| 409 | } | ||
| 410 | |||
| 411 | my $iri = _make_url($url); | ||
| 412 | my %headers = _make_headers(); | ||
| 413 | |||
| 414 | my $response = $agent->delete($iri, %headers); | ||
| 415 | |||
| 416 | unless ($response->is_success) { | ||
| 417 | printf STDERR "%s - failed to $url\n" , $response->code; | ||
| 418 | printf STDERR "%s\n" , $response->message; | ||
| 419 | return 2; | ||
| 420 | } | ||
| 421 | |||
| 422 | print STDERR $response->decoded_content , "\n"; | ||
| 423 | |||
| 424 | return 0; | ||
| 425 | } | ||
| 426 | |||
| 427 | sub cmd_mirror { | ||
| 428 | my ($url,$directory) = @_; | ||
| 429 | |||
| 430 | unless ($url) { | ||
| 431 | print STDERR "Need a url\n"; | ||
| 432 | return 2; | ||
| 433 | } | ||
| 434 | |||
| 435 | unless ($directory && -d $directory) { | ||
| 436 | print STDERR "Need a directory\n"; | ||
| 437 | return 2; | ||
| 438 | } | ||
| 439 | |||
| 440 | if ($url =~ /\/$/) { | ||
| 441 | # ok we are a container | ||
| 442 | } | ||
| 443 | else { | ||
| 444 | return _cmd_mirror($url,$directory); | ||
| 445 | } | ||
| 446 | |||
| 447 | my $files = _cmd_list($url); | ||
| 448 | |||
| 449 | return $files if $files && ref($files) eq ''; | ||
| 450 | |||
| 451 | for my $file (sort keys %$files) { | ||
| 452 | my $type = $files->{$file}; | ||
| 453 | my $base = substr($file,length($url)); | ||
| 454 | $base =~ s{\/$}{}; | ||
| 455 | if ($type eq 'container') { | ||
| 456 | if ($file ne $url && $base !~ /^\./ && $opt_recursive) { | ||
| 457 | path("$directory/$base")->mkpath; | ||
| 458 | cmd_mirror($file,"$directory/$base"); | ||
| 459 | } | ||
| 460 | } | ||
| 461 | else { | ||
| 462 | _cmd_mirror($file,$directory); | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | _cmd_mirror_delete($url,$files,$directory) if $opt_delete; | ||
| 467 | } | ||
| 468 | |||
| 469 | sub _cmd_mirror_delete { | ||
| 470 | my ($base,$files,$directory) = @_; | ||
| 471 | my $path_names; | ||
| 472 | for my $file (sort keys %$files) { | ||
| 473 | my $path = substr($file,length($base)); | ||
| 474 | $path =~ s{^\/}{}; | ||
| 475 | $path =~ s{\/$}{}; | ||
| 476 | next unless length($path); | ||
| 477 | $path_names->{$path} = 1; | ||
| 478 | } | ||
| 479 | |||
| 480 | for my $path (glob("$directory/*")) { | ||
| 481 | my $basename = basename($path); | ||
| 482 | next if -d $path; # we always keep directories | ||
| 483 | next if $path =~ /^[\.~]/; | ||
| 484 | |||
| 485 | if ($path_names->{$basename}) { | ||
| 486 | # ok , known path | ||
| 487 | } | ||
| 488 | else { | ||
| 489 | if ($opt_real) { | ||
| 490 | print STDERR "deleting: $path\n"; | ||
| 491 | unlink $path; | ||
| 492 | } | ||
| 493 | else { | ||
| 494 | print STDERR "deleting: $path [test : use -x for real delete]\n"; | ||
| 495 | } | ||
| 496 | } | ||
| 497 | } | ||
| 498 | } | ||
| 499 | |||
| 500 | sub _cmd_mirror { | ||
| 501 | my ($url,$directory) = @_; | ||
| 502 | |||
| 503 | my $path = $url; | ||
| 504 | $path =~ s{.*\/}{}; | ||
| 505 | |||
| 506 | my %headers = (); | ||
| 507 | |||
| 508 | if ($opt_skip && -e "$directory/$path" ) { | ||
| 509 | print STDERR "skipping $directory/$path - already exists\n"; | ||
| 510 | return 0; | ||
| 511 | } | ||
| 512 | |||
| 513 | if (-e "$directory/$path") { | ||
| 514 | my ($mtime) = ( stat("$directory/$path") )[9]; | ||
| 515 | $headers{'If-Modified-Since'} = HTTP::Date::time2str($mtime); | ||
| 516 | } | ||
| 517 | |||
| 518 | if ($opt_real) { | ||
| 519 | print "$url -> $directory/$path\n"; | ||
| 520 | my $response = _cmd_get($url,%headers); | ||
| 521 | |||
| 522 | return $response unless $response && ref($response) ne ''; | ||
| 523 | |||
| 524 | path("$directory/$path")->spew_raw($response->decoded_content); | ||
| 525 | } | ||
| 526 | else { | ||
| 527 | print "$url -> $directory/$path [test : use -x for real mirror]\n"; | ||
| 528 | } | ||
| 529 | |||
| 530 | return 0; | ||
| 531 | } | ||
| 532 | |||
| 533 | sub cmd_upload { | ||
| 534 | my ($directory,$url) = @_; | ||
| 535 | |||
| 536 | unless ($directory && -d $directory) { | ||
| 537 | print STDERR "Need a directory"; | ||
| 538 | return 2; | ||
| 539 | } | ||
| 540 | |||
| 541 | unless ($url =~ /\/$/) { | ||
| 542 | print STDERR "Url doesn't look like a container"; | ||
| 543 | return 2; | ||
| 544 | } | ||
| 545 | |||
| 546 | for my $file (glob("$directory/*")) { | ||
| 547 | my $upload_url; | ||
| 548 | my $upload_file = substr($file,length($directory) + 1); | ||
| 549 | |||
| 550 | if (-d $file) { | ||
| 551 | $upload_url = "$url$upload_file/"; | ||
| 552 | } | ||
| 553 | else { | ||
| 554 | $upload_url = "$url$upload_file"; | ||
| 555 | } | ||
| 556 | |||
| 557 | if ($opt_real) { | ||
| 558 | print "$file -> $upload_url\n"; | ||
| 559 | |||
| 560 | if (-d $file) { | ||
| 561 | cmd_put($upload_url); | ||
| 562 | } | ||
| 563 | else { | ||
| 564 | cmd_put($upload_url,$file); | ||
| 565 | } | ||
| 566 | } | ||
| 567 | else { | ||
| 568 | print "$file -> $upload_url [test : use -x for real upload]\n"; | ||
| 569 | } | ||
| 570 | |||
| 571 | if ($opt_recursive && -d $file) { | ||
| 572 | cmd_upload($file,$upload_url); | ||
| 573 | } | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | sub _crawl_container_url { | ||
| 578 | my ($url,$result) = @_; | ||
| 579 | |||
| 580 | $result //= {}; | ||
| 581 | |||
| 582 | unless ($url && $url =~ /^\//) { | ||
| 583 | print STDERR "Need a container url\n"; | ||
| 584 | return 2; | ||
| 585 | } | ||
| 586 | |||
| 587 | my $files = _cmd_list($url); | ||
| 588 | |||
| 589 | # return on error | ||
| 590 | return $files if $files && ref($files) eq ''; | ||
| 591 | |||
| 592 | for my $file (sort { $b cmp $a } keys %$files) { | ||
| 593 | my $type = $files->{$file}; | ||
| 594 | |||
| 595 | next if $file eq $url; | ||
| 596 | |||
| 597 | $result->{$file} = $type; | ||
| 598 | |||
| 599 | if ($type eq 'container') { | ||
| 600 | _crawl_container_url($file,$result); | ||
| 601 | } | ||
| 602 | } | ||
| 603 | |||
| 604 | return $result; | ||
| 605 | } | ||
| 606 | |||
| 607 | sub cmd_clean { | ||
| 608 | my ($url) = @_; | ||
| 609 | |||
| 610 | unless ($url && $url =~ /^\//) { | ||
| 611 | print STDERR "Need a container url\n"; | ||
| 612 | return 2; | ||
| 613 | } | ||
| 614 | |||
| 615 | my $files; | ||
| 616 | |||
| 617 | if ($opt_recursive) { | ||
| 618 | $files = _crawl_container_url($url); | ||
| 619 | } | ||
| 620 | else { | ||
| 621 | $files = _cmd_list($url); | ||
| 622 | |||
| 623 | } | ||
| 624 | |||
| 625 | for my $file (sort { $b cmp $a } keys %$files) { | ||
| 626 | my $type = $files->{$file}; | ||
| 627 | |||
| 628 | next if $file eq $url; | ||
| 629 | |||
| 630 | if ($opt_keep && $type eq 'container') { | ||
| 631 | print "skipping: $file\n"; | ||
| 632 | next; | ||
| 633 | } | ||
| 634 | |||
| 635 | if ($opt_real) { | ||
| 636 | print "deleting: $file\n"; | ||
| 637 | cmd_delete($file); | ||
| 638 | } | ||
| 639 | else { | ||
| 640 | print "deleting: $file [test : use -x for real upload]\n"; | ||
| 641 | } | ||
| 642 | } | ||
| 643 | } | ||
| 644 | |||
| 645 | sub cmd_authenticate { | ||
| 646 | |||
| 647 | unless ($webid) { | ||
| 648 | print STDERR "Need a WebId or SOLID_WEBID environment variable\n"; | ||
| 649 | return 2; | ||
| 650 | } | ||
| 651 | |||
| 652 | $auth->make_clean; | ||
| 653 | |||
| 654 | my $auth_url = $auth->make_authorization_request; | ||
| 655 | |||
| 656 | print "Please visit this URL and login:\n\n$auth_url\n\n"; | ||
| 657 | |||
| 658 | print "Starting callback server...\n"; | ||
| 659 | |||
| 660 | $auth->listen; | ||
| 661 | |||
| 662 | return 0; | ||
| 663 | } | ||
| 664 | |||
| 665 | sub cmd_headers { | ||
| 666 | my ($method,$url) = @_; | ||
| 667 | |||
| 668 | usage() unless $method && $url; | ||
| 669 | |||
| 670 | my $headers = _headers($method,$url); | ||
| 671 | |||
| 672 | print "$headers\n"; | ||
| 673 | |||
| 674 | return 0; | ||
| 675 | } | ||
| 676 | |||
| 677 | sub cmd_curl { | ||
| 678 | my (@rest) = @_; | ||
| 679 | |||
| 680 | usage() unless @rest; | ||
| 681 | |||
| 682 | my $method = 'GET'; | ||
| 683 | my $url = $rest[-1]; | ||
| 684 | |||
| 685 | shift @rest if @rest[0] eq '--'; | ||
| 686 | |||
| 687 | if (@rest) { | ||
| 688 | for (my $i = 0 ; $i < @rest ; $i++) { | ||
| 689 | if ($rest[$i] eq '-X') { | ||
| 690 | $method = $rest[$i+1]; | ||
| 691 | } | ||
| 692 | } | ||
| 693 | @rest = map { String::Escape::quote($_) } @rest; | ||
| 694 | } | ||
| 695 | |||
| 696 | my $headers = _headers($method,$url); | ||
| 697 | my $opts = join(" ",@rest); | ||
| 698 | system("curl $headers $opts") == 0; | ||
| 699 | } | ||
| 700 | |||
| 701 | sub cmd_access_token { | ||
| 702 | my $access = $auth->get_access_token; | ||
| 703 | |||
| 704 | unless ($webid) { | ||
| 705 | print STDERR "Need a WebId or SOLID_WEBID environment variable\n"; | ||
| 706 | return 2; | ||
| 707 | } | ||
| 708 | |||
| 709 | unless ($access && $access->{access_token}) { | ||
| 710 | print STDERR "No access_token found. You are not logged in yet?\n"; | ||
| 711 | return 2; | ||
| 712 | } | ||
| 713 | |||
| 714 | my $token = $access->{access_token}; | ||
| 715 | |||
| 716 | my ($header,$payload,$signature) = split(/\./,$token,3); | ||
| 717 | |||
| 718 | unless ($header && $payload, $signature) { | ||
| 719 | printf STDERR "Token is not a jwt token\n"; | ||
| 720 | } | ||
| 721 | |||
| 722 | my $json = JSON->new->pretty; | ||
| 723 | |||
| 724 | $header = JSON::decode_json(MIME::Base64::decode_base64url($header)); | ||
| 725 | $payload = JSON::decode_json(MIME::Base64::decode_base64url($payload)); | ||
| 726 | |||
| 727 | printf "Header: %s\n" , $json->encode($header); | ||
| 728 | printf "Payload: %s\n" , $json->encode($payload); | ||
| 729 | printf "Signature: (binary data)\n", MIME::Base64::decode_base64url($signature); | ||
| 730 | |||
| 731 | return 0; | ||
| 732 | } | ||
| 733 | |||
| 734 | sub cmd_id_token { | ||
| 735 | my $access = $auth->get_access_token; | ||
| 736 | |||
| 737 | unless ($access && $access->{id_token}) { | ||
| 738 | print STDERR "No access_token found. You are not logged in yet?\n"; | ||
| 739 | return 2; | ||
| 740 | } | ||
| 741 | |||
| 742 | my $token = $access->{id_token}; | ||
| 743 | |||
| 744 | my ($header,$payload,$signature) = split(/\./,$token,3); | ||
| 745 | |||
| 746 | unless ($header && $payload, $signature) { | ||
| 747 | printf STDERR "Token is not a jwt token\n"; | ||
| 748 | } | ||
| 749 | |||
| 750 | my $json = JSON->new->pretty; | ||
| 751 | |||
| 752 | $header = JSON::decode_json(MIME::Base64::decode_base64url($header)); | ||
| 753 | $payload = JSON::decode_json(MIME::Base64::decode_base64url($payload)); | ||
| 754 | |||
| 755 | printf "Header: %s\n" , $json->encode($header); | ||
| 756 | printf "Payload: %s\n" , $json->encode($payload); | ||
| 757 | printf "Signature: (binary data)\n", MIME::Base64::decode_base64url($signature); | ||
| 758 | |||
| 759 | return 0; | ||
| 760 | } | ||
| 761 | |||
| 762 | sub _make_headers { | ||
| 763 | my %headers = (); | ||
| 764 | for my $h (@$opt_header) { | ||
| 765 | my ($n,$v) = split(/\s*:\s*/,$h,2); | ||
| 766 | $headers{$n} = $v; | ||
| 767 | } | ||
| 768 | return %headers; | ||
| 769 | } | ||
| 770 | |||
| 771 | sub _make_url { | ||
| 772 | my $url = shift; | ||
| 773 | |||
| 774 | return $url if $url =~ /^http.*/; | ||
| 775 | |||
| 776 | return $url unless defined($webbase); | ||
| 777 | |||
| 778 | return $url unless $url =~ /^\.?(\/.*)?/; | ||
| 779 | |||
| 780 | return "$webbase$1"; | ||
| 781 | } | ||
| 782 | |||
| 783 | sub _headers { | ||
| 784 | my ($method,$url) = @_; | ||
| 785 | |||
| 786 | $webid //= $url; | ||
| 787 | |||
| 788 | my $headers = $auth->make_authentication_headers($url,$method); | ||
| 789 | |||
| 790 | unless ($headers) { | ||
| 791 | print STDERR "No access tokens found for $webid. Maybe you need to authenticate first?\n"; | ||
| 792 | } | ||
| 793 | |||
| 794 | my @headers = (); | ||
| 795 | for (keys %$headers) { | ||
| 796 | push @headers , "-H \"" . $_ . ":" . $headers->{$_} ."\""; | ||
| 797 | } | ||
| 798 | |||
| 799 | return join(" ",@headers); | ||
| 800 | } | ||
| 801 | |||
| 802 | sub _guess_mimetype { | ||
| 803 | my ($path) = @_; | ||
| 804 | |||
| 805 | my $magic = File::LibMagic->new; | ||
| 806 | |||
| 807 | # My own MIME magic | ||
| 808 | return "text/turtle" if ($path =~ /\.ttl$/); | ||
| 809 | return "text/turtle" if ($path =~ /\.acl$/); | ||
| 810 | return "application/ld+json" if ($path =~ /\.jsonld$/); | ||
| 811 | return "text/n3" if ($path =~ /\.n3$/); | ||
| 812 | return "application/rdf+xml" if ($path =~ /\.rdf$/); | ||
| 813 | |||
| 814 | # If the file is empty | ||
| 815 | if (! -e $path || -s $path) { | ||
| 816 | my $info = $magic->info_from_filename($path); | ||
| 817 | return $info->{mime_type}; | ||
| 818 | } | ||
| 819 | else { | ||
| 820 | # Open the file and do magic guessing | ||
| 821 | open my $fh, '<', $path or die $!; | ||
| 822 | my $info = $magic->info_from_handle($fh); | ||
| 823 | close($fh); | ||
| 824 | return $info->{mime_type}; | ||
| 825 | } | ||
| 826 | } | ||
| 827 | |||
| 828 | __END__ | ||
| 829 | |||
| 830 | =head1 NAME | ||
| 831 | |||
| 832 | solid_auth.pl - A Solid management tool | ||
| 833 | |||
| 834 | =head1 SYNOPSIS | ||
| 835 | |||
| 836 | # Set your default webid | ||
| 837 | export SOLID_WEBID=https://timbl.inrupt.net/profile/card#me | ||
| 838 | |||
| 839 | # Authentication to a pod | ||
| 840 | solid_auth.pl authenticate | ||
| 841 | |||
| 842 | # Get the http headers for a authenticated request | ||
| 843 | solid_auth.pl headers GET https://timbl.inrupt.net/inbox/ | ||
| 844 | |||
| 845 | # Act like a curl command and fetch authenticated content | ||
| 846 | solid_auth.pl curl -- -X GET https://timbl.inrupt.net/inbox/ | ||
| 847 | |||
| 848 | # Add some data | ||
| 849 | solid_auth.pl curl -- -X POST \ | ||
| 850 | -H "Content-Type: text/plain" \ | ||
| 851 | -d "abc" \ | ||
| 852 | https://timbl.inrupt.net/public/ | ||
| 853 | |||
| 854 | # Add a file | ||
| 855 | solid_auth.pl curl -- -X PUT \ | ||
| 856 | -H "Content-Type: application/ld+json" \ | ||
| 857 | -d "@myfile.jsonld" \ | ||
| 858 | https://timbl.inrupt.net/public/myfile.jsonld | ||
| 859 | |||
| 860 | # Set a solid base url | ||
| 861 | export SOLID_REMOTE_BASE=https://timbl.inrupt.net | ||
| 862 | |||
| 863 | # List all resources on some Pod path | ||
| 864 | solid_auth.pl list /public/ | ||
| 865 | |||
| 866 | # Get some data | ||
| 867 | solid_auth.pl get /inbox/ | ||
| 868 | |||
| 869 | # Post some data | ||
| 870 | solid_auth.pl post /inbox/ myfile.jsonld | ||
| 871 | |||
| 872 | # Put some data | ||
| 873 | solid_auth.pl put /public/myfile.txt myfile.txt | ||
| 874 | |||
| 875 | # Create a folder | ||
| 876 | solid_auth.pl put /public/mytestfolder/ | ||
| 877 | |||
| 878 | # Delete some data | ||
| 879 | solid_auth.pl delete /public/myfile.txt | ||
| 880 | |||
| 881 | # Mirror a resource, container or tree | ||
| 882 | mkdir /data/my_copy | ||
| 883 | solid_auth.pl -r mirror /public/ /data/my_copy | ||
| 884 | |||
| 885 | # Upload a directory to the pod | ||
| 886 | # Add the -x option to do it for real (only a test without this option) | ||
| 887 | solid_auth.pl -r upload /data/my_copy /public/ | ||
| 888 | |||
| 889 | # Clean all files in a container | ||
| 890 | # Add the -x option to do it for real (only a test without this option) | ||
| 891 | solid_auth.pl --keep clean /demo/ | ||
| 892 | |||
| 893 | # Clean a complete container | ||
| 894 | # Add the -x option to do it for real (only a test without this option) | ||
| 895 | solid_auth.pl -r clean /demo/ | ||
| 896 | |||
| 897 | =head1 ENVIRONMENT | ||
| 898 | |||
| 899 | =over | ||
| 900 | |||
| 901 | =item SOLID_WEBID | ||
| 902 | |||
| 903 | Your WebId. | ||
| 904 | |||
| 905 | =item SOLID_REMOTE_BASE | ||
| 906 | |||
| 907 | The Base URL that is used for all delete, get, head, options post, put, patch requests. | ||
| 908 | |||
| 909 | =item SOLID_CLIENT_ID | ||
| 910 | |||
| 911 | The URL to a static client configuration. See C<etc/web-solid-auth.jsonld> for an example. | ||
| 912 | This file, edited for your own environment, needs to be published on some public accessible | ||
| 913 | webserver. | ||
| 914 | |||
| 915 | =back | ||
| 916 | |||
| 917 | =head1 CONFIGURATION | ||
| 918 | |||
| 919 | =over | ||
| 920 | |||
| 921 | =item --webid | ||
| 922 | |||
| 923 | Your WebId. | ||
| 924 | |||
| 925 | =item --base | ||
| 926 | |||
| 927 | The Base URL that is used for all delete, get, head, options post, put, patch requests. | ||
| 928 | |||
| 929 | =item --clientid | ||
| 930 | |||
| 931 | The URL to a static client configuration. See C<etc/web-solid-auth.jsonld> for an example. | ||
| 932 | This file, edited for your own environment, needs to be published on some public accessible | ||
| 933 | webserver. | ||
| 934 | |||
| 935 | =item --skip | ||
| 936 | |||
| 937 | Skip resources that already exist (mirror). | ||
| 938 | |||
| 939 | =item --delete | ||
| 940 | |||
| 941 | Delete local files that are not in the remote container (mirror). | ||
| 942 | |||
| 943 | =item --keep | ||
| 944 | |||
| 945 | Keep containers when cleaning data (clean). | ||
| 946 | |||
| 947 | =item -r | ||
| 948 | |||
| 949 | Recursive (clean, mirror, upload). | ||
| 950 | |||
| 951 | =item -x | ||
| 952 | |||
| 953 | Do it for real. The commands C<clean> and C<upload> will run by default in safe mode. | ||
| 954 | |||
| 955 | =item -H name=value | ||
| 956 | |||
| 957 | Add a header to a request (repeatable) for C<get>, C<post>, C<head>, C<options> and C<delete>. | ||
| 958 | |||
| 959 | =back | ||
| 960 | |||
| 961 | =head1 COMMAND | ||
| 962 | |||
| 963 | =over | ||
| 964 | |||
| 965 | =item authenticate | ||
| 966 | |||
| 967 | Start an authentication process for your WebId. You will be presented with a | ||
| 968 | URL that you need to open in a webbrowser. After a successfull login the | ||
| 969 | command can be closed. | ||
| 970 | |||
| 971 | The webbrowser needs to be opened on the same host as the where you where you | ||
| 972 | run the solid_auth.pl command. | ||
| 973 | |||
| 974 | =item headers METHOD URL | ||
| 975 | |||
| 976 | Return the Authentication and DPoP headers for a HTTP C<METHOD> request to C<URL>. | ||
| 977 | |||
| 978 | =item curl CURL-OPTS | ||
| 979 | |||
| 980 | Execute a curl command with Authentication and DPoP headers added. Add a C<--> | ||
| 981 | option to the C<CURL-OPTS> to stop solid_auth.pl from interpreting Curl options. | ||
| 982 | |||
| 983 | =item list URL | ||
| 984 | |||
| 985 | List the resources in a LDP container at URL. | ||
| 986 | |||
| 987 | =item mirror [-rx] [--skip] [--delete] URL DIRECTORY | ||
| 988 | |||
| 989 | Mirror the contents of a container to a local directory. Optional provide C<-r> | ||
| 990 | option for recursive mirror. | ||
| 991 | |||
| 992 | =item upload [-rx] DIRECTORY URL | ||
| 993 | |||
| 994 | Upload a directorty to a container. Optional provide C<-r> option for recursive | ||
| 995 | upload. With the C<-x> option, the command will run in demo mode. | ||
| 996 | |||
| 997 | =item clean [-rx] [--keep] URL | ||
| 998 | |||
| 999 | Clean all resources in a directory. Optional provide C<-r> option for recursive | ||
| 1000 | clean. With the C<-x> option, the command will run in demo mode. With the | ||
| 1001 | C<--keep> options all container will be kept. | ||
| 1002 | |||
| 1003 | =item get URL | ||
| 1004 | |||
| 1005 | Return the response of a HTTP GET request to URL. | ||
| 1006 | |||
| 1007 | =item post URL FILE [MIMETYPE] | ||
| 1008 | |||
| 1009 | Return the HTTP Message of a HTTP POST request of the FILE with MIMETYPE. | ||
| 1010 | Uses libmagic to guess the mimetype. | ||
| 1011 | |||
| 1012 | =item put URL [FILE] [MIMETYPE] | ||
| 1013 | |||
| 1014 | Return the HTTP Message of a HTTP PUT request of the FILE with MIMETYPE. | ||
| 1015 | Uses libmagic to guess the mimetype. | ||
| 1016 | |||
| 1017 | When the URL ends with a slash (/), then a new container will be created. | ||
| 1018 | |||
| 1019 | =item head URL | ||
| 1020 | |||
| 1021 | Return the HTTP Header of a HTTP HEAD request to URL. | ||
| 1022 | |||
| 1023 | =item head OPTIONS | ||
| 1024 | |||
| 1025 | Return the HTTP Header of a HTTP OPTIONS request to URL. | ||
| 1026 | |||
| 1027 | =item delete URL | ||
| 1028 | |||
| 1029 | Return the HTTP Message of a HTTP DELETE request to URL. | ||
| 1030 | |||
| 1031 | =item id_token | ||
| 1032 | |||
| 1033 | Show the contents of the JWT id token. | ||
| 1034 | |||
| 1035 | =item access_token | ||
| 1036 | |||
| 1037 | Show the contents of the JWT access token. | ||
| 1038 | |||
| 1039 | =back | ||
| 1040 | |||
| 1041 | =head1 INSPIRATION | ||
| 1042 | |||
| 1043 | This was very much inspired by the Python solid-flask code by | ||
| 1044 | Rai L<http://agentydragon.com> at L<https://gitlab.com/agentydragon/solid-flask>, | ||
| 1045 | and Jeff Zucker's <https://github.com/jeff-zucker> Solid-Shell at L<https://www.npmjs.com/package/solid-shell>. | ||
| 1046 | |||
| 1047 | =head1 COPYRIGHT AND LICENSE | ||
| 1048 | |||
| 1049 | This software is copyright (c) 2021 by Patrick Hochstenbach. | ||
| 1050 | |||
| 1051 | This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. | ||
| 1052 | |||
| 1053 | =encoding utf8 | ||
| 1054 | |||
| 1055 | =cut |