| 1 | package Dancer::Plugin::Auth::Twitter; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | $Dancer::Plugin::Auth::Twitter::AUTHORITY = 'cpan:SUKRIA'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5 | #ABSTRACT: Authenticate with Twitter | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | $Dancer::Plugin::Auth::Twitter::VERSION = '0.07'; | |||||||||||||||||||||||||||||||||||||||||
| 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 Class::Load qw/ load_class /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Net::Twitter::Lite::WithAPIv1_1 0.12006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | # Net::Twitter singleton, accessible via 'twitter' | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | our $_twitter; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 15 | ▶ | sub twitter { $_twitter } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
twitter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | register 'twitter' => \&twitter; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # init method, to create the Net::Twitter object | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | my $consumer_key; | |||||||||||||||||||||||||||||||||||||||||
| 23 | my $consumer_secret; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | my $callback_url; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | my $callback_success; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | my $callback_fail; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | my $engine; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | register 'auth_twitter_init' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | my $config = plugin_setting; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=21.7
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | $consumer_secret = $config->{consumer_secret}; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | $consumer_key = $config->{consumer_key}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | $callback_url = $config->{callback_url}; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | $callback_success = $config->{callback_success} || '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'callback_success'} || '/'
MC/DC0%0/1
$config->{'callback_success'} || '/'
$config->{'callback_success'}
Truth table50%1/2
A
A= $config->{'callback_success'}
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | $callback_fail = $config->{callback_fail} || '/fail'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'callback_fail'} || '/fail'
MC/DC0%0/1
$config->{'callback_fail'} || '/fail'
$config->{'callback_fail'}
Truth table50%1/2
A
A= $config->{'callback_fail'}
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | for my $param (qw/consumer_key consumer_secret callback_url/) { | |||||||||||||||||||||||||||||||||||||||||
| 41 | croak "'$param' is expected but not found in configuration" | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | unless $config->{$param}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | debug "new twitter with $consumer_key , $consumer_secret, $callback_url"; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | $engine = $config->{engine} || 'Net::Twitter::Lite::WithAPIv1_1'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'engine'} || 'Net::Twitter::Lite::WithAPIv1_1'
MC/DC0%0/1
$config->{'engine'} || 'Net::Twitter::Lite::WithAPIv1_1'
$config->{'engine'}
Truth table50%1/2
A
A= $config->{'engine'}
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | $engine .= '::WithAPIv1_1' if $engine eq 'Net::Twitter::Lite'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | die "engine must be 'Net::Twitter' or 'Net::Twitter::Lite::WithAPIv1_1': '$engine' not supported\n" | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | unless grep { $engine eq $_ } qw/ Net::Twitter Net::Twitter::Lite::WithAPIv1_1 /; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | $_twitter = load_class($engine)->new( | |||||||||||||||||||||||||||||||||||||||||
| 55 | ( traits => [ qw/ API::RESTv1_1 OAuth / ] ) x ( $engine eq 'Net::Twitter' ), | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 'consumer_key' => $consumer_key, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 'consumer_secret' => $consumer_secret, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ssl => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # define a route handler that bounces to the OAuth authorization process | ||||||||||||||||||||||||||||||||||||||||||
| 64 | register 'auth_twitter_authorize_url' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (not defined twitter) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | croak "auth_twitter_init must be called first"; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $uri = twitter->get_authorization_url( | |||||||||||||||||||||||||||||||||||||||||
| 70 | 'callback' => $callback_url | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | session request_token => twitter->request_token; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | session request_token_secret => twitter->request_token_secret; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | session access_token => ''; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | session access_token_secret => ''; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | debug "auth URL : $uri"; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return $uri; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # define a route handler that bounces to the OAuth authentication process | ||||||||||||||||||||||||||||||||||||||||||
| 83 | register 'auth_twitter_authenticate_url' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | if (not defined twitter) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | croak "auth_twitter_init must be called first"; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | my $uri = twitter->get_authentication_url( | |||||||||||||||||||||||||||||||||||||||||
| 89 | 'callback' => $callback_url | ||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | session request_token => twitter->request_token; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | session request_token_secret => twitter->request_token_secret; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 2 | session access_token => ''; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | session access_token_secret => ''; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | debug "auth URL : $uri"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | return $uri; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | get '/auth/twitter/callback' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | ▶ | debug "in callback..."; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=24.1
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # A user denying access should be considered a failure | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | ▶ | return redirect $callback_fail if (params->{'denied'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | if ( !session('request_token') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
session("request_token") and session("request_token_secret") and params()->{"oauth_verifier"}
session("request_token") and session("request_token_secret")
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 table25%1/4
A and B and C
A= session("request_token")B= session("request_token_secret")C= params()->{"oauth_verifier"}
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | || !session('request_token_secret') | ||||||||||||||||||||||||||||||||||||||||||
| 110 | || !params->{'oauth_verifier'}) | ||||||||||||||||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return send_error 'no request token present, or no verifier'; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | my $token = session('request_token'); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | my $token_secret = session('request_token_secret'); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | my $access_token = session('access_token'); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | my $access_token_secret = session('access_token_secret'); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | my $verifier = params->{'oauth_verifier'}; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | if (!$access_token && !$access_token_secret) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$access_token or $access_token_secret
MC/DC0%0/2
$access_token or $access_token_secret
$access_token
$access_token_secret
Truth table33%1/3
A or B
A= $access_tokenB= $access_token_secret
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | twitter->request_token($token); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | twitter->request_token_secret($token_secret); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | ($access_token, $access_token_secret) = twitter->request_access_token('verifier' => $verifier); | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # this is in case we need to register the user after the oauth process | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | session access_token => $access_token; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | session access_token_secret => $access_token_secret; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | # get the user | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | twitter->access_token($access_token); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | twitter->access_token_secret($access_token_secret); | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | my $twitter_user_hash; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | $twitter_user_hash = twitter->verify_credentials(); | |||||||||||||||||||||||||||||||||||||||||
| 138 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | if ($@ || !$twitter_user_hash) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $twitter_user_hash
MC/DC0%0/2
$@ or not $twitter_user_hash
$@
not $twitter_user_hash
Truth table33%1/3
A or not(B)
A= $@B= not $twitter_user_hash
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | Dancer::Logger::core("no twitter_user_hash or error: ".$@); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return redirect $callback_fail; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | $twitter_user_hash->{'access_token'} = $access_token; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | $twitter_user_hash->{'access_token_secret'} = $access_token_secret; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # save the user | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | session 'twitter_user' => $twitter_user_hash; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | session 'twitter_access_token' => $access_token, | |||||||||||||||||||||||||||||||||||||||||
| 151 | session 'twitter_access_token_secret' => $access_token_secret, | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | redirect $callback_success; | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 2 | register_plugin; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||||||||||||||||