| 1 | package Dancer::Plugin::Auth::Facebook; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | $Dancer::Plugin::Auth::Facebook::VERSION = '0.08'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Net::Facebook::Oauth2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | my $_FB; | |||||||||||||||||||||||||||
| 14 | 4 | ▶ | sub facebook { $_FB } | ||||||||||||||||||||||||||
|
Subroutine
facebook: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | register 'facebook' => \&facebook; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | my $application_id; | |||||||||||||||||||||||||||
| 18 | my $application_secret; | ||||||||||||||||||||||||||||
| 19 | 1 | my $cb_url; | |||||||||||||||||||||||||||
| 20 | 1 | my $cb_success; | |||||||||||||||||||||||||||
| 21 | 1 | my $cb_fail; | |||||||||||||||||||||||||||
| 22 | 1 | my $fb_scope; | |||||||||||||||||||||||||||
| 23 | 1 | my @scope; | |||||||||||||||||||||||||||
| 24 | 1 | my $me_fields; | |||||||||||||||||||||||||||
| 25 | 1 | my $api_version; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | register 'auth_fb_init' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=23.1
| |||||||||||||||||||||||||||||
| 28 | 1 | my $config = plugin_setting; | |||||||||||||||||||||||||||
| 29 | 1 | $application_id = $config->{application_id}; | |||||||||||||||||||||||||||
| 30 | 1 | $application_secret = $config->{application_secret}; | |||||||||||||||||||||||||||
| 31 | 1 | $cb_url = $config->{callback_url}; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | $cb_success = $config->{callback_success} || '/'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'callback_success'} || '/'
MC/DC0%0/1
$config->{'callback_success'} || '/'
$config->{'callback_success'}
Truth table50%1/2
A
A= $config->{'callback_success'}
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $cb_fail = $config->{callback_fail} || '/fail'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'callback_fail'} || '/fail'
MC/DC0%0/1
$config->{'callback_fail'} || '/fail'
$config->{'callback_fail'}
Truth table50%1/2
A
A= $config->{'callback_fail'}
| |||||||||||||||||||||||||||||
| 35 | 1 | $fb_scope = $config->{scope}; | |||||||||||||||||||||||||||
| 36 | 1 | $me_fields = $config->{fields}; | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | $api_version = $config->{api_version} || 'v4.0'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'api_version'} || 'v4.0'
MC/DC0%0/1
$config->{'api_version'} || 'v4.0'
$config->{'api_version'}
Truth table50%1/2
A
A= $config->{'api_version'}
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | if (defined $fb_scope) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | foreach my $fs (split(/\s+/, $fb_scope)) { | |||||||||||||||||||||||||||
| 41 | 3 | ▶ | next unless ($fs =~ m/^[_A-Za-z0-9\.]+$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 3 | push(@scope, $fs); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | else { | ||||||||||||||||||||||||||||
| 46 | 0 | push(@scope, 'email'); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | for my $param (qw/application_id application_secret callback_url/) { | |||||||||||||||||||||||||||
| 50 | 3 | ▶ | croak "'$param' is expected but not found in configuration" unless $config->{$param}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | debug "new facebook with $application_id, $application_secret, $cb_url"; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | $_FB = Net::Facebook::Oauth2->new( | |||||||||||||||||||||||||||
| 56 | api_version => $api_version, | ||||||||||||||||||||||||||||
| 57 | application_id => $application_id, ##get this from your facebook developers platform | ||||||||||||||||||||||||||||
| 58 | application_secret => $application_secret, ##get this from your facebook developers platform | ||||||||||||||||||||||||||||
| 59 | callback => $cb_url, ##Callback URL, facebook will redirect users after authintication | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | }; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | register 'auth_fb_authenticate_url' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if (not defined facebook ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | croak "auth_fb_init must be called first"; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | my $url = facebook->get_authorization_url( | |||||||||||||||||||||||||||
| 70 | scope => \@scope, | ||||||||||||||||||||||||||||
| 71 | display => 'page', | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | session fb_access_token => ''; | |||||||||||||||||||||||||||
| 75 | 1 | debug "fb_auth_url: $url"; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | return $url; | |||||||||||||||||||||||||||
| 78 | 1 | }; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | get '/auth/facebook/callback' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=22.4
| |||||||||||||||||||||||||||||
| 81 | 3 | debug "entering facebook callback"; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 3 | ▶ | return redirect $cb_fail if (params->{'error'} || !params->{'code'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
params()->{"error"} or not params()->{"code"}
MC/DC100%2/2
params()->{"error"} or not params()->{"code"}
params()->{"error"}
not params()->{"code"}
Truth table100%3/3
A or not(B)
A= params()->{"error"}B= not params()->{"code"}
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | my $access_token = session('fb_access_token'); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | if (!$access_token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | eval { | |||||||||||||||||||||||||||
| 89 | 1 | $access_token = facebook->get_access_token(code => params->{'code'}); | |||||||||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | if (!$access_token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | error "facebook error fetching access token: $@"; | |||||||||||||||||||||||||||
| 93 | 0 | return redirect $cb_fail; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 1 | session fb_access_token => $access_token; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | my $fb = Net::Facebook::Oauth2->new( | |||||||||||||||||||||||||||
| 99 | api_version => $api_version, | ||||||||||||||||||||||||||||
| 100 | access_token => $access_token, | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | my ($me, $fb_response); | |||||||||||||||||||||||||||
| 104 | 1 | eval { | |||||||||||||||||||||||||||
| 105 | 1 | ▶ | $fb_response = $fb->get( 'https://graph.facebook.com/' . $api_version . '/me' . ($me_fields ? "?fields=$me_fields" : '') ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 1 | $me = $fb_response->as_hash; | |||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||
| 108 | 1 | ▶ | if ($@ || !$me) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $me
MC/DC0%0/2
$@ or not $me
$@
not $me
Truth table33%1/3
A or not(B)
A= $@B= not $me
| |||||||||||||||||||||||||||||
| 109 | 0 | error "error fetching facebook user: '$@' on response '$fb_response'"; | |||||||||||||||||||||||||||
| 110 | 0 | return redirect $cb_fail; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||
| 113 | 1 | session fb_user => $me; | |||||||||||||||||||||||||||
| 114 | 1 | return redirect $cb_success; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 1 | }; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | register_plugin; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||