| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 59 | $secret_key = decode_b64($secret_key); | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 59 | my $http = do { | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 59 | ▶ | 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 | 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")
$options->{"auth_key"} // croak("auth_key required")
$options->{"identity_scope"} // 0
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")
Truth table33%1/3
A // B
A= $options->{"auth_key"}B= croak("auth_key required")
Truth table50%1/2
A
A= $options->{"identity_scope"}
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 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
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 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||