blib/lib/Dancer/Plugin/Auth/Facebook.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 114 SCAR 6.9
CC
1
Cov
0%
facebook
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Dancer::Plugin::Auth::Facebook;
2  
3 $Dancer::Plugin::Auth::Facebook::VERSION = '0.06';
4  
5 use strict;
6 use warnings;
7  
8 use Dancer ':syntax';
9 use Dancer::Plugin;
10 use Net::Facebook::Oauth2;
11 use Carp 'croak';
12  
13 my $_FB;
14 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 my $config = plugin_setting;
28 $application_id = $config->{application_id};
29 $application_secret = $config->{application_secret};
30 $cb_url = $config->{callback_url};
31  
32 $cb_success = $config->{callback_success} || '/';
33 $cb_fail = $config->{callback_fail} || '/fail';
34 $fb_scope = $config->{scope};
35 $me_fields = $config->{fields};
36  
37 if (defined $fb_scope) {
38 foreach my $fs (split(/\s+/, $fb_scope)) {
39 next unless ($fs =~ m/^[_A-Za-z0-9\.]+$/);
40 push(@scope, $fs);
41 }
42 }
43 else {
44 push(@scope, 'email');
45 }
46  
47 for my $param (qw/application_id application_secret callback_url/) {
48 croak "'$param' is expected but not found in configuration" unless $config->{$param};
49 }
50  
51 debug "new facebook with $application_id, $application_secret, $cb_url";
52  
53 $_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 if (not defined facebook ) {
63 croak "auth_fb_init must be called first";
64 }
65  
66 my $url = facebook->get_authorization_url(
67 scope => \@scope,
68 display => 'page',
69 );
70  
71 session fb_access_token => '';
72 debug "fb_auth_url: $url";
73  
74 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__
119  
120 =pod
121  
122 =head1 NAME
123  
124 Dancer::Plugin::Auth::Facebook - Authenticate with Facebook OAuth
125  
126 =head1 SYNOPSIS
127  
128 package plugin::test;
129 use Dancer ':syntax';
130 use Dancer::Plugin::Auth::Facebook;
131  
132 auth_fb_init();
133  
134 hook before => sub {
135 #we don't want a redirect loop here.
136 return if request->path =~ m{/auth/facebook/callback};
137 if (not session('fb_user')) {
138 redirect auth_fb_authenticate_url;
139 }
140 };
141  
142 get '/' => sub {
143 "welcome, " . session('fb_user')->{name};
144 };
145
146 get '/fail' => sub { "FAIL" };
147
148 true;
149
150  
151 =head1 CONCEPT
152  
153 This plugin provides a simple way to authenticate your users through Facebook's
154 OAuth API. It provides you with a helper to build easily a redirect to the
155 authentication URL, defines automatically a callback route handler and saves the
156 authenticated user to your session when done.
157  
158 The authenticated user information will be available as a hash reference under
159 C<session('fb_user')>. You should probably associate the C<id> field to that
160 user, so you know which of your users just completed the login.
161  
162 The information under C<fb_user> is returned by the current user's basic
163 endpoint, known on Facebook's API as C</me>. You should note that Facebook
164 has a habit of changing which fields are returned on that endpoint. To force
165 any particular fields, please use the C<fields> setting in your plugin
166 configuration as shown below.
167  
168 Please refer to L<< Facebook's documentation | https://developers.facebook.com/docs/graph-api/reference/v2.8/user >>
169 for all available data.
170  
171 =head1 FACEBOOK GRAPH API VERSION
172  
173 This module complies to Facebook Graph API version 2.8, the latest
174 at the time of publication, B<< scheduled for deprecation on October 5th, 2018 >>.
175  
176 One month prior to that, Net::Facebook::Oauth2 (which this module uses to
177 access Facebook's API) will trigger a warning message during your Dancer
178 app's startup.
179  
180  
181 =head1 PREREQUISITES
182  
183 In order for this plugin to work, you need the following:
184  
185 =over 4
186  
187 =item * Facebook application
188  
189 Anyone can register a application at L<https://developers.facebook.com/>. When
190 done, make sure to configure the application as a I<Web> application.
191  
192 =item * Configuration
193  
194 You need to configure the plugin first: copy your C<application_id> and C<application_secret>
195 (provided by Facebook) to your Dancer's configuration under
196 C<plugins/Auth::Facebook>:
197  
198 # config.yml
199 ...
200 plugins:
201 'Auth::Facebook':
202 application_id: "1234"
203 application_secret: "abcd"
204 callback_url: "http://localhost:3000/auth/facebook/callback"
205 callback_success: "/"
206 callback_fail: "/fail"
207 scope: "email friends"
208 fields: "id,name,email"
209  
210 C<callback_success> , C<callback_fail>, C<scope> and C<fields> are optional
211 and default to '/' , '/fail', 'email' and (empty) respectively.
212  
213 Note that you also need to provide your callback url, whose route handler is automatically
214 created by the plugin.
215  
216 =item * Session backend
217  
218 For the authentication process to work, you need a session backend, in order for
219 the plugin to store the authenticated user's information.
220  
221 Use the session backend of your choice, it doesn't make a difference, see
222 L<Dancer::Session> for details about supported session engines, or
223 L<search the CPAN for new ones|http://search.cpan.org/search?query=Dancer-Session>.
224  
225 =back
226  
227 =head1 EXPORT
228  
229 The plugin exports the following symbols to your application's namespace:
230  
231 =head2 facebook
232  
233 The plugin uses a L<Net::Facebook::Oauth2> object to do its job. You can access this
234 object with the C<facebook> symbol, exported by the plugin.
235  
236 =head2 auth_fb_init
237  
238 This function should be called before your route handlers, in order to
239 initialize the underlying L<Net::Facebook::Oauth2> object. It will read your
240 configuration and create a new L<Net::Facebook::Oauth2> instance.
241  
242 =head2 auth_fb_authenticate_url
243  
244 this function returns an authentication URL for redirecting unauthenticated users.
245  
246 hook before => sub {
247 # we don't want a redirect loop here.
248 return if request->path =~ m{/auth/facebook/callback};
249 if (not session('fb_user')) {
250 redirect auth_fb_authenticate_url();
251 }
252 };
253  
254  
255 =head1 ROUTE HANDLERS
256  
257 The plugin defines the following route handler automatically
258  
259 =head2 /auth/facebook/callback
260  
261 This route handler is responsible for catching back a user that has just
262 authenticated herself with Facebook's OAuth. The route handler saves tokens and
263 user information such as email,username and name in the session and then redirects the user to the URI
264 specified by C<callback_success>.
265  
266 If the validation of the token returned by Facebook failed or was denied,
267 the user will be redirect to the URI specified by C<callback_fail>.
268  
269 =head1 ACKNOWLEDGEMENTS
270  
271 This project is a port of L<Dancer::Plugin::Auth::Twitter> written by Alexis Sukrieh which itself is a port of
272 L<Catalyst::Authentication::Credential::Twitter> written by Jesse Stay.
273  
274  
275 =head1 AUTHORS
276  
277 =over 4
278  
279 =item * Prajith Ndimensionz <prajith@ndimensionz>
280  
281 =back
282  
283 =head1 COPYRIGHT AND LICENSE
284  
285 This software is copyright (c) 2014-2016 by Prajith Ndimensionz.
286  
287 This is free software; you can redistribute it and/or modify it under
288 the same terms as the Perl 5 programming language system itself.
289  
290 =cut