| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 2627 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 2 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 19 |  | 
| 3 | 1 |  |  | 1 |  | 518 | use utf8; | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | package Amon2::Auth::Site::Facebook; | 
| 6 | 1 |  |  | 1 |  | 463 | use Mouse; | 
|  | 1 |  |  |  |  | 28592 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 7 | 1 |  |  | 1 |  | 1043 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 55087 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 8 | 1 |  |  | 1 |  | 8 | use URI; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 9 | 1 |  |  | 1 |  | 643 | use JSON; | 
|  | 1 |  |  |  |  | 9638 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 10 | 1 |  |  | 1 |  | 485 | use Amon2::Auth::Util qw(parse_content); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 72 |  | 
| 11 | 1 |  |  | 1 |  | 6 | use Amon2::Auth; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 472 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 0 |  |  | 0 | 0 |  | sub moniker { 'facebook' } | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | for (qw(client_id scope client_secret)) { | 
| 16 |  |  |  |  |  |  | has $_ => ( | 
| 17 |  |  |  |  |  |  | is => 'ro', | 
| 18 |  |  |  |  |  |  | isa => 'Str', | 
| 19 |  |  |  |  |  |  | required => 1, | 
| 20 |  |  |  |  |  |  | ); | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | has user_info => ( | 
| 24 |  |  |  |  |  |  | is => 'rw', | 
| 25 |  |  |  |  |  |  | isa => 'Bool', | 
| 26 |  |  |  |  |  |  | default => 1, | 
| 27 |  |  |  |  |  |  | ); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | has ua => ( | 
| 30 |  |  |  |  |  |  | is => 'ro', | 
| 31 |  |  |  |  |  |  | isa => 'LWP::UserAgent', | 
| 32 |  |  |  |  |  |  | lazy => 1, | 
| 33 |  |  |  |  |  |  | default => sub { | 
| 34 |  |  |  |  |  |  | my $ua = LWP::UserAgent->new(agent => "Amon2::Auth/$Amon2::Auth::VERSION"); | 
| 35 |  |  |  |  |  |  | }, | 
| 36 |  |  |  |  |  |  | ); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub auth_uri { | 
| 39 | 0 |  |  | 0 | 0 |  | my ($self, $c, $callback_uri) = @_; | 
| 40 | 0 | 0 |  |  |  |  | $callback_uri or die "Missing mandatory parameter: callback_uri"; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  |  | my $url = URI->new('https://www.facebook.com/dialog/oauth'); | 
| 43 | 0 |  |  |  |  |  | my %params; | 
| 44 | 0 |  |  |  |  |  | for (qw(client_id scope)) { | 
| 45 | 0 |  |  |  |  |  | $params{$_} = $self->$_; | 
| 46 |  |  |  |  |  |  | } | 
| 47 | 0 |  |  |  |  |  | $params{redirect_uri} = $callback_uri; | 
| 48 | 0 |  |  |  |  |  | $url->query_form(%params); | 
| 49 | 0 |  |  |  |  |  | return $url->as_string; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub callback { | 
| 53 | 0 |  |  | 0 | 0 |  | my ($self, $c, $callback) = @_; | 
| 54 | 0 | 0 |  |  |  |  | if (my $error_description = $c->req->param('error_description')) { | 
| 55 | 0 |  |  |  |  |  | return $callback->{on_error}->($error_description); | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 |  |  |  |  |  | my $uri = URI->new('https://graph.facebook.com/oauth/access_token'); | 
| 59 | 0 |  |  |  |  |  | my %params; | 
| 60 | 0 |  |  |  |  |  | for (qw(client_id client_secret)) { | 
| 61 | 0 |  |  |  |  |  | $params{$_} = $self->$_; | 
| 62 |  |  |  |  |  |  | } | 
| 63 | 0 |  |  |  |  |  | $params{redirect_uri} = $c->req->uri->as_string; | 
| 64 | 0 |  |  |  |  |  | $params{redirect_uri} =~ s/\?.+//; | 
| 65 | 0 | 0 |  |  |  |  | $params{code} = $c->req->param('code') or die; | 
| 66 | 0 |  |  |  |  |  | $uri->query_form(%params); | 
| 67 | 0 |  |  |  |  |  | my $res = $self->ua->get($uri->as_string); | 
| 68 | 0 | 0 |  |  |  |  | $res->is_success or do { | 
| 69 | 0 |  |  |  |  |  | warn $res->decoded_content; | 
| 70 | 0 |  |  |  |  |  | return $callback->{on_error}->($res->decoded_content); | 
| 71 |  |  |  |  |  |  | }; | 
| 72 | 0 |  |  |  |  |  | my $dat = parse_content($res->decoded_content); | 
| 73 | 0 | 0 |  |  |  |  | if (my $err = $dat->{error}) { | 
| 74 | 0 |  |  |  |  |  | return $callback->{on_error}->($err); | 
| 75 |  |  |  |  |  |  | } | 
| 76 | 0 | 0 |  |  |  |  | my $access_token = $dat->{access_token} or die "Cannot get a access_token"; | 
| 77 | 0 |  |  |  |  |  | my @args = ($access_token); | 
| 78 | 0 | 0 |  |  |  |  | if ($self->user_info) { | 
| 79 | 0 |  |  |  |  |  | my $res = $self->ua->get("https://graph.facebook.com/me?access_token=${access_token}"); | 
| 80 | 0 | 0 |  |  |  |  | $res->is_success or return $callback->{on_error}->($res->status_line); | 
| 81 | 0 |  |  |  |  |  | my $dat = decode_json($res->decoded_content); | 
| 82 | 0 |  |  |  |  |  | push @args, $dat; | 
| 83 |  |  |  |  |  |  | } | 
| 84 | 0 |  |  |  |  |  | return $callback->{on_finished}->(@args); | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | 1; | 
| 88 |  |  |  |  |  |  | __END__ |