Apache-AuthCAS
view release on metacpan or search on metacpan
lib/Apache/AuthCAS.pm view on Meta::CPAN
}
# see if this session already exists
my $sth = $dbh->prepare("SELECT id FROM $DB_SESSION_TABLE WHERE id=?;");
$sth->execute($sid);
if ($sth->fetch()) {
Apache->warn("$$: CAS: set_session_data(): found session sid='$sid' to update") unless ($LOG_LEVEL < $LOG_DEBUG);
#print "DEBUG: '$id', '$last_accessed', '$uid', '$pgtiou'\n";
Apache->warn("$$: CAS: set_session_data(): SQL: UPDATE $DB_SESSION_TABLE SET last_accessed='$last_accessed', uid='$uid', pgtiou='$pgtiou' WHERE id='$sid';") unless ($LOG_LEVEL < $LOG_DEBUG);
my $sth = $dbh->prepare("UPDATE $DB_SESSION_TABLE SET last_accessed=?, uid=?, pgtiou=? WHERE id=?;");
$sth->execute($last_accessed, $uid, $pgtiou, $sid);
my $rc = $sth->err;
# if we have an error when updating the session
if ($rc) {
Apache->warn("$$: CAS: set_session_data(): error updating session sid='$sid'") unless ($LOG_LEVEL < $LOG_DEBUG);
$sth->finish();
$dbh->disconnect();
return "";
}
Apache->warn("$$: CAS: set_session_data(): updated session sid='$sid': last_accessed='$last_accessed', uid='$uid', pgtiou='$pgtiou'") unless ($LOG_LEVEL < $LOG_DEBUG);
} else {
Apache->warn("$$: CAS: set_session_data(): creating new session sid='$sid' to update") unless ($LOG_LEVEL < $LOG_DEBUG);
#print "DEBUG2: '$id', '$last_accessed', '$uid', '$pgtiou'\n";
my $sth = $dbh->prepare("INSERT INTO $DB_SESSION_TABLE(id,last_accessed,uid,pgtiou) VALUES(?, ?, ?, ?);");
$sth->execute($sid, $last_accessed, $uid, $pgtiou);
my $rc = $sth->err;
# if we have an error when updating the session
if ($rc) {
$sth->finish();
$dbh->disconnect();
return "";
}
}
$sth->finish();
$dbh->disconnect();
return 1;
}
# takes a session id and returns an array
sub get_session_data($$) {
my $self = shift;
my $sid = shift;
Apache->warn("$$: CAS: get_session_data()") unless ($LOG_LEVEL < $LOG_DEBUG);
# retrieve a session object for this session id
my $dbh = DBI->connect("dbi:$DB_DRIVER:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT", $DB_USER, $DB_PASS, { AutoCommit => 1 });
if (!defined($dbh)) {
Apache->warn("$$: CAS: get_session_data(): db connect error: $DBI::errstr") unless ($LOG_LEVEL < $LOG_ERROR);
return ();
}
my $sth = $dbh->prepare("SELECT last_accessed, uid, pgtiou FROM $DB_SESSION_TABLE WHERE id=?;");
$sth->execute($sid);
my ($last_accessed, $uid, $pgtiou);
$sth->bind_columns(\$last_accessed, \$uid, \$pgtiou);
my $result = $sth->fetch();
$sth->finish();
$dbh->disconnect();
if ($result) {
Apache->warn("$$: CAS: get_session_data(): got session data for sid='$sid': last_accessed='$last_accessed' uid='$uid' pgtiou='$pgtiou'") unless ($LOG_LEVEL < $LOG_DEBUG);
return ($sid, $last_accessed, $uid, $pgtiou);
}
Apache->warn("$$: CAS: get_session_data(): couldn't get session data for sid='$sid'") unless ($LOG_LEVEL < $LOG_DEBUG);
return ();
}
# delete session
sub delete_session_data($$) {
my $self = shift;
my $sid = shift;
Apache->warn("$$: CAS: delete_session_data()") unless ($LOG_LEVEL < $LOG_DEBUG);
# retrieve a session object for this session id
my $dbh = DBI->connect("dbi:$DB_DRIVER:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT", $DB_USER, $DB_PASS, { AutoCommit => 1 });
if (!defined($dbh)) {
Apache->warn("$$: CAS: delete_session_data(): db connect error: $DBI::errstr") unless ($LOG_LEVEL < $LOG_ERROR);
return "";
}
my $sth = $dbh->prepare("DELETE FROM $DB_SESSION_TABLE WHERE id=?");
$sth->execute($sid);
# if we have an error when updating the session
my $rc = $sth->err;
my $count = $sth->rows;
if ($rc) {
Apache->warn("$$: CAS: delete_session_data(): error deleting session mapping for sid='$sid'") unless ($LOG_LEVEL < $LOG_DEBUG);
$sth->finish();
$dbh->disconnect();
return "";
}
Apache->warn("$$: CAS: delete_session_data(): deleted '$count' session mappings for sid='$sid'") unless ($LOG_LEVEL < $LOG_DEBUG);
$sth->finish();
$dbh->disconnect();
return 1;
}
# delete expired sessions
sub delete_expired_sessions($$) {
my $self = shift;
Apache->warn("$$: CAS: delete_expired_sessions()") unless ($LOG_LEVEL < $LOG_DEBUG);
my $oldest_valid_time = time() - $SESSION_TIMEOUT;
Apache->warn("$$: CAS: delete_expired_sessions(): deleting sessions older than '$oldest_valid_time'") unless ($LOG_LEVEL < $LOG_DEBUG);
# retrieve a session object for this session id
my $dbh = DBI->connect("dbi:$DB_DRIVER:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT", $DB_USER, $DB_PASS, { AutoCommit => 1 });
if (!defined($dbh)) {
Apache->warn("$$: CAS: delete_expired_sessions(): db connect error: $DBI::errstr") unless ($LOG_LEVEL < $LOG_ERROR);
return "";
}
lib/Apache/AuthCAS.pm view on Meta::CPAN
Apache->warn("$$: CAS: set_pgt()") unless ($LOG_LEVEL < $LOG_DEBUG);
my $dbh = DBI->connect("dbi:$DB_DRIVER:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT", $DB_USER, $DB_PASS, { AutoCommit => 1 });
if (!defined($dbh)) {
Apache->warn("$$: CAS: set_pgt(): db connect error: $DBI::errstr") unless ($LOG_LEVEL < $LOG_ERROR);
return "";
}
# see if this pgt already exists
my $sth = $dbh->prepare("SELECT pgt FROM $DB_PGTIOU_TABLE WHERE pgtiou=?;");
$sth->execute($pgtiou);
my $count = $sth->rows;
if ($sth->fetch()) {
# we shouldn't already have this!
$sth->finish();
$dbh->disconnect();
return "";
} else {
Apache->warn("$$: CAS: set_pgt(): adding pgtiou/pgt map for pgtiou='$pgtiou' pgt='$pgt'") unless ($LOG_LEVEL < $LOG_DEBUG);
#print "DEBUG2: '$pgtiou', '$pgt'\n";
my $created = time();
my $sth = $dbh->prepare("INSERT INTO $DB_PGTIOU_TABLE values(?, ?, ?);");
$sth->execute($pgtiou, $pgt, $created);
my $rc = $sth->err;
# if we have an error when updating the session
if ($rc) {
$sth->finish();
$dbh->disconnect();
return "";
}
}
Apache->warn("$$: CAS: set_pgt(): updated '$count' pgtiou/pgt map") unless ($LOG_LEVEL < $LOG_DEBUG);
$sth->finish();
$dbh->disconnect();
return 1;
}
# takes a pgtiou and returns a pgt
sub get_pgt($$) {
my $self = shift;
my $pgtiou = shift;
my $sid = shift || "";
Apache->warn("$$: CAS: get_pgt(): getting pgtiou/pgt map for pgtiou='$pgtiou'") unless ($LOG_LEVEL < $LOG_DEBUG);
# retrieve a pgt for this pgtiou
my $dbh = DBI->connect("dbi:$DB_DRIVER:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT", $DB_USER, $DB_PASS, { AutoCommit => 1 });
if (!defined($dbh)) {
Apache->warn("$$: CAS: get_pgt(): db connect error: $DBI::errstr") unless ($LOG_LEVEL < $LOG_ERROR);
return "";
}
my $sth = $dbh->prepare("SELECT pgt FROM $DB_PGTIOU_TABLE WHERE pgtiou=?;");
$sth->execute($pgtiou);
my $pgt;
$sth->bind_col(1, \$pgt);
my $result = $sth->fetch();
$sth->finish();
$dbh->disconnect();
if ($result) {
Apache->warn("$$: CAS: get_pgt(): got pgtiou/pgt map pgtiou='$pgtiou' pgt='$pgt'") unless ($LOG_LEVEL < $LOG_DEBUG);
return $pgt;
}
Apache->warn("$$: CAS: get_pgt(): coudln't get pgtiou/pgt map pgtiou='$pgtiou'") unless ($LOG_LEVEL < $LOG_DEBUG);
return "";
}
# deletes a pgt/pgtiou mapping
sub delete_pgt($$) {
my $self = shift;
my $pgtiou = shift;
Apache->warn("$$: CAS: delete_pgt()") unless ($LOG_LEVEL < $LOG_DEBUG);
# retrieve a session object for this session id
my $dbh = DBI->connect("dbi:$DB_DRIVER:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT", $DB_USER, $DB_PASS, { AutoCommit => 1 });
if (!defined($dbh)) {
Apache->warn("$$: CAS: delete_pgt(): db connect error: $DBI::errstr") unless ($LOG_LEVEL < $LOG_ERROR);
return "";
}
my $sth = $dbh->prepare("DELETE FROM $DB_PGTIOU_TABLE WHERE pgtiou=?;");
$sth->execute($pgtiou);
# if we have an error when updating the session
my $rc = $sth->err;
my $count = $sth->rows;
if ($rc) {
Apache->warn("$$: CAS: delete_pgt(): error deleting pgtiou/pgt mapping for pgtiou='$pgtiou'") unless ($LOG_LEVEL < $LOG_DEBUG);
$sth->finish();
$dbh->disconnect();
return "";
}
Apache->warn("$$: CAS: delete_pgt(): deleted '$count' pgtiou/pgt mappings for pgtiou='$pgtiou'") unless ($LOG_LEVEL < $LOG_DEBUG);
$sth->finish();
$dbh->disconnect();
return 1;
}
# delete pgts that have no session associated with 'em and are old
sub delete_expired_pgts($$) {
my $self = shift;
Apache->warn("$$: CAS: delete_expired_pgts()") unless ($LOG_LEVEL < $LOG_DEBUG);
# retrieve a session object for this session id
my $dbh = DBI->connect("dbi:$DB_DRIVER:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT", $DB_USER, $DB_PASS, { AutoCommit => 1 });
if (!defined($dbh)) {
Apache->warn("$$: CAS: delete_expired_pgts(): db connect error: $DBI::errstr") unless ($LOG_LEVEL < $LOG_ERROR);
return "";
}
my $oldest_valid_time = time() - $SESSION_TIMEOUT;
my $sth = $dbh->prepare("DELETE FROM $DB_PGTIOU_TABLE WHERE pgtiou NOT IN (SELECT pgtiou FROM $DB_SESSION_TABLE) AND created < ?;");
( run in 0.896 second using v1.01-cache-2.11-cpan-2398b32b56e )