Crypt-JWS-OpenSSL

 view release on metacpan or  search on metacpan

t/lib/token_cross.pl  view on Meta::CPAN

use Test::More;
use Test::Deep;

our $H1;
our $H2;
our $H1NAME;
our $H2NAME;


my $inputclaims = {
    'sub'   => 'Test subject',
    'roles' => 'testrole',
    'exp'   => 1786935249,
    'iss'   => 'this/test/file'
};


## 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 );
        
    SKIP: {
        
        skip qq(one or both handlers $H1NAME and $H2NAME do not support PKCS1 v1_5 RSA padding), if(!$H1->can_use_pkcs1_padding || !$H2->can_use_pkcs1_padding );
        
        for my $BITS ( '2048', '4096' ) {
            my $pubkey = $bit_key_map->{$BITS}->{'public'};
            my $prvkey = $bit_key_map->{$BITS}->{'private'};
            for my $algo ( @pkcs1algos ) {
                my $token_1 = $H1->encode(
                    claims      => $inputclaims,
                    secret      => $prvkey,
                    header      => { alg => $algo, typ => 'JWT' }
                );
                
                my $token_2 = $H2->encode(
                    claims      => $inputclaims,
                    secret      => $prvkey,
                    header      => { alg => $algo, typ => 'JWT' }
                );
                
                is($token_1, $token_2, qq($BITS bit $algo tokens produced by $H1NAME and $H2NAME are identical));
                
                my @token_1_parts   = split(/\./, $token_1);
                
                my $expected_1_decoded = {
                    claims      => $inputclaims,
                    header      => { alg => $algo , typ => 'JWT' },
                    verifytoken => join(':', $algo, $token_1_parts[0] . '.' . $token_1_parts[1],  $token_1_parts[2] ),
                };
                
                my @token_2_parts   = split(/\./, $token_2);
                
                my $expected_2_decoded = {
                    claims      => $inputclaims,
                    header      => { alg => $algo , typ => 'JWT' },
                    verifytoken => join(':', $algo, $token_2_parts[0] . '.' . $token_2_parts[1],  $token_2_parts[2] ),
                };
                 
                my $decoded_1 = $H1->decode_unverified( token  => $token_1 );
                my $decoded_2 = $H2->decode_unverified( token  => $token_2 );
                
                cmp_deeply($decoded_1, $expected_1_decoded, qq($BITS bit $algo $H1NAME decoded is expected decoded));
                cmp_deeply($decoded_2, $expected_2_decoded, qq($BITS bit $algo $H2NAME decoded is expected decoded));
                
                my $verified_1 = $H2->verify(
                    verifytoken => $decoded_1->{verifytoken},
                    secret      => $pubkey
                );



( run in 1.048 second using v1.01-cache-2.11-cpan-8dfa8b56332 )