| 1 | package Facebook::Graph::Cmdline::Role::HTTPAccessToken; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 2 | $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 | 2 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Any::Moose 'Role'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | #all provided by Facebook::Graph | ||||||||||||||||||||||||||||
| 12 | 2 | requires qw( | |||||||||||||||||||||||||||
| 13 | access_token | ||||||||||||||||||||||||||||
| 14 | authorize | ||||||||||||||||||||||||||||
| 15 | fetch | ||||||||||||||||||||||||||||
| 16 | postback | ||||||||||||||||||||||||||||
| 17 | request_access_token | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | has +postback => ( is => 'ro', required => 1 ); | |||||||||||||||||||||||||||
| 21 | 2 | has +access_token => ( is => 'rw', lazy_build => 1 ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use HTTP::Daemon 6.00; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | ### | ||||||||||||||||||||||||||||
| 27 | # provides code, token | ||||||||||||||||||||||||||||
| 28 | # requires permissions | ||||||||||||||||||||||||||||
| 29 | # can override prompt_message, success_message | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | 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 | 0 | ▶ | default => sub { [] } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 2 | ); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # fmt will be called with url as arg | ||||||||||||||||||||||||||||
| 45 | 2 | has prompt_message_fmt => ( | |||||||||||||||||||||||||||
| 46 | is => 'rw', | ||||||||||||||||||||||||||||
| 47 | default => "Please visit this url to authorize application:\n%s\n" | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | 2 | has success_message => ( | |||||||||||||||||||||||||||
| 50 | is => 'rw', | ||||||||||||||||||||||||||||
| 51 | default => 'Success!' | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub _build_code | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_code: not called CC=6 SCAR=34.7
| |||||||||||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 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 | 2 | ▶ | use HTTP::Daemon; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 0 | my $postback = URI->new( $self->postback ); | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $d = HTTP::Daemon->new( | ||||||||||||||||||||||||||
|
Condition0%0/2
'HTTP::Daemon'->new('LocalAddr', $postback->host, 'LocalPort', $postback->port) || die()
MC/DC0%0/1
'HTTP::Daemon'->new('LocalAddr', $postback->host, 'LocalPort', $postback->port) || die()
'HTTP::Daemon'->new('LocalAddr', $postback->host, 'LocalPort', $postback->port)
Truth table0%0/2
A
A= 'HTTP::Daemon'->new('LocalAddr', $postback->host, 'LocalPort', $postback->port)
| |||||||||||||||||||||||||||||
| 66 | LocalAddr => $postback->host, | ||||||||||||||||||||||||||||
| 67 | LocalPort => $postback->port, | ||||||||||||||||||||||||||||
| 68 | ) || die; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $code = ''; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | until ($code) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 0 | my $c = $d->accept; | |||||||||||||||||||||||||||
| 74 | 0 | my $r = $c->get_request; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | next unless $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( $r->url->path eq $postback->path | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->url->path eq $postback->path and $r->url->query_param("code")
MC/DC0%0/2
$r->url->path eq $postback->path and $r->url->query_param("code")
$r->url->path eq $postback->path
$r->url->query_param("code")
Truth table0%0/3
A and B
A= $r->url->path eq $postback->pathB= $r->url->query_param("code")
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_access_token: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 97 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | 0 | return $self->request_access_token( $self->code )->token; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub verify_access_token | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
verify_access_token: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | return 0 unless $self->has_access_token(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | say "verifying token"; ## DEBUG | |||||||||||||||||||||||||||
| 107 | #$self->access_token( $self->token ); | ||||||||||||||||||||||||||||
| 108 | 0 | my $resp; | |||||||||||||||||||||||||||
| 109 | 0 | eval { $resp = $self->fetch('me') }; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 2 | 1; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||