Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/JSON/WebToken.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ our $VERSION = '0.08';
use parent 'Exporter';

use Carp qw(croak);
use JSON qw(encode_json decode_json);
use JSON qw(decode_json);
use MIME::Base64 qw(encode_base64 decode_base64);
use Module::Runtime qw(use_module);

Expand Down Expand Up @@ -80,8 +80,8 @@ sub encode {
);
}

my $header_segment = encode_base64url(encode_json $header);
my $claims_segment = encode_base64url(encode_json $claims);
my $header_segment = encode_base64url(_encode_json($header));
my $claims_segment = encode_base64url(_encode_json($claims));
my $signature_input = join '.', $header_segment, $claims_segment;

my $signature = $class->_sign($algorithm, $signature_input, $secret);
Expand All @@ -94,6 +94,11 @@ sub encode_jwt {
__PACKAGE__->encode(@_);
}

my $JSON; # cache
sub _encode_json {
return ($JSON ||= JSON->new->utf8->canonical)->encode($_[0]);
}

sub decode {
my ($class, $jwt, $secret, $is_verify) = @_;
unless (defined $jwt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ my $secret = pack 'C*' => @{ [
] };

my $guard = mock_guard('JSON::WebToken' => {
encode_json => sub {
_encode_json => sub {
my $array = [$header, $claims];
sub { shift @$array };
}->(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ ok $rsa->verify($singing_input, $S);

my $guard = mock_guard(
'JSON::WebToken' => {
encode_json => sub {
_encode_json => sub {
my $array = [$header, $claims];
sub { shift @$array };
}->(),
Expand Down
2 changes: 1 addition & 1 deletion t/spec/draft-ietf-oauth-json-web-token-06-3.1.example.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ my $claims = pack 'C*' => @{ [
my $secret = '';

my $guard = mock_guard('JSON::WebToken' => {
encode_json => sub {
_encode_json => sub {
my $array = [$header, $claims];
sub { shift @$array };
}->(),
Expand Down