File:  [LON-CAPA] / loncom / auth / migrateuser.pm
Revision 1.26: download - view: text, annotated - select for diffs
Thu Nov 30 15:14:51 2017 UTC (6 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754 LTI Integration: LON-CAPA as LTI Provider
  - Work in progress

    1: # The LearningOnline Network
    2: # Starts a user off based of an existing token.
    3: #
    4: # $Id: migrateuser.pm,v 1.26 2017/11/30 15:14:51 raeburn Exp $
    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::migrateuser;
   30: 
   31: use strict;
   32: use LONCAPA qw(:DEFAULT :match);
   33: use Apache::Constants qw(:common :http :methods);
   34: use Apache::lonauth;
   35: use Apache::lonnet;
   36: use Apache::lonlocal;
   37: use Apache::lonlogin();
   38: 
   39: sub goto_login {
   40:     my ($r,$domain) = @_;
   41:     &Apache::loncommon::content_type($r,'text/html');
   42:     $r->send_http_header;
   43:     my $url = '/adm/login';
   44:     if ($domain) {
   45:         $url .= '?domain='.$domain;
   46:     }
   47:     $r->print(&Apache::loncommon::start_page('Going to login',undef,
   48: 					     {'redirect' => [0,$url],}).
   49: 	      '<h1>'.&mt('One moment please...').'</h1>'.
   50: 	      '<p>'.&mt('Transferring to login page.').'</p>'.
   51: 	      &Apache::loncommon::end_page());
   52:     return OK;
   53: }
   54: 
   55: 
   56: sub sso_lti_check {
   57:     my ($data) = @_;
   58:     my %extra_env;
   59:     if (ref($data) eq 'HASH') {
   60:         if ($data->{'sso.login'}) {
   61:             $extra_env{'request.sso.login'} = $data->{'sso.login'};
   62:         }
   63:         if ($data->{'sso.reloginserver'}) {
   64:             $extra_env{'request.sso.reloginserver'} = 
   65:                 $data->{'sso.reloginserver'};
   66:         }
   67:         if ($data->{'lti.login'}) {
   68:             $extra_env{'request.lti.login'} = $data->{'lti.login'};
   69:         }
   70:         if ($data->{'lti.passbackid'}) {
   71:             $extra_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
   72:         }
   73:         if ($data->{'lti.passbackurl'}) {
   74:             $extra_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
   75:         }
   76:         if ($data->{'lti.rosterid'}) {
   77:             $extra_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
   78:         }
   79:         if ($data->{'lti.rosterurl'}) {
   80:             $extra_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
   81:         }
   82:     }
   83:     return \%extra_env;
   84: }
   85: 
   86: sub ip_changed {
   87:     my ($r,$udom,$camefrom,$dataref) = @_;
   88:     &Apache::loncommon::content_type($r,'text/html');
   89:     $r->send_http_header;
   90:     if (ref($dataref) eq 'HASH') {
   91:         my $title = 'LON-CAPA Session redirected';
   92:         my $message = &mt('Your internet address has changed since you logged in.');  
   93:         my $rule_in_effect;
   94:         if ($dataref->{'balancer'}) {
   95:             my $baldom = &Apache::lonnet::host_domain($camefrom);
   96:             my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
   97:             my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
   98:             my $uprimaryid = &Apache::lonnet::domain($udom,'primary'); 
   99:             my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
  100:             my $dom_in_use;
  101:             if (($uintdom ne '') && ($uintdom eq $balintdom)) {
  102:                 $dom_in_use = $udom;
  103:             } else {
  104:                 $dom_in_use = $baldom;
  105:             }
  106:             my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
  107:             unless (defined($cached)) {
  108:                 my $cachetime = 60*60*24; 
  109:                 my %domconfig =
  110:                     &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
  111:                 if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  112:                     $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
  113:                                                             $domconfig{'loadbalancing'},$cachetime);
  114:                 }
  115:             }
  116:             if (ref($result) eq 'HASH') {
  117:                 (undef,my $currtargets,my $currrules) =
  118:                     &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
  119:                 if (ref($currrules) eq 'HASH') {
  120:                     if ($dataref->{'sso.login'}) {
  121:                         if ($currrules->{'_LC_ipchangesso'} ne '') {
  122:                             $rule_in_effect = $currrules->{'_LC_ipchangesso'};
  123:                         }
  124:                     } else {
  125:                         if ($currrules->{'_LC_ipchange'} ne '') {
  126:                             $rule_in_effect = $currrules->{'_LC_ipchange'};
  127:                         }
  128:                     }
  129:                 }
  130:             }
  131:         }
  132:         my $url;
  133:         my $lonhost= $r->dir_config('lonHostID');
  134:         my $switchto = $lonhost;
  135:         if ($rule_in_effect ne 'offloadedto') {
  136:             my $hosthere;
  137:             my @ids=&Apache::lonnet::current_machine_ids();
  138:             unless ($rule_in_effect eq 'balancer') {
  139:                 if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
  140:                     $hosthere = 1;
  141:                 }
  142:             }
  143:             unless ($hosthere) {
  144:                 if ($dataref->{'role'}) {
  145:                     my ($adom,$aname);
  146:                     if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
  147:                         $adom = $1;
  148:                         $aname = $dataref->{'username'};
  149:                     } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
  150:                         $adom = $1;
  151:                         $aname = $2;
  152:                     }
  153:                     if ($adom ne '' && $aname ne '') {
  154:                         my $ahome = &Apache::lonnet::homeserver($aname,$adom);
  155:                         unless ($ahome eq 'no_host') {
  156:                             if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
  157:                                 $hosthere = 1;
  158:                             }
  159:                         }
  160:                     }
  161:                 }
  162:             }
  163:             unless ($hosthere) {
  164:                 my $hostname;
  165:                 if ($rule_in_effect eq 'balancer') {
  166:                     $hostname = &Apache::lonnet::hostname($dataref->{'server'});
  167:                     if ($hostname) {
  168:                         $switchto = $dataref->{'server'};
  169:                     }
  170:                 } else {
  171:                     $hostname = &Apache::lonnet::hostname($rule_in_effect);
  172:                     if ($hostname) {
  173:                         $switchto = $rule_in_effect;
  174:                     }
  175:                 }
  176:                 if ($hostname) {
  177:                     my $protocol = $Apache::lonnet::protocol{$switchto};
  178:                     $protocol = 'http' if ($protocol ne 'https');
  179:                     $url = $protocol.'://'.$hostname;
  180:                     if ($rule_in_effect eq 'balancer') {
  181:                         $message .= '<br />'.
  182:                                     &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
  183:                     } else {
  184:                         $message .= '<br />'.
  185:                                     &mt('As a result, your LON-CAPA session is being redirected.');
  186:                     }
  187:                 }
  188:             }
  189:         }
  190:         if ($dataref->{'sso.login'}) {
  191:             $url .= '/adm/roles';
  192:         } else {
  193:             $url .= '/adm/login';
  194:             if ($udom) {
  195:                 $url .= '?domain='.$udom;
  196:             }
  197:             $message .= '<br />'.&mt('You will need to provide your password one more time.');
  198:         }
  199:         my %info= (
  200:                     'domain'          => $udom,
  201:                     'username'        => $dataref->{'username'},
  202:                     'role'            => $dataref->{'role'},
  203:                     'sessionserver'   => $lonhost,
  204:                   );
  205:         if ($dataref->{'origurl'}) {
  206:             $info{'origurl'} = $dataref->{'origurl'};
  207:         }
  208:         if ($dataref->{'symb'}) {
  209:             $info{'symb'} = $dataref->{'symb'};
  210:         }
  211:         my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
  212:         unless ($iptoken eq 'conlost') {
  213:             $url .= ($url =~ /\?/) ? '&' : '?';
  214:             $url .= 'iptoken='.$iptoken;
  215:         }
  216:         $r->print(&Apache::loncommon::start_page($title,undef,
  217:                                                  {'redirect' =>
  218:                                                   [2,$url],}).
  219:                   '<h1>'.&mt('One moment please...').'</h1>'.
  220:                   '<p class="LC_warning">'.$message.'</p>'.
  221:                   &Apache::loncommon::end_page());
  222:     } else {
  223:         return &goto_login($r);
  224:     }
  225:     return OK;
  226: }
  227: 
  228: sub handler {
  229:     my ($r) = @_;
  230:     
  231:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
  232:     my %data =   &Apache::lonnet::tmpget($env{'form.token'});
  233:     if (keys(%data) == 0) {
  234:         return &goto_login($r);
  235:     }
  236:     my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
  237: 
  238:     &Apache::lonlocal::get_language_handle($r);
  239: 
  240:     if ($delete ne 'ok') {
  241: 	return &goto_login($r);
  242:     }
  243: 
  244:     if (!defined($data{'username'}) || !defined($data{'domain'})) {
  245:         return &goto_login($r);
  246:     }
  247:     if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
  248:         &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
  249:                  $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
  250: 	return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
  251:     }
  252: 
  253:     &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
  254:     my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
  255:     my $udom;
  256:     if (&Apache::lonnet::domain($data{'domain'})) {
  257:         $udom=$data{'domain'};
  258:     }
  259:     if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom); }
  260: 
  261:     my $extra_env = &sso_lti_check(\%data);
  262: 
  263:     my %form;
  264:     if ($data{'symb'} ne '') {
  265:         $form{'symb'} = $data{'symb'};
  266:     }
  267:     if ($data{'iptoken'} ne '') {
  268:         $form{'iptoken'} = $data{'iptoken'};
  269:     }
  270:     if ($data{'noloadbalance'} ne '') {
  271:         $form{'noloadbalance'} = $data{'noloadbalance'};
  272:     }
  273: 
  274:     if (!$data{'role'}) {
  275: 	my $handle = &Apache::lonnet::check_for_valid_session($r);
  276: 	if ($handle) {
  277: 	    &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
  278: 						     $handle);
  279:             if ($data{'origurl'} ne '') {
  280:                 $r->internal_redirect($data{'origurl'});
  281:             } elsif ($env{'request.course.id'}) {
  282:                 $r->internal_redirect('/adm/navmaps');
  283: 	    } else {
  284: 		$r->internal_redirect('/adm/roles');
  285: 	    }
  286: 	} else {
  287:             my $desturl = '/adm/roles';
  288:             if ($data{'origurl'} ne '') {
  289:                 $desturl = $data{'origurl'};
  290:             }
  291: 	    &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
  292: 				      $home,$desturl,$extra_env,\%form);
  293: 
  294: 	}
  295: 	return OK;
  296: 
  297:     }
  298: 
  299:     my $next_url='/adm/roles?selectrole=1&amp;'.&escape($data{'role'}).'=1';
  300:     if ($data{'origurl'} ne '') {
  301:         $next_url .= '&amp;orgurl='.&escape($data{'origurl'});
  302:     }
  303:     &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
  304: 			      $next_url,$extra_env,\%form);
  305:     return OK;
  306: }
  307: 
  308: 1;
  309: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>