Annotation of loncom/auth/migrateuser.pm, revision 1.34
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.34 ! raeburn 4: # $Id: migrateuser.pm,v 1.33 2018/07/01 00:03:42 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.32 raeburn 97: if ($data->{'lti.sourcecrs'}) {
98: $lti_env{'request.lti.sourcecrs'} = $data->{'lti.sourcecrs'};
99: }
1.26 raeburn 100: }
101: if ($data->{'lti.passbackid'}) {
1.27 raeburn 102: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
1.26 raeburn 103: }
104: if ($data->{'lti.passbackurl'}) {
1.27 raeburn 105: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
1.26 raeburn 106: }
107: if ($data->{'lti.rosterid'}) {
1.27 raeburn 108: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
1.26 raeburn 109: }
110: if ($data->{'lti.rosterurl'}) {
1.27 raeburn 111: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
1.26 raeburn 112: }
1.7 albertel 113: }
1.27 raeburn 114: return \%lti_env;
1.18 raeburn 115: }
116:
117: sub ip_changed {
118: my ($r,$udom,$camefrom,$dataref) = @_;
119: &Apache::loncommon::content_type($r,'text/html');
120: $r->send_http_header;
121: if (ref($dataref) eq 'HASH') {
122: my $title = 'LON-CAPA Session redirected';
123: my $message = &mt('Your internet address has changed since you logged in.');
124: my $rule_in_effect;
1.22 raeburn 125: if ($dataref->{'balancer'}) {
126: my $baldom = &Apache::lonnet::host_domain($camefrom);
127: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
128: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
129: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
130: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
131: my $dom_in_use;
132: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
133: $dom_in_use = $udom;
134: } else {
135: $dom_in_use = $baldom;
1.21 raeburn 136: }
1.22 raeburn 137: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 138: unless (defined($cached)) {
139: my $cachetime = 60*60*24;
140: my %domconfig =
1.22 raeburn 141: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 142: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 143: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 144: $domconfig{'loadbalancing'},$cachetime);
145: }
146: }
147: if (ref($result) eq 'HASH') {
148: (undef,my $currtargets,my $currrules) =
149: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
150: if (ref($currrules) eq 'HASH') {
151: if ($dataref->{'sso.login'}) {
152: if ($currrules->{'_LC_ipchangesso'} ne '') {
153: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
154: }
155: } else {
156: if ($currrules->{'_LC_ipchange'} ne '') {
157: $rule_in_effect = $currrules->{'_LC_ipchange'};
158: }
159: }
160: }
161: }
162: }
163: my $url;
164: my $lonhost= $r->dir_config('lonHostID');
165: my $switchto = $lonhost;
1.23 raeburn 166: if ($rule_in_effect ne 'offloadedto') {
1.18 raeburn 167: my $hosthere;
1.23 raeburn 168: my @ids=&Apache::lonnet::current_machine_ids();
169: unless ($rule_in_effect eq 'balancer') {
170: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
171: $hosthere = 1;
172: }
173: }
174: unless ($hosthere) {
175: if ($dataref->{'role'}) {
176: my ($adom,$aname);
177: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
178: $adom = $1;
179: $aname = $dataref->{'username'};
180: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
181: $adom = $1;
182: $aname = $2;
183: }
184: if ($adom ne '' && $aname ne '') {
185: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
186: unless ($ahome eq 'no_host') {
187: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
188: $hosthere = 1;
189: }
1.18 raeburn 190: }
191: }
192: }
193: }
1.23 raeburn 194: unless ($hosthere) {
195: my $hostname;
196: if ($rule_in_effect eq 'balancer') {
197: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
198: if ($hostname) {
199: $switchto = $dataref->{'server'};
200: }
201: } else {
202: $hostname = &Apache::lonnet::hostname($rule_in_effect);
203: if ($hostname) {
204: $switchto = $rule_in_effect;
205: }
206: }
1.18 raeburn 207: if ($hostname) {
208: my $protocol = $Apache::lonnet::protocol{$switchto};
209: $protocol = 'http' if ($protocol ne 'https');
210: $url = $protocol.'://'.$hostname;
1.23 raeburn 211: if ($rule_in_effect eq 'balancer') {
212: $message .= '<br />'.
213: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
214: } else {
215: $message .= '<br />'.
216: &mt('As a result, your LON-CAPA session is being redirected.');
217: }
1.18 raeburn 218: }
219: }
220: }
221: if ($dataref->{'sso.login'}) {
1.21 raeburn 222: $url .= '/adm/roles';
1.18 raeburn 223: } else {
1.21 raeburn 224: $url .= '/adm/login';
1.24 raeburn 225: if ($udom) {
226: $url .= '?domain='.$udom;
227: }
1.20 raeburn 228: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 229: }
230: my %info= (
1.24 raeburn 231: 'domain' => $udom,
1.18 raeburn 232: 'username' => $dataref->{'username'},
233: 'role' => $dataref->{'role'},
234: 'sessionserver' => $lonhost,
235: );
236: if ($dataref->{'origurl'}) {
237: $info{'origurl'} = $dataref->{'origurl'};
238: }
239: if ($dataref->{'symb'}) {
240: $info{'symb'} = $dataref->{'symb'};
241: }
242: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
243: unless ($iptoken eq 'conlost') {
1.24 raeburn 244: $url .= ($url =~ /\?/) ? '&' : '?';
245: $url .= 'iptoken='.$iptoken;
1.18 raeburn 246: }
247: $r->print(&Apache::loncommon::start_page($title,undef,
248: {'redirect' =>
249: [2,$url],}).
250: '<h1>'.&mt('One moment please...').'</h1>'.
251: '<p class="LC_warning">'.$message.'</p>'.
252: &Apache::loncommon::end_page());
253: } else {
254: return &goto_login($r);
1.10 raeburn 255: }
1.18 raeburn 256: return OK;
1.7 albertel 257: }
258:
1.28 raeburn 259: sub logout {
260: my ($r,$handle,$data,$lti_env) = @_;
1.34 ! raeburn 261: my $lonidsdir=$r->dir_config('lonIDsDir');
! 262: if (unlink("$lonidsdir/$handle.id")) {
! 263: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
! 264: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
! 265: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) {
! 266: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
1.28 raeburn 267: }
268: }
269: my %temp=('logout' => time);
270: &Apache::lonnet::put('email_status',\%temp);
271: &Apache::lonnet::log($env{'user.domain'},
272: $env{'user.name'},
273: $env{'user.home'},
274: "Logout $ENV{'REMOTE_ADDR'}");
275:
276: &Apache::loncommon::content_type($r,'text/html');
277:
1.34 ! raeburn 278: #expire the cookies
! 279: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
! 280: foreach my $name (keys(%cookies)) {
! 281: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
! 282: my $c = new CGI::Cookie(-name => $name,
! 283: -value => '',
! 284: -expires => '-10y',);
! 285: $r->headers_out->add('Set-cookie' => $c);
1.28 raeburn 286: }
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'}) {
1.32 raeburn 423: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
1.28 raeburn 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:
1.33 raeburn 483: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1.15 raeburn 484: if ($data{'origurl'} ne '') {
1.33 raeburn 485: $next_url .= '&orgurl='.&escape($data{'origurl'});
1.30 raeburn 486: if ($data{'lti.login'}) {
1.31 raeburn 487: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
488: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
1.33 raeburn 489: $next_url .= '&navmap=1';
1.30 raeburn 490: }
491: }
1.15 raeburn 492: }
1.6 albertel 493: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.16 raeburn 494: $next_url,$extra_env,\%form);
1.6 albertel 495: return OK;
1.1 albertel 496: }
497:
498: 1;
499: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>