lib/Dancer/Plugin/Auth/Facebook.pm

filter: Statementstmt 92.0% 69 / 75 Branchbran 56.2% 9 / 16 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 82.0% 96 / 117 SCAR 27.8
CC
15
Cov
83%
__ANON__
8.8
__ANON__
7.9
__ANON__
2.1
CRAP 16.1
↑ Summary blib/lib/Dancer/Plugin/Auth/Facebook.pm »
1 package Dancer::Plugin::Auth::Facebook;
2  
3 1 $Dancer::Plugin::Auth::Facebook::VERSION = '0.05';
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  
25 register 'auth_fb_init' => sub {
26 1 ▶ my $config = plugin_setting;
Subroutine __ANON__: called CC=8 SCAR=21.7
27 1 $application_id = $config->{application_id};
28 1 $application_secret = $config->{application_secret};
29 1 $cb_url = $config->{callback_url};
30  
31 1 ▶ $cb_success = $config->{callback_success} || '/';
Condition50%1/2
$config->{'callback_success'} || '/'
l!l
10
MC/DC0%0/1
$config->{'callback_success'} || '/'
$config->{'callback_success'}
Truth table50%1/2
A
A= $config->{'callback_success'}
Aresult
11
00
32 1 ▶ $cb_fail = $config->{callback_fail} || '/fail';
Condition50%1/2
$config->{'callback_fail'} || '/fail'
l!l
10
MC/DC0%0/1
$config->{'callback_fail'} || '/fail'
$config->{'callback_fail'}
Truth table50%1/2
A
A= $config->{'callback_fail'}
Aresult
11
00
33 1 $fb_scope = $config->{scope};
34  
35 1 ▶ if (defined $fb_scope) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fb_scope) { } 1 0 1
36 1 foreach my $fs (split(/\s+/, $fb_scope)) {
37 3 ▶ next unless ($fs =~ m/^[_A-Za-z0-9\.]+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $fs =~ /^[_A-Za-z0-9\.]+$/ 0 3 3
38 3 push(@scope, $fs);
39 }
40 }
41 else {
42 0 push(@scope, 'email');
43 }
44  
45 1 for my $param (qw/application_id application_secret callback_url/) {
46 3 ▶ croak "'$param' is expected but not found in configuration" unless $config->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{$param} 0 3 3
47 }
48  
49 1 debug "new facebook with $application_id, $application_secret, $cb_url";
50  
51 1 $_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 1 };
58  
59 register 'auth_fb_authenticate_url' => sub {
60 1 ▶ if (not defined facebook ) {
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless (defined facebook()) 0 1 1
61 0 croak "auth_fb_init must be called first";
62 }
63  
64 1 my $url = facebook->get_authorization_url(
65 scope => \@scope,
66 display => 'page',
67 );
68  
69 1 session fb_access_token => '';
70 1 debug "fb_auth_url: $url";
71  
72 1 return $url;
73 1 };
74  
75 get '/auth/facebook/callback' => sub {
76 3 ▶ debug "entering facebook callback";
Subroutine __ANON__: called CC=7 SCAR=20.7
77  
78 3 ▶ return redirect $cb_fail if (params->{'error'} || !params->{'code'});
Branch: 1 branch
BranchTrueFalseTotal
if params()->{"error"} or not params()->{"code"} 2 1 3
Condition100%3/3
params()->{"error"} or not params()->{"code"}
l!l&&r!l&&!r
111
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"}
ABresult
1X1
011
000
79  
80 1 my $access_token = session('fb_access_token');
81  
82 1 ▶ if (!$access_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token) 1 0 1
83 1 eval {
84 1 $access_token = facebook->get_access_token(code => params->{'code'});
85 };
86 1 ▶ if (!$access_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token) 0 1 1
87 0 error "facebook error fetching access token: $@";
88 0 return redirect $cb_fail;
89 }
90 1 session fb_access_token => $access_token;
91 }
92  
93 1 my $fb = Net::Facebook::Oauth2->new(
94 access_token => $access_token,
95 );
96  
97 1 my ($me, $fb_response);
98 1 eval {
99 1 $fb_response = $fb->get( 'https://graph.facebook.com/v2.2/me' );
100 1 $me = $fb_response->as_hash;
101 };
102 1 ▶ if ($@ || !$me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $me) { } 0 1 1
Condition33%1/3
$@ or not $me
l!l&&r!l&&!r
001
MC/DC0%0/2
$@ or not $me
$@ not $me
Truth table33%1/3
A or not(B)
A= $@B= not $me
ABresult
1X1
011
000
103 0 error "error fetching facebook user: '$@' on response '$fb_response'";
104 0 return redirect $cb_fail;
105 }
106 else {
107 1 session fb_user => $me;
108 1 return redirect $cb_success;
109 }
110 1 };
111  
112 1 register_plugin;
113  
114 1 1;
115  
116 __END__
117  
118 =pod
119  
120 =head1 NAME
121  
122 Dancer::Plugin::Auth::Facebook - Authenticate with Facebook OAuth
123  
124 =head1 SYNOPSIS
125  
126 package plugin::test;
127 use Dancer ':syntax';
128 use Dancer::Plugin::Auth::Facebook;
129  
130 auth_fb_init();
131  
132 hook before => sub {
133 #we don't want a redirect loop here.
134 return if request->path =~ m{/auth/facebook/callback};
135 if (not session('fb_user')) {
136 redirect auth_fb_authenticate_url;
137 }
138 };
139  
140 get '/' => sub {
141 "welcome, " . session('fb_user')->{name};
142 };
143
144 get '/fail' => sub { "FAIL" };
145
146 true;
147
148  
149 =head1 CONCEPT
150  
151 This plugin provides a simple way to authenticate your users through Facebook's
152 OAuth API. It provides you with a helper to build easily a redirect to the
153 authentication URL, defines automatically a callback route handler and saves the
154 authenticated user to your session when done.
155  
156 The authenticated user information will be available as a hash reference under
157 C<session('fb_user')>. Please refer to Facebook's documentation for all available
158 data.
159  
160 =head1 PREREQUISITES
161  
162 In order for this plugin to work, you need the following:
163  
164 =over 4
165  
166 =item * Facebook application
167  
168 Anyone can register a application at L<https://developers.facebook.com/>. When
169 done, make sure to configure the application as a I<Web> application.
170  
171 =item * Configuration
172  
173 You need to configure the plugin first: copy your C<application_id> and C<application_secret>
174 (provided by Facebook) to your Dancer's configuration under
175 C<plugins/Auth::Facebook>:
176  
177 # config.yml
178 ...
179 plugins:
180 'Auth::Facebook':
181 application_id: "1234"
182 application_secret: "abcd"
183 callback_url: "http://localhost:3000/auth/facebook/callback"
184 callback_success: "/"
185 callback_fail: "/fail"
186 scope: "email friends"
187  
188 C<callback_success> , C<callback_fail> and C<scope> are optional and default to
189 '/' , '/fail', and 'email' respectively.
190  
191 Note that you also need to provide your callback url, whose route handler is automatically
192 created by the plugin.
193  
194 =item * Session backend
195  
196 For the authentication process to work, you need a session backend, in order for
197 the plugin to store the authenticated user's information.
198  
199 Use the session backend of your choice, it doesn't make a difference, see
200 L<Dancer::Session> for details about supported session engines, or
201 L<search the CPAN for new ones|http://search.cpan.org/search?query=Dancer-Session>.
202  
203 =back
204  
205 =head1 EXPORT
206  
207 The plugin exports the following symbols to your application's namespace:
208  
209 =head2 facebook
210  
211 The plugin uses a L<Net::Facebook::Oauth2> object to do its job. You can access this
212 object with the C<facebook> symbol, exported by the plugin.
213  
214 =head2 auth_fb_init
215  
216 This function should be called before your route handlers, in order to
217 initialize the underlying L<Net::Facebook::Oauth2> object. It will read your
218 configuration and create a new L<Net::Facebook::Oauth2> instance.
219  
220 =head2 auth_fb_authenticate_url
221  
222 this function returns an authentication URL for redirecting unauthenticated users.
223  
224 hook before => sub {
225 # we don't want a redirect loop here.
226 return if request->path =~ m{/auth/facebook/callback};
227 if (not session('fb_user')) {
228 redirect auth_fb_authenticate_url();
229 }
230 };
231  
232  
233 =head1 ROUTE HANDLERS
234  
235 The plugin defines the following route handler automatically
236  
237 =head2 /auth/facebook/callback
238  
239 This route handler is responsible for catching back a user that has just
240 authenticated herself with Facebook's OAuth. The route handler saves tokens and
241 user information such as email,username and name in the session and then redirects the user to the URI
242 specified by C<callback_success>.
243  
244 If the validation of the token returned by Facebook failed or was denied,
245 the user will be redirect to the URI specified by C<callback_fail>.
246  
247 =head1 ACKNOWLEDGEMENTS
248  
249 This project is a port of L<Dancer::Plugin::Auth::Twitter> written by Alexis Sukrieh which itself is a port of
250 L<Catalyst::Authentication::Credential::Twitter> written by Jesse Stay.
251  
252  
253 =head1 AUTHORS
254  
255 =over 4
256  
257 =item * Prajith Ndimensionz <prajith@ndimensionz.com>
258  
259 =back
260  
261 =head1 COPYRIGHT AND LICENSE
262  
263 This software is copyright (c) 2014-2015 by Prajith Ndimensionz.
264  
265 This is free software; you can redistribute it and/or modify it under
266 the same terms as the Perl 5 programming language system itself.
267  
268 =cut
↑ Summary blib/lib/Dancer/Plugin/Auth/Facebook.pm »