--- loncom/lti/ltiauth.pm	2022/02/08 15:08:53	1.33
+++ loncom/lti/ltiauth.pm	2022/06/26 04:03:48	1.38
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Basic LTI Authentication Module
 #
-# $Id: ltiauth.pm,v 1.33 2022/02/08 15:08:53 raeburn Exp $
+# $Id: ltiauth.pm,v 1.38 2022/06/26 04:03:48 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -30,7 +30,8 @@ package Apache::ltiauth;
 
 use strict;
 use LONCAPA qw(:DEFAULT :match);
-use Apache::Constants qw(:common :http);
+use Encode;
+use Apache::Constants qw(:common :http :remotehost);
 use Apache::lonlocal;
 use Apache::lonnet;
 use Apache::loncommon;
@@ -63,7 +64,7 @@ sub handler {
     my $params = {};
     foreach my $key (sort(keys(%env))) {
         if ($key =~ /^form\.(.+)$/) {
-            $params->{$1} = $env{$key};
+            $params->{$1} = &Encode::decode('UTF-8',$env{$key});
         }
     }
 #
@@ -116,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;
@@ -142,10 +143,10 @@ sub handler {
 # where url was /adm/launch/tiny/$cdom/$uniqueid
 #
 
-                        my ($itemid,$ltitype,%crslti,%lti_in_use);
-                        $itemid = &get_lti_itemid($requri,$hostname,$params,$cdom,$cnum,'deeplink');
+                        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,'provider');
+                            %crslti = &Apache::lonnet::get_course_lti($cnum,$cdom);
                         }
                         if (($itemid) && (ref($crslti{$itemid}) eq 'HASH')) {
                             $ltitype = 'c';
@@ -157,10 +158,10 @@ sub handler {
                                 return OK;
                             }
                         } else {
-                            $itemid = &get_lti_itemid($requri,$hostname,$params,$cdom,'','deeplink');
+                            $itemid = &get_lti_itemid($requri,$hostname,$params,$cdom,'','linkprot');
                             my %lti;
                             if ($itemid) {
-                                %lti = &Apache::lonnet::get_domain_lti($cdom,'provider');
+                                %lti = &Apache::lonnet::get_domain_lti($cdom,'linkprot');
                             }
                             if (($itemid) && (ref($lti{$itemid}) eq 'HASH')) {
                                 $ltitype = 'd';
@@ -185,7 +186,7 @@ sub handler {
                             }
                             if ($ltiauth) {
                                 my $possuname;
-                                my $mapuser = $crslti{$itemid}{'mapuser'};
+                                my $mapuser = $lti_in_use{'mapuser'};
                                 if ($mapuser eq 'sourcedid') {
                                     if ($params->{'lis_person_sourcedid'} =~ /^$match_username$/) {
                                         $possuname = $params->{'lis_person_sourcedid'};
@@ -228,10 +229,12 @@ sub handler {
                                                 return OK;
                                             }
                                         }
+                                        $ltiuser = $uname.':'.$cdom;
                                     }
                                 }
                                 if ($lti_in_use{'notstudent'} eq 'reject') {
                                     &invalid_request($r,'Information for valid user missing from launch request');
+                                    return OK;
                                 }
                             }
                         }
@@ -239,13 +242,20 @@ 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;
+                            }
+                            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');
@@ -984,7 +994,7 @@ sub lti_session {
                                                       $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,
@@ -1054,9 +1064,9 @@ sub linkprot_session {
     my ($r,$uname,$cnum,$cdom,$uhome,$itemid,$ltitype,$dest,$lonhost) = @_;
     $r->user($uname);
     if ($ltitype eq 'c') {
-        &Apache::lonnet::logthis("Link Protector: $itemid (c) authorized student: $uname:$cdom, course: $cdom\_$cnum");
+        &Apache::lonnet::logthis("Course Link Protector ($itemid) authorized student: $uname:$cdom, course: $cdom\_$cnum");
     } elsif ($ltitype eq 'd') {
-        &Apache::lonnet::logthis("Link Protector: $itemid (d) authorized student: $uname:$cdom, course: $cdom\_$cnum");
+        &Apache::lonnet::logthis("Domain LTI for link protection ($itemid) authorized student: $uname:$cdom, course: $cdom\_$cnum");
     }
     my ($is_balancer,$otherserver,$hosthere) = &check_balancer($r,$uname,$cdom);
     if (($is_balancer) && (!$hosthere)) {
@@ -1064,6 +1074,7 @@ 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;
         my $redirecturl = '/adm/switchserver';
         if ($otherserver ne '') {
@@ -1073,12 +1084,13 @@ 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,
@@ -1127,7 +1139,7 @@ sub invalid_request {
         '<h3>'.&mt('Invalid LTI launch request').'</h3>'.
         '<p class="LC_warning">'.
         &mt('Launch of LON-CAPA is unavailable from the "external tool" link you had followed in another web application.').
-        &mt('Launch failed for the following reason:').
+        ' '.&mt('Launch failed for the following reason:').
         '</p>'.
         '<p class="LC_error">'.$msg.'</p>'.
         &Apache::loncommon::end_page());