--- loncom/lti/ltiauth.pm	2022/04/06 21:44:49	1.36
+++ loncom/lti/ltiauth.pm	2023/06/02 01:20:28	1.42
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Basic LTI Authentication Module
 #
-# $Id: ltiauth.pm,v 1.36 2022/04/06 21:44:49 raeburn Exp $
+# $Id: ltiauth.pm,v 1.42 2023/06/02 01:20:28 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -31,7 +31,7 @@ package Apache::ltiauth;
 use strict;
 use LONCAPA qw(:DEFAULT :match);
 use Encode;
-use Apache::Constants qw(:common :http);
+use Apache::Constants qw(:common :http :remotehost);
 use Apache::lonlocal;
 use Apache::lonnet;
 use Apache::loncommon;
@@ -117,7 +117,7 @@ sub handler {
 #
     if ($requri =~ m{^/adm/launch(|/.*)$}) {
         my $tail = $1;
-        if ($tail =~ m{^/tiny/($match_domain)/(\w+)$}) {
+        if ($tail =~ m{^/tiny/$match_domain/\w+$}) {
             my ($urlcdom,$urlcnum) = &course_from_tinyurl($tail);
             if (($urlcdom ne '') && ($urlcnum ne '')) {
                 $cdom = $urlcdom;
@@ -143,10 +143,10 @@ sub handler {
 # where url was /adm/launch/tiny/$cdom/$uniqueid
 #
 
-                        my ($itemid,$ltitype,%crslti,%lti_in_use);
+                        my ($itemid,$ltitype,%crslti,%lti_in_use,$ltiuser);
                         $itemid = &get_lti_itemid($requri,$hostname,$params,$cdom,$cnum,'linkprot');
                         if ($itemid) {
-                            %crslti = &Apache::lonnet::get_course_lti($cnum,$cdom);
+                            %crslti = &Apache::lonnet::get_course_lti($cnum,$cdom,'provider');
                         }
                         if (($itemid) && (ref($crslti{$itemid}) eq 'HASH')) {
                             $ltitype = 'c';
@@ -175,6 +175,21 @@ 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'}};
+                            }
+                        }
+                        my ($pbid,$pburl);
+                        if ($params->{'lis_result_sourcedid'}) {
+                            $pbid = $params->{'lis_result_sourcedid'};
+                        }
+                        if ($params->{'lis_outcome_service_url'}) {
+                            $pburl = $params->{'lis_outcome_service_url'};
+                        }
                         if (($itemid) && ($lti_in_use{'requser'})) {
                             my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                             my $ltiauth;
@@ -225,10 +240,12 @@ 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,$pbid,$pburl);
                                                 return OK;
                                             }
                                         }
+                                        $ltiuser = $uname.':'.$cdom;
                                     }
                                 }
                                 if ($lti_in_use{'notstudent'} eq 'reject') {
@@ -241,13 +258,29 @@ sub handler {
                             foreach my $key (%{$params}) {
                                 delete($env{'form.'.$key});
                             }
-                            my $ltoken = &Apache::lonnet::tmpput({'linkprot' => $itemid.$ltitype.':'.$tail},
-                                                                 $lonhost,'link');
-                            if ($ltoken) {
+                            my %info = (
+                                          'linkprot' => $itemid.$ltitype.':'.$tail,
+                                       );
+                            if ($ltiuser ne '') {
+                                $info{'linkprotuser'} = $ltiuser;
+                            }
+                            if ($exiturl ne '') {
+                                $info{'linkprotexit'} = $exiturl; 
+                            }
+                            if ($pbid ne '') {
+                                $info{'linkprotpbid'} = $pbid;
+                            }
+                            if ($pburl ne '') {
+                                $info{'linkprotpburl'} = $pburl;
+                            }
+                            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') ||
+                                ($ltoken eq '')) {
+                                &invalid_request($r,'Failed to store information from launch request');
+                            } else {
                                 $r->internal_redirect($tail.'?ltoken='.$ltoken);
                                 $r->set_handlers('PerlHandler'=> undef);
-                            } else {
-                                &invalid_request($r,'Failed to store information from launch request');
                             }
                         } else {
                             &invalid_request($r,'Launch request could not be validated');
@@ -423,7 +456,7 @@ sub handler {
 #
 
     my %lti;
-    my $itemid = &get_lti_itemid($requri,$hostname,$params,$cdom);
+    my $itemid = &get_lti_itemid($requri,$hostname,$params,$cdom,'','provider');
     if ($itemid) {
         %lti = &Apache::lonnet::get_domain_lti($cdom,'provider');
     }
@@ -904,9 +937,8 @@ sub lti_session {
         # login but immediately go to switch server.
         &Apache::lonauth::success($r,$uname,$udom,$uhome,'noredirect');
         if (($ltihash->{'callback'}) && ($params->{$ltihash->{'callback'}})) {
-            &LONCAPA::ltiutils::setup_logout_callback($uname,$udom,$otherserver,
-                                                      $ltihash->{'key'},
-                                                      $ltihash->{'secret'},
+            &LONCAPA::ltiutils::setup_logout_callback($cdom,$cnum,'',$itemid,$ltihash->{'cipher'},
+                                                      $uname,$udom,$otherserver,
                                                       $params->{$ltihash->{'callback'}},
                                                       $r->dir_config('ltiIDsDir'),
                                                       $protocol,$r->hostname);
@@ -979,14 +1011,13 @@ sub lti_session {
             delete($env{'form.'.$key});
         }
         if (($ltihash->{'callback'}) && ($params->{$ltihash->{'callback'}})) {
-            &LONCAPA::ltiutils::setup_logout_callback($uname,$udom,$lonhost,
-                                                      $ltihash->{'key'},
-                                                      $ltihash->{'secret'},
+            &LONCAPA::ltiutils::setup_logout_callback($cdom,$cnum,'',$itemid,$ltihash->{'cipher'},
+                                                      $uname,$udom,$lonhost,
                                                       $params->{$ltihash->{'callback'}},
                                                       $r->dir_config('ltiIDsDir'),
                                                       $protocol,$r->hostname);
         }
-        my $ip = $r->get_remote_host();
+        my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
         my %info=('ip'        => $ip,
                   'domain'    => $udom,
                   'username'  => $uname,
@@ -1053,7 +1084,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,$pbid,$pburl) = @_;
     $r->user($uname);
     if ($ltitype eq 'c') {
         &Apache::lonnet::logthis("Course Link Protector ($itemid) authorized student: $uname:$cdom, course: $cdom\_$cnum");
@@ -1066,7 +1097,17 @@ 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;
+        }
+        if ($pbid ne '') {
+            $env{'request.linkprotpbid'} = $pbid;
+        }
+        if ($pburl ne '') {
+            $env{'request.linkprotpburl'} = $pburl;
+        }
         my $redirecturl = '/adm/switchserver';
         if ($otherserver ne '') {
             $redirecturl .= '?otherserver='.$otherserver;
@@ -1075,16 +1116,26 @@ sub linkprot_session {
         $r->set_handlers('PerlHandler'=> undef);
     } else {
         # need to login them in, so generate the data migrate expects to do login
-        my $ip = $r->get_remote_host();
+        my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
         my %info=('ip'             => $ip,
                   'domain'         => $cdom,
                   'username'       => $uname,
                   'server'         => $lonhost,
                   'linkprot'       => $itemid.$ltitype.':'.$dest,
+                  'linkprotuser'   => $uname.':'.$cdom,
                   'home'           => $uhome,
                   'origurl'        => $dest,
                   'deeplink.login' => $dest,
                  );
+        if ($pbid ne '') {
+            $info{'linkprotpbid'} = $pbid;
+        }
+        if ($pburl ne '') {
+            $info{'linkprotpburl'} = $pburl;
+        }
+        if ($exiturl ne '') {
+            $info{'linkprotexit'} = $exiturl; 
+        }
         my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
         $env{'form.token'} = $token;
         $r->internal_redirect('/adm/migrateuser');
@@ -1099,6 +1150,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);