blib/lib/Dancer/Plugin/Auth/Twitter.pm

filter: Statementstmt 34.8% 30 / 86 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 54.5% 6 / 11 Podpod n/a totaltotal 26.4% 36 / 136 CC 15 SCAR 36.8
CC
15
Cov
26%
__ANON__
90.0
__ANON__
30.0
__ANON__
6.0
CRAP 39.7
↑ Summary Build »
1 package Dancer::Plugin::Auth::Twitter;
2 #ABSTRACT: Authenticate with Twitter
3 2 $Dancer::Plugin::Auth::Twitter::VERSION = '0.06';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Net::Twitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Net::Twitter singleton, accessible via 'twitter'
13 2 my $_twitter;
14 0 ▶sub twitter { $_twitter }
Subroutine twitter: not called CC=1 SCAR=6.9
15 2 register 'twitter' => \&twitter;
16  
17 # init method, to create the Net::Twitter object
18 2 my $consumer_key;
19 my $consumer_secret;
20 2 my $callback_url;
21 2 my $callback_success;
22 2 my $callback_fail;
23  
24 register 'auth_twitter_init' => sub {
25 0 ▶ my $config = plugin_setting;
Subroutine __ANON__: not called CC=5 SCAR=34.0
26 0 $consumer_secret = $config->{consumer_secret};
27 0 $consumer_key = $config->{consumer_key};
28 0 $callback_url = $config->{callback_url};
29  
30 0 ▶ $callback_success = $config->{callback_success} || '/';
Condition0%0/2
$config->{'callback_success'} || '/'
l!l
00
MC/DC0%0/1
$config->{'callback_success'} || '/'
$config->{'callback_success'}
Truth table0%0/2
A
A= $config->{'callback_success'}
Aresult
11
00
31 0 ▶ $callback_fail = $config->{callback_fail} || '/fail';
Condition0%0/2
$config->{'callback_fail'} || '/fail'
l!l
00
MC/DC0%0/1
$config->{'callback_fail'} || '/fail'
$config->{'callback_fail'}
Truth table0%0/2
A
A= $config->{'callback_fail'}
Aresult
11
00
32  
33 0 for my $param (qw/consumer_key consumer_secret callback_url/) {
34 croak "'$param' is expected but not found in configuration"
35 0 ▶ unless $config->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{$param} 0 0 0
36 }
37  
38 0 debug "new twitter with $consumer_key , $consumer_secret, $callback_url";
39  
40 0 $_twitter = Net::Twitter->new({
41 'traits' => ['API::RESTv1_1', 'OAuth'],
42 'consumer_key' => $consumer_key,
43 'consumer_secret' => $consumer_secret,
44 ssl => 1,
45 });
46  
47 2 };
48  
49 # define a route handler that bounces to the OAuth authorization process
50 register 'auth_twitter_authorize_url' => sub {
51 0 ▶ if (not defined twitter) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless (defined twitter()) 0 0 0
52 0 croak "auth_twitter_init must be called first";
53 }
54  
55 0 my $uri = twitter->get_authorization_url(
56 'callback' => $callback_url
57 );
58  
59 0 session request_token => twitter->request_token;
60 0 session request_token_secret => twitter->request_token_secret;
61 0 session access_token => '';
62 0 session access_token_secret => '';
63  
64 0 debug "auth URL : $uri";
65 0 return $uri;
66 2 };
67  
68 # define a route handler that bounces to the OAuth authentication process
69 register 'auth_twitter_authenticate_url' => sub {
70 0 ▶ if (not defined twitter) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless (defined twitter()) 0 0 0
71 0 croak "auth_twitter_init must be called first";
72 }
73  
74 0 my $uri = twitter->get_authentication_url(
75 'callback' => $callback_url
76 );
77  
78 0 session request_token => twitter->request_token;
79 0 session request_token_secret => twitter->request_token_secret;
80 0 session access_token => '';
81 0 session access_token_secret => '';
82  
83 0 debug "auth URL : $uri";
84 0 return $uri;
85 2 };
86  
87 get '/auth/twitter/callback' => sub {
88  
89 0 ▶ debug "in callback...";
Subroutine __ANON__: not called CC=9 SCAR=45.0
90  
91 # A user denying access should be considered a failure
92 0 ▶ return redirect $callback_fail if (params->{'denied'});
Branch: 1 branch
BranchTrueFalseTotal
if params()->{"denied"} 0 0 0
93  
94 0 ▶ if ( !session('request_token')
Branch: 1 branch
BranchTrueFalseTotal
unless (session("request_token") and session("request_token_secret") and params()->{"oauth_verifier"}) 0 0 0
Condition0%0/6
session("request_token") and session("request_token_secret") and params()->{"oauth_verifier"}
!ll&&!rl&&r
000
session("request_token") and session("request_token_secret")
!ll&&!rl&&r
000
MC/DC0%0/3
session("request_token") and session("request_token_secret") and params()->{"oauth_verifier"}
session("request_token") session("request_token_secret") params()->{"oauth_verifier"}
Truth table0%0/4
A and B and C
A= session("request_token")B= session("request_token_secret")C= params()->{"oauth_verifier"}
ABCresult
0XX0
10X0
1100
1111
95 || !session('request_token_secret')
96 || !params->{'oauth_verifier'})
97 {
98 0 return send_error 'no request token present, or no verifier';
99 }
100  
101 0 my $token = session('request_token');
102 0 my $token_secret = session('request_token_secret');
103 0 my $access_token = session('access_token');
104 0 my $access_token_secret = session('access_token_secret');
105 0 my $verifier = params->{'oauth_verifier'};
106  
107 0 ▶ if (!$access_token && !$access_token_secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token or $access_token_secret) 0 0 0
Condition0%0/3
$access_token or $access_token_secret
l!l&&r!l&&!r
000
MC/DC0%0/2
$access_token or $access_token_secret
$access_token $access_token_secret
Truth table0%0/3
A or B
A= $access_tokenB= $access_token_secret
ABresult
1X1
011
000
108 0 twitter->request_token($token);
109 0 twitter->request_token_secret($token_secret);
110 0 ($access_token, $access_token_secret) = twitter->request_access_token('verifier' => $verifier);
111  
112 # this is in case we need to register the user after the oauth process
113 0 session access_token => $access_token;
114 0 session access_token_secret => $access_token_secret;
115 }
116  
117 # get the user
118 0 twitter->access_token($access_token);
119 0 twitter->access_token_secret($access_token_secret);
120  
121 0 my $twitter_user_hash;
122 0 eval {
123 0 $twitter_user_hash = twitter->verify_credentials();
124 };
125  
126 0 ▶ if ($@ || !$twitter_user_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $twitter_user_hash) 0 0 0
Condition0%0/3
$@ or not $twitter_user_hash
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $twitter_user_hash
$@ not $twitter_user_hash
Truth table0%0/3
A or not(B)
A= $@B= not $twitter_user_hash
ABresult
1X1
011
000
127 0 Dancer::Logger::core("no twitter_user_hash or error: ".$@);
128 0 return redirect $callback_fail;
129 }
130  
131 0 $twitter_user_hash->{'access_token'} = $access_token;
132 0 $twitter_user_hash->{'access_token_secret'} = $access_token_secret;
133  
134 # save the user
135 0 session 'twitter_user' => $twitter_user_hash;
136 0 session 'twitter_access_token' => $access_token,
137 session 'twitter_access_token_secret' => $access_token_secret,
138  
139 redirect $callback_success;
140 2 };
141
142 2 register_plugin;
143  
144 __END__
145  
146 =pod
147  
148 =head1 NAME
149  
150 Dancer::Plugin::Auth::Twitter - Authenticate with Twitter
151  
152 =head1 VERSION
153  
154 version 0.06
155  
156 =head1 SYNOPSIS
157  
158 package SomeDancerApp;
159 use Dancer ':syntax';
160 use Dancer::Plugin::Auth::Twitter;
161  
162 auth_twitter_init();
163  
164 before sub {
165 if (not session('twitter_user')) {
166 redirect auth_twitter_authenticate_url;
167 }
168 };
169  
170 get '/' => sub {
171 "welcome, ".session('twitter_user')->{'screen_name'};
172 };
173  
174 get '/fail' => sub { "FAIL" };
175  
176 ...
177  
178 =head1 CONCEPT
179  
180 This plugin provides a simple way to authenticate your users through Twitter's
181 OAuth API. It provides you with a helper to build easily a redirect to the
182 authentication URI, defines automatically a callback route handler and saves the
183 authenticated user to your session when done.
184  
185 =head1 PREREQUESITES
186  
187 In order for this plugin to work, you need the following:
188  
189 =over 4
190  
191 =item * Twitter application
192  
193 Anyone can register a Twitter application at L<http://dev.twitter.com/>. When
194 done, make sure to configure the application as a I<Web> application.
195  
196 =item * Configuration
197  
198 You need to configure the plugin first: copy your C<consumer_key> and C<consumer_secret>
199 (provided by Twitter) to your Dancer's configuration under
200 C<plugins/Auth::Twitter>:
201  
202 # config.yml
203 ...
204 plugins:
205 "Auth::Twitter":
206 consumer_key: "1234"
207 consumer_secret: "abcd"
208 callback_url: "http://localhost:3000/auth/twitter/callback"
209 callback_success: "/"
210 callback_fail: "/fail"
211  
212 C<callback_success> and C<callback_fail> are optional and default to
213 '/' and '/fail', respectively.
214  
215 Note that you also need to provide your callback url, whose route handler is automatically
216 created by the plugin.
217  
218 =item * Session backend
219  
220 For the authentication process to work, you need a session backend, in order for
221 the plugin to store the authenticated user's information.
222  
223 Use the session backend of your choice, it doesn't make a difference, see
224 L<Dancer::Session> for details about supported session engines, or
225 L<search the CPAN for new ones|http://search.cpan.org/search?query=Dancer-Session>.
226  
227 =back
228  
229 =head1 EXPORT
230  
231 The plugin exports the following symbols to your application's namespace:
232  
233 =head2 twitter
234  
235 The plugin uses a L<Net::Twitter> object to do its job. You can access this
236 object with the C<twitter> symbol, exported by the plugin.
237  
238 =head2 auth_twitter_init
239  
240 This function should be called before your route handlers, in order to
241 initialize the underlying L<Net::Twitter> object. It will read your
242 configuration and create a new L<Net::Twitter> instance.
243  
244 =head2 auth_twitter_authorize_url
245  
246 This function returns an authorize URI for redirecting unauthenticated users.
247 You should use this in a before filter like the following:
248  
249 before sub {
250 # we don't want to bounce for ever!
251 return if request->path =~ m{/auth/twitter/callback};
252
253 if (not session('twitter_user')) {
254 redirect auth_twitter_authorize_url();
255 }
256 };
257  
258 When the user authenticate with Twitter's OAuth interface, she's going to be
259 bounced back to C</auth/twitter/callback>.
260  
261 =head2 auth_twitter_authenticate_url
262  
263 Similar to auth_twitter_authorize_url, but this function instead returns an
264 authenticate instead of authorize URI for redirecting unauthenticated users,
265 which results in a slightly different behaviour.
266  
267 See L<https://dev.twitter.com/pages/sign_in_with_twitter|here> to learn about
268 the differences.
269  
270 =head1 ROUTE HANDLERS
271  
272 The plugin defines the following route handler automatically
273  
274 =head2 /auth/twitter/callback
275  
276 This route handler is responsible for catching back a user that has just
277 authenticated herself with Twitter's OAuth. The route handler saves tokens and
278 user information in the session and then redirects the user to the URI
279 specified by C<callback_success>.
280  
281 If the validation of the token returned by Twitter failed or was denied,
282 the user will be redirect to the URI specified by C<callback_fail>.
283  
284 =head1 ACKNOWLEDGEMENTS
285  
286 This plugin has been written as a port of
287 L<Catalyst::Authentication::Credential::Twitter> written by
288 Jesse Stay.
289  
290 This plugin was part of the Perl Dancer Advent Calendar 2010.
291  
292 =head1 AUTHORS
293  
294 =over 4
295  
296 =item *
297  
298 Alexis Sukrieh <sukria@sukria.net>
299  
300 =item *
301  
302 Dancer Core Developers
303  
304 =back
305  
306 =head1 COPYRIGHT AND LICENSE
307  
308 This software is copyright (c) 2010 by Alexis Sukrieh.
309  
310 This is free software; you can redistribute it and/or modify it under
311 the same terms as the Perl 5 programming language system itself.
312  
313 =cut
↑ Summary Build »