Diff for /loncom/interface/lonexttool.pm between versions 1.4 and 1.11

version 1.4, 2016/06/06 17:40:48 version 1.11, 2017/12/18 16:11:18
Line 43  package Apache::lonexttool; Line 43  package Apache::lonexttool;
   
 use strict;  use strict;
 use Apache::Constants qw(:common :http);  use Apache::Constants qw(:common :http);
 use Net::OAuth;  
 use Encode;  use Encode;
 use Digest::SHA;  use Digest::SHA;
 use HTML::Entities;  use HTML::Entities;
 use Apache::lonlocal;  use Apache::lonlocal;
 use Apache::lonnet;  use Apache::lonnet;
 use Apache::loncommon;  use Apache::loncommon;
   use LONCAPA::ltiutils;
   
 sub handler {  sub handler {
     my $r=shift;      my $r=shift;
Line 67  sub handler { Line 67  sub handler {
 # Is this even in a course?  # Is this even in a course?
     unless ($env{'request.course.id'}) {      unless ($env{'request.course.id'}) {
         if ($target ne 'tex') {          if ($target ne 'tex') {
             &Apache::loncommon::simple_error_page($r,'','Not in a course');              &Apache::loncommon::simple_error_page($r,'','Not in a course',
                                                     {'only_body' => 1});
         } else {          } else {
             $r->print('\textbf{Not in a course}\end{document}');              $r->print('\textbf{Not in a course}\end{document}');
         }          }
Line 94  sub handler { Line 95  sub handler {
     if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {      if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {
         my %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);          my %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
         if ($toolsettings{'id'}) {          if ($toolsettings{'id'}) {
             my %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);              my $idx = $toolsettings{'id'};
             if (ref($ltitools{$toolsettings{'id'}}) eq 'HASH') {              my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
                 my %toolhash = %{$ltitools{$toolsettings{'id'}}};               if (ref($ltitools{$idx}) eq 'HASH') {
                   my %toolhash = %{$ltitools{$idx}}; 
                 $toolhash{'display'} = {                  $toolhash{'display'} = {
                                            target => $toolsettings{'target'},                                             target => $toolsettings{'target'},
                                            width  => $toolsettings{'width'},                                             width  => $toolsettings{'width'},
Line 104  sub handler { Line 106  sub handler {
                                        };                                         };
                 $toolhash{'crslabel'} = $toolsettings{'crslabel'};                  $toolhash{'crslabel'} = $toolsettings{'crslabel'};
                 $toolhash{'crstitle'} = $toolsettings{'crstitle'};                  $toolhash{'crstitle'} = $toolsettings{'crstitle'};
                   $toolhash{'crsappend'} = $toolsettings{'crsappend'};
                 $is_tool = 1;                  $is_tool = 1;
                   my $launchok = 1;
                 if ($target eq 'tex') {                  if ($target eq 'tex') {
                     $r->print(&mt('External Tool'));                      $r->print(&mt('External Tool'));
                 } else {                  } else {
                       my $now = time;
                       if ($toolhash{'passback'}) {
                           if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'grade',$now,
                                                                           \%toolsettings,\%toolhash) eq 'ok') {
                               $toolhash{'gradesecret'} = $toolsettings{'gradesecret'};
                           } else {
                               undef($launchok);
                           }
                       }
                       if ($toolhash{'roster'}) {
                           if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'roster',$now,
                                                                   \%toolsettings,\%toolhash) eq 'ok') {
                               $toolhash{'rostersecret'} = $toolsettings{'rostersecret'};
                           }
                       }
                     my $submittext = &mt('Launch [_1]',$toolhash{'title'});                      my $submittext = &mt('Launch [_1]',$toolhash{'title'});
                     if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && ($toolhash{'url'} ne '')) {                      if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && 
                         my %lti = &lti_params($r,$cnum,$cdom,$submittext,\%toolhash);                          ($toolhash{'url'} ne '') && ($launchok)) {
                         $r->print(&launch_html($toolhash{'url'},$toolhash{'key'},                          my %lti = &lti_params($r,$cnum,$cdom,$idx,$submittext,\%toolhash);
                                                $toolhash{'secret'},$submittext,\%lti));                          my $url = $toolhash{'url'};
                           if ($toolhash{'crsappend'} ne '') {
                               $url .= $toolhash{'crsappend'};
                           }
                           $r->print(&launch_html($url,$toolhash{'key'},$toolhash{'secret'},
                                                  $submittext,\%lti));
                     } else {                      } else {
                         $r->print('<div>'.&mt('External Tool Unavailable').'</div>');                          $r->print('<div>'.&mt('External Tool Unavailable').'</div>');
                     }                      }
Line 131  sub handler { Line 155  sub handler {
 }  }
   
 sub lti_params {  sub lti_params {
     my ($r,$cnum,$cdom,$submittext,$toolsref) = @_;      my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_;
     my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,      my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
         $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs);          $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
           %display,%custom,@userlangs);
     if (ref($toolsref) eq 'HASH') {      if (ref($toolsref) eq 'HASH') {
         $version = $toolsref->{'version'};          $version = $toolsref->{'version'};
         $toolname = $toolsref->{'title'};          $toolname = $toolsref->{'title'};
         $passback = $toolsref->{'passback'};          $passback = $toolsref->{'passback'};
           $gradesecret = $toolsref->{'gradesecret'};
         $roster = $toolsref->{'roster'};          $roster = $toolsref->{'roster'};
           $rostersecret = $toolsref->{'rostersecret'};
         $msgtype = $toolsref->{'messagetype'};          $msgtype = $toolsref->{'messagetype'};
         if (ref($toolsref->{'fields'}) eq 'HASH') {          if (ref($toolsref->{'fields'}) eq 'HASH') {
             %fields = %{$toolsref->{'fields'}};              %fields = %{$toolsref->{'fields'}};
Line 242  sub lti_params { Line 269  sub lti_params {
         my $crsprotocol = $Apache::lonnet::protocol{$crshome};          my $crsprotocol = $Apache::lonnet::protocol{$crshome};
         unless ($crsprotocol eq 'https') {          unless ($crsprotocol eq 'https') {
             $crsprotocol = 'http';              $crsprotocol = 'http';
         }           }
         if ($passback) {          if (($passback) || ($roster)) {
             if ($ltirole eq 'Learner') {              if ($passback) {
                 $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltipassback';                  $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/passback';
                 $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};                  $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
                 $ltiparams{'lis_result_sourcedid'} = ''; #FIXME                  if ($gradesecret) {
                       my $uniqid = $digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'};
                       $ltiparams{'lis_result_sourcedid'} = &LONCAPA::ltiutils::get_service_id($gradesecret,$uniqid); 
                   }
             }              }
         }              if ($roster) {
         if ($roster) {                  if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
             if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {                      $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/roster';
                 $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltiroster';                      if ($rostersecret) {
                 $ltiparams{'ext_ims_lis_memberships_id'} = ''; #FIXME                          my $uniqid = $digest_symb.':::'.$env{'request.course.id'};
                           $ltiparams{'ext_ims_lis_memberships_id'} = &LONCAPA::ltiutils::get_service_id($rostersecret,$uniqid);
                       }
                   }
               }
               my %digesthash;
               if ($ltiparams{'lis_result_sourcedid'}) {
                   $digesthash{$ltiparams{'lis_result_sourcedid'}} = "$idx\0".time; 
               }
               if ($ltiparams{'ext_ims_lis_memberships_id'}) {
                   $digesthash{$ltiparams{'ext_ims_lis_memberships_id'}} = "$idx\0".time; 
               }
               if (($digest_symb) && ($gradesecret || $rostersecret)) {
                   $digesthash{$digest_symb} = $symb;
               }
               if (($passback) && ($gradesecret)) {
                   $digesthash{$digest_user} = $uname.':'.$udom;
               }
               if (keys(%digesthash)) {
                   &Apache::lonnet::put('exttools',\%digesthash,$cdom,$cnum);
             }              }
         }          }
     }      }
Line 287  sub lti_params { Line 336  sub lti_params {
         $ltiparams{'lis_person_contact_email_primary'} = $contact_email;          $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
     }      }
     if ($fields{'user'}) {      if ($fields{'user'}) {
         $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;           $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
     }      }
     if (keys(%custom)) {      if (keys(%custom)) {
         foreach my $key (keys(%custom)) {          foreach my $key (keys(%custom)) {
             $ltiparams{'custom_'.$key} = $custom{$key};              my $value = $custom{$key};
               $value =~ s/^\s+|\s+\$//g;
               if ($value =~ /^\QLONCAPA::env{\E([^\}]+)\}$/) {
                   if (exists($env{$1})) {
                       $value = $env{$1};
                   }
               }
               $ltiparams{'custom_'.$key} = $value;
         }          }
     }      }
     foreach my $key (keys(%ltiparams)) {      foreach my $key (keys(%ltiparams)) {
Line 303  sub lti_params { Line 359  sub lti_params {
   
 sub launch_html {  sub launch_html {
     my ($url,$key,$secret,$submittext,$paramsref) = @_;      my ($url,$key,$secret,$submittext,$paramsref) = @_;
     my $hashref = &sign_params($url,$key,$secret,$paramsref);      my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$paramsref);
       my $action = &HTML::Entities::encode($url,'<>&"');
     my $form = <<"END";      my $form = <<"END";
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">  <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <body>  <body>
 <div id="LCltiLaunch">  <div id="LCltiLaunch">
 <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$url" method="post" encType="application/x-www-form-urlencoded">  <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$action" method="post" encType="application/x-www-form-urlencoded">
 END  END
     if (ref($hashref) eq 'HASH') {      if (ref($hashref) eq 'HASH') {
         foreach my $item (keys(%{$hashref})) {          foreach my $item (keys(%{$hashref})) {
Line 336  ENDJS Line 393  ENDJS
     return $form;      return $form;
 }  }
   
 sub sign_params {  
     my ($url,$key,$secret,$paramsref) = @_;  
     my $nonce = Digest::SHA::sha1_hex(sprintf("%06x%06x",rand(0xfffff0),rand(0xfffff0)));  
     my $request = Net::OAuth->request("request token")->new(  
             consumer_key => $key,  
             consumer_secret => $secret,  
             request_url => $url,  
             request_method => 'POST',  
             signature_method => 'HMAC-SHA1',  
             timestamp => time,  
             nonce => $nonce,  
             callback => 'about:blank',  
             extra_params => $paramsref,  
             version      => '1.0',  
             );  
     $request->sign;  
     return $request->to_hash();  
 }  
   
 1;  1;

Removed from v.1.4  
changed lines
  Added in v.1.11


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>