Lite/Server/mod_perl2.pm untested

filter: Statementstmt 0.0% 0 / 287 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 123 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 54 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 587 SCAR 76.8
CC
46
Cov
0%
__service
156.0
__build_request_uri
110.0
new
72.0
CRAP 2162.0
1 package OAuth::Lite::Server::mod_perl2;
2  
3 use strict;
4 use warnings;
5  
6 use Apache2::Connection ();
7 use Apache2::RequestIO ();
8 use Apache2::RequestRec ();
9 use Apache2::RequestUtil ();
10 use Apache2::Response ();
11 use Apache2::URI ();
12 use Apache2::ServerRec ();
13 use URI::Escape ();
14 use Apache2::Const -compile => qw(OK);
15  
16 use List::MoreUtils qw(none any);
17 use bytes ();
18  
19 use OAuth::Lite::Util qw(:all);
20 use OAuth::Lite::ServerUtil;
21 use OAuth::Lite::AuthMethod qw(:all);
22 use OAuth::Lite::Problems qw(:all);
23  
24 use base qw(
25 Class::Accessor::Fast
26 Class::ErrorHandler
27 );
28  
29 use constant PROTECTED_RESOURCE => 'PROTECTED_RESOURCE';
30 use constant REQUEST_TOKEN => 'REQUEST_TOKEN';
31 use constant ACCESS_TOKEN => 'ACCESS_TOKEN';
32  
33 __PACKAGE__->mk_accessors(qw/request realm oauth xrds_location/);
34  
35 =head1 NAME
36  
37 OAuth::Lite::Server::mod_perl2 - mod_perl2 OAuth server
38  
39 =head1 SYNOPSIS
40  
41 Inherit this class, build your service with mod_perl2.
42 For example, write MyServiceWithOAuth.pm
43 And the source-code of L<OAuth::Lite::Server::Test::Echo> is nice example.
44 See it.
45  
46 package MyServiceWithOAuth;
47 use base 'OAuth::Lite::Server::mod_perl2';
48  
49 sub init {
50 my $self = shift;
51 $self->oauth->allow_extra_params(qw/file size/);
52 $self->oauth->support_signature_methods(qw/HMAC-SHA1 PLAINTEXT/);
53 }
54  
55 sub get_request_token_secret {
56 my ($self, $token_string) = @_;
57 my $token = MyDB::Scheme->resultset('RequestToken')->find($token_string);
58 unless ($token
59 && $token->is_authorized_by_user
60 && !$token->is_exchanged_to_access_token
61 && !$token->is_expired) {
62 return $self->error(q{Invalid token});
63 }
64 return $token->secret;
65 }
66  
67 sub get_access_token_secret {
68 my ($self, $token_string, $consumer_key) = @_;
69 my $token = MyDB::Scheme->resultset('AccessToken')->find($token_string);
70 unless ($token
71 && !$token->is_expired) {
72 return $self->error(q{Invalid token});
73 }
74 return $token->secret;
75 }
76  
77 sub get_consumer_secret {
78 my ($self, $consumer_key) = @_;
79 my $consumer = MyDB::Shceme->resultset('Consumer')->find($consumer_key);
80 unless ($consumer
81 && $consumer->is_valid) {
82 return $self->error(q{Inalid consumer_key});
83 }
84 return $consumer->secret;
85 }
86  
87 sub publish_request_token {
88 my ($self, $consumer_key, $callback_url) = @_;
89 my $token = OAuth::Lite::Token->new_random;
90 MyDB::Scheme->resultset('RequestToken')->create({
91 token => $token->token,
92 secret => $token->secret,
93 realm => $self->realm,
94 consumer_key => $consumer_key,
95 expired_on => '',
96 callback => $callback_url,
97 });
98 return $token;
99 }
100  
101 sub publish_access_token {
102 my ($self, $consumer_key, $request_token_string, $verifier) = @_;
103 my $request_token = MyDB::Scheme->resultset('RequestToken')->find($request_token_string);
104 unless ($request_token
105 && $request_token->is_authorized_by_user
106 && !$request_token->is_exchanged_to_access_token
107 && !$request_token->is_expired
108 && $request_token->has_verifier
109 && $request_token->verifier eq $verifier) {
110 return $self->error(q{Invalid token});
111 }
112 my $access_token = OAuth::Lite::Token->new_random;
113 MyDB::Scheme->resultset('AccessToken')->create({
114 token => $request_token->token,
115 realm => $self->realm,
116 secret => $request_token->secret,
117 consumer_key => $consumer_key,
118 author => $request_token->author,
119 expired_on => '',
120 });
121  
122 $request_token->is_exchanged_to_access_token(1);
123 $request_token->update();
124  
125 return $access_token;
126 }
127  
128 sub check_nonce_and_timestamp {
129 my ($self, $consumer_key, $nonce, $timestamp) = @_;
130 my $request_log = MyDB::Scheme->resultset('RequestLog');
131 # check against replay-attack
132 my $count = $request_log->count({
133 consumer_key => $consumer_key,
134 -nest => [
135 nonce => $nonce,
136 timestamp => { '>' => $timestamp },
137 ],
138 });
139 if ($count > 0) {
140 return $self->error(q{Invalid timestamp or consumer});
141 }
142 # save new request log.
143 $request_log->create({
144 consumer_key => $consumer_key,
145 nonce => $nonce,
146 timestamp => $timestamp,
147 });
148 return 1;
149 }
150  
151 sub service {
152 my $self = shift;
153 }
154  
155 in httpd.conf
156  
157 PerlSwitches -I/var/www/MyApp/lib
158 PerlModule MyServiceWithOAuth
159  
160 <VirtualHost *>
161  
162 ServerName api.example.com
163 DocumentRoot /var/www/MyApp/root
164  
165 PerlSetVar Realm "http://api.example.com/picture"
166  
167 <Location /picture/request_token>
168 SetHandler perl-script
169 PerlSetVar Mode REQUEST_TOKEN
170 PerlResponseHandler MyServiceWithOAuth
171 </Location>
172  
173 <Location /picture/access_token>
174 SetHandler perl-script
175 PerlSetVar Mode ACCESS_TOKEN
176 PerlResponseHandler MyServiceWithOAuth
177 </Location>
178  
179 <Location /picture/resource>
180 SetHandler perl-script
181 PerlSetVar Mode PROTECTED_RESOURCE
182 PerlResponseHandler MyServiceWithOAuth
183 </Location>
184  
185 </VirtualHost>
186  
187 =head1 DESCRIPTION
188  
189 This module is for mod_perl2 PerlResponseHandler, and allows you to
190 build services with OAuth easily.
191  
192 =head1 TUTORIAL
193  
194 All you have to do is to make a package inheritting this module,
195 and override some methods, and in httpd.conf file, write
196 three configuration, each configuration needs to be set Mode value.
197 The each value must be REQUEST_TOKEN, ACCESS_TOKEN, or PROTECTED_RESOURCE.
198 And the Realm value is needed for each resource.
199  
200 The methods you have to override is bellow.
201  
202 =head1 METHODS YOU HAVE TO OVERRIDE
203  
204 =head2 init
205  
206 In this method, you can do some initialization.
207 For example, set what signature method your service supports,
208 and what extra-param is allowed.
209  
210 sub init {
211 my $self = shift;
212 $self->oauth->support_signature_method(qw/HMAC-SHA1 PLAINTEXT/);
213 $self->oauth->allow_extra_params(qw/file size/);
214 }
215  
216 =head2 get_request_token_secret($token_string)
217  
218 In this method, you should check if the request-token-string is
219 valid, and returns token-secret value corresponds to the
220 token value passed as argument.
221 If the token is invalid, you should call 'error' method.
222  
223 =head2 get_access_token_secret($token_string, $consumer_key)
224  
225 In this method, you should check if the access-token-string is
226 valid, and returns token-secret value corresponds to the
227 token value passed as argument.
228 If the token is invalid, you should call 'error' method.
229  
230 =head2 get_consumer_secret($consumer_key)
231  
232 In this method, you should check if the consumer_key is valid,
233 and returns consumer_secret value corresponds to the consumer_key
234 passed as argument.
235 If the consumer is invalid, you should call 'error' method.
236  
237 =head2 check_nonce_and_timestamp($consumer_key, $nonce, $timestamp)
238  
239 Check passed nonce and timestamp.
240 Among requests the consumer send service-provider, there shouldn't be
241 same nonce, and new timestamp should be greater than old ones.
242 If they are valid, returns 1, or returns 0.
243  
244 =head2 publish_request_token($consumer_key, $callback_url)
245  
246 Create new request-token, and save it,
247 and returns it as L<OAuth::Lite::Token> object.
248  
249 =head2 publish_access_token($consumer_key, $request_token_string, $verifier)
250  
251 If the passed request-token is valid,
252 create new access-token, and save it,
253 and returns it as L<OAuth::Lite::Token> object.
254 And disables the exchanged request-token.
255  
256 =head2 verify_requestor_approval($consumer_key, $requestor_id)
257  
258 When the request is for OpenSocial Reverse Phone Home,
259 Check if the requestor has already given approval to consumer
260 to access the requestor's data.
261  
262 =head2 service
263  
264 Handle protected resource.
265 This method should returns Apache2::Const::OK.
266  
267 sub service {
268 my $self = shift;
269 my $params = $self->{params};
270 my $token_string = $params->{oauth_token};
271 my $access_token = MyDB::Scheme->resultset('RequestToken')->find($token_string);
272 my $user = $access_token->author;
273  
274 my $resource = $user->get_my_some_resource();
275  
276 $self->request->status(200);
277 $self->request->content_type(q{text/html; charset=utf-8});
278 $self->print($resource);
279 return Apache2::Const::OK;
280 }
281  
282 =head1 API
283  
284 =head2 handler
285  
286 Trigger method as response handler.
287  
288 =head2 new
289  
290 Constructor
291  
292 =head2 request
293  
294 Returns Apache request object.
295 See L<Apache2::RequestRec>, L<Apache2::RequestIO>, and etc...
296  
297 $self->request;
298  
299 =head2 realm
300  
301 The realm value you set in httpd.conf by PerlSetVar.
302  
303 =head2 oauth
304  
305 Returns l<OAuth::Lite::ServerUtil> object.
306  
307 =head2 allow_extra_param
308  
309 =head2 allow_extra_params
310  
311 =head2 support_signature_method
312  
313 =head2 support_signature_methods
314  
315 These methods are just only delegate methods.
316 For example,
317  
318 $self->allow_extra_param('foo');
319  
320 is same as
321  
322 $self->oauth->allow_extra_param('foo');
323  
324 =head2 request_method
325  
326 Request method (Upper Case).
327 When the raw request method is POST and X-HTTP-Method-Override is define in header,
328 return the value of X-HTTP-Method-Override.
329  
330 =head2 request_uri
331  
332 Returns request uri
333  
334 =head2 request_body
335  
336 Requets body data when the request's http-method is POST or PUT
337  
338 =head2 set_authenticate_header
339  
340 Set proper 'WWW-Authentication' response header
341  
342 =head2 is_required_request_token
343  
344 Check if current request requires request-token.
345  
346 =head2 is_required_access_token
347  
348 Check if current request requires access-token.
349  
350 =head2 is_required_protected_resource
351  
352 Check if current request requires protected-resource.
353  
354 =head2 is_consumer_request
355  
356 Chekc if the server accepts consumer-request and
357 the request is for protected resource without token.
358  
359 =head2 is_reverse_phone_home
360  
361 Check if the server accepts open-social reverse-phone-home
362 and the requests is for protected resource without token.
363  
364 =head2 xrds_location
365  
366 If you want to support OAuth Discovery, you need to
367 prepare XRDS document, and set the location as XRDSLocation value.
368 See below.
369  
370 <Location /resource>
371 PerlSetVar Mode PROTECTED_RESOURCE
372 PerlSetVar XRDSLocation "http://myservice/discovery/xrdsdocument"
373 PerlResponseHandler MyServiceWithOAuth
374 </Location>
375  
376  
377 Then you can get this url in your script.
378  
379 sub service {
380 my $self = shift;
381 my $xrds_location = $self->xrds_location;
382 }
383  
384 But normalry all you have to do is write location on httpd.conf.
385 And "errout" method automatically put it into response header properly.
386  
387 =head2 build_xrds
388  
389 In case client send request which includes application/xrds+xml in Accept header,
390 if The server is set XRDSLocation as above, return resuponse with it in header.
391 But you can also directly return XRDS-Document.
392  
393 Override build_xrds document.
394  
395 sub build_xrds {
396 my $self = shift;
397 my $xrds = q{
398 <?xml version="1.0" encoding="UTF-8"?>
399 <XRDS xmlns="xri://$xrds">
400 ...
401 </XRDS>
402 };
403 return $xrds;
404 }
405  
406 If the server doesn't support both XRDSLocation and build_xrds overriding,
407 The server doesn't support OAuth Discovery.
408  
409 =head2 accepts_consumer_request
410  
411 You can adopt OAuth Consumer Request 1.0.
412  
413 See http://oauth.googlecode.com/svn/spec/ext/consumer_request/1.0/drafts/1/spec.html
414  
415 To adopt this spec, you have to set var 'AcceptConsumerRequest' on httpd.conf
416  
417 <Location /resource>
418 PerlSetVar Mode PROTECTED_RESOURCE
419 PerlSetVar AcceptConsumerRequest 1
420 PerlResponseHandler MyServiceWithOAuth
421 </Location>
422  
423 Then override service method for protected resource.
424  
425 sub service {
426 my $self = shift;
427 my $params = $self->{params};
428  
429 my $resource_owner_id;
430  
431 if (exists $params->{oauth_token}) {
432  
433 my $access_token_value = $params->{oauth_token};
434 $resource_owner_id = $self->get_user_id_of_access_token($access_token_value);
435  
436 } else {
437  
438 my $consumer_key = $params->{oauth_consumer_key};
439 $resource_owner_id = $self->get_user_id_of_consumer_developer($consumer_key);
440  
441 }
442  
443 my @resources = MyDB::Scheme->resultset('SomeResource')->search({
444 user_id => $resource_owner_id,
445 });
446  
447 # output resource data in the manner your api defines.
448 ...
449  
450 return Apache2::Const::OK;
451  
452 }
453  
454 =head2 accepts_reverse_phone_home
455  
456 You can adopt OpenSocial Reverse Phone Home.
457  
458 <Location /resource>
459 PerlSetVar Mode PROTECTED_RESOURCE
460 PerlSetVar AcceptReversePhoneHome 1
461 PerlResponseHandler MyServiceWithOAuth
462 </Location>
463  
464 =head2 error
465  
466 L<Class::ErrorHandler> method.
467 In some check-method, when you find invalid request value,
468 call this method with error message and return it.
469  
470 sub check_nonce_and_timestamp {
471 my ($self, $consumer_key, $nonce, $timestamp) = @_;
472 if ($timestamp ...) {
473 return $self->error(q{Invalid timestamp});
474 }
475 return 1;
476 }
477  
478 =head2 errstr
479  
480 L<Class::ErrorHandler> method.
481 You can get error message that you set with error method.
482  
483 my $valid = $self->check_nonce_and_timestamp($consumer_key, $nonce, $timestamp);
484 if (!$valid) {
485 return $self->errout(401, $self->errstr);
486 }
487  
488 =head2 output(%params)
489  
490 Simply output response.
491 You can set 3 params, code, type and content.
492  
493 return $self->output(
494 code => 200,
495 type => 'text/plain; charset=utf-8'
496 content => 'success',
497 );
498  
499  
500 =head2 errout($code, $message)
501  
502 Output error message. This returns Apache2::Const::OK,
503 so, don't forget 'return';
504  
505 return $self->errout(400, q{Bad request});
506  
507 And you can override this and put some function into this process.
508 For example, logging.
509  
510 sub errout {
511 my ($self, $code, $message) = @_;
512 $self->my_log_process($code, $message);
513 return $self->SUPER::errout($code, $message);
514 }
515  
516 sub my_log_process {
517 my ($self, $code, $message) = @_;
518 warn ...
519 }
520  
521 =cut
522  
523 sub handler : method {
524 my $class = shift;
525 my $server = $class->new(@_);
526 return $server->__service();
527 }
528  
529 sub new {
530 my $class = shift;
531 my $r = shift;
532 my $self = bless {
533 request => $r,
534 oauth => OAuth::Lite::ServerUtil->new( strict => 0 ),
535 realm => undef,
536 secure => 0,
537 mode => PROTECTED_RESOURCE,
538 accepts_consumer_request => 0,
539 accepts_reverse_phone_home => 0,
540 params => {},
541 completed_validation => 0,
542 }, $class;
543 my $realm = $self->request->dir_config('Realm');
544 $self->{realm} = $realm if $realm;
545 my $accept_cr = $self->request->dir_config('AcceptConsumerRequest');
546 $self->{accepts_consumer_request} = 1 if $accept_cr;
547 my $accept_rp = $self->request->dir_config('AcceptReversePhoneHome');
548 if ($accept_rp) {
549 $self->{accepts_reverse_phone_home} = 1;
550 $self->allow_extra_param('xoauth_requestor_id');
551 }
552 my $mode = $self->request->dir_config('Mode');
553 my @valid_modes = (PROTECTED_RESOURCE, REQUEST_TOKEN, ACCESS_TOKEN);
554 if ($mode) {
555 if (none { $mode eq $_ } @valid_modes) {
556 die "Invalid mode.";
557 } else {
558 $self->{mode} = $mode;
559 }
560 }
561 my $xrds_location = $self->request->dir_config('XRDSLocation');
562 $self->{xrds_location} = $xrds_location if $xrds_location;
563 if ( ($INC{'Apache2/ModSSL.pm'} && $r->connection->is_https)
564 || ($r->subprocess_env('HTTPS') && $r->subprocess_env('HTTPS') eq 'ON') ) {
565 $self->{secure} = 1;
566 }
567 $self->init(@_);
568 $self;
569 }
570  
571 sub init {
572 my ($self, %args) = @_;
573 }
574  
575 sub request_body {
576 my $self = shift;
577 unless (defined $self->{_request_body}) {
578 my $length = $self->request->headers_in->{'Content-Length'} || 0;
579 my $body = "";
580 while ($length) {
581 $self->request->read( my $buffer, ($length < 8192) ? $length : 8192 );
582 $length -= bytes::length($buffer);
583 $body .= $buffer;
584 }
585 $self->{_request_body} = $body;
586 }
587 $self->{_request_body};
588 }
589  
590 sub request_uri {
591 my $self = shift;
592 return $self->request->uri;
593 }
594  
595 sub request_method {
596 my $self = shift;
597 unless (defined $self->{_request_method}) {
598 my $method = uc($self->request->method);
599 my $x_method = uc($self->request->headers_in->{'X-HTTP-Method-Override'} || '');
600 if ($method eq 'POST' && ($x_method eq 'PUT' || $x_method eq 'DELETE')) {
601 $self->{_request_method} = $x_method;
602 } else {
603 $self->{_request_method} = $method;
604 }
605 }
606 return $self->{_request_method};
607 }
608  
609 sub __service {
610  
611 my $self = shift;
612  
613 my $realm;
614 my $params = {};
615 my $is_authorized = 0;
616  
617 my $needs_to_check_token = ( $self->is_required_request_token
618 || ( $self->is_required_protected_resource
619 && ($self->accepts_consumer_request || $self->accepts_reverse_phone_home) ) )
620 ? 0
621 : 1;
622  
623 my $authorization = $self->request->headers_in->{Authorization};
624 if ($authorization && $authorization =~ /^\s*OAuth/) {
625 ($realm, $params) = parse_auth_header($authorization);
626 }
627  
628 if ( $self->request_method() eq 'POST'
629 && $self->request->headers_in->{'Content-Type'} =~ m!application/x-www-form-urlencoded!) {
630 for my $pair (split /&/, $self->request_body) {
631 my ($key, $value) = split /=/, $pair;
632 $params->{$key} = decode_param($value);
633 }
634 }
635  
636 for my $pair (split /&/, $self->request->args) {
637 my ($key, $value) = split /=/, $pair;
638 $params->{$key} = decode_param($value);
639 }
640  
641 unless ($self->oauth->validate_params($params, $needs_to_check_token)) {
642 return $self->errout(400, $self->oauth->errstr);
643 }
644  
645 $self->{params} = $params;
646  
647 my $consumer_key = $params->{oauth_consumer_key};
648 my $timestamp = $params->{oauth_timestamp};
649 my $nonce = $params->{oauth_nonce};
650  
651 my $consumer_secret = $self->get_consumer_secret($consumer_key);
652 unless (defined $consumer_secret) {
653 return $self->errout(401, $self->errstr||CONSUMER_KEY_UNKNOWN);
654 }
655  
656 $self->check_nonce_and_timestamp($consumer_key, $nonce, $timestamp)
657 or return $self->errout(400, $self->errstr||TIMESTAMP_REFUSED);
658  
659 my $request_uri = $self->__build_request_uri();
660  
661 if ($self->is_required_request_token) {
662  
663 $self->oauth->verify_signature(
664 method => $self->request_method(),
665 params => $params,
666 url => $request_uri,
667 consumer_secret => $consumer_secret,
668 ) or return $self->errout(401, $self->oauth->errstr||SIGNATURE_INVALID);
669  
670 my $callback_url = $params->{oauth_callback};
671 my $request_token = $self->publish_request_token($consumer_key, $callback_url)
672 or return $self->errout(401, $self->errstr);
673 return $self->__output_token($request_token);
674  
675 } elsif ($self->is_required_access_token) {
676  
677 my $token_value = $params->{oauth_token};
678 my $token_secret = $self->get_request_token_secret($token_value);
679 unless (defined $token_secret) {
680 return $self->errout(401, $self->errstr||TOKEN_REJECTED);
681 }
682 $self->oauth->verify_signature(
683 method => $self->request_method(),
684 params => $params,
685 url => $request_uri,
686 consumer_secret => $consumer_secret || '',
687 token_secret => $token_secret || '',
688 ) or return $self->errout(401, $self->oauth->errstr||SIGNATURE_INVALID);
689 my $verifier = $params->{oauth_verifier} || '';
690 my $access_token = $self->publish_access_token($consumer_key, $token_value, $verifier)
691 or return $self->errout(401, $self->errstr);
692 return $self->__output_token($access_token);
693  
694 } else {
695  
696 my $token_secret = '';
697 if (exists $params->{oauth_token}) {
698 my $token_value = $params->{oauth_token};
699 $token_secret = $self->get_access_token_secret($token_value, $consumer_key);
700 unless (defined $token_secret) {
701 return $self->errout(401, $self->errstr||TOKEN_REJECTED);
702 }
703 }
704  
705 $self->oauth->verify_signature(
706 method => $self->request_method(),
707 params => $params,
708 url => $request_uri,
709 consumer_secret => $consumer_secret || '',
710 token_secret => $token_secret,
711 ) or return $self->errout(401, $self->oauth->errstr||SIGNATURE_INVALID);
712  
713 if ($self->is_reverse_phone_home) {
714 $self->verify_requestor_approval($consumer_key, $params->{xoauth_requestor_id})
715 or return $self->errout(401, q{No approval});
716 }
717  
718 $self->{completed_validation} = 1;
719  
720 return $self->service($params);
721 }
722 }
723  
724 sub __build_request_uri {
725  
726 # borrowed from Catalyst::Engine::Apache
727 my $self = shift;
728  
729 my $scheme = $self->{secure} ? 'https' : 'http';
730 my $host = $self->request->hostname || 'localhost';
731 my $port = $self->request->get_server_port;
732  
733 PROXY_CHECK: {
734 last PROXY_CHECK unless $self->request->headers_in->{'X-Forwarded-Host'};
735
736 $host = $self->request->headers_in->{'X-Forwarded-Host'};
737  
738 if ( $host =~ /^(.+):(\d+)$/ ) {
739 $host = $1;
740 $port = $2;
741 } else {
742 $port = $self->{secure} ? 443 : 80;
743 }
744 }
745  
746 my $base_path = '';
747  
748 my $location = $self->request->location;
749 if ( $location && $location ne '/' ) {
750 $base_path = $location;
751 }
752
753 if ($host !~ /:/ ) {
754 $host .= ":$port";
755 }
756  
757 my ($path) = split /\?/, $self->request->unparsed_uri;
758  
759 if ( $base_path =~ m/[$URI::uric]/o ) {
760 my $match = qr/($base_path)/;
761 my ($base_match) = $path =~ $match;
762 $base_path = $base_match;
763 }
764  
765 $path =~ s{^/+}{};
766 $base_path = '/' unless length $base_path;
767 if ( $base_path ne '/' && $base_path ne $path && $base_path =~ m{/$path} ) {
768 $path = $base_path;
769 $path =~ s{^/+}{};
770 }
771 my $request_uri = $scheme . '://' . $host . '/' . $path;
772 return $request_uri;
773  
774 }
775  
776 sub __output_token {
777 my ($self, $token) = @_;
778 my $token_string = $token->as_encoded;
779 return $self->output(
780 code => 200,
781 type => q{text/plain; charset=utf-8},
782 content => $token_string,
783 );
784 }
785  
786 sub is_consumer_request {
787 my $self = shift;
788 return ($self->is_required_protected_resource
789 && $self->accepts_consumer_request
790 && !exists $self->{params}{oauth_token}) ? 1 : 0;
791 }
792  
793 sub is_required_request_token {
794 my $self = shift;
795 return ($self->{mode} eq REQUEST_TOKEN) ? 1 : 0;
796 }
797  
798 sub is_required_access_token {
799 my $self = shift;
800 return ($self->{mode} eq ACCESS_TOKEN) ? 1 : 0;
801 }
802  
803 sub is_required_protected_resource {
804 my $self = shift;
805 return ($self->{mode} eq PROTECTED_RESOURCE) ? 1 : 0;
806 }
807  
808 sub accepts_consumer_request {
809 my $self = shift;
810 return $self->{accepts_consumer_request};
811 }
812  
813 sub accepts_reverse_phone_home {
814 my $self = shift;
815 return $self->{accepts_reverse_phone_home};
816 }
817  
818 sub service {
819 my ($self, $params) = @_;
820 }
821  
822 sub get_request_token_secret {
823 my ($self, $token) = @_;
824 my $secret;
825 return $secret;
826 }
827  
828 sub get_access_token_secret {
829 my ($self, $token, $consumer_key) = @_;
830 my $secret;
831 return $secret;
832 }
833  
834 sub get_consumer_secret {
835 my ($self, $consumer_key);
836 my $consumer_secret;
837 return $consumer_secret;
838 }
839  
840 sub publish_request_token {
841 my ($self, $consumer_key, $callback_url) = @_;
842 my $token = OAuth::Lite::Token->new;
843 return $token;
844 }
845  
846 sub publish_access_token {
847 my ($self, $request_token_string, $verifier) = @_;
848 # validate request token
849 # and publish access token
850 # return $token;
851 my $token = OAuth::Lite::Token->new;
852 return $token;
853 }
854  
855 sub check_nonce_and_timestamp {
856 my ($self, $consumer_key, $timestamp, $nonce) = @_;
857 return $self->error(q{Invalid Consumer});
858 return $self->error(q{Invalid Timestamp});
859 return $self->error(q{Invalid Nonce});
860 return 1;
861 }
862  
863 sub set_authenticate_header {
864 my $self = shift;
865 my $problem = shift;
866 my %params = ();
867 $params{realm} = $self->realm if $self->realm;
868 $params{oauth_problem} = $problem if $problem;
869 my $header = "OAuth " . join(", ", map sprintf(q{%s="%s"}, $_, $params{$_}), keys %params);
870 $self->request->err_headers_out->add('WWW-Authenticate', $header);
871 }
872  
873 sub _check_if_request_accepts_xrds {
874 my $self = shift;
875 unless (defined $self->{__request_accepts_xrds}) {
876 my $accept = $self->request->headers_in->{Accept} || '';
877 my @types = map { (split ";", $_)[0] } split /\*s,\*s/, $accept;
878 if (any { $_ eq q{application/xrds+xml} } @types) {
879 $self->{__request_accepts_xrds} = 1;
880 } else {
881 $self->{__request_accepts_xrds} = 0;
882 }
883 }
884 return $self->{__request_accepts_xrds};
885 }
886  
887 sub build_xrds {
888 my $self = shift;
889 return;
890 }
891  
892 sub errout {
893 my ($self, $code, $message, $description) = @_;
894  
895 if ( ( $self->request_method() eq 'GET'
896 || $self->request_method() eq 'HEAD') &&
897 $self->is_required_protected_resource &&
898 $self->_check_if_request_accepts_xrds ) {
899 if ($self->xrds_locaton) {
900 $self->request->status(200);
901 $self->request->err_headers_out->add('X-XRDS-Location' => $self->xrds_location);
902 return Apache2::Const::OK;
903 } elsif ($self->request_method() eq 'GET' &&
904 (my $xrds = $self->build_xrds())) {
905 return $self->output(
906 code => 200,
907 type => q{application/xrds+xml},
908 content => $xrds,
909 );
910 }
911 }
912 my $problem;
913 if (OAuth::Lite::Problems->match($message)) {
914 $problem = $message;
915 $message = sprintf(q{oauth_problem=%s}, $message);
916 }
917  
918 $self->set_authenticate_header($problem);
919 return $self->output(
920 code => $code,
921 type => q{text/plain; charset=utf-8},
922 content => $message,
923 );
924 }
925  
926 sub output {
927 my $self = shift;
928 my %args = @_;
929 my $code = $args{code} || 200;
930 my $type = $args{type} || q{text/plain; charset=utf-8};
931 my $content = $args{content} || '';
932 $self->request->status($code);
933 if ($content) {
934 $self->request->content_type($type);
935 $self->request->set_content_length(bytes::length($content));
936 $self->request->print($content);
937 }
938 return Apache2::Const::OK;
939 }
940  
941 sub allow_extra_param {
942 my $self = shift;
943 $self->oauth->allow_extra_param(@_);
944 }
945  
946 sub allow_extra_params {
947 my $self = shift;
948 $self->oauth->allow_extra_params(@_);
949 }
950  
951 sub support_signature_method {
952 my $self = shift;
953 $self->oauth->support_signature_method(@_);
954 }
955  
956 sub support_signature_methods {
957 my $self = shift;
958 $self->oauth->support_signature_methods(@_);
959 }
960  
961 sub is_reverse_phone_home {
962 my $self = shift;
963 return ( $self->is_required_protected_resource
964 && $self->accepts_reverse_phone_home
965 && !exists $self->{params}{oauth_token}
966 && exists $self->{params}{xoauth_requestor_id}) ? 1 : 0
967 }
968  
969 sub verify_requestor_approval {
970 my $self = shift;
971 my ($consumer_key, $user_id) = @_;
972 return 1;
973 }
974  
975 =head1 SEE ALSO
976  
977 L<OAuth::Lite::ServerUtil>
978 L<OAuth::Lite::Server::Test::Echo>
979  
980 =head1 AUTHOR
981  
982 Lyo Kato, C<lyo.kato _at_ gmail.com>
983  
984 =head1 COPYRIGHT AND LICENSE
985  
986 This library is free software; you can redistribute it and/or modify
987 it under the same terms as Perl itself, either Perl version 5.8.6 or,
988 at your option, any later version of Perl 5 you may have available.
989  
990 =cut
991  
992 1;
993