blib/lib/UID2/Client.pm

filter: Statementstmt 87.8% 94 / 107 Branchbran 55.0% 11 / 20 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 95.8% 23 / 24 Podpod 100% 8 / 8 totaltotal 75.9% 142 / 187 SCAR 30.9
CC
18
Cov
77%
new
16.5
get_latest_keys
4.4
_parse_v2_response
3.3
CRAP 21.9
« blib/lib/UID2/Client/Decryption.pm ↑ Summary blib/lib/UID2/Client/KeyContainer.pm »
1 package UID2::Client;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '0.01';
6  
7 use Class::Accessor::Lite (
8 3 rw => [qw(endpoint auth_key secret_key identity_scope http keys)],
9 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Crypt::Misc qw(encode_b64 decode_b64);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use UID2::Client::Decryption;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use UID2::Client::Key;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use UID2::Client::KeyContainer;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use UID2::Client::Timestamp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use UID2::Client::IdentityScope;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 sub new {
24 59 ▶ my ($class, $options) = @_;
Subroutine new: called CC=9 SCAR=28.0
Pod documented
25 59 ▶ my $secret_key = $options->{secret_key} // croak 'secret_key required';
Condition33%1/3
$options->{"secret_key"} // croak("secret_key required")
l!l&&r!l&&!r
5900
MC/DC0%0/2
$options->{"secret_key"} // croak("secret_key required")
$options->{"secret_key"} croak("secret_key required")
Truth table33%1/3
A // B
A= $options->{"secret_key"}B= croak("secret_key required")
ABresult
1X1
011
000
26 59 $secret_key = decode_b64($secret_key);
27 59 my $http = do {
28 59 ▶ if ($options->{http_options} && $options->{http}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options->{'http_options'} and $options->{'http'}) { } 0 59 59
elsif ($options->{'http_options'}) { } 0 59 59
elsif ($options->{'http'}) { } 40 19 59
Condition33%1/3
$options->{"http_options"} and $options->{"http"}
!ll&&!rl&&r
5900
MC/DC0%0/2
$options->{"http_options"} and $options->{"http"}
$options->{"http_options"} $options->{"http"}
Truth table33%1/3
A and B
A= $options->{"http_options"}B= $options->{"http"}
ABresult
0X0
100
111
29 0 croak 'only one of http_options or http can be specified';
30 } elsif ($options->{http_options}) {
31 0 HTTP::Tiny->new(%{$options->{http_options}});
32 } elsif ($options->{http}) {
33 40 $options->{http};
34 } else {
35 19 HTTP::Tiny->new;
36 }
37 };
38 bless {
39 endpoint => $options->{endpoint} // croak('endpoint required'),
40 auth_key => $options->{auth_key} // croak('auth_key required'),
41 secret_key => $secret_key,
42 59 ▶ identity_scope => $options->{identity_scope} // UID2::Client::IdentityScope::UID2,
Condition37%3/8
$options->{"endpoint"} // croak("endpoint required")
l!l&&r!l&&!r
5900
$options->{"auth_key"} // croak("auth_key required")
l!l&&r!l&&!r
5900
$options->{"identity_scope"} // 0
l!l
590
MC/DC0%0/2
$options->{"endpoint"} // croak("endpoint required")
$options->{"endpoint"} croak("endpoint required")
MC/DC0%0/2
$options->{"auth_key"} // croak("auth_key required")
$options->{"auth_key"} croak("auth_key required")
MC/DC0%0/1
$options->{"identity_scope"} // 0
$options->{"identity_scope"}
Truth table33%1/3
A // B
A= $options->{"endpoint"}B= croak("endpoint required")
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $options->{"auth_key"}B= croak("auth_key required")
ABresult
1X1
011
000
Truth table50%1/2
A
A= $options->{"identity_scope"}
Aresult
11
00
43 http => $http,
44 keys => undef,
45 }, $class;
46 }
47  
48 sub new_euid {
49 0 ▶ my ($class, $options) = @_;
Subroutine new_euid: not called CC=1 SCAR=6.9
Pod documented
50 0 $class->new({ %$options, identity_scope => UID2::Client::IdentityScope::EUID });
51 }
52  
53 sub refresh {
54 40 ▶ my $self = shift;
Subroutine refresh: called CC=2 SCAR=7.2
Pod documented
55 40 eval {
56 40 $self->keys(_parse_json($self->get_latest_keys));
57 40 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 40 40
58 0 return { is_success => undef, reason => $@ };
59 }
60 40 +{ is_success => 1 };
61 }
62  
63 sub refresh_json {
64 14 ▶ my ($self, $json) = @_;
Subroutine refresh_json: called CC=2 SCAR=7.2
Pod documented
65 14 eval {
66 14 $self->keys(_parse_json($json));
67 14 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 14 14
68 0 return { is_success => undef, reason => $@ };
69 }
70 14 +{ is_success => 1 };
71 }
72  
73 3 my $V2_NONCE_LEN = 8;
74  
75 sub get_latest_keys {
76 40 ▶ my $self = shift;
Subroutine get_latest_keys: called CC=3 SCAR=14.8
Pod documented
77 40 my $nonce = random_bytes($V2_NONCE_LEN);
78 40 my $res = $self->http->post($self->endpoint . '/v2/key/latest', {
79 headers => {
80 'Authorization' => 'Bearer ' . $self->auth_key,
81 'Content-Type' => 'text/plain',
82 },
83 content => $self->_make_v2_request($nonce),
84 });
85 40 ▶ unless ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"success"}) 0 40 40
86 0 ▶ if ($res->{status} == 599) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'status'} == 599) { } 0 0 0
87 0 chomp(my $content = $res->{content});
88 0 croak $content;
89 } else {
90 0 croak "$res->{status} $res->{reason}";
91 }
92 }
93 40 $self->_parse_v2_response($res->{content}, $nonce);
94 }
95  
96 sub _make_v2_request {
97 40 ▶ my ($self, $nonce, $now) = @_;
Subroutine _make_v2_request: called CC=2 SCAR=7.1
98 40 ▶ $now //= UID2::Client::Timestamp->now;
Condition33%1/3
$now //= "UID2::Client::Timestamp"->now
l!l&&r!l&&!r
0400
MC/DC0%0/2
$now //= "UID2::Client::Timestamp"->now
$now "UID2::Client::Timestamp"->now
Truth table33%1/3
A //= B
A= $nowB= "UID2::Client::Timestamp"->now
ABresult
1X1
011
000
99 40 my $data = pack 'q> a*', $now->get_epoch_milli, $nonce;
100 40 my $payload = UID2::Client::Decryption::encrypt_gcm($data, $self->secret_key),
101 my $version = 1;
102 40 my $envelope = pack 'C a*', $version, $payload;
103 40 encode_b64($envelope);
104 }
105  
106 sub _parse_v2_response {
107 40 ▶ my ($self, $envelope, $nonce) = @_;
Subroutine _parse_v2_response: called CC=3 SCAR=11.8
108 40 my $envelope_bytes = decode_b64($envelope);
109 40 my $payload = UID2::Client::Decryption::decrypt_gcm($envelope_bytes, $self->secret_key);
110 40 ▶ if (length($payload) < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $payload < 16) 0 40 40
111 0 croak 'invalid payload';
112 }
113 40 my ($res_nonce, $data) = unpack "x8 a${V2_NONCE_LEN} a*", $payload;
114 40 ▶ if ($res_nonce ne $nonce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_nonce ne $nonce) 0 40 40
115 0 croak 'nonce mismatch';
116 }
117 40 $data;
118 }
119  
120 sub _parse_json {
121 54 ▶ my $content = shift;
Subroutine _parse_json: called CC=2 SCAR=6.9
122 54 my $obj = decode_json($content);
123 54 my @keys;
124 54 for my $entry (@{$obj->{body}}) {
125 132 $entry->{secret} = decode_b64($entry->{secret});
126 132 push @keys, UID2::Client::Key->new($entry);
127 }
128 54 UID2::Client::KeyContainer->new(@keys);
129 }
130  
131 sub decrypt {
132 18 ▶ my ($self, $token, $now) = @_;
Subroutine decrypt: called CC=1 SCAR=0.0
Pod documented
133 18 UID2::Client::Decryption::decrypt_token($token, $now, $self->keys, $self->identity_scope);
134 }
135  
136 sub encrypt_data {
137 31 ▶ my ($self, $data, $request) = @_;
Subroutine encrypt_data: called CC=2 SCAR=6.9
Pod documented
138 31 $request->{identity_scope} = $self->identity_scope;
139 31 ▶ $request->{keys} = $self->keys unless $request->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->{"key"} 21 10 31
140 31 UID2::Client::Decryption::encrypt_data($data, $request);
141 }
142  
143 sub decrypt_data {
144 30 ▶ my ($self, $data) = @_;
Subroutine decrypt_data: called CC=1 SCAR=0.0
Pod documented
145 30 UID2::Client::Decryption::decrypt_data($data, $self->keys, $self->identity_scope);
146 }
147  
148 3 1;
149 __END__
« blib/lib/UID2/Client/Decryption.pm ↑ Summary blib/lib/UID2/Client/KeyContainer.pm »