--- loncom/interface/lonexttool.pm 2016/06/06 17:40:48 1.4
+++ loncom/interface/lonexttool.pm 2017/12/07 15:36:34 1.9
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Launch External Tool Provider (LTI)
#
-# $Id: lonexttool.pm,v 1.4 2016/06/06 17:40:48 raeburn Exp $
+# $Id: lonexttool.pm,v 1.9 2017/12/07 15:36:34 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -43,13 +43,13 @@ package Apache::lonexttool;
use strict;
use Apache::Constants qw(:common :http);
-use Net::OAuth;
use Encode;
use Digest::SHA;
use HTML::Entities;
use Apache::lonlocal;
use Apache::lonnet;
use Apache::loncommon;
+use LONCAPA::ltiutils;
sub handler {
my $r=shift;
@@ -94,9 +94,10 @@ sub handler {
if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {
my %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
if ($toolsettings{'id'}) {
- my %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
- if (ref($ltitools{$toolsettings{'id'}}) eq 'HASH') {
- my %toolhash = %{$ltitools{$toolsettings{'id'}}};
+ my $idx = $toolsettings{'id'};
+ my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
+ if (ref($ltitools{$idx}) eq 'HASH') {
+ my %toolhash = %{$ltitools{$idx}};
$toolhash{'display'} = {
target => $toolsettings{'target'},
width => $toolsettings{'width'},
@@ -104,15 +105,37 @@ sub handler {
};
$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'}) {
+ 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'});
- if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && ($toolhash{'url'} ne '')) {
- my %lti = <i_params($r,$cnum,$cdom,$submittext,\%toolhash);
- $r->print(&launch_html($toolhash{'url'},$toolhash{'key'},
- $toolhash{'secret'},$submittext,\%lti));
+ 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').'
');
}
@@ -131,14 +154,17 @@ sub handler {
}
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,
- $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs);
+ $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
+ %display,%custom,@userlangs);
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'};
if (ref($toolsref->{'fields'}) eq 'HASH') {
%fields = %{$toolsref->{'fields'}};
@@ -242,18 +268,40 @@ sub lti_params {
my $crsprotocol = $Apache::lonnet::protocol{$crshome};
unless ($crsprotocol eq 'https') {
$crsprotocol = 'http';
- }
- if ($passback) {
- if ($ltirole eq 'Learner') {
- $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltipassback';
+ }
+ 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'};
- $ltiparams{'lis_result_sourcedid'} = ''; #FIXME
+ if ($gradesecret) {
+ my $uniqid = $digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'};
+ $ltiparams{'lis_result_sourcedid'} = &LONCAPA::ltiutils::get_unique_callback($gradesecret,$uniqid);
+ }
}
- }
- if ($roster) {
- if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
- $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltiroster';
- $ltiparams{'ext_ims_lis_memberships_id'} = ''; #FIXME
+ 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 $uniqid = $digest_symb.':::'.$env{'request.course.id'};
+ $ltiparams{'ext_ims_lis_memberships_id'} = &LONCAPA::ltiutils::get_unique_callback($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);
}
}
}
@@ -287,11 +335,18 @@ sub lti_params {
$ltiparams{'lis_person_contact_email_primary'} = $contact_email;
}
if ($fields{'user'}) {
- $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
+ $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
}
if (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)) {
@@ -303,13 +358,14 @@ sub lti_params {
sub launch_html {
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";
-