Annotation of loncom/auth/migrateuser.pm, revision 1.29
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.29 ! raeburn 4: # $Id: migrateuser.pm,v 1.28 2018/04/14 01:50:43 raeburn Exp $
1.1 albertel 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:
1.2 albertel 29: package Apache::migrateuser;
1.1 albertel 30:
31: use strict;
1.19 raeburn 32: use LONCAPA qw(:DEFAULT :match);
1.1 albertel 33: use Apache::Constants qw(:common :http :methods);
34: use Apache::lonauth;
35: use Apache::lonnet;
1.6 albertel 36: use Apache::lonlocal;
1.18 raeburn 37: use Apache::lonlogin();
1.27 raeburn 38: use Apache::ltiauth;
1.28 raeburn 39: use CGI::Cookie;
1.1 albertel 40:
41: sub goto_login {
1.27 raeburn 42: my ($r,$domain,$data) = @_;
43: if ((ref($data) eq 'HASH') && ($data->{'lti.login'})) {
44: &Apache::ltiauth::invalid_request($r,'22');
45: } else {
46: &Apache::loncommon::content_type($r,'text/html');
47: $r->send_http_header;
48: my $url = '/adm/login';
49: if ($domain) {
50: $url .= '?domain='.$domain;
51: }
52: $r->print(&Apache::loncommon::start_page('Going to login',undef,
53: {'redirect' => [0,$url],}).
54: '<h1>'.&mt('One moment please...').'</h1>'.
55: '<p>'.&mt('Transferring to login page.').'</p>'.
56: &Apache::loncommon::end_page());
57: }
1.2 albertel 58: return OK;
1.1 albertel 59: }
60:
1.27 raeburn 61: sub sso_check {
1.7 albertel 62: my ($data) = @_;
1.8 albertel 63: my %extra_env;
1.18 raeburn 64: if (ref($data) eq 'HASH') {
65: if ($data->{'sso.login'}) {
66: $extra_env{'request.sso.login'} = $data->{'sso.login'};
67: }
68: if ($data->{'sso.reloginserver'}) {
69: $extra_env{'request.sso.reloginserver'} =
70: $data->{'sso.reloginserver'};
71: }
1.27 raeburn 72: }
73: return \%extra_env;
74: }
75:
76: sub lti_check {
77: my ($data) = @_;
78: my %lti_env;
79: if (ref($data) eq 'HASH') {
1.26 raeburn 80: if ($data->{'lti.login'}) {
1.27 raeburn 81: $lti_env{'request.lti.login'} = $data->{'lti.login'};
82: if ($data->{'lti.reqcrs'}) {
83: $lti_env{'request.lti.reqcrs'} = $data->{'lti.reqcrs'};
84: }
85: if ($data->{'lti.reqrole'}) {
86: $lti_env{'request.lti.reqrole'} = $data->{'lti.reqrole'};
87: }
88: if ($data->{'lti.selfenrollrole'}) {
89: $lti_env{'request.lti.selfenrollrole'} = $data->{'lti.selfenrollrole'};
90: }
1.29 ! raeburn 91: if ($data->{'lti.uri'}) {
! 92: $lti_env{'request.lti.uri'} = $data->{'lti.uri'};
! 93: }
! 94: if ($data->{'lti.target'}) {
! 95: $lti_env{'request.lti.target'} = $data->{'lti.target'};
! 96: }
1.26 raeburn 97: }
98: if ($data->{'lti.passbackid'}) {
1.27 raeburn 99: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
1.26 raeburn 100: }
101: if ($data->{'lti.passbackurl'}) {
1.27 raeburn 102: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
1.26 raeburn 103: }
104: if ($data->{'lti.rosterid'}) {
1.27 raeburn 105: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
1.26 raeburn 106: }
107: if ($data->{'lti.rosterurl'}) {
1.27 raeburn 108: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
1.26 raeburn 109: }
1.7 albertel 110: }
1.27 raeburn 111: return \%lti_env;
1.18 raeburn 112: }
113:
114: sub ip_changed {
115: my ($r,$udom,$camefrom,$dataref) = @_;
116: &Apache::loncommon::content_type($r,'text/html');
117: $r->send_http_header;
118: if (ref($dataref) eq 'HASH') {
119: my $title = 'LON-CAPA Session redirected';
120: my $message = &mt('Your internet address has changed since you logged in.');
121: my $rule_in_effect;
1.22 raeburn 122: if ($dataref->{'balancer'}) {
123: my $baldom = &Apache::lonnet::host_domain($camefrom);
124: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
125: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
126: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
127: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
128: my $dom_in_use;
129: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
130: $dom_in_use = $udom;
131: } else {
132: $dom_in_use = $baldom;
1.21 raeburn 133: }
1.22 raeburn 134: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 135: unless (defined($cached)) {
136: my $cachetime = 60*60*24;
137: my %domconfig =
1.22 raeburn 138: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 139: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 140: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 141: $domconfig{'loadbalancing'},$cachetime);
142: }
143: }
144: if (ref($result) eq 'HASH') {
145: (undef,my $currtargets,my $currrules) =
146: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
147: if (ref($currrules) eq 'HASH') {
148: if ($dataref->{'sso.login'}) {
149: if ($currrules->{'_LC_ipchangesso'} ne '') {
150: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
151: }
152: } else {
153: if ($currrules->{'_LC_ipchange'} ne '') {
154: $rule_in_effect = $currrules->{'_LC_ipchange'};
155: }
156: }
157: }
158: }
159: }
160: my $url;
161: my $lonhost= $r->dir_config('lonHostID');
162: my $switchto = $lonhost;
1.23 raeburn 163: if ($rule_in_effect ne 'offloadedto') {
1.18 raeburn 164: my $hosthere;
1.23 raeburn 165: my @ids=&Apache::lonnet::current_machine_ids();
166: unless ($rule_in_effect eq 'balancer') {
167: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
168: $hosthere = 1;
169: }
170: }
171: unless ($hosthere) {
172: if ($dataref->{'role'}) {
173: my ($adom,$aname);
174: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
175: $adom = $1;
176: $aname = $dataref->{'username'};
177: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
178: $adom = $1;
179: $aname = $2;
180: }
181: if ($adom ne '' && $aname ne '') {
182: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
183: unless ($ahome eq 'no_host') {
184: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
185: $hosthere = 1;
186: }
1.18 raeburn 187: }
188: }
189: }
190: }
1.23 raeburn 191: unless ($hosthere) {
192: my $hostname;
193: if ($rule_in_effect eq 'balancer') {
194: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
195: if ($hostname) {
196: $switchto = $dataref->{'server'};
197: }
198: } else {
199: $hostname = &Apache::lonnet::hostname($rule_in_effect);
200: if ($hostname) {
201: $switchto = $rule_in_effect;
202: }
203: }
1.18 raeburn 204: if ($hostname) {
205: my $protocol = $Apache::lonnet::protocol{$switchto};
206: $protocol = 'http' if ($protocol ne 'https');
207: $url = $protocol.'://'.$hostname;
1.23 raeburn 208: if ($rule_in_effect eq 'balancer') {
209: $message .= '<br />'.
210: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
211: } else {
212: $message .= '<br />'.
213: &mt('As a result, your LON-CAPA session is being redirected.');
214: }
1.18 raeburn 215: }
216: }
217: }
218: if ($dataref->{'sso.login'}) {
1.21 raeburn 219: $url .= '/adm/roles';
1.18 raeburn 220: } else {
1.21 raeburn 221: $url .= '/adm/login';
1.24 raeburn 222: if ($udom) {
223: $url .= '?domain='.$udom;
224: }
1.20 raeburn 225: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 226: }
227: my %info= (
1.24 raeburn 228: 'domain' => $udom,
1.18 raeburn 229: 'username' => $dataref->{'username'},
230: 'role' => $dataref->{'role'},
231: 'sessionserver' => $lonhost,
232: );
233: if ($dataref->{'origurl'}) {
234: $info{'origurl'} = $dataref->{'origurl'};
235: }
236: if ($dataref->{'symb'}) {
237: $info{'symb'} = $dataref->{'symb'};
238: }
239: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
240: unless ($iptoken eq 'conlost') {
1.24 raeburn 241: $url .= ($url =~ /\?/) ? '&' : '?';
242: $url .= 'iptoken='.$iptoken;
1.18 raeburn 243: }
244: $r->print(&Apache::loncommon::start_page($title,undef,
245: {'redirect' =>
246: [2,$url],}).
247: '<h1>'.&mt('One moment please...').'</h1>'.
248: '<p class="LC_warning">'.$message.'</p>'.
249: &Apache::loncommon::end_page());
250: } else {
251: return &goto_login($r);
1.10 raeburn 252: }
1.18 raeburn 253: return OK;
1.7 albertel 254: }
255:
1.28 raeburn 256: sub logout {
257: my ($r,$handle,$data,$lti_env) = @_;
258: unlink($handle);
259: if ($env{'user.linkedenv'} ne '') {
260: my $lonidsdir=$r->dir_config('lonIDsDir');
261: if ((-l $env{'user.linkedenv'}) &&
262: (readlink($env{'user.linkedenv'}) eq "$lonidsdir/$handle.id")) {
263: unlink($env{'user.linkedenv'});
264: }
265: }
266: my %temp=('logout' => time);
267: &Apache::lonnet::put('email_status',\%temp);
268: &Apache::lonnet::log($env{'user.domain'},
269: $env{'user.name'},
270: $env{'user.home'},
271: "Logout $ENV{'REMOTE_ADDR'}");
272:
273: &Apache::loncommon::content_type($r,'text/html');
274:
275: #expire the cookie
276: my $c = new CGI::Cookie(-name => 'lonID',
277: -value => '',
278: -expires => '-10y',);
279: $r->headers_out->add('Set-cookie' => $c);
280: if ($env{'user.linkedenv'}) {
281: my $linked = new CGI::Cookie(-name => 'lonLinkID',
282: -value => '',
283: -expires => '-10y',);
284: $r->headers_out->add('Set-cookie' => $linked);
285: }
286: my $lonhost = $r->dir_config('lonHostID');
287: my (%info,%user_info,%lti_info);
288: if (ref($lti_env) eq 'HASH') {
289: %lti_info = %{$lti_env};
290: }
291: my $lonhost = $r->dir_config('lonHostID');
292: if (ref($data) eq 'HASH') {
293: %user_info=('ip' => $ENV{'REMOTE_ADDR'},
294: 'domain' => $data->{'domain'},
295: 'username' => $data->{'username'},
296: 'role' => $data->{'role'},
297: 'origurl' => $data->{'origurl'},
298: 'symb' => $data->{'symb'},
299: 'server' => $lonhost);
300: }
301: %info = (%user_info,%lti_info);
302: my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
303: my $url = '/adm/migrateuser?token='.$token;
304: $r->send_http_header;
305: $r->print(
306: &Apache::loncommon::start_page('Updating Session ...',undef,
307: {'redirect' => [0.1,$url],
308: 'only_body' => 1,}).
309: &Apache::loncommon::end_page());
310: $r->register_cleanup(\&flush_course_logs);
311: return;
312: }
313:
314: sub flush_course_logs {
315: &Apache::lonnet::flushcourselogs();
316: return OK;
317: }
318:
1.1 albertel 319: sub handler {
320: my ($r) = @_;
321:
322: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 323: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 324: if (keys(%data) == 0) {
325: return &goto_login($r);
326: }
1.4 albertel 327: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
328:
1.6 albertel 329: &Apache::lonlocal::get_language_handle($r);
330:
1.4 albertel 331: if ($delete ne 'ok') {
1.27 raeburn 332: return &goto_login($r,undef,\%data);
1.4 albertel 333: }
1.2 albertel 334:
1.18 raeburn 335: if (!defined($data{'username'}) || !defined($data{'domain'})) {
1.27 raeburn 336: return &goto_login($r,undef,\%data);
1.18 raeburn 337: }
338: if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
1.24 raeburn 339: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
340: $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
1.18 raeburn 341: return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
1.2 albertel 342: }
343:
1.17 raeburn 344: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
1.2 albertel 345: my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
1.24 raeburn 346: my $udom;
347: if (&Apache::lonnet::domain($data{'domain'})) {
348: $udom=$data{'domain'};
349: }
1.27 raeburn 350: if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom,\%data); }
351:
352: my $sso_env = &sso_check(\%data);
353: my $lti_env = <i_check(\%data);
1.2 albertel 354:
1.27 raeburn 355: my $extra_env;
356: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
357: $extra_env = $sso_env;
358: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
359: $extra_env = $lti_env;
360: }
1.8 albertel 361:
1.16 raeburn 362: my %form;
363: if ($data{'symb'} ne '') {
364: $form{'symb'} = $data{'symb'};
365: }
1.21 raeburn 366: if ($data{'iptoken'} ne '') {
367: $form{'iptoken'} = $data{'iptoken'};
368: }
1.25 raeburn 369: if ($data{'noloadbalance'} ne '') {
370: $form{'noloadbalance'} = $data{'noloadbalance'};
371: }
1.16 raeburn 372:
1.3 albertel 373: if (!$data{'role'}) {
1.12 albertel 374: my $handle = &Apache::lonnet::check_for_valid_session($r);
375: if ($handle) {
1.11 albertel 376: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
377: $handle);
1.27 raeburn 378: if ($data{'lti.login'}) {
1.28 raeburn 379: my $needslogout;
380: if ($env{'request.lti.login'}) {
381: if (($env{'user.name'} ne $data{'username'}) ||
382: ($env{'user.domain'} ne $data{'domain'})) {
383: $needslogout = 1;
384: }
385: } else {
386: $needslogout = 1;
387: }
388: # If access is via LTI, and user already has a non-LTI session cookie
389: # (and session) or has an LTI session cookie for a different username,
390: # logout the existing session, and start a new one
391: if ($needslogout) {
392: &logout($r,$handle,\%data,$lti_env);
393: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
1.27 raeburn 394: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
395: $form{'lti.reqrole'} = $data{'lti.reqrole'};
396: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
1.29 ! raeburn 397: $form{'lti.uri'} = $data{'lti.uri'};
1.28 raeburn 398: if ($data{'lti.passbackid'}) {
399: $form{'lti.passbackid'} = $data{'lti.passbackid'};
400: }
401: if ($data{'lti.passbackurl'}) {
402: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
403: }
404: if ($data{'lti.rosterid'}) {
405: $form{'lti.rosterid'} = $data{'lti.rosterid'};
406: }
407: if ($data{'lti.rosterurl'}) {
408: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
409: }
1.29 ! raeburn 410: if ($data{'lti.target'}) {
! 411: $form{'lti.target'} = $data{'lti.target'};
! 412: }
1.27 raeburn 413: &Apache::loncommon::content_type($r,'text/html');
414: $r->send_http_header;
415: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
1.28 raeburn 416: } else {
417: if (ref($lti_env) eq 'HASH') {
418: delete($lti_env->{'reqcrs'});
419: delete($lti_env->{'reqrole'});
420: delete($lti_env->{'selfenrollrole'});
421: }
422: if ($data{'lti.selfenrollrole'}) {
423: if (&Apache::ltiauth::lti_enroll($data{'username'},data{'domain'},
424: $data{'lti.selfenrollrole'}) eq 'ok') {
425: my $url = '/adm/roles?selectrole=1&'.
426: &escape($data{'lti.selfenrollrole'}).'=1';
427: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
428: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
429: } elsif ($data{'origurl'} ne '') {
430: $url .= '&orgurl='.$data{'origurl'};
431: }
432: if (ref($lti_env) eq 'HASH') {
433: &Apache::lonnet::appenv($lti_env);
434: }
435: $r->internal_redirect($url);
436: } else {
437: &Apache::ltiauth::invalid_request($r,23);
438: }
439: } elsif ($data{'origurl'} ne '') {
440: my $url = $data{'origurl'};
441: if ($url =~ m{/default_\d+\.sequence$}) {
442: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
443: }
444: if (ref($lti_env) eq 'HASH') {
445: &Apache::lonnet::appenv($lti_env);
1.27 raeburn 446: }
447: $r->internal_redirect($url);
448: } else {
1.28 raeburn 449: if (ref($lti_env) eq 'HASH') {
450: &Apache::lonnet::appenv($lti_env);
451: }
1.27 raeburn 452: }
453: }
454: } elsif ($data{'origurl'} ne '') {
1.14 raeburn 455: $r->internal_redirect($data{'origurl'});
456: } elsif ($env{'request.course.id'}) {
457: $r->internal_redirect('/adm/navmaps');
1.11 albertel 458: } else {
459: $r->internal_redirect('/adm/roles');
460: }
461: } else {
1.14 raeburn 462: my $desturl = '/adm/roles';
463: if ($data{'origurl'} ne '') {
464: $desturl = $data{'origurl'};
1.27 raeburn 465: if ($data{'lti.login'}) {
466: $desturl = $data{'origurl'};
467: if ($desturl =~ m{/default_\d+\.sequence$}) {
468: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
469: }
470: }
471: }
472: my $skipcritical;
473: if (($data{'lti.login'}) && ($data{'lti.reqcrs'}) &&
474: ($data{'lti.reqrole'} eq 'cc')) {
475: $skipcritical = 1;
1.14 raeburn 476: }
1.11 albertel 477: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.27 raeburn 478: $home,$desturl,$extra_env,\%form,$skipcritical);
1.11 albertel 479: }
1.1 albertel 480: return OK;
481: }
1.6 albertel 482:
483: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1.15 raeburn 484: if ($data{'origurl'} ne '') {
485: $next_url .= '&orgurl='.&escape($data{'origurl'});
486: }
1.6 albertel 487: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.16 raeburn 488: $next_url,$extra_env,\%form);
1.6 albertel 489: return OK;
1.1 albertel 490: }
491:
492: 1;
493: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>