--- loncom/lti/ltiauth.pm	2022/06/18 02:10:19	1.37
+++ loncom/lti/ltiauth.pm	2022/07/12 22:55:46	1.40
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Basic LTI Authentication Module
 #
-# $Id: ltiauth.pm,v 1.37 2022/06/18 02:10:19 raeburn Exp $
+# $Id: ltiauth.pm,v 1.40 2022/07/12 22:55:46 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -175,6 +175,14 @@ sub handler {
                                 }
                             }
                         }
+                        my $exiturl;
+                        if (($itemid) && ($lti_in_use{'returnurl'} ne '')) {
+                            if (exists($params->{$lti_in_use{'returnurl'}})) {
+                                $exiturl = $params->{$lti_in_use{'returnurl'}};
+                            } elsif (exists($params->{'custom_'.$lti_in_use{'returnurl'}})) {
+                                $exiturl = $params->{'custom_'.$lti_in_use{'returnurl'}};
+                            }
+                        }
                         if (($itemid) && ($lti_in_use{'requser'})) {
                             my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                             my $ltiauth;
@@ -225,7 +233,7 @@ sub handler {
                                                 foreach my $key (%{$params}) {
                                                     delete($env{'form.'.$key});
                                                 }
-                                                &linkprot_session($r,$uname,$cnum,$cdom,$uhome,$itemid,$ltitype,$tail,$lonhost);
+                                                &linkprot_session($r,$uname,$cnum,$cdom,$uhome,$itemid,$ltitype,$tail,$lonhost,$exiturl);
                                                 return OK;
                                             }
                                         }
@@ -248,6 +256,9 @@ sub handler {
                             if ($ltiuser ne '') {
                                 $info{'linkprotuser'} = $ltiuser;
                             }
+                            if ($exiturl ne '') {
+                                $info{'linkprotexit'} = $exiturl; 
+                            }
                             my $ltoken = &Apache::lonnet::tmpput(\%info,$lonhost,'link');
                             if (($ltoken eq 'con_lost') || ($ltoken eq 'refused') || ($ltoken =~ /^error:/) ||
                                 ($ltoken eq 'unknown_cmd') || ($ltoken eq 'no_such_host') ||
@@ -1061,7 +1072,7 @@ sub lti_session {
 }
 
 sub linkprot_session {
-    my ($r,$uname,$cnum,$cdom,$uhome,$itemid,$ltitype,$dest,$lonhost) = @_;
+    my ($r,$uname,$cnum,$cdom,$uhome,$itemid,$ltitype,$dest,$lonhost,$exiturl) = @_;
     $r->user($uname);
     if ($ltitype eq 'c') {
         &Apache::lonnet::logthis("Course Link Protector ($itemid) authorized student: $uname:$cdom, course: $cdom\_$cnum");
@@ -1074,7 +1085,11 @@ sub linkprot_session {
         &Apache::lonauth::success($r,$uname,$cdom,$uhome,'noredirect');
         $env{'form.origurl'} = $dest;
         $env{'request.linkprot'} = $itemid.$ltitype.':'.$dest;
+        $env{'request.linkprotuser'} = $uname.':'.$cdom;
         $env{'request.deeplink.login'} = $dest;
+        if ($exiturl ne '') {
+            $env{'request.linkprotexit'} = $exiturl;
+        }
         my $redirecturl = '/adm/switchserver';
         if ($otherserver ne '') {
             $redirecturl .= '?otherserver='.$otherserver;
@@ -1089,10 +1104,14 @@ sub linkprot_session {
                   'username'       => $uname,
                   'server'         => $lonhost,
                   'linkprot'       => $itemid.$ltitype.':'.$dest,
+                  'linkprotuser'   => $uname.':'.$cdom,
                   'home'           => $uhome,
                   'origurl'        => $dest,
                   'deeplink.login' => $dest,
                  );
+        if ($exiturl ne '') {
+            $info{'linkprotexit'} = $exiturl; 
+        }
         my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
         $env{'form.token'} = $token;
         $r->internal_redirect('/adm/migrateuser');
@@ -1107,6 +1126,11 @@ sub check_balancer {
     ($is_balancer,$otherserver) =
         &Apache::lonnet::check_loadbalancing($uname,$udom,'login');
     if ($is_balancer) {
+        # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
+        my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
+        if (($found_server) && ($balancer_cookie =~ /^\Q$udom\E_\Q$uname\E_/)) {
+            $otherserver = $found_server;
+        }
         if ($otherserver eq '') {
             my $lowest_load;
             ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($udom);