|
Lines 104-110
use MARC::File::XML;
Link Here
|
| 104 |
|
104 |
|
| 105 |
use constant CLIENT_READ_TIMEOUT => 5; |
105 |
use constant CLIENT_READ_TIMEOUT => 5; |
| 106 |
use constant CLIENT_READ_BUFFER_SIZE => 100000; |
106 |
use constant CLIENT_READ_BUFFER_SIZE => 100000; |
| 107 |
use constant AUTH_URI => "/cgi-bin/koha/mainpage.pl"; |
107 |
use constant AUTH_URI => "/cgi-bin/koha/svc/authentication"; |
| 108 |
use constant IMPORT_SVC_URI => "/cgi-bin/koha/svc/import_bib"; |
108 |
use constant IMPORT_SVC_URI => "/cgi-bin/koha/svc/import_bib"; |
| 109 |
|
109 |
|
| 110 |
sub new { |
110 |
sub new { |
|
Lines 239-244
sub _ua {
Link Here
|
| 239 |
return $ua; |
239 |
return $ua; |
| 240 |
} |
240 |
} |
| 241 |
|
241 |
|
|
|
242 |
sub get_current_csrf_token { |
| 243 |
my $self = shift; |
| 244 |
my $ua = $self->{ua}; |
| 245 |
my $url = $self->{koha} . AUTH_URI; |
| 246 |
return $ua->get($url)->header('CSRF-TOKEN'); |
| 247 |
} |
| 248 |
|
| 249 |
sub authenticate { |
| 250 |
my $self = shift; |
| 251 |
my $ua = $self->{ua}; |
| 252 |
my $url = $self->{koha} . AUTH_URI; |
| 253 |
my $resp = $ua->post( |
| 254 |
$url, |
| 255 |
{ |
| 256 |
login_userid => $self->{user}, |
| 257 |
login_password => $self->{password}, |
| 258 |
csrf_token => $self->get_current_csrf_token, |
| 259 |
} |
| 260 |
); |
| 261 |
if ( !$resp->is_success ) { |
| 262 |
$self->log("Authentication failed", $resp->request->as_string, $resp->as_string); |
| 263 |
return; |
| 264 |
} |
| 265 |
return $resp->header('CSRF-TOKEN'); |
| 266 |
} |
| 267 |
|
| 242 |
sub read_request { |
268 |
sub read_request { |
| 243 |
my ( $self, $io ) = @_; |
269 |
my ( $self, $io ) = @_; |
| 244 |
|
270 |
|
|
Lines 355-392
sub handle_request {
Link Here
|
| 355 |
} |
381 |
} |
| 356 |
|
382 |
|
| 357 |
my $base_url = $self->{koha}; |
383 |
my $base_url = $self->{koha}; |
|
|
384 |
my $post_body = { |
| 385 |
'nomatch_action' => $self->{params}->{nomatch_action}, |
| 386 |
'overlay_action' => $self->{params}->{overlay_action}, |
| 387 |
'match' => $self->{params}->{match}, |
| 388 |
'import_mode' => $self->{params}->{import_mode}, |
| 389 |
'framework' => $self->{params}->{framework}, |
| 390 |
'overlay_framework' => $self->{params}->{overlay_framework}, |
| 391 |
'item_action' => $self->{params}->{item_action}, |
| 392 |
'xml' => $data |
| 393 |
}; |
| 394 |
|
| 395 |
# If we have a token, try it, else, authenticate for the first time. |
| 396 |
$self->{csrf_token} = $self->authenticate unless $self->{csrf_token}; |
| 358 |
my $resp = $ua->post( |
397 |
my $resp = $ua->post( |
| 359 |
$base_url . IMPORT_SVC_URI, |
398 |
$base_url . IMPORT_SVC_URI, |
| 360 |
{ |
399 |
$post_body, |
| 361 |
'nomatch_action' => $self->{params}->{nomatch_action}, |
400 |
csrf_token => $self->{csrf_token}, |
| 362 |
'overlay_action' => $self->{params}->{overlay_action}, |
|
|
| 363 |
'match' => $self->{params}->{match}, |
| 364 |
'import_mode' => $self->{params}->{import_mode}, |
| 365 |
'framework' => $self->{params}->{framework}, |
| 366 |
'overlay_framework' => $self->{params}->{overlay_framework}, |
| 367 |
'item_action' => $self->{params}->{item_action}, |
| 368 |
'xml' => $data |
| 369 |
} |
| 370 |
); |
401 |
); |
| 371 |
|
402 |
|
| 372 |
my $status = $resp->code; |
403 |
my $status = $resp->code; |
| 373 |
if ( $status == HTTP_UNAUTHORIZED || $status == HTTP_FORBIDDEN ) { |
404 |
if ( $status == HTTP_UNAUTHORIZED || $status == HTTP_FORBIDDEN ) { |
| 374 |
my $user = $self->{user}; |
405 |
# Our token might have expired. Re-authenticate and post again. |
| 375 |
my $password = $self->{password}; |
406 |
$self->{csrf_token} = $self->authenticate; |
| 376 |
$resp = $ua->post( $base_url . AUTH_URI, { userid => $user, password => $password } ); |
|
|
| 377 |
$resp = $ua->post( |
407 |
$resp = $ua->post( |
| 378 |
$base_url . IMPORT_SVC_URI, |
408 |
$base_url . IMPORT_SVC_URI, |
| 379 |
{ |
409 |
$post_body, |
| 380 |
'nomatch_action' => $self->{params}->{nomatch_action}, |
410 |
csrf_token => $self->{csrf_token}, |
| 381 |
'overlay_action' => $self->{params}->{overlay_action}, |
411 |
) |
| 382 |
'match' => $self->{params}->{match}, |
|
|
| 383 |
'import_mode' => $self->{params}->{import_mode}, |
| 384 |
'framework' => $self->{params}->{framework}, |
| 385 |
'overlay_framework' => $self->{params}->{overlay_framework}, |
| 386 |
'item_action' => $self->{params}->{item_action}, |
| 387 |
'xml' => $data |
| 388 |
} |
| 389 |
) if $resp->is_success; |
| 390 |
} |
412 |
} |
| 391 |
unless ($resp->is_success) { |
413 |
unless ($resp->is_success) { |
| 392 |
$self->log("Unsuccessful request", $resp->request->as_string, $resp->as_string); |
414 |
$self->log("Unsuccessful request", $resp->request->as_string, $resp->as_string); |
| 393 |
- |
|
|