| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Validate and return data from a Smart Card URL | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Health::SHC::Validate; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | #use IO::Uncompress::RawInflate qw/ rawinflate /; | ||||||||||||||||||||||||||||
| 11 | #use MIME::Base64 qw/ decode_base64url /; | ||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Crypt::JWT qw(decode_jwt); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use JSON::Parse qw(read_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use File::ShareDir qw(dist_file); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Health::SHC::Validate - Validate the Signature of the Smart Health Card data | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use Health::SHC::Validate; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $shc_valid = Health::SHC::Validate->new(); | ||||||||||||||||||||||||||||
| 25 | my $data = $shc_valid->get_valid_data($qr, $keys_json); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | This perl module validates the signature of the JSON Web Token containing the | ||||||||||||||||||||||||||||
| 32 | Smart Health Card data. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 PREREQUISITES | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item * L<Crypt::JWT> | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =item * L<JSON::Parse> | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =back | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | The following copyright notice applies to all the files provided in | ||||||||||||||||||||||||||||
| 51 | this distribution, including binary files, unless explicitly noted | ||||||||||||||||||||||||||||
| 52 | otherwise. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Copyright 2021 - 2024 Timothy Legge <timlegge@gmail.com> | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 LICENCE | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 59 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 METHODS | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head3 B<new(...)> | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Constructor; see OPTIONS above. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 75 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head3 B<get_valid_data($shc)> | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Validates the signature of the Smart Health Card URI data from the | ||||||||||||||||||||||||||||
| 81 | QR code and returns a hash of the valid data. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Arguments: | ||||||||||||||||||||||||||||
| 84 | $shc: string Smart Health Card (shc:/) URI | ||||||||||||||||||||||||||||
| 85 | $keys: string filename of custom keys in JSON format | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Returns: hash of data from the shc:/ data | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | sub get_valid_data { | ||||||||||||||||||||||||||
|
Subroutine
get_valid_data: called CC=5 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 93 | 1 | my $shc = shift; | |||||||||||||||||||||||||||
| 94 | 1 | my $keys = shift; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | $shc =~ s/shc:\///g; | |||||||||||||||||||||||||||
| 97 | 1 | my @elements = $shc =~ m/(..)/g; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | my $str; | |||||||||||||||||||||||||||
| 100 | 1 | my $i = 0; | |||||||||||||||||||||||||||
| 101 | 1 | foreach my $a (@elements) { | |||||||||||||||||||||||||||
| 102 | 765 | $str .= chr($a + 45); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | #my @tokens = split('\.', $str); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | #my $header = decode_base64url($tokens[0]); | ||||||||||||||||||||||||||||
| 108 | #my $payload = decode_base64url($tokens[1]); | ||||||||||||||||||||||||||||
| 109 | #my $signature = decode_base64url($tokens[2]); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | #my $request = ''; | ||||||||||||||||||||||||||||
| 112 | #rawinflate \$payload => \$request; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | my $k; | |||||||||||||||||||||||||||
| 115 | 1 | ▶ | if ( (defined $keys) && (-e $keys)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $keys and -e $keys
MC/DC0%0/2
defined $keys and -e $keys
defined $keys
-e $keys
Truth table33%1/3
A and B
A= defined $keysB= -e $keys
| |||||||||||||||||||||||||||||
| 116 | 1 | $k = read_json ($keys); | |||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 0 | my $keyfile = dist_file('Health-SHC', 'keys.json'); | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ( ! -e $keyfile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | return; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | $k = read_json ($keyfile); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | return decode_jwt(token=>$str,kid_keys=>$k); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 3 | 1; | |||||||||||||||||||||||||||