From 083de6eb81c591d293e5c3db7e7ea9bd45aac647 Mon Sep 17 00:00:00 2001 From: Nicholas van Oudtshoorn Date: Wed, 17 Apr 2024 16:11:59 +0800 Subject: [PATCH] Allow OAuth login to redirect to referring url on successful login. --- Koha/REST/V1/OAuth/Client.pm | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/Koha/REST/V1/OAuth/Client.pm b/Koha/REST/V1/OAuth/Client.pm index 2840b18715..a629c4b750 100644 --- a/Koha/REST/V1/OAuth/Client.pm +++ b/Koha/REST/V1/OAuth/Client.pm @@ -51,17 +50,27 @@ sub login { my $uri; my $redirect_url; + my $referrer_url; + $referrer_url = $c->session('auth-refer-uri'); if ( $interface eq 'opac' ) { $redirect_url = C4::Context->preference('OPACBaseURL') . '/api/v1/public/oauth/login/'; - if ( C4::Context->preference('OpacPublic') ) { - $uri = '/cgi-bin/koha/opac-user.pl'; + if ( $referrer_url ) { + $uri = $referrer_url; } else { - $uri = '/cgi-bin/koha/opac-main.pl'; + if ( C4::Context->preference('OpacPublic') ) { + $uri = '/cgi-bin/koha/opac-user.pl'; + } else { + $uri = '/cgi-bin/koha/opac-main.pl'; + } } } else { - $redirect_url = C4::Context->preference('staffClientBaseURL') . '/api/v1/oauth/login/'; - $uri = '/cgi-bin/koha/mainpage.pl'; + $redirect_url = C4::Context->preference('staffClientBaseURL') . '/api/v1/oauth/login/'; + if ( $referrer_url ) { + $uri = $referrer_url; + } else { + $uri = '/cgi-bin/koha/mainpage.pl'; + } } unless ( $provider_config && $provider_config->{authorize_url} ) { @@ -98,6 +107,21 @@ sub login { else { # initial request, generate CSRF token $state = Koha::Token->new->generate_csrf( { session_id => $c->req->cookie('CGISESSID')->value } ); + + # Check to see if the current referrer is part of the staff of opac interfaces, and store it if it is + my $interface = $c->param('interface'); + my $referrer_uri = $c->req->headers->referrer; + my $logoutregex = qr/logout\.x=1/mp; + my $int_host; + if ($interface eq 'opac') { + $int_host = C4::Context->preference('OPACBaseURL'); + } else { + $int_host = C4::Context->preference('staffClientBaseURL'); + } + if ($referrer_uri && ($referrer_uri =~ /^\Q$int_host\E/ )) { + $referrer_uri =~ s/$logoutregex//rg; + $c->session({'auth-refer-uri' => $referrer_uri }); + } } return $c->oauth2->get_token_p( $provider => { ( !$is_callback ? ( state => $state ) : () ), redirect_uri => $redirect_url . $provider . "/" . $interface } )->then( -- 2.34.1