Crypt-JWS-OpenSSL

 view release on metacpan or  search on metacpan

t/12_jwks.t  view on Meta::CPAN

use Test::More;
use Test::Deep;
use JSON::MaybeXS 1.002002 ();
use lib 't/lib';

BEGIN {   
    use_ok('Crypt::JWS::OpenSSL');
    #use_ok('Crypt::JWS::OpenSSL::Util::JWK');
}

## using canonical JSON ordering for tests. DON'T do this in production
my $handler = Crypt::JWS::OpenSSL->new('JSON' => JSON::MaybeXS->new->canonical->utf8(1));
$handler->throw_errors(1);

my $JWK_STORE;

load_jwk_store();

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 $prvjwk2048 = get_jwk('id-rsa-2048-private');
    my $pubjwk2048 = get_jwk('id-rsa-2048-public');
    my $prvjwk4096 = get_jwk('id-rsa-4096-private');
    my $pubjwk4096 = get_jwk('id-rsa-4096-public');
    
    my $bit_key_map = {
        '2048' => {
            public      => $pubkey2048,
            private     => $prvkey2048,
            jwk_public  => $pubjwk2048,
            jwk_private => $prvjwk2048,
        },
        '4096' => {
            public      => $pubkey4096,
            private     => $prvkey4096,
            jwk_public  => $pubjwk4096,
            jwk_private => $prvjwk4096,
        },
    };
    
    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'};
        my $pubjwk = $bit_key_map->{$BITS}->{'jwk_public'};
        my $prvjwk = $bit_key_map->{$BITS}->{'jwk_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      => $prvjwk,
                    header      => { alg => $algo, typ => 'JWT' }
                );
                
                ok( $token, qq(RSA $BITS $algo - token returned) );
                my @token_parts   = split(/\./, $token);
                is( scalar @token_parts, 3, qq(RSA $BITS $algo - token has 3 parts));
                
                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));



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