API/Event.pm

filter: Statementstmt 100% 90 / 90 Branchbran 83.3% 5 / 6 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 2 totaltotal 94.6% 123 / 130 CC 8 SCAR 5.9
CC
8
Cov
97%
validate_signature
4.1
_secure_compare
3.0
parse_events_json
3.0
CRAP 1.8
« API/Builder/SendMessage.pm ↑ Summary API/Response/AudienceGroupForClickRetargeting.pm »
1 package LINE::Bot::API::Event;
2 53 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 53 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 53 ▶use Carp 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 our @CARP_NOT = qw( LINE::Bot::API::Event LINE::Bot::API);
7  
8 53 ▶use Digest::SHA 'hmac_sha256';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 53 ▶use JSON::XS 'decode_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 53 ▶use MIME::Base64 'decode_base64';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 53 ▶use LINE::Bot::API::Event::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 53 ▶use LINE::Bot::API::Event::Follow;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 53 ▶use LINE::Bot::API::Event::Unfollow;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 53 ▶use LINE::Bot::API::Event::Join;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 53 ▶use LINE::Bot::API::Event::Leave;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 53 ▶use LINE::Bot::API::Event::MemberJoin;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 53 ▶use LINE::Bot::API::Event::MemberLeave;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 53 ▶use LINE::Bot::API::Event::Postback;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 53 ▶use LINE::Bot::API::Event::BeaconDetection;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 53 ▶use LINE::Bot::API::Event::Things;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 53 ▶use LINE::Bot::API::Event::AccountLink;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 53 ▶use LINE::Bot::API::Event::Unsend;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 53 ▶use LINE::Bot::API::Event::VideoViewingComplete;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 53 ▶use LINE::Bot::API::Event::Unknown;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 53 my %TYPE2CLASS = (
28 message => 'LINE::Bot::API::Event::Message',
29 follow => 'LINE::Bot::API::Event::Follow',
30 unfollow => 'LINE::Bot::API::Event::Unfollow',
31 join => 'LINE::Bot::API::Event::Join',
32 leave => 'LINE::Bot::API::Event::Leave',
33 memberJoined => 'LINE::Bot::API::Event::MemberJoin',
34 memberLeft => 'LINE::Bot::API::Event::MemberLeave',
35 postback => 'LINE::Bot::API::Event::Postback',
36 beacon => 'LINE::Bot::API::Event::BeaconDetection',
37 things => 'LINE::Bot::API::Event::Things',
38 accountLink => 'LINE::Bot::API::Event::AccountLink',
39 unsend => 'LINE::Bot::API::Event::Unsend',
40 unknown => 'LINE::Bot::API::Event::Unknown',
41 videoPlayComplete => 'LINE::Bot::API::Event::VideoViewingComplete',
42 );
43  
44 2 ▶sub parse_events_json {
Subroutine parse_events_json: called CC=3 SCAR=11.0
Pod undocumented
45 2 my($self, $json) = @_;
46 2 my $events = [];
47  
48 2 my $data = decode_json $json;
49 2 for my $event_data (@{ $data->{events} }) {
50 43 my $type = $event_data->{type};
51 43 my $event_class = $TYPE2CLASS{$type};
52 43 ▶ unless ($event_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($event_class) 1 42 43
53 1 carp 'Unsupported event type: ' . $type . ", parse as unknown event";
54 1 $event_class = $TYPE2CLASS{'unknown'};
55 }
56  
57 43 my $event = $event_class->new(%{ $event_data });
58 43 push @{ $events }, $event;
59 }
60  
61 2 $events;
62 }
63  
64 4 ▶sub validate_signature {
Subroutine validate_signature: called CC=4 SCAR=14.0
Pod undocumented
65 4 my($class, $json, $channel_secret, $signature) = @_;
66 4 ▶ return unless $signature && $json && $channel_secret;
Branch: 1 branch
BranchTrueFalseTotal
unless $signature and $json and $channel_secret 2 2 4
Condition66%4/6
$signature and $json and $channel_secret
!ll&&!rl&&r
202
$signature and $json
!ll&&!rl&&r
202
MC/DC33%1/3
$signature and $json and $channel_secret
$signature $json $channel_secret
Truth table50%2/4
A and B and C
A= $signatureB= $jsonC= $channel_secret
ABCresult
0XX0
10X0
1100
1111
67 2 my $json_signature = hmac_sha256($json, $channel_secret);
68 2 _secure_compare(decode_base64($signature), $json_signature);
69 }
70  
71 # Constant time string comparison for timing attacks.
72 2 ▶sub _secure_compare {
Subroutine _secure_compare: called CC=3 SCAR=11.0
73 2 my($x, $y) = @_;
74 2 ▶ return unless length $x == length $y;
Branch: 1 branch
BranchTrueFalseTotal
unless length $x == length $y 0 2 2
75 2 my @a = unpack 'C*', $x;
76 2 my @b = unpack 'C*', $y;
77 2 my $compare = 0;
78 2 for my $i (0..(scalar(@a) - 1)) {
79 64 $compare |= $a[$i] ^ $b[$i];
80 }
81 2 return !$compare;
82 }
83  
84 53 1;
85 __END__
« API/Builder/SendMessage.pm ↑ Summary API/Response/AudienceGroupForClickRetargeting.pm »