Subject: | Support for HTTP authentication |
Hello, I'd like to be able to use HTTP authentication to filter
authorized from unauthorized JSON-RPC requests.
Please find attached a patch that add this feature to JSONRPC::Server.
If it seems OK to you, please apply it on CPAN.
Subject: | http_auth.patch |
--- booya/lib/JSON/RPC/Server.pm 2008-02-25 02:48:24.000000000 +0100
+++ JSON-RPC-0.96/lib/JSON/RPC/Server.pm 2012-06-18 11:02:54.781281293 +0200
@@ -30,7 +30,7 @@
BEGIN {
for my $method (qw/request path_info json version error_message max_length charset content_type
- error_response_header return_die_message/)
+ error_response_header return_die_message auth_callback auth_realm/)
{
eval qq|
sub $method {
@@ -55,6 +55,7 @@
charset => 'UTF-8',
content_type => 'application/json',
json => $class->create_json_coder,
+ auth_realm => "JSONRPCRealm",
loaded_module => { name => {}, order => [], },
@_,
}, $class;
@@ -103,6 +104,18 @@
my ($self) = @_;
my ($obj, $res, $jsondata);
+ if (defined($self->{auth_callback}) && !$self->request->header('Authorization')) {
+ return $self->response(HTTP::Response->new(
+ 401 => 'Authorization Required',
+ [ 'WWW-Authenticate' => 'Basic realm="'.$self->{auth_realm}.'"' ],
+ ));
+ } elsif (defined($self->{auth_callback})) {
+ my ($login, $password) = $self->request->authorization_basic();
+ unless (&{$self->{auth_callback}}($login,$password)) {
+ return $self->response($self->response_header(403, 'Forbidden'));
+ }
+ }
+
if ($self->request->method eq 'POST') {
$jsondata = $self->retrieve_json_from_post();
}
@@ -598,6 +611,23 @@
Default is 'application/json'.
+=item auth_callback
+
+Setter/Getter to function to call for HTTP authentication.
+Default is C<undef> , meaning no authentication required.
+
+$server->auth_callback(sub {
+ my ($login, $password) = @_;
+ return (($login=="admin") && ($password=="secret"));
+});
+
+
+=item auth_realm
+
+Setter/Getter to the name of the realm for HTTP authentication.
+Default is 'JSONRPCRealm'.
+
+
=item return_die_message
When your program dies in your procedure,