lib/Dancer/Plugin/Auth/Twitter.pm

filter: Statementstmt 84.8% 67 / 79 Branchbran 50.0% 8 / 16 Conditioncond 56.2% 9 / 16 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 73.2% 96 / 131 CC 16 SCAR 18.5
CC
16
Cov
76%
__ANON__
12.2
__ANON__
6.0
__ANON__
5.2
CRAP 6.4
↑ Summary
1 package Dancer::Plugin::Auth::Twitter;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $Dancer::Plugin::Auth::Twitter::AUTHORITY = 'cpan:SUKRIA';
4 }
5 #ABSTRACT: Authenticate with Twitter
6 2 $Dancer::Plugin::Auth::Twitter::VERSION = '0.08';
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Twitter::API 1.0002;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Net::Twitter singleton, accessible via 'twitter'
16 2 our $_twitter;
17 12 ▶sub twitter { $_twitter }
Subroutine twitter: called CC=1 SCAR=0.0
18 2 register 'twitter' => \&twitter;
19  
20 # init method, to create the Net::Twitter object
21 2 my $consumer_key;
22 my $consumer_secret;
23 2 my $callback_url;
24 2 my $callback_success;
25 2 my $callback_fail;
26  
27 1 ▶register 'auth_twitter_init' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
28 1 my $config = plugin_setting;
29  
30 1 $consumer_secret = $config->{consumer_secret};
31 1 $consumer_key = $config->{consumer_key};
32 1 $callback_url = $config->{callback_url};
33  
34 1 ▶ $callback_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
35 1 ▶ $callback_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
36  
37 1 for my $param (qw/consumer_key consumer_secret callback_url/) {
38 croak "'$param' is expected but not found in configuration"
39 3 ▶ unless $config->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{$param} 0 3 3
40 }
41  
42 1 debug "new twitter with $consumer_key , $consumer_secret, $callback_url";
43  
44 1 $_twitter = Twitter::API->new_with_traits(
45 traits => [ qw/Enchilada/ ],
46 'consumer_key' => $consumer_key,
47 'consumer_secret' => $consumer_secret,
48 );
49  
50 2 };
51  
52 # define a route handler that bounces to the OAuth authorization process
53 0 ▶register 'auth_twitter_authorize_url' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
54 0 ▶ if (not defined twitter) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined twitter()) 0 0 0
55 0 croak "auth_twitter_init must be called first";
56 }
57  
58 0 my $oauth_tokens = twitter->oauth_request_token({
59 callback => $callback_url
60 });
61  
62 my $uri = twitter->oauth_authorization_url({
63 oauth_token => $oauth_tokens->{oauth_token}
64 0 });
65  
66 0 session request_token => $oauth_tokens->{oauth_token};
67 0 session request_token_secret => $oauth_tokens->{oauth_token_secret};
68  
69 0 debug "auth URL : $uri";
70 0 return $uri;
71 2 };
72  
73 # define a route handler that bounces to the OAuth authentication process
74 3 ▶register 'auth_twitter_authenticate_url' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
75 3 ▶ if (not defined twitter) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined twitter()) 0 3 3
76 0 croak "auth_twitter_init must be called first";
77 }
78  
79 3 my $oauth_tokens = twitter->oauth_request_token({
80 callback => $callback_url
81 });
82  
83 my $uri = twitter->oauth_authentication_url({
84 oauth_token => $oauth_tokens->{oauth_token}
85 3 });
86  
87 3 session request_token => $oauth_tokens->{oauth_token};
88 3 session request_token_secret => $oauth_tokens->{oauth_token_secret};
89  
90 3 debug "auth URL : $uri";
91 3 return $uri;
92 2 };
93  
94 2 ▶get '/auth/twitter/callback' => sub {
Subroutine __ANON__: called CC=10 SCAR=25.0
95 2 my $token = session('request_token');
96 2 my $token_secret = session('request_token_secret');
97 2 my $verifier = params->{'oauth_verifier'};
98 2 my $denied = params->{'denied'};
99  
100 2 ▶ if (!$denied && $token && $token_secret && $verifier) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $denied and $token and $token_secret and $verifier) { } 1 1 2
Condition66%6/9
not $denied and $token and $token_secret and $verifier
!ll&&!rl&&r
101
not $denied and $token and $token_secret
!ll&&!rl&&r
101
not $denied and $token
!ll&&!rl&&r
101
MC/DC25%1/4
not $denied and $token and $token_secret and $verifier
not $denied $token $token_secret $verifier
Truth table40%2/5
not(A) and B and C and D
A= not $deniedB= $tokenC= $token_secretD= $verifier
ABCDresult
0XXX0
10XX0
110X0
11100
11111
101 # everything went well:
102 1 my $access = twitter->oauth_access_token({
103 token => $token,
104 token_secret => $token_secret,
105 verifier => $verifier,
106 });
107 1 my $twitter_user_hash;
108 1 my $success = eval {
109 $twitter_user_hash = twitter->verify_credentials({
110 -token => $access->{oauth_token},
111 -token_secret => $access->{oauth_token_secret},
112 1 });
113 1 1;
114 };
115 1 ▶ if (!$success || !$twitter_user_hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success and $twitter_user_hash) 0 1 1
Condition33%1/3
$success and $twitter_user_hash
!ll&&!rl&&r
001
MC/DC0%0/2
$success and $twitter_user_hash
$success $twitter_user_hash
Truth table33%1/3
A and B
A= $successB= $twitter_user_hash
ABresult
0X0
100
111
116 0 Dancer::Logger::core("no twitter_user_hash or error: $@");
117 0 return redirect $callback_fail;
118 }
119 $twitter_user_hash->{'access_token'} = $access->{oauth_token},
120 1 ▶ unless exists $twitter_user_hash->{'access_token'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $twitter_user_hash->{"access_token"} 0 1 1
121 $twitter_user_hash->{'access_token_secret'} = $access->{oauth_token_secret}
122 1 ▶ unless exists $twitter_user_hash->{'access_token_secret'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $twitter_user_hash->{"access_token_secret"} 0 1 1
123  
124 # save the user
125 1 session 'twitter_user' => $twitter_user_hash;
126 1 return redirect $callback_success;
127 }
128 else {
129 # user did NOT authenticate/authorize:
130 1 session request_token => '';
131 1 session request_token_secret => '';
132 1 ▶ return redirect $callback_fail if $denied; # user denied access
Branch: 1 branch
BranchTrueFalseTotal
if $denied 1 0 1
133 0 return send_error 'no request token present, or no verifier';
134 }
135 2 };
136  
137 2 register_plugin;
138  
139 __END__
↑ Summary