Annotation of loncom/lti/ltiauth.pm, revision 1.4
1.1 raeburn 1: # The LearningOnline Network
2: # Basic LTI Authentication Module
3: #
1.4 ! raeburn 4: # $Id: ltiauth.pm,v 1.3 2017/12/09 16:15:47 raeburn Exp $
1.1 raeburn 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::ltiauth;
30:
31: use strict;
32: use LONCAPA qw(:DEFAULT :match);
33: use Apache::Constants qw(:common :http);
34: use Net::OAuth;
35: use Apache::lonlocal;
36: use Apache::lonnet;
37: use Apache::loncommon;
38: use Apache::lonacc;
1.2 raeburn 39: use LONCAPA::ltiutils;
1.1 raeburn 40:
41: sub handler {
42: my $r = shift;
43: my $requri = $r->uri;
44: #
45: # Retrieve data POSTed by LTI Consumer on launch
46: #
47: &Apache::lonacc::get_posted_cgi($r);
48: my $params = {};
49: foreach my $key (sort(keys(%env))) {
50: if ($key =~ /^form\.(.+)$/) {
51: $params->{$1} = $env{$key};
52: }
53: }
54:
55: unless (keys(%{$params})) {
56: &invalid_request($r,1);
57: return OK;
58: }
59:
60: unless ($params->{'oauth_consumer_key'} &&
61: $params->{'oauth_nonce'} &&
62: $params->{'oauth_timestamp'} &&
63: $params->{'oauth_version'} &&
64: $params->{'oauth_signature'} &&
65: $params->{'oauth_signature_method'}) {
66: &invalid_request($r,2);
67: return OK;
68: }
69:
70: #
71: # Retrieve "internet domains" for all this institution's LON-CAPA
72: # nodes.
73: #
74: my ($udom,$uname,$uhome,$cdom,$cnum,$symb,$mapurl,@intdoms);
75: my $lonhost = $r->dir_config('lonHostID');
76: my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
77: if (ref($internet_names) eq 'ARRAY') {
78: @intdoms = @{$internet_names};
79: }
80:
81: #
82: # For user who launched LTI in Consumer, determine user's domain in
83: # LON-CAPA.
84: #
85: # Order is:
86: #
87: # (a) from custom_userdomain item in POSTed data
88: # (b) from lis_person_sourcedid in POSTed data
89: # (c) from default "log-in" domain for node
90: # (can support multidomain servers, where specific domain is
91: # first part of hostname).
92: #
93: # Note: "internet domain" for user's domain must be one of the
94: # "internet domain(s)" for the institution's LON-CAPA servers.
95: #
96: if (exists($params->{'custom_userdomain'})) {
97: if ($params->{'custom_userdomain'} =~ /^$match_domain$/) {
98: my $uprimary_id = &Apache::lonnet::domain($params->{'custom_userdomain'},'primary');
99: if ($uprimary_id ne '') {
100: my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
101: if (($uintdom ne '') && (grep(/^\Q$uintdom\E$/,@intdoms))) {
102: $udom = $params->{'custom_userdomain'};
103: }
104: }
105: }
106: }
107: my $defdom = &Apache::lonnet::default_login_domain();
108: my ($domain,$possuname,$possudom,$possmapuser);
109: if ($env{'form.lis_person_sourcedid'} =~ /^($match_username)\:($match_domain)$/) {
110: ($possuname,$possudom) = ($1,$2);
111: if ($udom eq '') {
112: my $uintdom = &Apache::lonnet::domain($possudom,'primary');
113: if (($uintdom ne '') && (grep(/^\Q$uintdom\E$/,@intdoms))) {
114: $udom = $possudom;
115: $possmapuser = 'lis_person_sourcedid';
116: } else {
117: $udom = $defdom;
118: }
119: } elsif ($udom eq $possudom) {
120: $possmapuser = 'lis_person_sourcedid';
121: }
122: }
123: unless ($possuname) {
124: if ($env{'form.lis_person_sourcedid'} =~ /^$match_username$/) {
125: $possuname = $env{'form.lis_person_sourcedid'};
126: $possmapuser = 'lis_person_sourcedid';
127: } elsif ($env{'form.lis_person_contact_email_primary'} =~ /^$match_username$/) {
128: $possuname = $env{'form.lis_person_contact_email_primary'};
129: $possmapuser = 'lis_person_contact_email_primary';
130: }
131: unless ($udom) {
132: $udom = $defdom;
133: }
134: }
135:
136: #
137: # Determine course's domain in LON-CAPA
138: #
139: # Order is:
140: #
141: # (a) from custom_coursedomain item in POSTed data
142: # (b) from tail of requested URL (after /adm/lti) if it has format of a symb
143: # (c) from tail of requested URL (after /adm/lti) if it has format of a map
144: # (d) from tail of requested URL (after /adm/lti) if it has format /domain/courseID
145: # (e) from tail of requested URL (after /adm/lti) if it has format /tiny/domain/...
146: # i.e., a shortened URL (see bug #6400) -- not implemented yet.
147: # (f) same as user's domain
148: #
149: # Request invalid if custom_coursedomain is defined and is inconsistent with
150: # domain contained in requested URL.
151: #
152: # Note: "internet domain" for course's domain must be one of the
153: # internet domains for the institution's LON-CAPA servers.
154: #
155:
156: if (exists($params->{'custom_coursedomain'})) {
157: if ($params->{'custom_coursedomain'} =~ /^$match_domain$/) {
158: my $cprimary_id = &Apache::lonnet::domain($params->{'custom_coursedomain'},'primary');
159: if ($cprimary_id ne '') {
160: my $cintdom = &Apache::lonnet::internet_dom($cprimary_id);
161: if (($cintdom ne '') && (grep(/^\Q$cintdom\E$/,@intdoms))) {
162: $cdom = $params->{'custom_coursedomain'};
163: }
164: }
165: }
166: }
167:
168: my ($tail) = ($requri =~ m{^/adm/lti(|/.*)$});
169: my $urlcnum;
170: if ($tail ne '') {
171: my $urlcdom;
172: if ($tail =~ m{^/uploaded/($match_domain)/($match_courseid)/(?:default|supplemental)(?:|_\d+)\.(?:sequence|page)(|___\d+___.+)$}) {
173: ($urlcdom,$urlcnum,my $rest) = ($1,$2,$3);
174: if (($cdom ne '') && ($cdom ne $urlcdom)) {
175: &invalid_request($r,3);
176: return OK;
177: }
178: if ($rest eq '') {
179: $mapurl = $tail;
180: } else {
181: $symb = $tail;
182: $symb =~ s{^/+}{};
183: }
184: #FIXME Need to handle encrypted URLs
185: } elsif ($tail =~ m{^/($match_domain)/($match_courseid)$}) {
186: ($urlcdom,$urlcnum) = ($1,$2);
187: if (($cdom ne '') && ($cdom ne $urlcdom)) {
188: &invalid_request($r,4);
189: return OK;
190: }
191: }
192: if (($cdom eq '') && ($urlcdom ne '')) {
193: my $cprimary_id = &Apache::lonnet::domain($urlcdom,'primary');
194: if ($cprimary_id ne '') {
195: my $cintdom = &Apache::lonnet::internet_dom($cprimary_id);
196: if (($cintdom ne '') && (grep(/^\Q$cintdom\E$/,@intdoms))) {
197: $cdom = $urlcdom;
198: }
199: } else {
200: $urlcnum = '';
201: }
202: }
203: }
204: if ($cdom eq '') {
205: if ($udom ne '') {
206: $cdom = $udom;
207: } else {
208: $cdom = $defdom;
209: }
210: }
211:
212: #
213: # Retrieve information for LTI Consumers in course domain
214: # and populate hash -- %lti_by_key -- for which keys
215: # are those defined in domain configuration for LTI.
216: #
217:
218: my %lti = &Apache::lonnet::get_domain_lti($cdom,'provider');
219: unless (keys(%lti) > 0) {
220: &invalid_request($r,5);
221: return OK;
222: }
223: my %lti_by_key;
224: if (keys(%lti)) {
225: foreach my $id (keys(%lti)) {
226: if (ref($lti{$id}) eq 'HASH') {
227: my $key = $lti{$id}{'key'};
228: push(@{$lti_by_key{$key}},$id);
229: }
230: }
231: }
232:
233: #
234: # Verify the signed request using the secret for those
235: # Consumers for which the key in the POSTed data matches
236: # keys in the domain configuration for LTI.
237: #
238: my $hostname = $r->hostname;
239: my $protocol = 'http';
240: if ($ENV{'SERVER_PORT'} == 443) {
241: $protocol = 'https';
242: }
243:
1.3 raeburn 244: my ($itemid,$consumer_key,$secret,@ltiroles);
245: $consumer_key = $params->{'oauth_consumer_key'};
246: if (ref($lti_by_key{$consumer_key}) eq 'ARRAY') {
247: foreach my $id (@{$lti_by_key{$consumer_key}}) {
1.1 raeburn 248: if (ref($lti{$id}) eq 'HASH') {
1.2 raeburn 249: $secret = $lti{$id}{'secret'};
1.1 raeburn 250: my $request = Net::OAuth->request('request token')->from_hash($params,
251: request_url => $protocol.'://'.$hostname.$requri,
252: request_method => $env{'request.method'},
253: consumer_secret => $secret,);
254: if ($request->verify()) {
255: $itemid = $id;
256: last;
257: }
258: }
259: }
260: }
261:
262: #
263: # Request is invalid if the signed request could not be verified
264: # for the Consumer key and Consumer secret from the domain
265: # configuration in LON-CAPA for that LTI Consumer.
266: #
267: unless (($itemid) && (ref($lti{$itemid}) eq 'HASH')) {
268: &invalid_request($r,6);
269: return OK;
270: }
271:
272: #
273: # Determine if nonce in POSTed data has expired.
274: # If unexpired, confirm it has not already been used.
275: #
1.2 raeburn 276: unless (&LONCAPA::ltiutils::check_nonce($params->{'oauth_nonce'},$params->{'oauth_timestamp'},
277: $lti{$itemid}{'lifetime'},$cdom,$r->dir_config('lonLTIDir'))) {
1.1 raeburn 278: &invalid_request($r,7);
279: return OK;
280: }
281:
282: #
283: # Determinine if source of username matches requirement from the
284: # domain configuration for the specific LTI Consumer.
285: #
286:
287: if ($lti{$itemid}{'mapuser'} eq $possmapuser) {
288: $uname = $possuname;
289: } elsif ($lti{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
290: if ($params->{'lis_person_sourcedid'} =~ /^$match_username$/) {
291: $uname = $possuname;
292: }
293: } elsif ($lti{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
294: if ($params->{'lis_person_contact_email_primary'} =~ /^$match_username$/) {
295: $uname = $params->{'lis_person_contact_email_primary'};
296: }
297: } elsif (exists($params->{$lti{$itemid}{'mapuser'}})) {
298: if ($params->{$lti{$itemid}{'mapuser'}} =~ /^$match_username$/) {
299: $uname = $params->{$lti{$itemid}{'mapuser'}};
300: }
301: }
302:
303: #
304: # Determine the courseID of the LON-CAPA course to which the
305: # launch of LON-CAPA should provide access.
306: #
307: # Order is:
308: #
309: # (a) from course mapping (if the link between Consumer "course" and
310: # Provider "course" has been established previously).
311: # (b) from tail of requested URL (after /adm/lti) if it has format of a symb
312: # (c) from tail of requested URL (after /adm/lti) if it has format of a map
313: # (d) from tail of requested URL (after /adm/lti) if it has format /domain/courseID
314: # (e) from tail of requested URL (after /adm/lti) if it has format /tiny/domain/...
315: # i.e., a shortened URL (see bug #6400) -- not implemented yet.
316: #
317: # If Consumer course included in POSTed data points as a target course which
318: # has a format which matches a LON-CAPA courseID, but the course does not
319: # exist, the request is invalid.
320: #
321:
322: my ($sourcecrs,%consumers);
323: if ($lti{$itemid}{'mapcrs'} eq 'course_offering_sourcedid') {
324: $sourcecrs = $params->{'course_offering_sourcedid'};
325: } elsif ($lti{$itemid}{'mapcrs'} eq 'context_id') {
326: $sourcecrs = $params->{'context_id'};
327: } elsif ($lti{$itemid}{'mapcrs'} ne '') {
328: $sourcecrs = $params->{$lti{$itemid}{'mapcrs'}};
329: }
330:
331: my $posscnum;
332: if ($sourcecrs ne '') {
333: %consumers = &Apache::lonnet::get_dom('lticonsumers',[$sourcecrs],$cdom);
334: if (exists($consumers{$sourcecrs})) {
335: if ($consumers{$sourcecrs} =~ /^$match_courseid$/) {
336: my $crshome = &Apache::lonnet::homeserver($consumers{$sourcecrs},$cdom);
337: if ($crshome =~ /(con_lost|no_host|no_such_host)/) {
338: &invalid_request($r,8);
339: return OK;
340: } else {
341: $posscnum = $consumers{$sourcecrs};
342: }
343: }
344: }
345: }
346:
347: if ($urlcnum ne '') {
348: if ($posscnum ne '') {
349: if ($posscnum ne $urlcnum) {
350: &invalid_request($r,9);
351: return OK;
352: } else {
353: $cnum = $posscnum;
354: }
355: } else {
356: my $crshome = &Apache::lonnet::homeserver($urlcnum,$cdom);
357: if ($crshome =~ /(con_lost|no_host|no_such_host)/) {
358: &invalid_request($r,10);
359: return OK;
360: } else {
361: $cnum = $urlcnum;
362: }
363: }
364: } elsif ($posscnum ne '') {
365: $cnum = $posscnum;
366: }
367:
368: #
369: # Get LON-CAPA role to use from role-mapping of Consumer roles
370: # defined in domain configuration for the appropriate LTI
371: # Consumer.
372: #
373: # If multiple LON-CAPA roles are indicated, choose based
374: # on the order: cc, in, ta, ep, st
375: #
376:
377: my $reqrole;
378:
379: my @roleorder = ('cc','in','ta','ep','st');
380: if ($params->{'roles'} =~ /,/) {
381: @ltiroles = split(/\s*,\s*/,$params->{'role'});
382: } else {
383: my $singlerole = $params->{'roles'};
384: $singlerole =~ s/^\s|\s+$//g;
385: @ltiroles = ($singlerole);
386: }
387: if (@ltiroles) {
388: if (ref($lti{$itemid}{maproles}) eq 'HASH') {
389: my %possroles;
390: map { $possroles{$lti{$itemid}{maproles}{$_}} = 1; } @ltiroles;
391: my @possibles = keys(%possroles);
392: if (@possibles == 1) {
393: if (grep(/^\Q$possibles[0]\E$/,@roleorder)) {
394: $reqrole = $possibles[0];
395:
396: }
397: } elsif (@possibles > 1) {
398: foreach my $item (@roleorder) {
399: if ($possroles{$item}) {
400: $reqrole = $item;
401: last;
402: }
403: }
404: }
405: }
406: }
407:
408: #
409: # If no LON-CAPA username -- is user allowed to create one?
410: #
411:
412: my $selfcreate;
413: if (($uname ne '') && ($udom ne '')) {
414: $uhome = &Apache::lonnet::homeserver($uname,$udom);
415: if ($uhome =~ /(con_lost|no_host|no_such_host)/) {
416: &Apache::lonnet::logthis(" LTI authorized unknown user $uname:$udom ");
417: if (ref($lti{$itemid}{'makeuser'}) eq 'ARRAY') {
418: if (@{$lti{$itemid}{'makeuser'}} > 0) {
419: foreach my $ltirole (@ltiroles) {
420: if (grep(/^\Q$ltirole\E$/,@{$lti{$itemid}{'makeuser'}})) {
421: $selfcreate = 1;
422: }
423: }
424: }
425: }
426: if ($selfcreate) {
427: #FIXME Do user creation here.
428: return OK
429: } else {
430: &invalid_request($r,11);
431: return OK;
432: }
433: }
434: } else {
435: &invalid_request($r,12);
436: return OK;
437: }
438:
439: #
440: # If no LON-CAPA course available, check if domain's configuration
441: # for the specific LTI Consumer allows a new course to be created
442: # (requires role in Consumer to be: Instructor).
443: #
444:
445: if ($cnum eq '') {
446: if ((@ltiroles) && (grep(/^Instructor$/,@ltiroles)) &&
447: ($lti{$itemid}{'mapcrs'})) {
448: #FIXME Create a new LON-CAPA course here.
449: return OK;
450: } else {
451: &invalid_request($r,13);
452: return OK;
453: }
454: }
455:
456: #
457: # If LON-CAPA course is a Community, and LON-CAPA role
458: # indicated is cc, change role indicated to co.
459: #
460:
461: if ($reqrole eq 'cc') {
462: if (($cdom ne '') && ($cnum ne '')) {
463: my %crsenv = &Apache::lonnet::coursedescription($cnum.'_'.$cdom,{ 'one_time' => 1,});
464: if ($crsenv{'type'} eq 'Community') {
465: $reqrole = 'co';
466: }
467: }
468: }
469:
470: #
471: # Determine if user has required LON-CAPA role
472: # in the mapped LON-CAPA course.
473: #
474:
475: my $role;
476: my %crsroles = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',undef,[$reqrole],[$cdom]);
477: if (exists($crsroles{$cnum.':'.$cdom.':'.$reqrole})) {
478: $role = $reqrole.'./'.$cdom.'/'.$cnum;
479: #FIXME Need to accommodate sections
480: } elsif (ref($lti{$itemid}{'selfenroll'}) eq 'ARRAY') {
481: if (grep(/^\Q$reqrole\E$/,@{$lti{$itemid}{'selfenroll'}})) {
482: #FIXME Do self-enrollment here
483: return OK;
484: } else {
485: &invalid_request($r,14);
1.4 ! raeburn 486: return OK;
1.1 raeburn 487: }
488: }
489:
490: #
491: # Store consumer-to-LON-CAPA course mapping
492: #
493: if (($sourcecrs ne '') && ($consumers{$sourcecrs} eq '') && ($cnum ne '')) {
494: &Apache::lonnet::put_dom('lticonsumers',{ $sourcecrs => $cnum },$cdom);
495: }
496:
497: #
498: # Check if user should be hosted here or switched to another server.
499: #
500:
1.3 raeburn 501: &Apache::lonnet::logthis(" LTI authorized user: $uname:$udom role: $role course: $cdom\_$cnum");
1.1 raeburn 502: $r->user($uname);
503: my ($is_balancer,$otherserver,$hosthere);
504: ($is_balancer,$otherserver) =
505: &Apache::lonnet::check_loadbalancing($uname,$udom,'login');
506: if ($is_balancer) {
507: if ($otherserver eq '') {
508: my $lowest_load;
509: ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($udom);
510: if ($lowest_load > 100) {
511: $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$udom);
512: }
513: }
514: if ($otherserver ne '') {
515: my @hosts = &Apache::lonnet::current_machine_ids();
516: if (grep(/^\Q$otherserver\E$/,@hosts)) {
517: $hosthere = $otherserver;
518: }
519: }
520: }
521: if (($is_balancer) && (!$hosthere)) {
522: # login but immediately go to switch server.
523: &Apache::lonauth::success($r,$uname,$udom,$uhome,'noredirect');
524: if ($symb) {
525: $env{'form.symb'} = $symb;
526: }
527: if ($role) {
528: $env{'form.role'} = $role;
529: }
530: if ($lti{$itemid}{'passback'}) {
531: if ($params->{'lis_result_sourcedid'}) {
532: $env{'request.lti.passbackid'} = $params->{'lis_result_sourcedid'};
533: }
534: if ($params->{'lis_outcome_service_url'}) {
535: $env{'request.lti.passbackurl'} = $params->{'lis_outcome_service_url'};
536: }
537: }
538: if (($lti{$itemid}{'roster'}) && (grep(/^Instructor$/,@ltiroles))) {
539: if ($params->{'ext_ims_lis_memberships_id'}) {
540: $env{'request.lti.rosterid'} = $params->{'ext_ims_lis_memberships_id'};
541: }
542: if ($params->{'ext_ims_lis_memberships_url'}) {
543: $env{'request.lti.rosterurl'} = $params->{'ext_ims_lis_memberships_url'};
544: }
545: }
546: $env{'request.lti.login'} = 1;
547: foreach my $key (%{$params}) {
548: delete($env{'form.'.$key});
549: }
550: my $redirecturl = '/adm/switchserver';
551: if ($otherserver ne '') {
552: $redirecturl .= '?otherserver='.$otherserver;
553: }
554: $r->internal_redirect($redirecturl);
555: $r->set_handlers('PerlHandler'=> undef);
556: } else {
557: # need to login them in, so generate the need data that
558: # migrate expects to do login
559: foreach my $key (%{$params}) {
560: delete($env{'form.'.$key});
561: }
562: my $ip = $r->get_remote_host();
563: my %info=('ip' => $ip,
564: 'domain' => $udom,
565: 'username' => $uname,
566: 'server' => $lonhost,
567: 'lti.login' => 1,
568: );
569: if ($role) {
570: $info{'role'} = $role;
571: }
572: if ($symb) {
573: $info{'symb'} = $symb;
574: }
575: if ($lti{$itemid}{'passback'}) {
576: if ($params->{'lis_result_sourcedid'}) {
577: $info{'lti.passbackid'} = $params->{'lis_result_sourcedid'}
578: }
579: if ($params->{'lis_outcome_service_url'}) {
580: $info{'lti.passbackurl'} = $params->{'lis_outcome_service_url'}
581: }
582: }
583: if (($lti{$itemid}{'roster'}) && (grep(/^Instructor$/,@ltiroles))) {
584: if ($params->{'ext_ims_lis_memberships_id'}) {
585: $info{'lti.rosterid'} = $params->{'ext_ims_lis_memberships_id'};
586: }
587: if ($params->{'ext_ims_lis_memberships_url'}) {
588: $info{'lti.rosterurl'} = $params->{'ext_ims_lis_memberships_url'};
589: }
590: }
591: unless ($info{'symb'}) {
592: if ($mapurl) {
593: $info{'origurl'} = $mapurl;
594: if ($mapurl =~ m{/default_\d+\.sequence$}) {
595: $info{'origurl'} .= (($mapurl =~/\?/)?'&':'?').'navmap=1';
596: }
597: } else {
598: unless ($tail eq '/adm/roles') {
599: $info{'origurl'} = '/adm/navmaps';
600: }
601: }
602: }
603: if (($is_balancer) && ($hosthere)) {
604: $info{'noloadbalance'} = $hosthere;
605: }
606: my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
607: $env{'form.token'} = $token;
608: $r->internal_redirect('/adm/migrateuser');
609: $r->set_handlers('PerlHandler'=> undef);
610: }
611: return OK;
612: }
613:
614: sub invalid_request {
615: my ($r,$num) = @_;
616: &Apache::loncommon::content_type($r,'text/html');
617: $r->send_http_header;
618: if ($r->header_only) {
619: return;
620: }
621: &Apache::lonlocal::get_language_handle($r);
622: $r->print(
623: &Apache::loncommon::start_page('Invalid LTI call').
624: &mt('Invalid LTI call [_1]',$num).
625: &Apache::loncommon::end_page());
626: return;
627: }
628:
629: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>