Annotation of loncom/auth/migrateuser.pm, revision 1.25.2.5.2.10
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.25.2.5.2.10! (raeburn 4:: # $Id: migrateuser.pm,v 1.25.2.5.2.9 2023/07/05 20:42:05 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.25.2.5 raeburn 33: use Apache::Constants qw(:common :http :methods :remotehost);
1.1 albertel 34: use Apache::lonauth;
35: use Apache::lonnet;
1.25.2.5.2.9 raeburn 36: use Apache::loncommon;
1.6 albertel 37: use Apache::lonlocal;
1.18 raeburn 38: use Apache::lonlogin();
1.25.2.5.2.9 raeburn 39: use Apache::ltiauth;
40: use Apache::switchserver;
1.25.2.5.2.2 raeburn 41: use CGI::Cookie;
1.1 albertel 42:
43: sub goto_login {
1.25.2.5.2.7 raeburn 44: my ($r,$domain,$data) = @_;
45: my $write_to_opener;
46: if (ref($data) eq 'HASH') {
1.25.2.5.2.9 raeburn 47: if ($data->{'lti.login'}) {
48: &Apache::ltiauth::invalid_request($r,'22');
49: } else {
50: $write_to_opener = $data->{'lcssowin'};
51: }
1.25.2.5.2.7 raeburn 52: }
1.2 albertel 53: &Apache::loncommon::content_type($r,'text/html');
54: $r->send_http_header;
1.24 raeburn 55: my $url = '/adm/login';
56: if ($domain) {
57: $url .= '?domain='.$domain;
58: }
1.5 albertel 59: $r->print(&Apache::loncommon::start_page('Going to login',undef,
1.25.2.5.2.7 raeburn 60: {'redirect' => [0,$url,'',$write_to_opener],}).
1.5 albertel 61: '<h1>'.&mt('One moment please...').'</h1>'.
62: '<p>'.&mt('Transferring to login page.').'</p>'.
63: &Apache::loncommon::end_page());
1.2 albertel 64: return OK;
1.1 albertel 65: }
66:
1.7 albertel 67: sub sso_check {
68: my ($data) = @_;
1.8 albertel 69: my %extra_env;
1.18 raeburn 70: if (ref($data) eq 'HASH') {
71: if ($data->{'sso.login'}) {
72: $extra_env{'request.sso.login'} = $data->{'sso.login'};
73: }
74: if ($data->{'sso.reloginserver'}) {
75: $extra_env{'request.sso.reloginserver'} =
76: $data->{'sso.reloginserver'};
77: }
1.7 albertel 78: }
1.18 raeburn 79: return \%extra_env;
80: }
81:
1.25.2.5.2.9 raeburn 82: sub lti_check {
83: my ($data) = @_;
84: my %lti_env;
85: if (ref($data) eq 'HASH') {
86: if ($data->{'lti.login'}) {
87: $lti_env{'request.lti.login'} = $data->{'lti.login'};
88: if ($data->{'lti.reqcrs'}) {
89: $lti_env{'request.lti.reqcrs'} = $data->{'lti.reqcrs'};
90: }
91: if ($data->{'lti.reqrole'}) {
92: $lti_env{'request.lti.reqrole'} = $data->{'lti.reqrole'};
93: }
94: if ($data->{'lti.selfenrollrole'}) {
95: $lti_env{'request.lti.selfenrollrole'} = $data->{'lti.selfenrollrole'};
96: }
97: if ($data->{'lti.uri'}) {
98: $lti_env{'request.lti.uri'} = $data->{'lti.uri'};
99: }
100: if ($data->{'lti.target'}) {
101: $lti_env{'request.lti.target'} = $data->{'lti.target'};
102: }
103: if ($data->{'lti.sourcecrs'}) {
104: $lti_env{'request.lti.sourcecrs'} = $data->{'lti.sourcecrs'};
105: }
106: }
107: if ($data->{'lti.passbackid'}) {
108: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
109: }
110: if ($data->{'lti.passbackurl'}) {
111: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
112: }
113: if ($data->{'lti.rosterid'}) {
114: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
115: }
116: if ($data->{'lti.rosterurl'}) {
117: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
118: }
119: }
120: return \%lti_env;
121: }
122:
1.18 raeburn 123: sub ip_changed {
124: my ($r,$udom,$camefrom,$dataref) = @_;
125: &Apache::loncommon::content_type($r,'text/html');
126: $r->send_http_header;
127: if (ref($dataref) eq 'HASH') {
128: my $title = 'LON-CAPA Session redirected';
129: my $message = &mt('Your internet address has changed since you logged in.');
130: my $rule_in_effect;
1.22 raeburn 131: if ($dataref->{'balancer'}) {
132: my $baldom = &Apache::lonnet::host_domain($camefrom);
133: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
134: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
135: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
136: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
137: my $dom_in_use;
138: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
139: $dom_in_use = $udom;
140: } else {
141: $dom_in_use = $baldom;
1.21 raeburn 142: }
1.22 raeburn 143: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 144: unless (defined($cached)) {
145: my $cachetime = 60*60*24;
146: my %domconfig =
1.22 raeburn 147: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 148: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 149: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 150: $domconfig{'loadbalancing'},$cachetime);
151: }
152: }
153: if (ref($result) eq 'HASH') {
154: (undef,my $currtargets,my $currrules) =
155: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
156: if (ref($currrules) eq 'HASH') {
157: if ($dataref->{'sso.login'}) {
158: if ($currrules->{'_LC_ipchangesso'} ne '') {
159: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
160: }
161: } else {
162: if ($currrules->{'_LC_ipchange'} ne '') {
163: $rule_in_effect = $currrules->{'_LC_ipchange'};
164: }
165: }
166: }
167: }
168: }
169: my $url;
170: my $lonhost= $r->dir_config('lonHostID');
171: my $switchto = $lonhost;
1.23 raeburn 172: if ($rule_in_effect ne 'offloadedto') {
1.18 raeburn 173: my $hosthere;
1.23 raeburn 174: my @ids=&Apache::lonnet::current_machine_ids();
175: unless ($rule_in_effect eq 'balancer') {
176: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
177: $hosthere = 1;
178: }
179: }
180: unless ($hosthere) {
181: if ($dataref->{'role'}) {
182: my ($adom,$aname);
183: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
184: $adom = $1;
185: $aname = $dataref->{'username'};
186: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
187: $adom = $1;
188: $aname = $2;
189: }
190: if ($adom ne '' && $aname ne '') {
191: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
192: unless ($ahome eq 'no_host') {
193: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
194: $hosthere = 1;
195: }
1.18 raeburn 196: }
197: }
198: }
199: }
1.23 raeburn 200: unless ($hosthere) {
201: my $hostname;
202: if ($rule_in_effect eq 'balancer') {
203: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
204: if ($hostname) {
205: $switchto = $dataref->{'server'};
206: }
207: } else {
208: $hostname = &Apache::lonnet::hostname($rule_in_effect);
209: if ($hostname) {
210: $switchto = $rule_in_effect;
211: }
212: }
1.18 raeburn 213: if ($hostname) {
214: my $protocol = $Apache::lonnet::protocol{$switchto};
215: $protocol = 'http' if ($protocol ne 'https');
1.25.2.5 raeburn 216: my $alias = &Apache::lonnet::use_proxy_alias($r,$switchto);
217: $hostname = $alias if ($alias ne '');
1.18 raeburn 218: $url = $protocol.'://'.$hostname;
1.23 raeburn 219: if ($rule_in_effect eq 'balancer') {
220: $message .= '<br />'.
221: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
222: } else {
223: $message .= '<br />'.
224: &mt('As a result, your LON-CAPA session is being redirected.');
225: }
1.18 raeburn 226: }
227: }
1.25.2.1 raeburn 228: unless ($hosthere) {
229: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
230: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
231: }
232: }
1.18 raeburn 233: }
234: if ($dataref->{'sso.login'}) {
1.21 raeburn 235: $url .= '/adm/roles';
1.18 raeburn 236: } else {
1.21 raeburn 237: $url .= '/adm/login';
1.24 raeburn 238: if ($udom) {
239: $url .= '?domain='.$udom;
240: }
1.20 raeburn 241: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 242: }
243: my %info= (
1.24 raeburn 244: 'domain' => $udom,
1.18 raeburn 245: 'username' => $dataref->{'username'},
246: 'sessionserver' => $lonhost,
247: );
1.25.2.5.2.9 raeburn 248: my @names = ('origurl','symb','role','linkprotuser','linkprotexit','linkprot','linkkey','linkprotpbid','linkprotpburl');
1.25.2.5 raeburn 249: foreach my $name (@names) {
250: if ($dataref->{$name} ne '') {
251: $info{$name} = $dataref->{$name};
252: }
1.18 raeburn 253: }
1.25.2.5.2.8 raeburn 254: if ($info{'origurl'} eq '/adm/email') {
255: if (($dataref->{'display'}) && ($dataref->{'mailrecip'})) {
256: if (&unescape($dataref->{'mailrecip'}) eq $dataref->{'username'}.':'.$dataref->{'domain'}) {
257: $info{'display'} = $dataref->{'display'};
258: $info{'mailrecip'} = $dataref->{'mailrecip'};
259: }
260: }
261: }
1.25.2.5 raeburn 262: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto,'link');
1.18 raeburn 263: unless ($iptoken eq 'conlost') {
1.25.2.5 raeburn 264: $url .= (($url =~ /\?/) ? '&' : '?') . 'iptoken='.$iptoken;
1.18 raeburn 265: }
266: $r->print(&Apache::loncommon::start_page($title,undef,
267: {'redirect' =>
1.25.2.5.2.7 raeburn 268: [2,$url,'',$dataref->{'lcssowin'}]}).
1.18 raeburn 269: '<h1>'.&mt('One moment please...').'</h1>'.
270: '<p class="LC_warning">'.$message.'</p>'.
271: &Apache::loncommon::end_page());
272: } else {
273: return &goto_login($r);
1.10 raeburn 274: }
1.18 raeburn 275: return OK;
1.7 albertel 276: }
277:
1.25.2.5.2.2 raeburn 278: sub logout {
1.25.2.5.2.6 raeburn 279: my ($r,$ip,$handle,$data,$lti_env,$linkprot_info) = @_;
1.25.2.5.2.2 raeburn 280: my $lonidsdir=$r->dir_config('lonIDsDir');
281: if (unlink("$lonidsdir/$handle.id")) {
282: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
283: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
284: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) {
285: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
286: }
287: }
288: my %temp=('logout' => time);
289: &Apache::lonnet::put('email_status',\%temp);
290: &Apache::lonnet::log($env{'user.domain'},
291: $env{'user.name'},
292: $env{'user.home'},
293: "Logout $ip");
294:
295: &Apache::loncommon::content_type($r,'text/html');
296:
297: #expire the cookies
298: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
299: foreach my $name (keys(%cookies)) {
300: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
301: my $c = new CGI::Cookie(-name => $name,
302: -value => '',
303: -expires => '-10y',);
304: $r->headers_out->add('Set-cookie' => $c);
305: }
306: my $lonhost = $r->dir_config('lonHostID');
1.25.2.5.2.7 raeburn 307: my ($url,$token,$delay,$write_to_opener,%info,%user_info);
1.25.2.5.2.2 raeburn 308: if (ref($data) eq 'HASH') {
309: %user_info=('ip' => $ip,
310: 'domain' => $data->{'domain'},
311: 'username' => $data->{'username'},
312: 'home' => $data->{'home'},
313: 'role' => $data->{'role'},
314: 'origurl' => $data->{'origurl'},
315: 'symb' => $data->{'symb'},
316: 'server' => $lonhost);
1.25.2.5.2.7 raeburn 317: $write_to_opener = $data->{'lcssowin'};
1.25.2.5.2.2 raeburn 318: }
1.25.2.5.2.6 raeburn 319: $delay = 0.1;
320: if (ref($linkprot_info) eq 'HASH') {
321: if ($linkprot_info->{'linkprotuser'} ne '') {
322: $token = &Apache::lonnet::tmpput($linkprot_info,$lonhost);
323: $url = '/adm/login?ltoken='.$token;
324: } else {
325: foreach my $key (keys(%user_info)) {
326: if ($user_info{$key} eq '') {
327: delete($user_info{$key});
328: }
329: }
330: %info = (%user_info,%{$linkprot_info});
331: $token = &Apache::lonnet::tmpput(\%info,$lonhost);
332: $url = '/adm/migrateuser?token='.$token;
333: }
334: $delay = 0;
335: } else {
336: my %lti_info;
337: if (ref($lti_env) eq 'HASH') {
338: foreach my $key (sort(keys(%{$lti_env}))) {
339: if ($key =~ /^request\.(.+)$/) {
340: $lti_info{$1} = $lti_env->{$key};
341: }
342: }
343: }
344: %info = (%user_info,%lti_info);
345: $token = &Apache::lonnet::tmpput(\%info,$lonhost);
346: $url = '/adm/migrateuser?token='.$token;
347: }
1.25.2.5.2.2 raeburn 348: $r->send_http_header;
349: $r->print(
350: &Apache::loncommon::start_page('Updating Session ...',undef,
1.25.2.5.2.7 raeburn 351: {'redirect' => [$delay,$url,'',$write_to_opener],
1.25.2.5.2.2 raeburn 352: 'only_body' => 1,}).
353: &Apache::loncommon::end_page());
354: if ($env{'request.balancercookie'}) {
355: my ($balancer,$cookie) = split(/:/,$env{'request.balancercookie'});
356: if ((&Apache::lonnet::hostname($balancer)) && ($cookie =~ /^[a-f0-9]{32}$/)) {
357: $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cookie;
358: &Apache::lonnet::delbalcookie($cookie,$balancer);
359: }
360: }
361: $r->register_cleanup(\&flush_course_logs);
362: return;
363: }
364:
1.25.2.5.2.5 raeburn 365: sub flush_course_logs {
366: &Apache::lonnet::flushcourselogs();
367: return OK;
368: }
369:
1.1 albertel 370: sub handler {
371: my ($r) = @_;
1.25.2.5.2.1 raeburn 372:
1.1 albertel 373: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 374: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 375: if (keys(%data) == 0) {
376: return &goto_login($r);
377: }
1.4 albertel 378: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
379:
1.6 albertel 380: &Apache::lonlocal::get_language_handle($r);
381:
1.4 albertel 382: if ($delete ne 'ok') {
1.25.2.5.2.7 raeburn 383: return &goto_login($r,'',\%data);
1.4 albertel 384: }
1.2 albertel 385:
1.18 raeburn 386: if (!defined($data{'username'}) || !defined($data{'domain'})) {
1.25.2.5.2.7 raeburn 387: return &goto_login($r,'',\%data);
1.18 raeburn 388: }
1.25.2.5 raeburn 389: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.25.2.4 raeburn 390: if ($data{'ip'} ne $ip) {
1.24 raeburn 391: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
1.25.2.4 raeburn 392: $data{'ip'}.'; now: '.$ip.' for '.$data{'username'}.':'.$data{'domain'});
1.18 raeburn 393: return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
1.2 albertel 394: }
395:
396: my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
1.24 raeburn 397: my $udom;
398: if (&Apache::lonnet::domain($data{'domain'})) {
399: $udom=$data{'domain'};
400: }
1.25.2.5.2.7 raeburn 401: if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom,\%data); }
1.2 albertel 402:
1.25.2.5.2.9 raeburn 403: my $rolemsg;
404: if ($data{'role'}) {
405: $rolemsg = "role: $data{'role'}";
406: } else {
407: $rolemsg = '(no role)';
408: }
409:
410: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} $rolemsg");
411:
412: my $sso_env = &sso_check(\%data);
413: my $lti_env = <i_check(\%data);
414:
415: my $extra_env;
416: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
417: $extra_env = $sso_env;
418: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
419: $extra_env = $lti_env;
420: }
421:
1.25.2.1 raeburn 422: if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) {
423: if (ref($extra_env) eq 'HASH') {
424: $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'};
425: } else {
426: $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} };
427: }
1.25.2.3 raeburn 428: } elsif (($data{'server'}) && ($data{'otherbalcookie'})) {
429: my ($balancer,$balcookie) = split(/:/,$data{'otherbalcookie'});
430: if (defined(&Apache::lonnet::hostname($balancer)) && $balcookie =~ /^[a-f0-9]{32}$/) {
431: my $baldom = &Apache::lonnet::host_domain($balancer);
432: if (&Apache::lonnet::shared_institution($baldom)) {
433: my $cookieid = join('_',$udom,$data{'username'},$balcookie);
434: &Apache::lonnet::updatebalcookie($cookieid,$balancer,$data{'server'});
435: if (ref($extra_env) eq 'HASH') {
436: $extra_env->{'request.balancercookie'} = $data{'otherbalcookie'};
437: } else {
438: $extra_env = { 'request.balancercookie' => $data{'otherbalcookie'} };
439: }
440: }
441: }
1.25.2.1 raeburn 442: }
1.8 albertel 443:
1.25.2.2 raeburn 444: my (%form,$cid);
1.16 raeburn 445: if ($data{'symb'} ne '') {
446: $form{'symb'} = $data{'symb'};
447: }
1.21 raeburn 448: if ($data{'iptoken'} ne '') {
449: $form{'iptoken'} = $data{'iptoken'};
450: }
1.25 raeburn 451: if ($data{'noloadbalance'} ne '') {
452: $form{'noloadbalance'} = $data{'noloadbalance'};
453: }
1.25.2.2 raeburn 454: if ($data{'role'}) {
455: if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) {
456: unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') {
457: $cid = $1.'_'.$2;
458: }
459: }
460: } else {
1.25.2.5 raeburn 461: my ($handle,$expirepub);
1.25.2.5.2.1 raeburn 462: $handle = &Apache::lonnet::check_for_valid_session($r);
1.25.2.5 raeburn 463:
464: # For "public user" - remove any existing "public" cookie so actual user is logged in.
465: if ($handle) {
466: if ($handle=~/^publicuser\_/) {
467: my $lonidsdir=$r->dir_config('lonIDsDir');
468: if ($lonidsdir ne '') {
469: unlink("$lonidsdir/$handle.id");
470: }
471: undef($handle);
472: $expirepub = 1;
473: }
474: }
1.12 albertel 475: if ($handle) {
1.11 albertel 476: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
477: $handle);
1.25.2.5.2.10! (raeburn 478:: my ($checklaunch,$prevlaunch);
1.25.2.5.2.1 raeburn 479: if ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
1.25.2.5.2.3 raeburn 480: unless ($env{'request.linkprot'} eq $data{'linkprot'}) {
481: $checklaunch = 1;
1.25.2.5.2.1 raeburn 482: }
1.25.2.5.2.6 raeburn 483: unless ($env{'request.linkprotexit'} eq $data{'linkprotexit'}) {
484: $checklaunch = 1;
485: }
1.25.2.5.2.9 raeburn 486: unless ($env{'request.linkprotpbid'} eq $data{'linkprotpbid'}) {
487: $checklaunch = 1;
488: }
489: unless ($env{'request.linkprotpburl'} eq $data{'linkprotpburl'}) {
490: $checklaunch = 1;
491: }
1.25.2.5.2.3 raeburn 492: unless ($env{'request.linkkey'} eq $data{'linkkey'}) {
493: $checklaunch = 1;
494: }
495: unless ($env{'request.deeplink.login'} eq $data{'deeplink.login'}) {
496: $checklaunch = 1;
1.25.2.5.2.1 raeburn 497: }
498: }
499: if ($data{'linkprot'} ne '') {
1.25.2.5.2.2 raeburn 500: if (($env{'user.name'} ne $data{'username'}) ||
501: ($env{'user.domain'} ne $data{'domain'})) {
502: my %linkprot_env;
1.25.2.5.2.9 raeburn 503: foreach my $item ('linkprot','linkprotexit','linkprotpbid','linkprotpburl','deeplink.login') {
1.25.2.5.2.2 raeburn 504: if ($data{$item}) {
1.25.2.5.2.6 raeburn 505: $linkprot_env{$item} = $data{$item};
1.25.2.5.2.2 raeburn 506: }
507: }
1.25.2.5.2.6 raeburn 508: &logout($r,$ip,$handle,\%data,undef,\%linkprot_env);
1.25.2.5.2.2 raeburn 509: return OK;
510: }
1.25.2.5.2.1 raeburn 511: &Apache::lonnet::appenv({'request.linkprot' => $data{'linkprot'}});
1.25.2.5.2.6 raeburn 512: if ($data{'linkprotexit'}) {
513: &Apache::lonnet::appenv({'request.linkprotexit' => $data{'linkprotexit'}});
514: }
1.25.2.5.2.1 raeburn 515: if ($env{'request.linkkey'}) {
516: &Apache::lonnet::delenv('request.linkkey');
517: }
518: my ($linkprotector,$deeplink) = split(/:/,$data{'linkprot'},2);
519: if ($env{'user.linkprotector'}) {
520: my @protectors = split(/,/,$env{'user.linkprotector'});
521: unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
522: push(@protectors,$linkprotector);
523: @protectors = sort { $a <=> $b } @protectors;
524: &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
525: }
526: } else {
527: &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
528: }
529: if ($env{'user.linkproturi'}) {
530: my @proturis = split(/,/,$env{'user.linkproturi'});
531: unless(grep(/^\Q$deeplink\E$/,@proturis)) {
532: push(@proturis,$deeplink);
533: @proturis = sort @proturis;
534: &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
535: }
536: } else {
537: &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
538: }
539: } elsif ($data{'linkkey'} ne '') {
540: &Apache::lonnet::appenv({'request.linkkey' => $data{'linkkey'}});
541: if ($env{'request.linkprot'}) {
542: &Apache::lonnet::delenv('request.linkprot');
543: }
544: my $deeplink = $data{'deeplink.login'};
545: my $linkkey = $data{'linkkey'};
546: if ($env{'user.deeplinkkey'} ne '') {
547: my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
548: unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
549: push(@linkkeys,$linkkey);
550: &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
551: }
552: } else {
553: &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
554: }
555: if ($deeplink) {
556: if ($env{'user.keyedlinkuri'}) {
557: my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
558: unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
559: push(@keyeduris,$deeplink);
560: &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
561: }
562: } else {
563: &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
564: }
565: }
566: }
567: if ($data{'deeplink.login'}) {
568: &Apache::lonnet::appenv({'request.deeplink.login' => $data{'deeplink.login'}});
1.25.2.5.2.4 raeburn 569: if ($env{'environment.remote'} eq 'on') {
570: &Apache::lonnet::appenv({'environment.remote' => 'off'});
571: }
1.25.2.5.2.10! (raeburn 572:: } elsif ($env{'request.deeplink.login'}) {
! 573:: $prevlaunch = $env{'request.deeplink.login'};
1.25.2.5.2.1 raeburn 574: }
1.25.2.5.2.9 raeburn 575: if ($data{'lti.login'}) {
576: if ($env{'environment.remote'} eq 'on') {
577: &Apache::lonnet::appenv({'environment.remote' => 'off'});
578: }
579: my $needslogout;
580: if ($env{'request.lti.login'}) {
581: if (($env{'user.name'} ne $data{'username'}) ||
582: ($env{'user.domain'} ne $data{'domain'})) {
583: $needslogout = 1;
584: }
585: } else {
586: $needslogout = 1;
587: }
588: # If access is via LTI, and user already has a non-LTI session cookie
589: # (and session) or has an LTI session cookie for a different username,
590: # logout the existing session, and start a new one
591: if ($needslogout) {
592: &logout($r,$ip,$handle,\%data,$lti_env);
593: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
594: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
595: $form{'lti.reqrole'} = $data{'lti.reqrole'};
596: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
597: $form{'lti.uri'} = $data{'lti.uri'};
598: if ($data{'lti.passbackid'}) {
599: $form{'lti.passbackid'} = $data{'lti.passbackid'};
600: }
601: if ($data{'lti.passbackurl'}) {
602: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
603: }
604: if ($data{'lti.rosterid'}) {
605: $form{'lti.rosterid'} = $data{'lti.rosterid'};
606: }
607: if ($data{'lti.rosterurl'}) {
608: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
609: }
610: if ($data{'lti.target'}) {
611: $form{'lti.target'} = $data{'lti.target'};
612: }
613: &Apache::loncommon::content_type($r,'text/html');
614: $r->send_http_header;
615: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
616: } else {
617: if (ref($lti_env) eq 'HASH') {
618: delete($lti_env->{'reqcrs'});
619: delete($lti_env->{'reqrole'});
620: delete($lti_env->{'selfenrollrole'});
621: }
622: if ($data{'lti.selfenrollrole'}) {
623: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
624: $data{'lti.selfenrollrole'}) eq 'ok') {
625: my $url = '/adm/roles?selectrole=1&'.
626: &escape($data{'lti.selfenrollrole'}).'=1';
627: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
628: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
629: } elsif ($data{'origurl'} ne '') {
630: $url .= '&orgurl='.$data{'origurl'};
631: }
632: if (ref($lti_env) eq 'HASH') {
633: &Apache::lonnet::appenv($lti_env);
634: }
635: $r->internal_redirect($url);
636: } else {
637: &Apache::ltiauth::invalid_request($r,23);
638: }
639: } elsif ($data{'origurl'} ne '') {
640: my $url = $data{'origurl'};
641: if ($url =~ m{/default_\d+\.sequence$}) {
642: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
643: }
644: if (ref($lti_env) eq 'HASH') {
645: &Apache::lonnet::appenv($lti_env);
646: }
647: $r->internal_redirect($url);
648: } else {
649: if (ref($lti_env) eq 'HASH') {
650: &Apache::lonnet::appenv($lti_env);
651: }
652: }
653: }
654: } elsif ($data{'origurl'} ne '') {
1.25.2.5.2.1 raeburn 655: my $dest = $data{'origurl'};
656: if (($env{'request.deeplink.login'} eq $data{'origurl'}) &&
657: (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) {
658: my %info;
659: if ($env{'request.linkprot'}) {
660: $info{'linkprot'} = $env{'request.linkprot'};
1.25.2.5.2.9 raeburn 661: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
1.25.2.5.2.6 raeburn 662: if ($data{$item}) {
663: $info{$item} = $data{$item};
664: }
665: }
1.25.2.5.2.1 raeburn 666: } elsif ($env{'request.linkkey'} ne '') {
667: $info{'linkkey'} = $env{'request.linkkey'};
668: }
669: $info{'origurl'} = $data{'origurl'};
670: if ($checklaunch) {
671: $info{'checklaunch'} = 1;
672: }
1.25.2.5.2.10! (raeburn 673:: if ($prevlaunch) {
! 674:: $info{'prevlaunch'} = $prevlaunch;
! 675:: }
1.25.2.5.2.1 raeburn 676: my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
1.25.2.5.2.6 raeburn 677: unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
1.25.2.5.2.1 raeburn 678: ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
679: $dest .= (($dest =~ /\?/) ? '&' : '?') . 'ttoken='.$token;
680: }
681: }
682: $r->internal_redirect($dest);
1.14 raeburn 683: } elsif ($env{'request.course.id'}) {
684: $r->internal_redirect('/adm/navmaps');
1.11 albertel 685: } else {
686: $r->internal_redirect('/adm/roles');
687: }
688: } else {
1.14 raeburn 689: my $desturl = '/adm/roles';
1.25.2.5.2.9 raeburn 690: if ($data{'lti.login'}) {
691: if ($data{'lti.selfenrollrole'}) {
692: $desturl .= '?selectrole=1&'.
693: &escape($data{'lti.selfenrollrole'}).'=1';
694: if ($data{'origurl'} ne '') {
695: $desturl .= '&orgurl='.$data{'origurl'};
696: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
697: $desturl .= '&navmap=1';
698: }
699: }
700: } elsif ($data{'origurl'} ne '') {
701: $desturl = $data{'origurl'};
702: if ($desturl =~ m{/default_\d+\.sequence$}) {
703: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
704: }
705: }
706: } elsif ($data{'origurl'} ne '') {
1.14 raeburn 707: $desturl = $data{'origurl'};
1.25.2.5.2.1 raeburn 708: if ($data{'linkprot'}) {
709: my ($linkprotector,$linkuri) = split(/:/,$data{'linkprot'},2);
710: if ($linkprotector) {
711: if (ref($extra_env) eq 'HASH') {
712: $extra_env->{'user.linkprotector'} = $linkprotector;
713: $extra_env->{'user.linkproturi'} = $linkuri;
714: $extra_env->{'request.linkprot'} = $data{'linkprot'};
715: } else {
716: $extra_env = {'user.linkprotector' => $linkprotector,
717: 'user.linkproturi' => $linkuri,
718: 'request.linkprot' => $data{'linkprot'}};
719: }
1.25.2.5.2.6 raeburn 720: if ($data{'linkprotexit'}) {
721: $extra_env->{'request.linkprotexit'} = $data{'linkprotexit'};
722: }
1.25.2.5.2.9 raeburn 723: if ($data{'linkprotpbid'}) {
724: $extra_env->{'request.linkprotpbid'} = $data{'linkprotpbid'};
725: }
726: if ($data{'linkprotpburl'}) {
727: $extra_env->{'request.linkprotpburl'} = $data{'linkprotpburl'};
728: }
1.25.2.5.2.6 raeburn 729: }
1.25.2.5.2.9 raeburn 730: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
1.25.2.5.2.6 raeburn 731: if ($data{$item} ne '') {
732: $form{$item} = $data{$item};
733: }
1.25.2.5.2.1 raeburn 734: }
1.25.2.5.2.9 raeburn 735: } elsif ($data{'linkkey'} ne '') {
1.25.2.5.2.1 raeburn 736: if (ref($extra_env) eq 'HASH') {
737: $extra_env->{'user.deeplinkkey'} = $data{'linkkey'};
738: $extra_env->{'user.keyedlinkuri'} = $data{'deeplink.login'};
739: $extra_env->{'request.linkkey'} = $data{'linkkey'};
740: } else {
741: $extra_env = {'user.deeplinkkey' => $data{'linkkey'},
742: 'user.keyedlinkuri' => $data{'deeplink.login'},
743: 'request.linkkey' => $data{'linkkey'}};
744: }
745: }
746: if ($data{'deeplink.login'}) {
747: if (ref($extra_env) eq 'HASH') {
748: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
749: } else {
750: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
751: }
752: }
1.25.2.5.2.8 raeburn 753: if ($desturl eq '/adm/email') {
754: if (($data{'display'}) && ($data{'mailrecip'})) {
755: if (&unescape($data{'mailrecip'}) eq "$data{'username'}:$data{'domain'}") {
756: $form{'display'} = &unescape($data{'display'});
757: }
758: }
759: }
1.14 raeburn 760: }
1.25.2.5.2.9 raeburn 761: my $skipcritical;
762: if ($data{'lti.login'}) {
763: if ((($data{'lti.reqcrs'}) &&
764: ($data{'lti.reqrole'} eq 'cc')) ||
765: ($data{'lti.selfenrollrole'})) {
766: $skipcritical = 1;
767: }
768: }
1.11 albertel 769: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.25.2.5.2.9 raeburn 770: $home,$desturl,$extra_env,\%form,$skipcritical,'',
1.25.2.5.2.7 raeburn 771: $expirepub,$data{'lcssowin'});
1.11 albertel 772: }
1.1 albertel 773: return OK;
774: }
1.6 albertel 775:
1.25.2.5.2.9 raeburn 776: if ($data{'lti.login'}) {
777: my $handle = &Apache::lonnet::check_for_valid_session($r);
778: if ($handle) {
779: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
780: $handle);
781: my $needslogout;
782: if ($env{'request.lti.login'}) {
783: if (($env{'user.name'} ne $data{'username'}) ||
784: ($env{'user.domain'} ne $data{'domain'})) {
785: $needslogout = 1;
786: }
787: } else {
788: $needslogout = 1;
789: }
790: if ($needslogout) {
791: &logout($r,$ip,$handle,\%data,$lti_env);
792: return OK;
793: }
794: }
1.15 raeburn 795: }
1.25.2.5.2.9 raeburn 796:
797: # check if current user, and role match those expected.
798: my ($next_url,$reuse_session);
799: if (($env{'user.name'} eq $data{'username'}) && ($env{'user.domain'} eq $data{'domain'}) &&
800: ($env{'request.role'} eq $data{'role'}) && ($data{'origurl'} ne '')) {
801: $next_url = $data{'origurl'};
802: $reuse_session = 1;
803: } elsif ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
804: $next_url=$data{'origurl'};
805: } else {
806: if ($data{'lti.login'}) {
807: $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
808: if ($data{'origurl'} ne '') {
809: $next_url .= '&orgurl='.&escape($data{'origurl'});
810: }
1.25.2.5.2.1 raeburn 811: } else {
1.25.2.5.2.9 raeburn 812: $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
813: if ($data{'origurl'} ne '') {
814: $next_url .= '&orgurl='.&escape($data{'origurl'});
815: }
1.25.2.5.2.1 raeburn 816: }
1.25.2.5.2.9 raeburn 817: }
818: if ($data{'lti.login'}) {
819: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
820: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
821: if ($reuse_session) {
822: $next_url .= '&navmap=1';
823: } else {
824: $next_url .= '&navmap=1';
1.25.2.5.2.6 raeburn 825: }
1.25.2.5.2.9 raeburn 826: }
827: }
828: if ($reuse_session) {
829: $r->internal_redirect($next_url);
830: } else {
831: if ($data{'deeplink.login'}) {
832: if (ref($extra_env) eq 'HASH') {
833: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
834: } else {
835: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
836: }
837: if ($data{'linkprot'}) {
838: $extra_env->{'request.linkprot'} = $data{'linkprot'};
839: if ($data{'linkprotexit'}) {
840: $extra_env->{'request.linkprotexit'} = $data{'linkprotexit'};
1.25.2.5.2.6 raeburn 841: }
1.25.2.5.2.9 raeburn 842: if ($data{'linkprotpbid'}) {
843: $extra_env->{'request.linkprotpassbid'} = $data{'linkprotpbid'};
844: }
845: if ($data{'linkprotpburl'}) {
846: $extra_env->{'request.linkprotpassburl'} = $data{'linkprotpburl'};
847: }
848: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
849: if ($data{$item}) {
850: $form{'request.'.$item} = $data{$item};
851: }
852: }
853: } elsif ($data{'linkkey'} ne '') {
854: $extra_env->{'request.linkkey'} = $data{'linkkey'};
1.25.2.5.2.6 raeburn 855: }
1.25.2.5.2.1 raeburn 856: }
1.25.2.5.2.9 raeburn 857: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
858: $next_url,$extra_env,\%form,'',$cid,'',$data{'lcssowin'});
1.25.2.5.2.1 raeburn 859: }
1.6 albertel 860: return OK;
1.1 albertel 861: }
862:
863: 1;
864: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>