[LON-CAPA-cvs] cvs: modules /purdue purdueauth.pm

albertel lon-capa-cvs-allow@mail.lon-capa.org
Sat, 18 Aug 2007 00:38:10 -0000


albertel		Fri Aug 17 20:38:10 2007 EDT

  Added files:                 
    /modules/purdue	purdueauth.pm 
  Log:
  - adding in sso module for purdue
  
  

Index: modules/purdue/purdueauth.pm
+++ modules/purdue/purdueauth.pm
# The LearningOnline Network with CAPA
# Handler to integrate with my.sfu.ca
#

package Apache::purdueauth;

use strict;
use CGI::Cookie();
use Apache::Constants qw(:common :http :methods);
use Apache::lonauth();
use Apache::lonnet;
use Apache::loncommon();
use Apache::lonlocal;
use LWP::UserAgent;

use LONCAPA;

my $login_url = "https://www.itap.purdue.edu/tlt/loncapa/";
my $post_url  = "https://www.itap.purdue.edu/tlt/loncapa/verify.cfm";

sub goto_login_page {
    my ($r,$error) = @_;


    &Apache::lonlocal::get_language_handle($r);
    &Apache::loncommon::content_type($r,'text/html');
    my $meta = '<meta http-equiv="refresh" content="5;URL='.$login_url.'" />';

    my $start_page = 
	&Apache::loncommon::start_page('Switching to '.$login_url,$meta,
				       { domain           => 'purdue',
				         no_auto_mt_title => 1,
				         no_inline_link   => 1,
				     });
    my $end_page = 
	&Apache::loncommon::end_page();
    $r->send_http_header();
    if (!$error) {
	$error = "You must go to $login_url first to login.";
    }
    $r->print(<<PAGE);
$start_page
     <h1>One moment please...</h1>
     <p>$error</p>

     <p>
         You are being redirected, if that fails please please click here:
         <a href="$login_url">$login_url</a>
     </p>

     <hr />
$end_page
PAGE

    return;
}

sub unknown_userid {
    my ($r,$uname) = @_;
    &Apache::lonlocal::get_language_handle($r);
    &Apache::loncommon::content_type($r,'text/html');
    my $start_page = 
	&Apache::loncommon::start_page('Invalid userid',undef,
				       { domain           => 'purdue',
				         no_auto_mt_title => 1,
				         no_inline_link   => 1, });
    my $end_page = 
	&Apache::loncommon::end_page();
    $r->send_http_header();
    $r->print(<<PAGE);
$start_page
     <h1>Unknown userid '$uname' </h1>
     <p>The requested userid ($uname) is unknown to LON-CAPA</p>

     <p>
         Please login a different userid
         <a href="$login_url">$login_url</a>
     </p>

     <hr />
$end_page
PAGE

    return;
}

sub check_token {
    my ($token) = @_;
    my %form = ('token' => $token,
		'ip'    => $ENV{'REMOTE_ADDR'},);

    my $ua=new LWP::UserAgent;
    my $response = $ua->post($post_url,\%form);

    if ($response->is_error()) {
	return (undef,"Unable to verify identity, received an error of ".
		$response->code.$response->content);
    }

    my $username = &parse_response($response);
    if (defined($username)) {
	return ($username);
    }
    return (undef,"Unable to verify identity.");
}

sub parse_response {
    my ($response) = @_;
    my $location = $response->header('Location');
    my ($userid) = 
	($location =~ m{http://educog\.com/adm/purdue-auth\?userid=([^&]*)&});
    return $userid;
}

sub handler {
    my ($r) = @_;
    
    my $q = new CGI;
    my $token = $q->param('token');
    if ($token eq '') {
	&goto_login_page($r);
	return OK;
    }

    my ($uname, $error) = &check_token($token);
    if ($error) {
	#error or invalid token
	&goto_login_page($r,$error);
    } else {
	my $udom = 'purdue';
	my $home=&Apache::lonnet::homeserver($uname,$udom);
	if ($home ne 'no_host') {
	    my %extra_env = ( 'request.sso.login'     => 1,
			      'request.sso.norelogin' => 1,);
			   
	    &Apache::lonauth::success($r,$uname,$udom,$home,'/adm/roles',
				      \%extra_env);
	} else {
	    &unknown_userid($r,$uname);
	}
    }
    return OK;
}

1;
__END__