| 1 | package UID2::Client; | ||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||
| 5 | 4 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Class::Accessor::Lite ( | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 4 | rw => [qw(endpoint auth_key secret_key identity_scope http keys)], | |||||||||||||||||||||||||||||||||||
| 9 | 4 | ); | |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Crypt::PRNG qw(random_bytes); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Crypt::Misc qw(encode_b64 decode_b64); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use UID2::Client::Encryption; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use UID2::Client::Key; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use UID2::Client::KeyContainer; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use UID2::Client::Timestamp; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use UID2::Client::IdentityScope; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | 107 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=26.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 24 | 107 | my ($class, $options) = @_; | |||||||||||||||||||||||||||||||||||
| 25 | 107 | ▶ | my $secret_key = $options->{secret_key} // croak 'secret_key required'; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{"secret_key"} // croak("secret_key required")
MC/DC0%0/1
$options->{"secret_key"} // croak("secret_key required")
$options->{"secret_key"}
Truth table50%1/2
A
A= $options->{"secret_key"}
| |||||||||||||||||||||||||||||||||||||
| 26 | 107 | $secret_key = decode_b64($secret_key); | |||||||||||||||||||||||||||||||||||
| 27 | 107 | my $http = do { | |||||||||||||||||||||||||||||||||||
| 28 | 107 | ▶ | if ($options->{http_options} && $options->{http}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$options->{"http_options"} and $options->{"http"}
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"}
| |||||||||||||||||||||||||||||||||||||
| 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 | 80 | $options->{http}; | |||||||||||||||||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||||||||||||||||
| 35 | 27 | 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 | 107 | ▶ | identity_scope => $options->{identity_scope} // UID2::Client::IdentityScope::UID2, | ||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$options->{"endpoint"} // croak("endpoint required")
$options->{"auth_key"} // croak("auth_key required")
$options->{"identity_scope"} // 0
MC/DC0%0/1
$options->{"endpoint"} // croak("endpoint required")
$options->{"endpoint"}
MC/DC0%0/1
$options->{"auth_key"} // croak("auth_key required")
$options->{"auth_key"}
MC/DC0%0/1
$options->{"identity_scope"} // 0
$options->{"identity_scope"}
Truth table50%1/2
A
A= $options->{"endpoint"}
Truth table50%1/2
A
A= $options->{"auth_key"}
Truth table50%1/2
A
A= $options->{"identity_scope"}
| |||||||||||||||||||||||||||||||||||||
| 43 | http => $http, | ||||||||||||||||||||||||||||||||||||
| 44 | keys => undef, | ||||||||||||||||||||||||||||||||||||
| 45 | }, $class; | ||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub new_euid { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new_euid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 49 | 0 | my ($class, $options) = @_; | |||||||||||||||||||||||||||||||||||
| 50 | 0 | $class->new({ %$options, identity_scope => UID2::Client::IdentityScope::EUID }); | |||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | 80 | ▶ | sub refresh { | ||||||||||||||||||||||||||||||||||
|
Subroutine
refresh: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 54 | 80 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 55 | 80 | eval { | |||||||||||||||||||||||||||||||||||
| 56 | 80 | $self->keys(_parse_json($self->get_latest_keys)); | |||||||||||||||||||||||||||||||||||
| 57 | 80 | ▶ | }; if ($@) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 58 | 0 | return { is_success => undef, reason => $@ }; | |||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||
| 60 | 80 | +{ is_success => 1 }; | |||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 17 | ▶ | sub refresh_json { | ||||||||||||||||||||||||||||||||||
|
Subroutine
refresh_json: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 64 | 17 | my ($self, $json) = @_; | |||||||||||||||||||||||||||||||||||
| 65 | 17 | eval { | |||||||||||||||||||||||||||||||||||
| 66 | 17 | $self->keys(_parse_json($json)); | |||||||||||||||||||||||||||||||||||
| 67 | 17 | ▶ | }; if ($@) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 68 | 0 | return { is_success => undef, reason => $@ }; | |||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||
| 70 | 17 | +{ is_success => 1 }; | |||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | 4 | my $V2_NONCE_LEN = 8; | |||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | 80 | ▶ | sub get_latest_keys { | ||||||||||||||||||||||||||||||||||
|
Subroutine
get_latest_keys: called CC=3 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 76 | 80 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 77 | 80 | my $nonce = random_bytes($V2_NONCE_LEN); | |||||||||||||||||||||||||||||||||||
| 78 | 80 | 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 | 80 | ▶ | unless ($res->{success}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($res->{status} == 599) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 87 | 0 | chomp(my $content = $res->{content}); | |||||||||||||||||||||||||||||||||||
| 88 | 0 | croak $content; | |||||||||||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||||||||||
| 90 | 0 | croak "$res->{status} $res->{reason}"; | |||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||
| 93 | 80 | $self->_parse_v2_response($res->{content}, $nonce); | |||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | 80 | ▶ | sub _make_v2_request { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_make_v2_request: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||
| 97 | 80 | my ($self, $nonce, $now) = @_; | |||||||||||||||||||||||||||||||||||
| 98 | 80 | ▶ | $now //= UID2::Client::Timestamp->now; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$now //= "UID2::Client::Timestamp"->now
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
| |||||||||||||||||||||||||||||||||||||
| 99 | 80 | my $data = pack 'q> a*', $now->get_epoch_milli, $nonce; | |||||||||||||||||||||||||||||||||||
| 100 | 80 | my $payload = UID2::Client::Encryption::encrypt_gcm($data, $self->secret_key), | |||||||||||||||||||||||||||||||||||
| 101 | my $version = 1; | ||||||||||||||||||||||||||||||||||||
| 102 | 80 | my $envelope = pack 'C a*', $version, $payload; | |||||||||||||||||||||||||||||||||||
| 103 | 80 | encode_b64($envelope); | |||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | 80 | ▶ | sub _parse_v2_response { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_v2_response: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||||||||||
| 107 | 80 | my ($self, $envelope, $nonce) = @_; | |||||||||||||||||||||||||||||||||||
| 108 | 80 | my $envelope_bytes = decode_b64($envelope); | |||||||||||||||||||||||||||||||||||
| 109 | 80 | my $payload = UID2::Client::Encryption::decrypt_gcm($envelope_bytes, $self->secret_key); | |||||||||||||||||||||||||||||||||||
| 110 | 80 | ▶ | if (length($payload) < 16) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 111 | 0 | croak 'invalid payload'; | |||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||
| 113 | 80 | my ($res_nonce, $data) = unpack "x8 a${V2_NONCE_LEN} a*", $payload; | |||||||||||||||||||||||||||||||||||
| 114 | 80 | ▶ | if ($res_nonce ne $nonce) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 115 | 0 | croak 'nonce mismatch'; | |||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||
| 117 | 80 | $data; | |||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||
| 120 | 97 | ▶ | sub _parse_json { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_json: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 121 | 97 | my $content = shift; | |||||||||||||||||||||||||||||||||||
| 122 | 97 | my $obj = decode_json($content); | |||||||||||||||||||||||||||||||||||
| 123 | 97 | my @keys; | |||||||||||||||||||||||||||||||||||
| 124 | 97 | for my $entry (@{$obj->{body}}) { | |||||||||||||||||||||||||||||||||||
| 125 | 248 | $entry->{secret} = decode_b64($entry->{secret}); | |||||||||||||||||||||||||||||||||||
| 126 | 248 | push @keys, UID2::Client::Key->new($entry); | |||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | 97 | UID2::Client::KeyContainer->new(@keys); | |||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | 27 | ▶ | sub decrypt { | ||||||||||||||||||||||||||||||||||
|
Subroutine
decrypt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 132 | 27 | my ($self, $token, $now) = @_; | |||||||||||||||||||||||||||||||||||
| 133 | 27 | UID2::Client::Encryption::decrypt_token($token, $now, $self->keys, $self->identity_scope); | |||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | 62 | ▶ | sub encrypt_data { | ||||||||||||||||||||||||||||||||||
|
Subroutine
encrypt_data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 137 | 62 | my ($self, $data, $request) = @_; | |||||||||||||||||||||||||||||||||||
| 138 | 62 | $request->{identity_scope} = $self->identity_scope; | |||||||||||||||||||||||||||||||||||
| 139 | 62 | ▶ | $request->{keys} = $self->keys unless $request->{key}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 140 | 62 | UID2::Client::Encryption::encrypt_data($data, $request); | |||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | 52 | ▶ | sub decrypt_data { | ||||||||||||||||||||||||||||||||||
|
Subroutine
decrypt_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 144 | 52 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||
| 145 | 52 | UID2::Client::Encryption::decrypt_data($data, $self->keys, $self->identity_scope); | |||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||
| 148 | 4 | 1; | |||||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||||||