#!/usr/bin/perl
#
#
# Copyright(c) SECIOSS CORPORATION 2011
#

use strict;
use DBI;
use Config::General;
use Data::Dumper;

our $CONF = '/var/www/cgi-bin/lismapi.conf';

my $config = Config::General->new($CONF);
my %conf = $config->getall;

if (!defined($conf{oauth_dsn}) || !defined($conf{oauth_dbuser}) || !defined($conf{oauth_dbpasswd})) {
    print "No database config\n";
    exit 0;
}

my $db = DBI->connect($conf{oauth_dsn}, $conf{oauth_dbuser}, $conf{oauth_dbpasswd});
if (!$db) {
    print STDERR "Can't connect database\n";
    exit 1;
}

foreach my $table ('auth_codes', 'tokens') {
    my $expires = time() - 3600 * 6;
    my $sql = "delete from $table where expires < $expires";
    my $sth = $db->prepare($sql);
    if (!$sth->execute) {
        print STDERR "Can't clean $table";
        exit 1;
    }
    $sth->finish;
}

exit 0;
