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