| 1 | package WWW::Suffit::JWT; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | WWW::Suffit::JWT - JSON Web Token for Suffit authorization | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | use WWW::Suffit::JWT; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | my $jwt = WWW::Suffit::JWT->new( | ||||||||||||||||||||||||||||||||||||||||||
| 16 | secret => "MySecret", | ||||||||||||||||||||||||||||||||||||||||||
| 17 | payload => {foo => 'bar'}, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | my $token = $jwt->encode->token or die $jwt->error; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | my $payload = $jwt->decode($token)->payload; | ||||||||||||||||||||||||||||||||||||||||||
| 21 | die $jwt->error if $jwt->error; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | use WWW::Suffit::RSA; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | my $rsa = WWW::Suffit::RSA->new(key_size => 1024); | ||||||||||||||||||||||||||||||||||||||||||
| 25 | $rsa->keygen; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | my $private_key = $rsa->private_key; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | my $public_key = $rsa->public_key; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | my $jwt = WWW::Suffit::JWT->new( | ||||||||||||||||||||||||||||||||||||||||||
| 29 | private_key => $private_key, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | payload => {foo => 'bar'}, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | algorithm => 'RS512', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | my $token = $jwt->encode->token or die $jwt->error; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | my $payload = $jwt->public_key($public_key)->decode($token)->payload; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | die $jwt->error if $jwt->error; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | JSON Web Token for Suffit authorization | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | This module based on L<Mojo::JWT> | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | JSON Web Token is described in L<https://tools.ietf.org/html/rfc7519>. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | This class implements the following attributes | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =head2 algorithm | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | The algorithm to be used to sign a JWT during encoding or else the algorithm that was used for the most recent decoding. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | Defaults to C<HS256> until a decode is performed. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | none no integrity (NOTE: disabled for decode method) | ||||||||||||||||||||||||||||||||||||||||||
| 55 | HS256 HMAC+SHA256 integrity | ||||||||||||||||||||||||||||||||||||||||||
| 56 | HS384 HMAC+SHA384 integrity | ||||||||||||||||||||||||||||||||||||||||||
| 57 | HS512 HMAC+SHA512 integrity | ||||||||||||||||||||||||||||||||||||||||||
| 58 | RS256 RSA+PKCS1-V1_5 + SHA256 signature | ||||||||||||||||||||||||||||||||||||||||||
| 59 | RS384 RSA+PKCS1-V1_5 + SHA384 signature | ||||||||||||||||||||||||||||||||||||||||||
| 60 | RS512 RSA+PKCS1-V1_5 + SHA512 signature | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | B<NOTE!> We recommend using RS512 | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head2 error | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | $jwt->error($new_error); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | my $error = $jwt->error; | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | Sets/gets the error string | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =head2 expires | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | The epoch time value after which the JWT value should not be considered valid. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | This value (if set and not undefined) will be used as the C<exp> key in the payload | ||||||||||||||||||||||||||||||||||||||||||
| 75 | or was extracted from the payload during the most recent decoding. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =head2 header | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Header - first part of JWT structure | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | You may set your own headers when encoding the JWT bypassing a hash reference to the L</header> attribute. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | Please note that there are two default headers set. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | B<alg> is set to the value of L</algorithm> or 'HS256' and B<typ> is set to 'JWT'. These cannot be overridden. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | my $header = $jwt->header; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | Returns a hash reference representing the JWT header, constructed from instance attributes (see L</algorithm>). | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =head2 iat | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | It is epoch time value that will be set as C<iat> during L</encode>. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =head2 not_before | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | The epoch time value before which the JWT value should not be considered valid. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | This value (if set and not undefined) will be used as the C<nbf> key in the payload | ||||||||||||||||||||||||||||||||||||||||||
| 97 | or was extracted from the payload during the most recent decoding. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =head2 payload | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | Payload - second part of JWT structure | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | The payload is a user data structure to be encoded. This must be a hash reference only. | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =head2 private_key | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | The RSA private key to be used in edcoding an asymmetrically signed JWT. See L<WWW::Suffit::RSA> | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =head2 public_key | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | The RSA public key to be used in decoding an asymmetrically signed JWT. See L<WWW::Suffit::RSA> | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | =head2 secret | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | The symmetric secret used in encoding an symmetrically HMAC | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =head2 token | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | The most recently encoded or decoded token. | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | $jwt->token($new_token); | ||||||||||||||||||||||||||||||||||||||||||
| 122 | my $token = $jwt->token; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | Sets/gets the token | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | This class inherits all of the methods from L<Mojo::Base> | ||||||||||||||||||||||||||||||||||||||||||
| 129 | and implements the following new ones | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =head2 decode | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | my $payload = $jwt->decode($token)->payload; | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | Decode and parse a JSON Web Token string and return the payload hashref (see L</payload>). | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =head2 encode | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | my $token = $jwt->encode->token; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | Encode the data expressed in the instance attributes: L</algorithm>, L</payload>, L</expires>, L</not_before>. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | Note that if the timing attributes are given, they override existing keys in the L</payload>. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | Calling C<encode> immediately clears the L</token> and upon completion sets it to the result (See L</token>) | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =head2 sign_hmac | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | my $signature = $jwt->sign_hmac($size, $string); | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Returns the HMAC SHA signature for the given size and string. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | The L</secret> attribute is used as the symmetric key. | ||||||||||||||||||||||||||||||||||||||||||
| 151 | The result is base64url encoded! | ||||||||||||||||||||||||||||||||||||||||||
| 152 | This method is provided mostly for the purposes of subclassing. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =head2 sign_rsa | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | my $signature = $jwt->sign_rsa($size, $string); | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | Returns the RSA signature for the given size and string. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | The L</private_key> attribute is used as the private key. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | The result is base64url encoded! | ||||||||||||||||||||||||||||||||||||||||||
| 161 | This method is provided mostly for the purposes of subclassing. | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =head2 verify_hmac | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | my $bool = $jwt->verify_hmac($size, $string, $signature); | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | Returns true if the given HMAC size algorithm validates the given string and signature. | ||||||||||||||||||||||||||||||||||||||||||
| 168 | The L</secret> attribute is used as the HMAC passphrase. | ||||||||||||||||||||||||||||||||||||||||||
| 169 | The signature is base64url encoded! | ||||||||||||||||||||||||||||||||||||||||||
| 170 | This method is provided mostly for the purposes of subclassing. | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | =head2 verify_rsa | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | my $bool = $jwt->verify_rsa($size, $string, $signature); | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | Returns true if the given RSA size algorithm validates the given string and signature. | ||||||||||||||||||||||||||||||||||||||||||
| 177 | The L</public_key> attribute is used as the public key. | ||||||||||||||||||||||||||||||||||||||||||
| 178 | The signature is base64url encoded! | ||||||||||||||||||||||||||||||||||||||||||
| 179 | This method is provided mostly for the purposes of subclassing. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | L<WWW::Suffit::RSA> | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | =head1 HISTORY | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | See C<Changes> file | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =head1 TO DO | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | See C<TODO> file | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | L<WWW::Suffit::RSA>, L<Crypt::OpenSSL::RSA>, L<Mojo::JWT>, | ||||||||||||||||||||||||||||||||||||||||||
| 196 | L<Acme::JWT>, L<JSON::WebToken>, L<https://jwt.io/> | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =head2 CONTRIBUTORS | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | Joel Berger, E<lt>joel.a.berger@gmail.comE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | Christopher Raa (mishanti1) | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | Cameron Daniel (ccakes) | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | Copyright (C) 1998-2026 D&D Corporation | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | Copyright (C) 2023 by L</CONTRIBTORS>. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | This program is distributed under the terms of the Artistic License Version 2.0 | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | ▶ | use Mojo::JSON qw/encode_json decode_json/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | ▶ | use Mojo::Util qw/b64_encode b64_decode/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | ▶ | use MIME::Base64 qw/encode_base64url decode_base64url/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | ▶ | use Digest::SHA qw//; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | ▶ | use WWW::Suffit::RSA; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | ▶ | use constant DEFAULT_ALGORITHM => 'HS256'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | has 'token'; # Result token | |||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | has 'secret' => ''; # For HMAC | |||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | has 'algorithm' => DEFAULT_ALGORITHM(); # By default | |||||||||||||||||||||||||||||||||||||||||
| 237 | 1 | has 'expires'; # Expire time | |||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | has 'not_before'; # Time "not before" | |||||||||||||||||||||||||||||||||||||||||
| 239 | 1 | has 'iat' => 0; # Payload iat param | |||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | has 'jti' => 0; # Payload jti param | |||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | has 'private_key'; # For RSA | |||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | has 'public_key'; # For RSA | |||||||||||||||||||||||||||||||||||||||||
| 243 | 1 | has 'error' => ''; # Error string | |||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | ▶ | has 'header' => sub { {} }; # Header (first part of JWT) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | has 'payload' => sub { {} }; # Payload (second parg of JWT) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # Regexps | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 1 | my $re_hs = qr/^HS(\d+)$/; # HMAC | |||||||||||||||||||||||||||||||||||||||||
| 249 | 1 | my $re_rs = qr/^RS(\d+)$/; # RSA | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 2 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=11 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 2 | $self->error(''); # Flush error string first | |||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | $self->token(''); # Flush old token | |||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | # Header | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | my %header = %{ $self->header // {} }; # Get Header | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->header // {}
MC/DC0%0/1
$self->header // {}
$self->header
Truth table50%1/2
A
A= $self->header
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 2 | $header{typ} = 'JWT'; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 2 | $header{alg} = $self->algorithm; | |||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | # Payload | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | ▶ | my %payload = %{ $self->payload // {} }; # Get Payload | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->payload // {}
MC/DC0%0/1
$self->payload // {}
$self->payload
Truth table50%1/2
A
A= $self->payload
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | ▶ | $payload{iat} = $self->iat if $self->iat; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | ▶ | $payload{jti} = $self->jti if $self->jti; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | ▶ | $payload{exp} = $self->expires if defined($self->expires); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 2 | ▶ | $payload{nbf} = $self->not_before if defined($self->not_before); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | # Concat (<Header>.<Payload>) | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | my $concat = sprintf("%s.%s", | |||||||||||||||||||||||||||||||||||||||||
| 270 | encode_base64url(encode_json(\%header)), | ||||||||||||||||||||||||||||||||||||||||||
| 271 | encode_base64url(encode_json(\%payload)) | ||||||||||||||||||||||||||||||||||||||||||
| 272 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # Signature | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | my $signature; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 2 | my $algo = $self->algorithm; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 2 | ▶ | if ($algo eq 'none') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | $signature = ''; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } elsif ($algo =~ $re_hs) { # HMAC | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 1 | $signature = $self->sign_hmac($1, $concat); | |||||||||||||||||||||||||||||||||||||||||
| 281 | } elsif ($algo =~ $re_rs) { # RSA | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 1 | $signature = $self->sign_rsa($1, $concat); | |||||||||||||||||||||||||||||||||||||||||
| 283 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $self->error('Unknown algorithm'); | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 1 | ▶ | return $self unless defined $signature; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | # Set new token | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | $self->token(sprintf("%s.%s", $concat, $signature)); | |||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # Returns OBJECT!! | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | ▶ | sub decode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=28 SCAR=44.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 3 | my $token = shift; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 3 | $self->error(''); # Flush error string first | |||||||||||||||||||||||||||||||||||||||||
| 298 | 3 | ▶ | $self->token($token) if $token; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | # Reset | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | $self->header({}); | |||||||||||||||||||||||||||||||||||||||||
| 302 | 3 | $self->payload({}); | |||||||||||||||||||||||||||||||||||||||||
| 303 | 3 | $self->algorithm(DEFAULT_ALGORITHM); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 3 | $self->expires(undef); | |||||||||||||||||||||||||||||||||||||||||
| 305 | 3 | $self->not_before(undef); | |||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | # Load parts | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 3 | ▶ | return $self->error('Token not specified') unless $self->token; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 3 | my ($hstring, $pstring, $signature) = split /\./, $self->token; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 3 | my ($header, $payload) = ({}, {}); | |||||||||||||||||||||||||||||||||||||||||
| 311 | 3 | ▶ | if ($hstring) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 3 | ▶ | $header = eval { decode_json(decode_base64url($hstring)) } || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
decode_json(decode_base64url($hstring))
}
} || {}
MC/DC0%0/1
eval {
do {
decode_json(decode_base64url($hstring))
}
} || {}
eval {
do {
decode_json(decode_base64url($hstring))
}
}
Truth table50%1/2
A
A= eval {
do {
decode_json(decode_base64url($hstring))
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | chomp $@; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | return $self->error(sprintf "Incorrect JWT header: %s", $@ || 'unknown parser error'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'unknown parser error'
MC/DC0%0/1
$@ || 'unknown parser error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 3 | ▶ | if ($pstring) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 3 | ▶ | $payload = eval { decode_json(decode_base64url($pstring)) } || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
decode_json(decode_base64url($pstring))
}
} || {}
MC/DC0%0/1
eval {
do {
decode_json(decode_base64url($pstring))
}
} || {}
eval {
do {
decode_json(decode_base64url($pstring))
}
}
Truth table50%1/2
A
A= eval {
do {
decode_json(decode_base64url($pstring))
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | chomp $@; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | return $self->error(sprintf "Incorrect JWT payload: %s", $@ || 'unknown parser error'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'unknown parser error'
MC/DC0%0/1
$@ || 'unknown parser error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 3 | ▶ | return $self->error('Signature not found') unless $signature; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 3 | ▶ | my $concat = sprintf("%s.%s", $hstring // '', $pstring // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$hstring // ""
$pstring // ""
MC/DC0%0/1
$hstring // ""
$hstring
MC/DC0%0/1
$pstring // ""
$pstring
Truth table50%1/2
A
A= $hstring
Truth table50%1/2
A
A= $pstring
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | # typ header is only recommended and is ignored | ||||||||||||||||||||||||||||||||||||||||||
| 329 | # https://tools.ietf.org/html/rfc7519#section-5.1 | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 3 | ▶ | delete $header->{typ} if exists $header->{typ}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | # Get algo | ||||||||||||||||||||||||||||||||||||||||||
| 333 | 3 | my $algo = $header->{alg}; | |||||||||||||||||||||||||||||||||||||||||
| 334 | 3 | ▶ | return $self->error('Required header field "alg" not specified') unless $algo; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 3 | delete $header->{alg}; | |||||||||||||||||||||||||||||||||||||||||
| 336 | 3 | $self->algorithm($algo); | |||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | # Set Header | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 3 | $self->header($header); | |||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | # Check signature | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 3 | my $status = 0; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 3 | ▶ | if ($algo eq 'none') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | $self->error('Algorithm "none" is prohibited, which means that token has no signature'); | |||||||||||||||||||||||||||||||||||||||||
| 345 | } elsif ($algo =~ $re_hs) { # HMAC | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 3 | $status = $self->verify_hmac($1, $concat, $signature); | |||||||||||||||||||||||||||||||||||||||||
| 347 | 3 | ▶ | if ($self->error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 1 | $self->error(sprintf('Failed HS validation: %s', $self->error)) | |||||||||||||||||||||||||||||||||||||||||
| 349 | } elsif (!$status) { | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 1 | $self->error('Failed HS validation'); | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | } elsif ($algo =~ $re_rs) { # RSA | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | $status = $self->verify_rsa($1, $concat, $signature); | |||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | if ($self->error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | $self->error(sprintf('Failed RS validation: %s', $self->error)) | |||||||||||||||||||||||||||||||||||||||||
| 356 | } elsif (!$status) { | ||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | $self->error('Failed RS validation'); | |||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | $self->error('Unsupported signing algorithm'); | |||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 3 | ▶ | return $self unless $status; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | # Check timing | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | my $now = time; | |||||||||||||||||||||||||||||||||||||||||
| 366 | 1 | my $exp = $payload->{exp}; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 1 | my $nbf = $payload->{nbf}; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 1 | ▶ | if (defined($exp)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | return $self->error('JWT has expired') if $now > $exp; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | $self->expires($exp); | |||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 1 | ▶ | if (defined($nbf)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | return $self->error('JWT is not yet valid') if $now < $nbf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | $self->not_before($nbf); | |||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | # Set Payload | ||||||||||||||||||||||||||||||||||||||||||
| 378 | 1 | $self->payload($payload); | |||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | # Returns OBJECT!! | ||||||||||||||||||||||||||||||||||||||||||
| 381 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 4 | ▶ | sub sign_hmac { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sign_hmac: called CC=7 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 4 | my ($self, $size, $concat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | # Get Symmetric key or return | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 4 | my $secret = $self->secret; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 4 | ▶ | $self->error('Symmetric key (secret) not specified') && return unless $secret; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
$secret || $self->error('Symmetric key (secret) not specified') && (return)
$self->error("Symmetric key (secret) not specified") and return
MC/DC0%0/2
$secret || $self->error('Symmetric key (secret) not specified') && (return)
$secret
$self->error("Symmetric key (secret) not specified")
Truth table66%2/3
A || B
A= $secretB= $self->error("Symmetric key (secret) not specified")
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | # Get HMAC function | ||||||||||||||||||||||||||||||||||||||||||
| 391 | 3 | my $f = Digest::SHA->can("hmac_sha$size"); | |||||||||||||||||||||||||||||||||||||||||
| 392 | 3 | ▶ | $self->error('Unsupported HS signing algorithm') && return unless $f; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
$f || $self->error('Unsupported HS signing algorithm') && (return)
$self->error("Unsupported HS signing algorithm") and return
MC/DC0%0/2
$f || $self->error('Unsupported HS signing algorithm') && (return)
$f
$self->error("Unsupported HS signing algorithm")
Truth table33%1/3
A || B
A= $fB= $self->error("Unsupported HS signing algorithm")
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | # Sign! | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 3 | ▶ | my $sign = $f->($concat, $secret) // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
&$f($concat, $secret) // ""
MC/DC0%0/1
&$f($concat, $secret) // ""
&$f($concat, $secret)
Truth table50%1/2
A
A= &$f($concat, $secret)
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 3 | ▶ | return '' unless length $sign; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 3 | return encode_base64url($sign); | |||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 1 | ▶ | sub sign_rsa { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sign_rsa: called CC=5 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | 1 | my ($self, $size, $concat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | # Get RSA private key | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 1 | my $private_key = $self->private_key; | |||||||||||||||||||||||||||||||||||||||||
| 404 | 1 | ▶ | $self->error('Private key (private_key) not specified') && return unless $private_key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
$private_key || $self->error('Private key (private_key) not specified') && (return)
$self->error("Private key (private_key) not specified") and return
MC/DC0%0/2
$private_key || $self->error('Private key (private_key) not specified') && (return)
$private_key
$self->error("Private key (private_key) not specified")
Truth table33%1/3
A || B
A= $private_keyB= $self->error("Private key (private_key) not specified")
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | # Create RSA object | ||||||||||||||||||||||||||||||||||||||||||
| 407 | 1 | my $rsa = WWW::Suffit::RSA->new(private_key => $private_key); | |||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | # Sign! | ||||||||||||||||||||||||||||||||||||||||||
| 410 | 1 | ▶ | my $sign = $rsa->sign($concat, $size) // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$rsa->sign($concat, $size) // ""
MC/DC0%0/1
$rsa->sign($concat, $size) // ""
$rsa->sign($concat, $size)
Truth table0%0/2
A
A= $rsa->sign($concat, $size)
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | ▶ | unless (length($sign)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | $self->error($rsa->error); | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | return _b64_to_b64url($sign); | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | 3 | ▶ | sub verify_hmac { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify_hmac: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | 3 | my ($self, $size, $concat, $signature) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | # Sign | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 3 | my $got = $self->sign_hmac($size, $concat); | |||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | # Return status | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 3 | ▶ | return 1 if $signature && $got && $signature eq $got; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$signature and $got and $signature eq $got
$signature and $got
MC/DC66%2/3
$signature and $got and $signature eq $got
$signature
$got
$signature eq $got
Truth table75%3/4
A and B and C
A= $signatureB= $gotC= $signature eq $got
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | sub verify_rsa { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify_rsa: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | my ($self, $size, $concat, $signature) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | # Get RSA public key | ||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | my $public_key = $self->public_key; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | ▶ | $self->error('Public key (public_key) not specified') && return unless $public_key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$public_key || $self->error('Public key (public_key) not specified') && (return)
$self->error("Public key (public_key) not specified") and return
MC/DC0%0/2
$public_key || $self->error('Public key (public_key) not specified') && (return)
$public_key
$self->error("Public key (public_key) not specified")
Truth table0%0/3
A || B
A= $public_keyB= $self->error("Public key (public_key) not specified")
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | # Create RSA object | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | my $rsa = WWW::Suffit::RSA->new(public_key => $public_key); | |||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | # Verify! | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | return 1 if $rsa->verify($concat, _b64url_to_b64($signature)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | $self->error($rsa->error); | |||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | sub _b64_to_b64url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_b64_to_b64url: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | my $e = shift // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | $e =~ s/=+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | $e =~ tr[+/][-_]; | |||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | return $e; | |||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | sub _b64url_to_b64 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_b64url_to_b64: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | ▶ | my $s = shift // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | $s =~ tr[-_][+/]; | |||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | ▶ | $s .= '=' while length($s) % 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | __END__ | ||||||||||||||||||||||||||||||||||||||||||