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