Annotation of loncom/auth/lonauth.pm, revision 1.5

1.1       albertel    1: # The LearningOnline Network
                      2: # User Authentication Module
1.2       www         3: # 5/21/99,5/22,5/25,5/26,5/27,5/29,6/2,6/11,6/14,6/15
1.4       www         4: # 16/11,12/16,
1.5     ! www         5: # 1/14,2/24,2/28,2/29 Gerd Kortemeyer
1.1       albertel    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 {
1.4       www        18:     my ($r, $username, $domain, $authhost) = @_;
1.1       albertel   19:     my $lonids=$r->dir_config('lonIDsDir');
1.4       www        20: 
                     21: # See if old ID present, if so, remove
1.1       albertel   22:     my $cookie;
1.4       www        23:     while ($cookie=<$lonids/$username\_*\_$domain\_$authhost.id>) {
                     24: 	unlink($cookie);
                     25:     }
                     26: 
                     27: # Give them a new cookie
                     28: 
1.5     ! www        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:        {
1.1       albertel   39: 	    my $idf=Apache::File->new(">$lonids/$cookie.id");
1.4       www        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" };
1.1       albertel   44:         }
1.4       www        45: 
1.5     ! www        46: # ------------------------------------------------------------ Get cookie ready
        !            47: 
1.1       albertel   48:     $cookie="lonID=$cookie; path=/";
1.5     ! www        49: 
        !            50: # ------------------------------------------------- Output for successful login
        !            51: 
1.1       albertel   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>
1.4       www        60: <title>Successful Login to the LearningOnline Network with CAPA</title>
1.1       albertel   61: </head>
                     62: <frameset rows="80,*" border=0>
                     63: <frame scrolling="no" name="loncontrol" src="/adm/menu">
1.4       www        64: <frame name="loncontent" src="/adm/roles">
1.1       albertel   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>
1.4       www        81: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
1.1       albertel   82: </head>
                     83: <html>
                     84: <body bgcolor="#FFFFFF">
                     85: <h1>Sorry ...</h1>
1.4       www        86: <h2>$message to use the Learning<i>Online</i> Network with CAPA</h2>
1.1       albertel   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: 
1.4       www       135:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost);
1.1       albertel  136:     return OK;
                    137: }
                    138: 
                    139: 1;
                    140: __END__

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