blib/lib/Amon2/Auth/Site/Hatena.pm

filter: Statementstmt 54.8% 34 / 62 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 4 totaltotal 37.3% 40 / 107 CC 12 SCAR 33.6
CC
12
Cov
40%
callback
72.0
detect_authorize_url_from
12.0
auth_uri
12.0
CRAP 28.7
↑ Summary
1 package Amon2::Auth::Site::Hatena;
2 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use OAuth::Lite::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use OAuth::Lite::Consumer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Woothee;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.04';
10  
11 1 has consumer_key => (
12 is => 'ro',
13 isa => 'Str',
14 required => 1,
15 );
16  
17 1 has consumer_secret => (
18 is => 'ro',
19 isa => 'Str',
20 required => 1,
21 );
22  
23 has scope => (
24 is => 'ro',
25 isa => 'ArrayRef',
26 0 ▶ default => sub { +[qw(read_public)] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 1 );
28  
29 1 has user_info => (
30 is => 'rw',
31 isa => 'Bool',
32 default => 1,
33 );
34  
35 has ua => (
36 is => 'ro',
37 isa => 'OAuth::Lite::Consumer',
38 lazy => 1,
39 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 OAuth::Lite::Consumer->new(
41 consumer_key => $_[0]->consumer_key,
42 consumer_secret => $_[0]->consumer_secret,
43 site => $_[0]->site,
44 request_token_path => $_[0]->request_token_path,
45 access_token_path => $_[0]->access_token_path,
46 );
47 },
48 1 );
49  
50 1 has site => (
51 is => 'ro',
52 isa => 'Str',
53 default => 'https://www.hatena.com',
54 );
55  
56 1 has request_token_path => (
57 is => 'ro',
58 isa => 'Str',
59 default => '/oauth/initiate',
60 );
61  
62 1 has access_token_path => (
63 is => 'ro',
64 isa => 'Str',
65 default => '/oauth/token',
66 );
67  
68 1 has authorize_url => (
69 is => 'ro',
70 isa => 'Str',
71 default => 'https://www.hatena.ne.jp/oauth/authorize',
72 );
73  
74 1 has authorize_url_touch => (
75 is => 'ro',
76 isa => 'Str',
77 default => 'https://www.hatena.ne.jp/touch/oauth/authorize',
78 );
79  
80 1 has authorize_url_mobile => (
81 is => 'ro',
82 isa => 'Str',
83 default => 'http://www.hatena.ne.jp/mobile/oauth/authorize',
84 );
85  
86 1 has user_info_url => (
87 is => 'ro',
88 isa => 'Str',
89 default => 'http://n.hatena.com/applications/my.json',
90 );
91  
92 1 has redirect_url => (
93 is => 'ro',
94 isa => 'Str',
95 );
96  
97 1 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 __PACKAGE__->meta->make_immutable;
99  
100 0 ▶sub moniker { 'hatena' }
Subroutine moniker: not called CC=1 SCAR=6.9
Pod undocumented
101  
102 0 ▶sub detect_authorize_url_from {
Subroutine detect_authorize_url_from: not called CC=3 SCAR=24.8
Pod undocumented
103 0 my ($self, $c) = @_;
104  
105 0 my $category = Woothee->parse($c->req->env->{'HTTP_USER_AGENT'})->{category};
106 0 ▶ $category eq 'smartphone' ? $self->authorize_url_touch :
Branch: 2 branches
BranchTrueFalseTotal
$category eq 'smartphone' ? : 0 0 0
$category eq 'mobilephone' ? : 0 0 0
107 $category eq 'mobilephone' ? $self->authorize_url_mobile :
108 $self->authorize_url ;
109 }
110  
111 0 ▶sub auth_uri {
Subroutine auth_uri: not called CC=3 SCAR=24.8
Pod undocumented
112 0 my ($self, $c, $callback_uri) = @_;
113  
114 my $request_token = $self->ua->get_request_token(
115 callback_url => $callback_uri || $self->redirect_url,
116 0 ▶ scope => join(',', @{$self->scope}),
Branch: 1 branch
BranchTrueFalseTotal
my $request_token = $self->ua->get_request_token("callback_url", $callback_uri || $self->redirect_url, "scope", join(",", @{$self->scope;})) or die $self->ua->errstr 0 0 0
Condition0%0/3
$callback_uri || $self->redirect_url
l!l&&r!l&&!r
000
MC/DC0%0/2
$callback_uri || $self->redirect_url
$callback_uri $self->redirect_url
Truth table0%0/3
A || B
A= $callback_uriB= $self->redirect_url
ABresult
1X1
011
000
117 ) or die $self->ua->errstr;
118  
119 0 $c->session->set(auth_hatena => {
120 request_token => $request_token->token,
121 request_token_secret => $request_token->secret,
122 });
123  
124 0 $self->ua->{authorize_path} = $self->detect_authorize_url_from($c);
125 0 $self->ua->url_to_authorize(token => $request_token);
126 }
127  
128 0 ▶sub callback {
Subroutine callback: not called CC=8 SCAR=42.8
Pod undocumented
129 0 my ($self, $c, $callback) = @_;
130 0 my $error = $callback->{on_error};
131  
132 0 ▶ my $verifier = $c->req->param('oauth_verifier')
Branch: 1 branch
BranchTrueFalseTotal
my $verifier = $c->req->param("oauth_verifier") or return &$error("Cannot get a `oauth_verifier' parameter") 0 0 0
133 or return $error->("Cannot get a `oauth_verifier' parameter");
134  
135 0 ▶ my $session = $c->session->get('auth_hatena') || {};
Condition0%0/2
$c->session->get('auth_hatena') || {}
l!l
00
MC/DC0%0/1
$c->session->get('auth_hatena') || {}
$c->session->get('auth_hatena')
Truth table0%0/2
A
A= $c->session->get('auth_hatena')
Aresult
11
00
136 0 my $token = $session->{request_token};
137 0 my $token_secret = $session->{request_token_secret};
138  
139 0 ▶ return $error->('request_token, request_token_secret are both required')
Branch: 1 branch
BranchTrueFalseTotal
unless $token and $token_secret 0 0 0
Condition0%0/3
$token and $token_secret
!ll&&!rl&&r
000
MC/DC0%0/2
$token and $token_secret
$token $token_secret
Truth table0%0/3
A and B
A= $tokenB= $token_secret
ABresult
0X0
100
111
140 if (!$token || !$token_secret);
141  
142 0 my $request_token = OAuth::Lite::Token->new(
143 token => $token,
144 secret => $token_secret,
145 );
146 0 ▶ my $access_token = $self->ua->get_access_token(
Branch: 1 branch
BranchTrueFalseTotal
my $access_token = $self->ua->get_access_token("token", $request_token, "verifier", $verifier) or return &$error($self->ua->errstr) 0 0 0
147 token => $request_token,
148 verifier => $verifier,
149 ) or return $error->($self->ua->errstr);
150  
151 0 my @args = ($access_token->token, $access_token->secret);
152  
153 0 ▶ if ($self->user_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->user_info) 0 0 0
154 0 my $res = $self->ua->get($self->user_info_url);
155 0 ▶ return $error->($self->ua->errstr) if $res->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_error 0 0 0
156  
157 0 my $data = decode_json($res->decoded_content);
158 0 push @args, $data;
159 }
160  
161 0 $callback->{on_finished}->(@args);
162 }
163  
164 1 1;
165  
166 __END__
↑ Summary