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