/
PSGI.pm
364 lines (302 loc) · 8.3 KB
/
PSGI.pm
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package XML::Atom::Server::PSGI;
use strict;
use Digest::SHA1 ();
use MIME::Base64 ();
use Plack::Request;
use Scope::Guard ();
use XML::Atom::Entry;
use XML::Atom::Util ();
use Class::Accessor::Lite
rw => [ qw(callbacks request response xml_parser) ],
;
use constant NS_SOAP => 'http://schemas.xmlsoap.org/soap/envelope/';
use constant NS_WSSE => 'http://schemas.xmlsoap.org/ws/2002/07/secext';
use constant NS_WSU => 'http://schemas.xmlsoap.org/ws/2002/07/utility';
our $VERSION = "0.04";
sub psgi_app {
my $self = shift;
return sub {
$self->handle_psgi(@_);
};
}
# alias
*req = \&request;
*res = \&response;
sub new {
my $klass = shift;
my $self = bless {
(@_ == 1 && ref($_[0]) eq 'HASH' ? %{$_[0]} : @_),
}, $klass;
if (! $self->xml_parser()) {
$self->xml_parser(XML::Atom::Server::PSGI::XMLParser->new);
}
if (! $self->callbacks) {
$self->callbacks({});
}
return $self;
}
sub handle_psgi {
my ($self, $env) = @_;
my $req = Plack::Request->new($env);
my $res = $req->new_response(200);
$res->content_type('application/x.atom+xml');
$self->request($req);
$self->response($res);
# Make sure these things are all cleaned up afterwards
my $guard = Scope::Guard->new(sub {
$self->request(undef);
$self->response(undef);
});
$env->{'xml.atom.server.request_method'} = $req->method;
# Process parameters in path_info
my $path_info = $req->path_info;
my $params = Hash::MultiValue->new;
$path_info =~ s/^\///;
foreach my $arg (split /\//, $path_info) {
my ($k, $v) = split /=/, $arg, 2;
$params->add($k, $v);
}
$env->{'xml.atom.server.request_params'} = $params;
if (my $action = $req->header('SOAPAction')) {
$env->{'xml.atom.server.is_soap'} = 1;
$action =~ s/"//g;
my ($method) = $action =~ m!/([^/]+)$!;
$env->{'xml.atom.server.request_method'} = $method;
}
eval {
$self->_call('handle_request');
if ($self->is_soap) {
my $body = $res->body;
if (defined $body) {
$body =~ s!^(<\?xml.*?\?>)!!;
$body = <<SOAP;
$1
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>$body</soap:Body>
</soap:Envelope>
SOAP
$res->body($body);
}
}
};
if (my $E = $@) {
# Escape
$E =~ s/</</g;
$E =~ s/>/>/g;
$res->code(500);
$res->body(<<EOXML);
<?xml version="1.0" encoding="UTF-8"?>
<error>$E</error>
EOXML
}
return $res->finalize;
}
# for compat
sub request_param {
shift->request_params->get(@_);
}
sub request_params {
return $_[0]->req->env->{'xml.atom.server.request_params'};
}
# for compat
sub request_content {
shift->req->content;
}
# for compat
sub uri {
return $_[0]->req->uri;
}
# for compat
sub request_method {
return $_[0]->req->env->{'xml.atom.server.request_method'};
}
# for compat
sub request_header {
return $_[0]->req->header($_[1]);
}
# for compat
sub response_header {
return shift->res->header(@_);
}
# for compat
sub response_content_type {
return shift->res->content_type(@_);
}
# for compat
sub response_code {
return shift->res->code(@_);
}
# for compat
sub is_soap {
return $_[0]->req->env->{'xml.atom.server.is_soap'};
}
sub _call {
my ($self, $name, @args) = @_;
my $cb = $self->callbacks->{"on_$name"} ||
$self->can($name);
if (! $cb) {
Carp::croak("no callback nor overridden method $name found");
}
return $cb->($self, @args);
}
sub get_auth_info {
my $self = shift;
my $req = $self->req;
my %param; # XXX Hash::MultiValue?
if ($self->is_soap) {
my $xml = $self->xml_body;
my $auth = XML::Atom::Util::first($xml, NS_WSSE, 'UsernameToken');
$param{Username} = XML::Atom::Util::textValue($auth, NS_WSSE, 'Username');
$param{PasswordDigest} = XML::Atom::Util::textValue($auth, NS_WSSE, 'Password');
$param{Nonce} = XML::Atom::Util::textValue($auth, NS_WSSE, 'Nonce');
$param{Created} =
# Using XML::Atom::Client, Created comes with WSU namespace,
# but some the original code in XML::Atom::Server only looks
# at WSSE
XML::Atom::Util::textValue($auth, NS_WSSE, 'Created') ||
XML::Atom::Util::textValue($auth, NS_WSU, 'Created');
} else {
my $wsse = $req->header('X-WSSE');
if (! $wsse) {
$self->auth_failure(401, 'X-WSSE authentication required');
return;
}
$wsse =~ s/^(?:WSSE|UsernameToken) //;
for my $i (split /,\s*/, $wsse) {
my($k, $v) = split /=/, $i, 2;
$v =~ s/^"//;
$v =~ s/"$//;
$param{$k} = $v;
}
}
return \%param;
}
sub authenticate {
my $self = shift;
my $auth = $self->get_auth_info;
if (! $auth) {
return;
}
for my $f (qw( Username PasswordDigest Nonce Created )) {
if (! $auth->{$f}) {
$self->auth_failure(400, "X-WSSE requires $f");
return;
}
}
my $password = $self->_call('password_for_user', $auth->{Username});
if (! defined $password) {
$self->auth_failure(403, 'Invalid login');
return;
}
my $expected = MIME::Base64::encode_base64(
Digest::SHA1::sha1(
MIME::Base64::decode_base64($auth->{Nonce}) .
$auth->{Created} .
$password
),
'');
if ($expected ne $auth->{PasswordDigest}) {
$self->auth_failure(403, 'Invalid login');
return;
}
return 1;
}
sub auth_failure {
my ($self, $code, $reason) = @_;
my $res = $self->res;
$res->header('WWW-Authenticate', 'WSSE profile="UsernameToken"');
$self->error($code, $reason);
}
sub error {
my ($self, $code, $reason) = @_;
my $res = $self->res;
$res->code($code);
# XXX PSGI doesn't really give us a way to override the
# message portion of response status line, so shove it in
# X-Reason header
$res->header('X-Reason', $reason);
}
sub xml_body {
my $self = shift;
my $req = $self->req;
my $env = $req->env;
my $body = $env->{'xml.server.xml_body'};
if (defined $body) {
return $body;
}
$body = $self->xml_parser->parse_string($req->content);
if (defined $body) {
$env->{'xml.server.xml_body'} = $body;
}
return $body;
}
sub atom_body {
my $self = shift;
my $req = $self->req;
my $env = $req->env;
my $atom;
if ($self->is_soap) {
my $xml = $self->xml_body;
$atom = XML::Atom::Entry->new(Doc => XML::Atom::Util::first($xml, NS_SOAP, 'Body'));
} else {
$atom = XML::Atom::Entry->new(Stream => \$req->content);
}
return $atom;
}
package
XML::Atom::Server::PSGI::XMLParser;
use strict;
use Class::Accessor::Lite
new => 1,
rw => [ qw(parser) ]
;
BEGIN {
if (! XML::Atom::LIBXML) {
require XML::XPath;
XML::XPath->import;
}
}
sub parse_string {
my ($self, $string) = @_;
if (XML::Atom::LIBXML) {
my $parser = $self->parser;
if (! $parser) {
$parser = XML::Atom->libxml_parser;
$self->parser($parser);
}
return $parser->parse_string($string);
} else {
return XML::XPath->new(xml => $string);
}
}
1;
__END__
=encoding utf-8
=head1 NAME
XML::Atom::Server::PSGI - XML::Atom::Server for PSGI
=head1 SYNOPSIS
use XML::Atom::Server::PSGI;
my $server = XML::Atom::Server::PSGI->new(
callbacks => {
on_password_for_user => sub { ... }
on_handle_request => sub { ... }
}
);
$server->psgi_app;
package MyServer;
use strict;
use base qw(XML::Atom::Server::PSGI);
sub handle_request {
...
}
1;
MyServer->new->psgi_app;
=head1 DESCRIPTION
XML::Atom::Server::PSGI is a drop in replacement for XML::Atom::Server, which assumes either mod_perl or CGI environment. This module assumes, you guessed it, that you use it from a PSGI compatible app.
=head1 LICENSE
Copyright (C) Daisuke Maki.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
Daisuke Maki E<lt>lestrrat+github@gmail.comE<gt>
=cut