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

filter: Statementstmt 34.1% 41 / 120 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 38.8% 7 / 18 Podpod 0.0% 0 / 9 totaltotal 24.4% 48 / 196 CC 17 SCAR 36.2
CC
17
Cov
26%
callback
110.0
auth_uri
20.0
get_state
12.0
CRAP 37.4
↑ Summary
1 package Amon2::Auth::Site::LINE;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 1 our $VERSION = '0.05';
12  
13 0 ▶sub moniker { 'line' }
Subroutine moniker: not called CC=1 SCAR=6.9
Pod undocumented
14  
15 1 has client_id => (
16 is => 'ro',
17 isa => 'Str',
18 required => 1,
19 );
20
21 1 has client_secret => (
22 is => 'ro',
23 isa => 'Str',
24 required => 1,
25 );
26
27 1 has redirect_uri => (
28 is => 'ro',
29 isa => 'Str',
30 );
31  
32 1 has state => (
33 is => 'ro',
34 isa => 'Str',
35 );
36  
37 has scope => (
38 is => 'ro',
39 isa => 'ArrayRef',
40 0 ▶ default => sub { [qw(profile)] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 1 );
42  
43 1 has nonce => (
44 is => 'ro',
45 isa => 'Str',
46 );
47  
48 1 has prompt => (
49 is => 'ro',
50 isa => 'Str',
51 );
52  
53 1 has max_age => (
54 is => 'ro',
55 isa => 'Int',
56 );
57  
58 1 has ui_locales => (
59 is => 'ro',
60 isa => 'Str',
61 );
62  
63 1 has bot_prompt => (
64 is => 'ro',
65 isa => 'Str',
66 );
67  
68 1 has user_info => (
69 is => 'rw',
70 isa => 'Bool',
71 default => 1,
72 );
73  
74 1 has state_session_key => (
75 is => 'ro',
76 isa => 'Str',
77 default => 'line_login_state',
78 );
79  
80 1 has nonce_session_key => (
81 is => 'ro',
82 isa => 'Str',
83 default => 'line_login_nonce',
84 );
85  
86 1 has authorize_url => (
87 is => 'ro',
88 isa => 'Str',
89 default => 'https://access.line.me/oauth2/v2.1/authorize',
90 );
91  
92 1 has access_token_url => (
93 is => 'ro',
94 isa => 'Str',
95 default => 'https://api.line.me/oauth2/v2.1/token',
96 );
97  
98 1 has verify_url => (
99 is => 'ro',
100 isa => 'Str',
101 default => 'https://api.line.me/oauth2/v2.1/verify',
102 );
103  
104 1 has profile_url => (
105 is => 'ro',
106 isa => 'Str',
107 default => 'https://api.line.me/v2/profile',
108 );
109  
110 has ua => (
111 is => 'ro',
112 isa => 'LWP::UserAgent',
113 lazy => 1,
114 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 LWP::UserAgent->new(agent => "Amon2::Auth::Site::LINE/$VERSION");
116 },
117 1 );
118  
119 0 ▶sub auth_uri {
Subroutine auth_uri: not called CC=4 SCAR=30.0
Pod undocumented
120 0 my($self, $c, $callback_uri) = @_;
121  
122 # required parameters
123 0 ▶ my $redirect_uri = $self->redirect_uri || $callback_uri;
Condition0%0/3
$self->redirect_uri || $callback_uri
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->redirect_uri || $callback_uri
$self->redirect_uri $callback_uri
Truth table0%0/3
A || B
A= $self->redirect_uriB= $callback_uri
ABresult
1X1
011
000
124 my %params = (
125 response_type => 'code',
126 client_id => $self->client_id,
127 0 scope => join(' ', @{$self->scope}),
128 redirect_uri => $redirect_uri,
129 state => $self->get_state($c),
130 );
131  
132 # optional parameters
133 0 $params{nonce} = $self->get_nonce($c);
134  
135 0 for my $key (qw(prompt max_age ui_locales bot_prompt)) {
136 0 my $value = $self->$key;
137 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
138 0 $params{$key} = $value;
139 }
140 }
141  
142 0 my $auth_uri = URI->new($self->authorize_url);
143 0 $auth_uri->query_form(%params);
144  
145 0 return $auth_uri->as_string;
146 }
147  
148 0 ▶sub callback {
Subroutine callback: not called CC=10 SCAR=47.0
Pod undocumented
149 0 my($self, $c, $callback) = @_;
150  
151 # state mismatch
152 0 ▶ if ($c->req->param('state') ne $self->get_state($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->param("state") ne $self->get_state($c)) 0 0 0
153 0 return $callback->{on_error}->('state parameter mismatch');
154 }
155  
156 # access denied
157 0 ▶ if ($c->req->param('error')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->param("error")) 0 0 0
158 0 return $callback->{on_error}->($c->req->param('error_description'));
159 }
160
161 0 my @args = ();
162  
163 0 my %api_response = ();
164  
165 # getting an access token
166 0 my $token_data;
167 {
168 0 ▶ my $redirect_uri = $self->redirect_uri || do { # it should be me
Condition0%0/3
$self->redirect_uri || do { my $current_uri = $c->req->uri; $current_uri->query(undef); $current_uri->as_string }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->redirect_uri || do { my $current_uri = $c->req->uri; $current_uri->query(undef); $current_uri->as_string }
$self->redirect_uri do { my $current_uri = $c->req->uri; $current_uri->query(undef); $current_uri->as_string }
Truth table0%0/3
A || B
A= $self->redirect_uriB= do { my $current_uri = $c->req->uri; $current_uri->query(undef); $current_uri->as_string }
ABresult
1X1
011
000
169 0 my $current_uri = $c->req->uri;
170 0 $current_uri->query(undef);
171 0 $current_uri->as_string;
172 };
173 0 my $res = $self->ua->post($self->access_token_url => +{
174 grant_type => 'authorization_code',
175 code => $c->req->param('code'),
176 redirect_uri => $redirect_uri,
177 client_id => $self->client_id,
178 client_secret => $self->client_secret,
179 });
180 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
181 0 warn $res->decoded_content;
182 0 return $callback->{on_error}->($res->status_line);
183 }
184  
185 0 $token_data = decode_json($res->content);
186 0 %api_response = (%api_response, %$token_data);
187 }
188  
189 # verify access token
190 0 my $verify_data;
191 {
192 0 my $uri = URI->new($self->verify_url);
193 0 $uri->query_form(access_token => $token_data->{access_token});
194  
195 0 my $res = $self->ua->get($uri->as_string);
196 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
197 0 warn $res->decoded_content;
198 0 return $callback->{on_error}->($res->status_line);
199 }
200  
201 0 $verify_data = decode_json($res->content);
202 0 ▶ if ($verify_data->{client_id} ne $self->client_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verify_data->{"client_id"} ne $self->client_id) 0 0 0
203 0 return $callback->{on_error}->('client_id mismatch');
204 }
205  
206 0 push @args, $token_data->{access_token};
207 0 %api_response = (%api_response, %$verify_data);
208 }
209  
210 # get user profile
211 0 ▶ if ($self->user_info && $verify_data->{scope} =~ /\bprofile\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->user_info and $verify_data->{"scope"} =~ /\bprofile\b/) 0 0 0
Condition0%0/3
$self->user_info and $verify_data->{"scope"} =~ /\bprofile\b/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->user_info and $verify_data->{"scope"} =~ /\bprofile\b/
$self->user_info $verify_data->{"scope"} =~ /\bprofile\b/
Truth table0%0/3
A and B
A= $self->user_infoB= $verify_data->{"scope"} =~ /\bprofile\b/
ABresult
0X0
100
111
212 0 my $uri = URI->new($self->profile_url);
213 my $res = $self->ua->get(
214 $uri->as_string,
215 Authorization => 'Bearer ' . $token_data->{access_token},
216 0 );
217 0 ▶ $res->is_success or do {
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success or do { warn $res->decoded_content; return $callback->{"on_error"}->($res->decoded_content) } 0 0 0
218 0 warn $res->decoded_content;
219 0 return $callback->{on_error}->($res->decoded_content);
220 };
221 0 my $user = decode_json($res->content);
222 0 %api_response = (%api_response, %$user);
223 }
224 0 push @args, \%api_response;
225  
226 0 $self->clear_state($c);
227 0 $self->clear_nonce($c);
228  
229 0 $callback->{on_finished}->(@args);
230 }
231  
232 0 ▶sub get_state {
Subroutine get_state: not called CC=3 SCAR=24.8
Pod undocumented
233 0 my($self, $c) = @_;
234 0 ▶ my $state = $self->state || $c->session->get($self->state_session_key) || do {
Condition0%0/6
$self->state || $c->session->get($self->state_session_key) || do { require Crypt::URandom::Token; Crypt::URandom::Token::urandom_token(16) }
l!l&&r!l&&!r
000
$self->state or $c->session->get($self->state_session_key)
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->state || $c->session->get($self->state_session_key) || do { require Crypt::URandom::Token; Crypt::URandom::Token::urandom_token(16) }
$self->state $c->session->get($self->state_session_key) do { require Crypt::URandom::Token; Crypt::URandom::Token::urandom_token(16) }
Truth table0%0/4
A or B || C
A= $self->stateB= $c->session->get($self->state_session_key)C= do { require Crypt::URandom::Token; Crypt::URandom::Token::urandom_token(16) }
ABCresult
1XX1
01X1
0011
0000
235 0 require Crypt::URandom::Token;
236 0 Crypt::URandom::Token::urandom_token(16)
237 };
238 0 $self->set_state($c, $state);
239 0 return $state;
240 }
241  
242 0 ▶sub set_state {
Subroutine set_state: not called CC=1 SCAR=6.9
Pod undocumented
243 0 my($self, $c, $state) = @_;
244 0 return $c->session->set($self->state_session_key => $state);
245 }
246  
247 0 ▶sub clear_state {
Subroutine clear_state: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my($self, $c) = @_;
249 0 return $c->session->remove($self->state_session_key);
250 }
251  
252 0 ▶sub get_nonce {
Subroutine get_nonce: not called CC=3 SCAR=24.8
Pod undocumented
253 0 my($self, $c) = @_;
254 0 ▶ my $nonce = $self->nonce || $c->session->get($self->nonce_session_key) || do {
Condition0%0/6
$self->nonce || $c->session->get($self->nonce_session_key) || do { require Crypt::URandom::Token; Crypt::URandom::Token::urandom_token(16) }
l!l&&r!l&&!r
000
$self->nonce or $c->session->get($self->nonce_session_key)
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->nonce || $c->session->get($self->nonce_session_key) || do { require Crypt::URandom::Token; Crypt::URandom::Token::urandom_token(16) }
$self->nonce $c->session->get($self->nonce_session_key) do { require Crypt::URandom::Token; Crypt::URandom::Token::urandom_token(16) }
Truth table0%0/4
A or B || C
A= $self->nonceB= $c->session->get($self->nonce_session_key)C= do { require Crypt::URandom::Token; Crypt::URandom::Token::urandom_token(16) }
ABCresult
1XX1
01X1
0011
0000
255 0 require Crypt::URandom::Token;
256 0 Crypt::URandom::Token::urandom_token(16)
257 };
258 0 $self->set_nonce($c, $nonce);
259 0 return $nonce;
260 }
261  
262 0 ▶sub set_nonce {
Subroutine set_nonce: not called CC=1 SCAR=6.9
Pod undocumented
263 0 my($self, $c, $nonce) = @_;
264 0 return $c->session->set($self->nonce_session_key => $nonce);
265 }
266  
267 0 ▶sub clear_nonce {
Subroutine clear_nonce: not called CC=1 SCAR=6.9
Pod undocumented
268 0 my($self, $c) = @_;
269 0 return $c->session->remove($self->nonce_session_key);
270 }
271
272 1 1;
273 __END__
↑ Summary