Acme-JWT

 view release on metacpan or  search on metacpan

lib/Acme/JWT.pm  view on Meta::CPAN

            HS512 => $hmac,
        };

        if ($has_sha2) {
            $algorithm = {
                %$algorithm,
                (
                    RS256 => $verify_method,
                    RS384 => $verify_method,
                    RS512 => $verify_method,
                ),
            };
        }
        if (exists($algorithm->{$algo})) {
            unless ($algorithm->{$algo}->($algo, $key, $signing_input, $signature)) {
                die 'Signature verifacation failed';
            }
        } else {
            die 'Algorithm not supported';
        }
    }
    return $payload;
}

sub sign {
    my $self = shift;
    my ($algo, $key, $signing_input) = @_;
    my $hmac = sub {
        my ($algo, $key, $signing_input) = @_;
        $self->sign_hmac($algo, $key, $signing_input);
    };
    my $rsa = sub {
        my ($algo, $key, $signing_input) = @_;
        $self->sign_rsa($algo, $key, $signing_input);
    };
    my $algorithm = {
        HS256 => $hmac,
        HS384 => $hmac,
        HS512 => $hmac,
    };
    if ($has_sha2) {
        $algorithm = {
            %$algorithm,
            (
                RS256 => $rsa,
                RS384 => $rsa,
                RS512 => $rsa,
            ),
        };
    }
    unless (exists($algorithm->{$algo})) {
        die 'Unsupported signing method';
    }
    $algorithm->{$algo}->($algo, $key, $signing_input);
}

sub sign_rsa {
    my $self = shift;
    my ($algo, $key, $msg) = @_;
    $algo =~ s/\D+//;
    my $private_key = Crypt::OpenSSL::RSA->new_private_key($key);
    $private_key->can("use_sha${algo}_hash")->($private_key);
    $private_key->sign($msg);
}

sub verify_rsa {
    my $self = shift;
    my ($algo, $key, $signing_input, $signature) = @_;
    $algo =~ s/\D+//;
    my $public_key = Crypt::OpenSSL::RSA->new_public_key($key);
    $public_key->can("use_sha${algo}_hash")->($public_key);
    $public_key->verify($signing_input, $signature);
}

sub sign_hmac {
    my $self = shift;
    my ($algo, $key, $msg) = @_;
    $algo =~ s/\D+//;
    my $method = $self->can("hmac_sha$algo");
    $method->($msg, $key);
}

1;
__END__

=head1 NAME

Acme::JWT - JWT utilities.

=head1 SYNOPSIS

  use Acme::JWT;

=head1 DESCRIPTION

Acme::JWT is provided JWT method.
JWT is JSON Web Token
see http://self-issued.info/docs/draft-jones-json-web-token-06.html

rewrite from ruby version.

=head1 AUTHOR

NAGAYA Shinichiro E<lt>clairvy@gmail.comE<gt>

=head1 SEE ALSO

https://github.com/clairvy/p5-Acme-JWT

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.825 second using v1.00-cache-2.02-grep-82fe00e-cpan-dad7e4baca0 )