785c061ce067efa9a6003ecb72afb58edf8790fb
[monkeysphere-validation-agent.git] / msva
1 #!/usr/bin/perl -wT
2
3 use warnings;
4 use strict;
5
6 {
7   package MSVA;
8
9   use HTTP::Server::Simple::CGI;
10   use base qw(HTTP::Server::Simple::CGI);
11   use warnings;
12   use strict;
13
14   use JSON;
15
16   my %dispatch = (
17                   '/reviewcert' => \&reviewcert,
18                   '/extracerts' => \&extracerts,
19                  );
20
21   sub handle_request {
22     my $self = shift;
23     my $cgi  = shift;
24
25     my $path = $cgi->path_info();
26     my $handler = $dispatch{$path};
27
28     if (ref($handler) eq "CODE") {
29       # FIXME: ensure that this actually is a POST
30       printf STDERR ("Got POST %s\n", $path);
31
32       my ($status, $object) = $handler->($cgi);
33       my $ret = to_json($object);
34       printf STDERR ("returning: %s\n", $ret);
35       printf("HTTP/1.0 %s\r\nContent-Type: application/json\r\n\r\n%s", $status, $ret);
36
37     } else {
38       printf("HTTP/1.0 404 Not Found -- not handled by Monkeysphere validation agent\r\nContent-Type: text/plain\r\n\r\nHTTP/1.0 404 Not Found -- the path:\r\n   %s\r\nis not handled by the MonkeySphere validation agent.\r\nPlease try one of the following paths instead:\r\n\r\n%s\r\n", $path, ' * '.join("\r\n * ", keys %dispatch) );
39     }
40   }
41
42   sub reviewcert {
43     my $cgi  = shift;   # CGI.pm object
44     return if !ref $cgi;
45
46     # open a json blob instead of using CGI params.
47     my $data = from_json($cgi->param('POSTDATA'));
48
49     use Data::Dumper;
50     print STDERR Dumper($data);
51
52     my $uid = $data->{context}.'://'.$data->{uid};
53
54     my $ret = { valid => JSON::true,
55                 message => sprintf('tried to validate "%s" through the OpenPGP Web of Trust', $uid) };
56     my $status = '200 match found, authentication details to follow';
57
58     return $status, $ret;
59   }
60
61   sub extracerts {
62     my $cgi = shift;
63
64     return '500 not yet implemented', { };
65   }
66 }
67
68 # start the server on port 8091
69 my $server = MSVA->new(8901);
70 $server->run();