Dancer2-Plugin-JWT
view release on metacpan or search on metacpan
lib/Dancer2/Plugin/JWT.pm view on Meta::CPAN
die 'Unknown algoritm';
}
if ( $need_enc ) {
unless ( exists $config->{enc} && defined $config->{enc} ) {
die 'JWE cannot be used with empty encryption method';
}
if ( $config->{enc} =~ /^A(128|192|256)GCM$/ ) {
$enc = $config->{enc};
} elsif ( $config->{enc} =~ /^A(128|192|256)CBC-HS(256|384|512)$/ ) {
my $a = $1;
my $hs = $2;
if ( ( ( $a * 2 ) - $hs ) != 0 ) {
die 'Incompatible A and HS values';
}
$enc = $config->{enc};
}
}
use Test::WWW::Mechanize::PSGI;
use HTTP::Request::Common;
use Crypt::JWT qw(encode_jwt decode_jwt);
#plan tests => 5;
{
use Dancer2;
BEGIN {
set plugins => { JWT => { secret => 'secret', alg => 'PBES2-HS256+A128KW', enc => 'A128CBC-HS256' } };
}
use Dancer2::Plugin::JWT;
set log => 'debug';
get '/defined/jwt' => sub {
defined(jwt) ? "DEFINED" : "UNDEFINED";
};
get '/define/jwt' => sub {
$mech->get_ok('/defined/jwt');
$mech->content_is("UNDEFINED", "by default it is undef");
$mech->get_ok('/define/jwt');
$mech->content_is("OK", "No exceptions on defining jwt");
my $response = $mech->res();
my $authorization = $response->authorization;
ok($authorization, "We have something");
my $x = decode_jwt( token => $authorization, key => "secret", alg => 'PBES2-HS256+A128KW', enc => 'A128CBC-HS256');
is_deeply($x, {my => 'data'}, "Got correct data back");
$mech->add_header("Authorization" => $authorization);
$mech->get_ok("/defined/jwt");
$mech->content_is("DEFINED", "We got something");
$mech->delete_header("Authorization");
$mech->get_ok("/redirect/jwt");
$mech->content_is("OK", "we redirected");
$response = $mech->res();
$authorization = $response->authorization;
ok($authorization, "Redirect keeped jwt");
$x = eval { decode_jwt( token => $authorization, key => "secret", alg => 'PBES2-HS256+A128KW', enc => 'A128CBC-HS256' ) };
is_deeply($x, {my => 'redirect'}, "Got correct data back even with redirect");
done_testing();
__END__
test_psgi $app, sub {
my $cb = shift;
is $cb->(GET '/defined/jwt')->content, "UNDEFINED", "by default it is undef";
#--
{
my $ans = $cb->(GET '/define/jwt');
is $ans->content, "OK", "No exceptions on defining jwt";
my $authorization = $ans->header("Authorization");
ok($authorization, "We have something");
my $x = decode_jwt( token => $authorization, key => "secret", alg => 'PBES2-HS256+A128KW', enc => 'A128CBC-HS256' );
is_deeply($x, {my => 'data'}, "Got correct data back");
is $cb->(HTTP::Request->new(GET => '/defined/jwt',
HTTP::Headers->new(Authorization => $authorization)))->content,
"DEFINED", "we got something";
}
#--
};
t/05-optional-returns.t view on Meta::CPAN
};
}
my $app = __PACKAGE__->to_app;
is (ref $app, 'CODE', 'Got the test app');
my $mech = Test::WWW::Mechanize::PSGI -> new ( app => $app );
my $secret = 'test-secret';
my $alg = 'PBES2-HS256+A128KW';
my $enc = 'A128CBC-HS256';
my $need_iat = 1;
my $need_nbf = 1;
my $need_exp = 2;
my %jwt_claims = ( 'some' => 1, 'jwt' => 2, 'stuff' => 3 );
my $jwt = encode_jwt(
payload => \%jwt_claims,
key => $secret,
alg => $alg,
enc => $enc,
auto_iat => $need_iat,
( run in 0.678 second using v1.01-cache-2.11-cpan-df04353d9ac )