File Coverage

blib/lib/Facebook/Graph/Cmdline/Role/HTTPAccessToken.pm
Criterion Covered Total %
statement 15 44 34.0
branch 0 8 0.0
condition 0 5 0.0
subroutine 5 8 62.5
pod 0 1 0.0
total 20 66 30.3


line stmt bran cond sub pod time code
1             package Facebook::Graph::Cmdline::Role::HTTPAccessToken;
2             {
3             $Facebook::Graph::Cmdline::Role::HTTPAccessToken::VERSION = '0.123490';
4             }
5              
6             #ABSTRACT: Embeds an HTTP::Daemon to implement OAuth callback for Facebook Authorization of Commandline Facebook apps.
7              
8 1     1   845 use v5.10;
  1         5  
  1         57  
9 1     1   5 use Any::Moose 'Role';
  1         2  
  1         12  
10              
11             #all provided by Facebook::Graph
12             requires qw(
13             access_token
14             authorize
15             fetch
16             postback
17             request_access_token
18             );
19              
20             has +postback => ( is => 'ro', required => 1 );
21             has +access_token => ( is => 'rw', lazy_build => 1 );
22              
23 1     1   3348 use HTTP::Daemon 6.00;
  1         70686  
  1         16  
24 1     1   13394 use URI;
  1         4  
  1         181  
25              
26             ###
27             # provides code, token
28             # requires permissions
29             # can override prompt_message, success_message
30              
31             has code => (
32             is => 'rw',
33             lazy_build => 1,
34             );
35             #has token => (
36             # is => 'rw',
37             # lazy_build => 1,
38             #);
39             has permissions => (
40             is => 'ro',
41             default => sub { [] }
42             );
43              
44             # fmt will be called with url as arg
45             has prompt_message_fmt => (
46             is => 'rw',
47             default => "Please visit this url to authorize application:\n%s\n"
48             );
49             has success_message => (
50             is => 'rw',
51             default => 'Success!'
52             );
53              
54             sub _build_code
55             {
56 0     0     my $self = shift;
57 0           my $uri = $self
58             ->authorize
59 0           ->extend_permissions( @{ $self->permissions } )
60             ->uri_as_string;
61 0           printf $self->prompt_message_fmt, $uri;
62              
63 1     1   7 use HTTP::Daemon;
  1         2  
  1         9  
64 0           my $postback = URI->new( $self->postback );
65 0   0       my $d = HTTP::Daemon->new(
66             LocalAddr => $postback->host,
67             LocalPort => $postback->port,
68             ) || die;
69              
70 0           my $code = '';
71 0           until ($code)
72             {
73 0           my $c = $d->accept;
74 0           my $r = $c->get_request;
75 0 0         next unless $r;
76              
77 0 0 0       if ( $r->url->path eq $postback->path
78             and $r->url->query_param('code') )
79             {
80 0           $code = $r->url->query_param('code');
81 0           $c->send_response(
82             HTTP::Response->new(
83             200, undef, undef, $self->success_message
84             )
85             );
86             }
87             else
88             {
89 0           $c->send_response('204');
90             }
91             }
92 0           $code;
93             }
94              
95             sub _build_access_token
96             {
97 0     0     my $self = shift;
98 0           return $self->request_access_token( $self->code )->token;
99             }
100              
101             sub verify_access_token
102             {
103 0     0 0   my $self = shift;
104 0 0         return 0 unless $self->has_access_token();
105              
106 0           say "verifying token"; ## DEBUG
107             #$self->access_token( $self->token );
108 0           my $resp;
109 0           eval { $resp = $self->fetch('me') };
  0            
110 0 0         if ($@)
111             {
112 0           say "Bad access_token, deleting"; ## INFO
113 0           $self->clear_access_token;
114 0           return 0;
115             }
116 0           return 1;
117             }
118              
119             1;
120              
121             __END__
122             =pod
123              
124             =head1 NAME
125              
126             Facebook::Graph::Cmdline::Role::HTTPAccessToken - Embeds an HTTP::Daemon to implement OAuth callback for Facebook Authorization of Commandline Facebook apps.
127              
128             =head1 VERSION
129              
130             version 0.123490
131              
132             =head1 AUTHOR
133              
134             Andrew Grangaard <spazm@cpan.org>
135              
136             =head1 COPYRIGHT AND LICENSE
137              
138             This software is copyright (c) 2012 by Andrew Grangaard.
139              
140             This is free software; you can redistribute it and/or modify it under
141             the same terms as the Perl 5 programming language system itself.
142              
143             =cut
144