--- loncom/interface/lonexttool.pm 2017/12/06 02:15:35 1.8 +++ loncom/interface/lonexttool.pm 2020/04/09 21:11:36 1.20.2.1 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Launch External Tool Provider (LTI) # -# $Id: lonexttool.pm,v 1.8 2017/12/06 02:15:35 raeburn Exp $ +# $Id: lonexttool.pm,v 1.20.2.1 2020/04/09 21:11:36 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -43,14 +43,13 @@ package Apache::lonexttool; use strict; use Apache::Constants qw(:common :http); -use Net::OAuth; use Encode; use Digest::SHA; -use UUID::Tiny ':std'; use HTML::Entities; use Apache::lonlocal; use Apache::lonnet; use Apache::loncommon; +use LONCAPA::ltiutils; sub handler { my $r=shift; @@ -63,12 +62,15 @@ sub handler { # ------------------------------------------------------------ Print the screen if ($target eq 'tex') { $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'})); + } else { + $target = 'web'; } # Is this even in a course? unless ($env{'request.course.id'}) { 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 { $r->print('\textbf{Not in a course}\end{document}'); } @@ -90,53 +92,24 @@ sub handler { my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; my $chome = $env{'course.'.$env{'request.course.id'}.'.home'}; - my $is_tool; + my ($idx,$is_tool,%toolhash,%toolsettings); if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") { - my %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum); + %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum); if ($toolsettings{'id'}) { - my $idx = $toolsettings{'id'}; + $idx = $toolsettings{'id'}; my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer'); if (ref($ltitools{$idx}) eq 'HASH') { - my %toolhash = %{$ltitools{$idx}}; + %toolhash = %{$ltitools{$idx}}; $toolhash{'display'} = { target => $toolsettings{'target'}, width => $toolsettings{'width'}, height => $toolsettings{'height'}, }; - $toolhash{'crslabel'} = $toolsettings{'crslabel'}; - $toolhash{'crstitle'} = $toolsettings{'crstitle'}; - $toolhash{'crsappend'} = $toolsettings{'crsappend'}; - $is_tool = 1; - my $launchok = 1; - if ($target eq 'tex') { - $r->print(&mt('External Tool')); - } else { - my $now = time; - if ($toolhash{'passback'}) { - unless (&set_callback_secret($cdom,$cnum,$marker,'grade',$now, - \%toolsettings,\%toolhash) eq 'ok') { - undef($launchok); - } - } - if ($toolhash{'roster'}) { - &set_callback_secret($cdom,$cnum,$marker,'roster',$now, - \%toolsettings,\%toolhash); - } - my $submittext = &mt('Launch [_1]',$toolhash{'title'}); - if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && - ($toolhash{'url'} ne '') && ($launchok)) { - my %lti = <i_params($r,$cnum,$cdom,$idx,$submittext,\%toolhash); - my $url = $toolhash{'url'}; - if ($toolhash{'crsappend'} ne '') { - $url .= $toolhash{'crsappend'}; - } - $r->print(&launch_html($url,$toolhash{'key'},$toolhash{'secret'}, - $submittext,\%lti)); - } else { - $r->print('
'.&mt('External Tool Unavailable').'
'); - } + foreach my $item (qw(crslabel crstitle crsappend)) { + $toolhash{$item} = $toolsettings{$item}; } + $is_tool = 1; } } } @@ -146,71 +119,67 @@ sub handler { } else { $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}'); } + return OK; } - return OK; -} -sub set_callback_secret { - my ($cdom,$cnum,$marker,$name,$now,$toolsettings,$toolhash) = @_; - return unless ((ref($toolsettings) eq 'HASH') && (ref($toolhash) eq 'HASH')); - my $warning; - my ($needsnew,$oldsecret,$lifetime); - if ($name eq 'grade') { - $lifetime = $toolhash->{'passbackvalid'} - } elsif ($name eq 'roster') { - $lifetime = $toolhash->{'rostervalid'}; - } - if ($toolsettings->{$name} eq '') { - $needsnew = 1; - } elsif (($toolsettings->{$name.'date'} + $lifetime) < $now) { - $oldsecret = $toolsettings->{$name.'secret'}; - $needsnew = 1; - } - if ($needsnew) { - if (&get_tool_lock($cdom,$cnum,$marker,$now) eq 'ok') { - my $secret = UUID::Tiny::create_uuid_as_string(UUID_V4); - $toolhash->{$name.'secret'} = $secret; - my %secrethash = ( - $name.'secret' => $secret, - $name.'secretdate' => $now, - ); - if ($oldsecret ne '') { - $secrethash{'old'.$name.'secret'} = $oldsecret; - } - my $putres = &Apache::lonnet::put('exttool_'.$marker, - \%secrethash,$cdom,$cnum); - my $delresult = &release_tool_lock($cdom,$cnum,$marker); - if ($delresult ne 'ok') { - $warning = $delresult ; + my ($symb,$status,$open,$close,$msg,$donebuttonresult,$donemsg); + if (($target eq 'tex') || ($toolhash{'gradable'})) { + ($symb) = &Apache::lonnet::whichuser(); + } + if ($target eq 'tex') { + my $title = &Apache::lonnet::gettitle($symb); + $r->print(&mt('External Tool: [_1]','\textit{'.$title.'}').'\\\\'); + } + my ($status,$open,$close,$msg)=&Apache::londatecheck::content_date_check(); + if ($status ne 'OPEN') { + if ($target eq 'tex') { + $r->print(&mt('Not open to be viewed').'\end{document}'); + } else { + $r->print($msg); + } + return OK; + } else { + ($status,$msg)=&Apache::lonipcheck::ip_access_check(); + if ($status ne 'OPEN') { + if ($target eq 'tex') { + $r->print(&mt('Not open to be viewed').'\end{document}'); + } else { + $r->print($msg); } - if ($putres eq 'ok') { - return 'ok'; + return OK; + } + } + my $launchok = 1; + if ($target eq 'tex') { + $r->print('\end{document}'); + } else { + my $now = time; + my $submittext = &mt('Launch [_1]',$toolhash{'title'}); + if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && + ($toolhash{'url'} ne '') && ($launchok)) { + my %lti = <i_params($r,$cnum,$cdom,$idx,$submittext,\%toolhash); + my $url = $toolhash{'url'}; + if ($toolhash{'crsappend'} ne '') { + $url .= $toolhash{'crsappend'}; } + $r->print(&launch_html($url,$toolhash{'key'},$toolhash{'secret'}, + $toolhash{'sigmethod'},$submittext,\%lti)); } else { - $warning = ''. - &mt('Could not obtain exclusive lock'). - ''; + $r->print('
'.&mt('External Tool Unavailable').'
'); } - } else { - $toolhash->{$name.'secret'} = $toolsettings->{$name.'secret'}; - return 'ok'; } - return; + return OK; } sub lti_params { my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_; my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale, - $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap, - %display,%custom,@userlangs); + $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs,$incdom); if (ref($toolsref) eq 'HASH') { $version = $toolsref->{'version'}; $toolname = $toolsref->{'title'}; - $passback = $toolsref->{'passback'}; - $gradesecret = $toolsref->{'gradesecret'}; - $roster = $toolsref->{'roster'}; - $rostersecret = $toolsref->{'rostersecret'}; $msgtype = $toolsref->{'messagetype'}; + $incdom = $toolsref->{'incdom'}; if (ref($toolsref->{'fields'}) eq 'HASH') { %fields = %{$toolsref->{'fields'}}; } @@ -236,10 +205,10 @@ sub lti_params { $msgtype = 'basic-lti-launch-request'; } if ($crslabel eq '') { - $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'}, + $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'}; } if ($crstitle eq '') { - $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'},; + $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'}; } my $lonhost = $r->dir_config('lonHostID'); my $loncaparev = $r->dir_config('lonVersion'); @@ -251,8 +220,10 @@ sub lti_params { unless (grep(/^\Q$ltirole\E$/,@possroles)) { $ltirole = 'Learner'; } + my @possdigest; my $digest_user = &Encode::decode_utf8($uname.':'.$udom); $digest_user = &Digest::SHA::sha1_hex($digest_user); + push(@possdigest,$digest_user); if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') { @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/, $env{'course.'.$env{'request.course.id'}.'.languages'})); @@ -275,6 +246,7 @@ sub lti_params { if ($symb) { $digest_symb = &Encode::decode_utf8($symb); $digest_symb = &Digest::SHA::sha1_hex($digest_symb); + push(@possdigest,$digest_symb); my $navmap = Apache::lonnavmaps::navmap->new(); if (ref($navmap)) { my $res = $navmap->getBySymb($symb); @@ -296,7 +268,7 @@ sub lti_params { tool_consumer_instance_guid => $lonhost, tool_consumer_instance_description => $domdesc, tool_consumer_info_product_family_code => 'loncapa', - tool_consumer_instance_name => $int_dom, + tool_consumer_instance_name => $int_dom, tool_consumer_instance_url => $portal_url, tool_consumer_info_version => $loncaparev, user_id => $digest_user, @@ -314,43 +286,6 @@ sub lti_params { unless ($crsprotocol eq 'https') { $crsprotocol = 'http'; } - if (($passback) || ($roster)) { - if ($passback) { - $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/passback'; - $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'}; - if ($gradesecret) { - my $result_sig = - Digest::SHA::sha1_hex($gradesecret.':::'.$digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'}); - $ltiparams{'lis_result_sourcedid'} = - $result_sig.':::'.$digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'}; - } - } - if ($roster) { - if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) { - $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/roster'; - if ($rostersecret) { - my $roster_sig = Digest::SHA::sha1_hex($rostersecret.':::'.$digest_symb.':::'.$env{'request.course.id'}); - $ltiparams{'ext_ims_lis_memberships_id'} = $roster_sig.':::'.$digest_symb.':::'.$env{'request.course.id'}; - } - } - } - 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); - } - } } if ($display{'target'}) { $ltiparams{'launch_presentation_document_target'} = $display{'target'}; @@ -382,7 +317,11 @@ sub lti_params { $ltiparams{'lis_person_contact_email_primary'} = $contact_email; } if ($fields{'user'}) { - $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom; + if ($incdom) { + $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom; + } else { + $ltiparams{'lis_person_sourcedid'} = $uname; + } } if (keys(%custom)) { foreach my $key (keys(%custom)) { @@ -404,8 +343,8 @@ sub lti_params { } sub launch_html { - my ($url,$key,$secret,$submittext,$paramsref) = @_; - my $hashref = &sign_params($url,$key,$secret,$paramsref); + my ($url,$key,$secret,$sigmethod,$submittext,$paramsref) = @_; + my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$sigmethod,$paramsref); my $action = &HTML::Entities::encode($url,'<>&"'); my $form = <<"END"; @@ -439,57 +378,4 @@ ENDJS 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(); -} - -sub get_tool_lock { - my ($cdom,$cnum,$marker,$now) = @_; - # get lock for tool for which gradesecret is being set - my $lockhash = { - $marker."\0".'lock' => $now.':'.$env{'user.name'}. - ':'.$env{'user.domain'}, - }; - my $tries = 0; - my $gotlock = &Apache::lonnet::newput('exttools',$lockhash,$cdom,$cnum); - - while (($gotlock ne 'ok') && $tries <3) { - $tries ++; - sleep(1); - $gotlock = &Apache::lonnet::newput('exttools',$lockhash,$cdom,$cnum); - } - return $gotlock; -} - -sub release_tool_lock { - my ($cdom,$cnum,$marker) = @_; - # remove lock - my @del_lock = ($marker."\0".'lock'); - my $dellockoutcome=&Apache::lonnet::del('exttools',\@del_lock,$cdom,$cnum); - if ($dellockoutcome ne 'ok') { - return ('
' - .&mt('Warning: failed to release lock for exttool: [_1].',''.$marker.'') - .'
' - ); - } else { - return 'ok'; - } -} - 1;