9f7e5b7e72850030282b6401726e5d94dc0d24c2
[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     # FIXME: ensure that this is a POST
29     if (ref($handler) eq "CODE") {
30       printf STDERR ("Got POST %s\n", $path);
31       my ($status, $object) = $handler->($cgi);
32       printf("HTTP/1.0 %s\r\nContent-Type: application/json\r\n\r\n%s", $status, to_json ($object));
33
34     } else {
35       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) );
36     }
37   }
38
39   sub reviewcert {
40     my $cgi  = shift;   # CGI.pm object
41     return if !ref $cgi;
42
43     # FIXME: these should be opening up a json blob instead of using CGI params.
44     my $data = from_json($cgi->param('POSTDATA'));
45
46     use Data::Dumper;
47     print STDERR Dumper($data);
48
49     my $ret = { valid => 'true' };
50     # my $status = '404 no match found for the public key in this certificate';
51     # or:
52     my $status = '200 match found, authentication details to follow';
53
54     return $status, $ret;
55   }
56
57   sub extracerts {
58     my $cgi = shift;
59
60     return '500 not yet implemented', { };
61   }
62 }
63
64 # start the server on port 8091
65 my $server = MSVA->new(8901);
66 $server->run();