File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.7: download - view: text, annotated - select for diffs
Mon Jun 5 20:28:17 2000 UTC (24 years ago) by www
Branches: MAIN
CVS tags: HEAD
Updates. Implement roles control on browsing and menu.

    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,3/7,5/29,5/30,5/31,6/1,6/5 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,$lowerurl) = @_;
   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: # ------------------------------------ Check browser type and MathML capability
   37: 
   38:     my @browsertype=split(/\&/,$r->dir_config("lonBrowsDet"));
   39:     my %mathcap=split(/\&/,$r->dir_config("lonMathML"));
   40:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
   41:     my $i;
   42:     my $clientbrowser='unknown';
   43:     my $clientversion='0';
   44:     my $clientmathml='';
   45:     for ($i=0;$i<=$#browsertype;$i++) {
   46:         my ($bname,$match,$notmatch,$vreg,$minv)=split(/\:/,$browsertype[$i]);
   47: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
   48: 	    $clientbrowser=$bname;
   49:             $httpbrowser=~/$vreg/i;
   50: 	    $clientversion=$1;
   51:             $clientmathml=($clientversion>=$minv);
   52:         }
   53:     }
   54:     my $clientos='unknown';
   55:     if (($httpbrowser=~/linux/i) ||
   56:         ($httpbrowser=~/unix/i) ||
   57:         ($httpbrowser=~/ux/i) ||
   58:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
   59:     if (($httpbrowser=~/vax/i) ||
   60:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
   61:     if ($httpbrowser=~/next/i) { $clientos='next'; }
   62:     if (($httpbrowser=~/mac/i) ||
   63:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
   64:     if ($httpbrowser=~/win/) { $clientos='win'; }
   65: 
   66: # --------------------------------------------------------- Write first profile
   67: 
   68:        {
   69: 	    my $idf=Apache::File->new(">$lonids/$cookie.id");
   70:             print $idf "user.name=$username\n";
   71:             print $idf "user.domain=$domain\n";
   72:             print $idf "user.home=$authhost\n";
   73:             print $idf "browser.type=$clientbrowser\n";
   74:             print $idf "browser.version=$clientversion\n";
   75:             print $idf "browser.mathml=$clientmathml\n";
   76:             print $idf "browser.os=$clientos\n";
   77:             if ($userroles ne '') { print $idf "$userroles" };
   78:         }
   79: # -------------------------------------------------------------------- Log this
   80: 
   81:     &Apache::lonnet::log($domain,$username,$authhost,
   82:                          "Login $ENV{'REMOTE_ADDR'}");
   83: 
   84: # ------------------------------------------------------------ Get cookie ready
   85: 
   86:     $cookie="lonID=$cookie; path=/";
   87: 
   88: # ------------------------------------------------- Output for successful login
   89: 
   90:     $r->send_cgi_header(<<ENDHEADER);
   91: Content-type: text/html
   92: Set-cookie: $cookie
   93: 
   94: ENDHEADER
   95:     $r->print(<<ENDSUCCESS);
   96: <html>
   97: <head>
   98: <title>Successful Login to the LearningOnline Network with CAPA</title>
   99: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$lowerurl">
  100: </head>
  101: <body bgcolor="#FFFFFF">
  102: <script>
  103: menu=window.open("/res/adm/pages/menu.html","LONCAPAmenu",
  104:                  "height=350,width=150,scrollbars=no,menubar=no");
  105: </script>
  106: <h1>Welcome!</h1>
  107: </body>
  108: </html>
  109: ENDSUCCESS
  110: }
  111: 
  112: # --------------------------------------------------------------- Failed login!
  113: 
  114: sub failed {
  115:     my ($r,$message) = @_;
  116:     $r->send_cgi_header(<<ENDFHEADER);
  117: Content-type: text/html
  118: 
  119: ENDFHEADER
  120:     $r->print(<<ENDFAILED);
  121: <html>
  122: <head>
  123: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
  124: </head>
  125: <html>
  126: <body bgcolor="#FFFFFF">
  127: <h1>Sorry ...</h1>
  128: <h2>$message to use the Learning<i>Online</i> Network with CAPA</h2>
  129: </body>
  130: </html>
  131: ENDFAILED
  132: }
  133: 
  134: # ---------------------------------------------------------------- Main handler
  135: 
  136: sub handler {
  137:     my $r = shift;
  138: 
  139:     my $buffer;
  140:     $r->read($buffer,$r->header_in('Content-length'));
  141:     my @pairs=split(/&/,$buffer);
  142:     my $pair; my $name; my $value; my %FORM;
  143:     foreach $pair (@pairs) {
  144:        ($name,$value) = split(/=/,$pair);
  145:        $value =~ tr/+/ /;
  146:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  147:        $FORM{$name}=$value;
  148:     } 
  149: 
  150:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
  151: 	failed($r,'Username, password and domain need to be specified');
  152:         return OK;
  153:     }
  154:     $FORM{'uname'} =~ s/\W//g;
  155:     $FORM{'udom'}  =~ s/\W//g;
  156: 
  157:     my $role   = $r->dir_config('lonRole');
  158:     my $domain = $r->dir_config('lonDefDomain');
  159:     my $prodir = $r->dir_config('lonUsersDir');
  160: 
  161: # ---------------------------------------------------------------- Authenticate
  162:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
  163:                                               $FORM{'upass'},
  164:                                               $FORM{'udom'});
  165:     
  166: # --------------------------------------------------------------------- Failed?
  167: 
  168:     if ($authhost eq 'no_host') {
  169: 	failed($r,'Username and/or password could not be authenticated');
  170:         return OK;
  171:     }
  172: 
  173:     if ($FORM{'firsturl'} eq '') {
  174: 	$FORM{'firsturl'}='/res/index.html';
  175:     }
  176: 
  177:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$FORM{'firsturl'});
  178:     return OK;
  179: }
  180: 
  181: 1;
  182: __END__
  183: 
  184: 

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