summaryrefslogtreecommitdiff
path: root/authtoken.json
blob: ef279c4eee3b168af98b12faf74e8eff43f3d8db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl

use strict;
use warnings;
use Config::IniFiles;
use DBI;

sub get_token
{
  my $username = shift;

  my $RD_CONF = "/etc/rd.conf";
  my $cfg = Config::IniFiles->new(-file => $RD_CONF)
    or return ('ERROR', "Config File Error: " . join("\n", @Config::IniFiles::errors), '');

  my $dbhost = $cfg->val('mySQL', 'Hostname');
  my $dbname = $cfg->val('mySQL', 'Database');
  my $dbuser = $cfg->val('mySQL', 'Loginname');
  my $dbpasswd = $cfg->val('mySQL', 'Password');

  my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost","$dbuser","$dbpasswd")
    or return ('ERROR', "Database Error: " . $DBI::errstr, '');

  my $sth = $dbh->prepare('select PASSWORD from USERS where LOGIN_NAME = ?')
    or return ('ERROR', "Database Error: " . $dbh->errstr, '');

  $sth->execute($username)
    or return ('ERROR', "Database Error: " . $sth->errstr, '');

  my ($token) = $sth->fetchrow_array;
  return ('OK', 'success', $token) if(defined $token);

  return ('ERROR', "user '" . $username . "' not known by rivendell", '')
}

my $status = 'ERROR';
my $errorstring = 'unknown';
my $username = '';
my $token = '';
if(defined $ENV{REMOTE_USER}) {
  ($status, $errorstring, $token) = get_token($ENV{REMOTE_USER});
  $username = $ENV{REMOTE_USER};
} else {
  $errorstring = 'no username defined - are you logged in?';
}

print "Content-type: application/json\n\n";

print "{\n";
print ' "status": "' . $status . '"'. ",\n";
print ' "errorstring": "' . $errorstring . '"'. ",\n";
print ' "username": "' . $username . '"' . ",\n";
print ' "token": "' . $token . '"' . "\n";
print "}\n";