| 1 | package Dancer::Plugin::Auth::Facebook; | ||
| 2 | |||
| 3 | $Dancer::Plugin::Auth::Facebook::VERSION = '0.05'; | ||
| 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 | |||
| 25 | register 'auth_fb_init' => sub { | ||
| 26 | my $config = plugin_setting; | ||
| 27 | $application_id = $config->{application_id}; | ||
| 28 | $application_secret = $config->{application_secret}; | ||
| 29 | $cb_url = $config->{callback_url}; | ||
| 30 | |||
| 31 | $cb_success = $config->{callback_success} || '/'; | ||
| 32 | $cb_fail = $config->{callback_fail} || '/fail'; | ||
| 33 | $fb_scope = $config->{scope}; | ||
| 34 | |||
| 35 | if (defined $fb_scope) { | ||
| 36 | foreach my $fs (split(/\s+/, $fb_scope)) { | ||
| 37 | next unless ($fs =~ m/^[_A-Za-z0-9\.]+$/); | ||
| 38 | push(@scope, $fs); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | else { | ||
| 42 | push(@scope, 'email'); | ||
| 43 | } | ||
| 44 | |||
| 45 | for my $param (qw/application_id application_secret callback_url/) { | ||
| 46 | croak "'$param' is expected but not found in configuration" unless $config->{$param}; | ||
| 47 | } | ||
| 48 | |||
| 49 | debug "new facebook with $application_id, $application_secret, $cb_url"; | ||
| 50 | |||
| 51 | $_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 | if (not defined facebook ) { | ||
| 61 | croak "auth_fb_init must be called first"; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $url = facebook->get_authorization_url( | ||
| 65 | scope => \@scope, | ||
| 66 | display => 'page', | ||
| 67 | ); | ||
| 68 | |||
| 69 | session fb_access_token => ''; | ||
| 70 | debug "fb_auth_url: $url"; | ||
| 71 | |||
| 72 | 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__ | ||
| 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 |