File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.5: download - view: text, annotated - select for diffs
Tue Feb 29 16:24:00 2000 UTC (24 years, 3 months ago) by www
Branches: MAIN
CVS tags: HEAD
Authentication changes - incorporated into environment

    1: # The LearningOnline Network
    2: # User Authentication Module
    3: # 5/21/99,5/22,5/25,5/26,5/27,5/29,6/2,6/11,6/14,6/15
    4: # 16/11,12/16,
    5: # 1/14,2/24,2/28,2/29 Gerd Kortemeyer
    6: 
    7: package Apache::lonauth;
    8: 
    9: use Apache::Constants qw(:common);
   10: use Apache::File;
   11: use CGI qw(:standard);
   12: use CGI::Cookie();
   13: use Apache::lonnet();
   14: 
   15: # ------------------------------------------------------------ Successful login
   16: 
   17: sub success {
   18:     my ($r, $username, $domain, $authhost) = @_;
   19:     my $lonids=$r->dir_config('lonIDsDir');
   20: 
   21: # See if old ID present, if so, remove
   22:     my $cookie;
   23:     while ($cookie=<$lonids/$username\_*\_$domain\_$authhost.id>) {
   24: 	unlink($cookie);
   25:     }
   26: 
   27: # Give them a new cookie
   28: 
   29:     my $now=time;
   30:     $cookie="$username\_$now\_$domain\_$authhost";
   31: 
   32: # Initialize roles
   33: 
   34:     my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
   35: 
   36: # Write first profile
   37: 
   38:        {
   39: 	    my $idf=Apache::File->new(">$lonids/$cookie.id");
   40:             print $idf "user.name=$username\n";
   41:             print $idf "user.domain=$domain\n";
   42:             print $idf "user.home=$authhost\n";
   43:             if ($userroles ne '') { print $idf "$userroles" };
   44:         }
   45: 
   46: # ------------------------------------------------------------ Get cookie ready
   47: 
   48:     $cookie="lonID=$cookie; path=/";
   49: 
   50: # ------------------------------------------------- Output for successful login
   51: 
   52:     $r->send_cgi_header(<<ENDHEADER);
   53: Content-type: text/html
   54: Set-cookie: $cookie
   55: 
   56: ENDHEADER
   57:     $r->print(<<ENDSUCCESS);
   58: <html>
   59: <head>
   60: <title>Successful Login to the LearningOnline Network with CAPA</title>
   61: </head>
   62: <frameset rows="80,*" border=0>
   63: <frame scrolling="no" name="loncontrol" src="/adm/menu">
   64: <frame name="loncontent" src="/adm/roles">
   65: </frameset>
   66: </html>
   67: ENDSUCCESS
   68: }
   69: 
   70: # --------------------------------------------------------------- Failed login!
   71: 
   72: sub failed {
   73:     my ($r,$message) = @_;
   74:     $r->send_cgi_header(<<ENDFHEADER);
   75: Content-type: text/html
   76: 
   77: ENDFHEADER
   78:     $r->print(<<ENDFAILED);
   79: <html>
   80: <head>
   81: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
   82: </head>
   83: <html>
   84: <body bgcolor="#FFFFFF">
   85: <h1>Sorry ...</h1>
   86: <h2>$message to use the Learning<i>Online</i> Network with CAPA</h2>
   87: </body>
   88: </html>
   89: ENDFAILED
   90: }
   91: 
   92: # ---------------------------------------------------------------- Main handler
   93: 
   94: sub handler {
   95:     my $r = shift;
   96: 
   97:     my $buffer;
   98:     $r->read($buffer,$r->header_in('Content-length'));
   99:     my @pairs=split(/&/,$buffer);
  100:     my $pair; my $name; my $value; my %FORM;
  101:     foreach $pair (@pairs) {
  102:        ($name,$value) = split(/=/,$pair);
  103:        $FORM{$name}=$value;
  104:     } 
  105: 
  106:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
  107: 	failed($r,'Username, password and domain need to be specified');
  108:         return OK;
  109:     }
  110:     $FORM{'uname'} =~ s/\W//g;
  111:     $FORM{'upass'} =~ s/\W//g;
  112:     $FORM{'udom'}  =~ s/\W//g;
  113: 
  114:     my $role   = $r->dir_config('lonRole');
  115:     my $domain = $r->dir_config('lonDefDomain');
  116:     my $prodir = $r->dir_config('lonUsersDir');
  117: 
  118: # ---------------------------------------------------------------- Authenticate
  119:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
  120:                                               $FORM{'upass'},
  121:                                               $FORM{'udom'});
  122:     
  123: # --------------------------------------------------------------------- Failed?
  124: 
  125:     if ($authhost eq 'no_host') {
  126: 	failed($r,'Username and/or password could not be authenticated');
  127:         return OK;
  128:     }
  129: 
  130:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  131:     my $lonurl=$cookies{'lonURL'};
  132:     if (!$lonurl) { failed($r,'Cookies need to be activated'); return OK; }
  133:     my $lowerurl=$lonurl->value;
  134: 
  135:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost);
  136:     return OK;
  137: }
  138: 
  139: 1;
  140: __END__

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