Crypt-JWS-OpenSSL
view release on metacpan or search on metacpan
t/10_basic.t view on Meta::CPAN
{ algo => 'HS384', secret => 't/keys/hmac_384.secret', bits => 384 },
{ algo => 'HS512', secret => 't/keys/hmac_512.secret', bits => 512 },
);
my $SECRETS = {};
for my $cnf ( @configs ) {
my $algo = $cnf->{algo};
my $secret = load_key($cnf->{secret});
$secret = MIME::Base64::decode_base64($secret);
$SECRETS->{$algo} = $secret;
}
for my $cnf ( @configs ) {
my $algo = $cnf->{algo};
my $BITS = $cnf->{bits};
my $prvkey = $SECRETS->{$algo};
my $pubkey = $SECRETS->{$algo};
my $token = $handler->encode({
claims => $inputclaims,
secret => $prvkey,
header => { alg => $algo, typ => 'JWT' },
});
ok( $token, qq($algo - token returned) );
my @parts = split(/\./, $token );
is( scalar @parts, 3, qq($algo - token has 3 parts));
my $expected_decoded = {
claims => $inputclaims,
header => { alg => $algo, typ => 'JWT' },
verifytoken => join(':', $algo, $parts[0] . '.' . $parts[1], $parts[2] ),
};
my $bad_verify_token = join(':', $algo, $parts[0] . '.' . $parts[1], random_byte_base64_signature($BITS) );
my $decoded = $handler->decode_unverified( token => $token );
cmp_deeply($decoded, $expected_decoded, qq($algo - decoded is expected decoded));
my $verified = $handler->verify(
verifytoken => $decoded->{verifytoken},
secret => $pubkey
);
is($verified, 1, qq($algo - verified));
$verified = $handler->verify(
verifytoken => $bad_verify_token,
secret => $pubkey
);
is($verified, 0, qq($algo - bad verify token fails in correct part of code));
}
}
## RSA TESTS
{
my $prvkey2048 = load_key('t/keys/rsa_private_key.pem');
my $pubkey2048 = load_key('t/keys/rsa_public_key.pem');
my $prvkey4096 = load_key('t/keys/rsa_private_key_4096.pem');
my $pubkey4096 = load_key('t/keys/rsa_public_key_4096.pem');
my $bit_key_map = {
'2048' => { public => $pubkey2048, private => $prvkey2048 },
'4096' => { public => $pubkey4096, private => $prvkey4096 },
};
my $badrsakey = load_key('t/keys/rsa_bad_public_key.pem');
my @pkcs1algos = qw( RS256 RS384 RS512 );
my @pssalgos = qw( PS256 PS384 PS512 );
for my $BITS ( '2048', '4096' ) {
my $pubkey = $bit_key_map->{$BITS}->{'public'};
my $prvkey = $bit_key_map->{$BITS}->{'private'};
SKIP: {
skip 'installed version of Crypt::OpenSSL::RSA does not support PKCS1 v1_5 RSA padding', if!$handler->can_use_pkcs1_padding;
for my $algo ( @pkcs1algos ) {
my $token = $handler->encode(
claims => $inputclaims,
secret => $prvkey,
header => { alg => $algo, typ => 'JWT' }
);
my $compare_token = $handler->encode(
claims => $inputclaims,
secret => $prvkey,
header => { alg => $algo, typ => 'JWT' }
);
my @token_parts = split(/\./, $token);
my @compare_parts = split(/\./, $compare_token);
is($token_parts[0], $compare_parts[0], qq(RSA $BITS $algo - comparing header of 2 tokens with identical content));
is($token_parts[1], $compare_parts[1], qq(RSA $BITS $algo - comparing claims of 2 tokens with identical content));
if ($algo =~ m!^R!) {
is($token_parts[2], $compare_parts[2], qq(RSA $BITS $algo - signatures of 2 tokens with same content should be same));
} else {
isnt($token_parts[2], $compare_parts[2], qq(RSA $BITS $algo - signatures of 2 tokens with same content should be different));
}
ok( $token, qq(RSA $BITS $algo - token returned) );
my @parts = split(/\./, $token );
is( scalar @parts, 3, qq(RSA $BITS $algo - token has 3 parts));
my $expected_decoded = {
claims => $inputclaims,
header => { alg => $algo , typ => 'JWT' },
verifytoken => join(':', $algo, $parts[0] . '.' . $parts[1], $parts[2] ),
};
my $bad_verify_token = join(':', $algo, $parts[0] . '.' . $parts[1], random_byte_base64_signature($BITS) );
my $decoded = $handler->decode_unverified( token => $token );
cmp_deeply($decoded, $expected_decoded, qq(RSA $BITS $algo - decoded is expected decoded));
my $verified = $handler->verify(
( run in 1.645 second using v1.01-cache-2.11-cpan-8dfa8b56332 )