| 1 | package OAuth::Lite::SignatureMethod::RSA_SHA1; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 3 | ▶ | use base 'OAuth::Lite::SignatureMethod'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 3 | ▶ | use Crypt::OpenSSL::RSA; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use Digest::SHA (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 3 | ▶ | use MIME::Base64 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 3 | __PACKAGE__->method_name('RSA-SHA1'); | |
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | OAuth::Lite::SignatureMethod::RSA_SHA1 - RSA_SHA1 signature method class; | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | # Consumer side | ||
| 21 | my $signer = OAuth::Lite::SignatureMethod::RSA_SHA1->new( | ||
| 22 | consumer_secret => $rsa_private_key, | ||
| 23 | ); | ||
| 24 | |||
| 25 | my $signature = $signer->sign($base_string); | ||
| 26 | |||
| 27 | # Service Provider side | ||
| 28 | my $verifier = OAuth::Lite::SignatureMethod::RSA_SHA1->new( | ||
| 29 | consumer_secret => $rsa_public_key, | ||
| 30 | ); | ||
| 31 | unless ($verifier->verify($base_string, $signature)) { | ||
| 32 | say "Signature is invalid!"; | ||
| 33 | } | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | RSA_SHA1 signature method class. | ||
| 38 | |||
| 39 | =head1 PRIVATE KEY AND PUBLIC KEY | ||
| 40 | |||
| 41 | RSA needs two keys that called public key and private key. | ||
| 42 | If you runs OAuth consumer application and want to use this RSA_SHA1 method | ||
| 43 | for signature on OAuth protocol, you have to prepare these keys. | ||
| 44 | |||
| 45 | To generate them in Perl, here is an example. | ||
| 46 | |||
| 47 | my $rsa = Crypt::OpenSSL::RSA->generate_key(1024); | ||
| 48 | my $public_key = $rsa->get_public_key_string(); | ||
| 49 | my $private_key = $rsa->get_private_key_string(); | ||
| 50 | |||
| 51 | And prior to use OAuth protocol with a service provider, | ||
| 52 | you have to register public key onto the service provider. | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =head2 method_name | ||
| 57 | |||
| 58 | Class method. Returns this method's name. | ||
| 59 | |||
| 60 | say OAuth::Lite::SignatureMethod::RSA_SHA1->method_name; | ||
| 61 | # RSA_SHA1 | ||
| 62 | |||
| 63 | =head2 build_body_hash | ||
| 64 | |||
| 65 | say OAuth::Lite::SignatureMethod::RSA_SHA1->build_body_hash($content); | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub build_body_hash { | ||
| 70 | 1 | ▶ | my ( $class, $content ) = @_; |
|
Subroutine
build_body_hash: called CC=1 SCAR=0.0
Pod
documented
| |||
| 71 | 1 | my $hash = MIME::Base64::encode_base64(Digest::SHA::sha1($content)); | |
| 72 | 1 | $hash =~ s/\n//g; | |
| 73 | 1 | return $hash; | |
| 74 | } | ||
| 75 | |||
| 76 | =head2 new(%params) | ||
| 77 | |||
| 78 | On the consumer side, you should pass RSA private key for consumer_secret, | ||
| 79 | But for service provider to verify signature, pass RSA public key that | ||
| 80 | consumer register on service provider beforehand. | ||
| 81 | |||
| 82 | =head3 parameters | ||
| 83 | |||
| 84 | =over 4 | ||
| 85 | |||
| 86 | =item consumer_secret | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | my $signer = OAuth::Lite::SignatureMethod::RSA_SHA1->new( | ||
| 91 | consumer_secret => $rsa_private_key, | ||
| 92 | ); | ||
| 93 | |||
| 94 | my $verifier = OAuth::Lite::SignatureMethod::RSA_SHA1->new( | ||
| 95 | consumer_secret => $rsa_public_key, | ||
| 96 | ); | ||
| 97 | |||
| 98 | =head2 sign($base_string) | ||
| 99 | |||
| 100 | Generate signature from base string. | ||
| 101 | |||
| 102 | my $signature = $method->sign($base_string); | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub sign { | ||
| 107 | 1 | ▶ | my ($self, $base_string) = @_; |
|
Subroutine
sign: called CC=1 SCAR=0.0
Pod
documented
| |||
| 108 | 1 | my $private_key_pem = $self->{consumer_secret}; | |
| 109 | 1 | my $private_key = Crypt::OpenSSL::RSA->new_private_key($private_key_pem); | |
| 110 | 1 | my $signature = MIME::Base64::encode_base64($private_key->sign($base_string)); | |
| 111 | 1 | chomp $signature; | |
| 112 | 1 | $signature; | |
| 113 | } | ||
| 114 | |||
| 115 | =head2 verify($base_string, $signature) | ||
| 116 | |||
| 117 | Verify signature with base string. | ||
| 118 | |||
| 119 | my $signature_is_valid = $method->verify($base_string, $signature); | ||
| 120 | unless ($signature_is_valid) { | ||
| 121 | say "Signature is invalid!"; | ||
| 122 | } | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub verify { | ||
| 127 | 2 | ▶ | my ($self, $base_string, $signature) = @_; |
|
Subroutine
verify: called CC=1 SCAR=0.0
Pod
documented
| |||
| 128 | 2 | my $public_key_pem = $self->{consumer_secret}; | |
| 129 | 2 | my $public_key = Crypt::OpenSSL::RSA->new_public_key($public_key_pem); | |
| 130 | 2 | return $public_key->verify($base_string, MIME::Base64::decode_base64($signature)); | |
| 131 | } | ||
| 132 | |||
| 133 | =head1 AUTHOR | ||
| 134 | |||
| 135 | Lyo Kato, C<lyo.kato _at_ gmail.com> | ||
| 136 | |||
| 137 | =head1 COPYRIGHT AND LICENSE | ||
| 138 | |||
| 139 | This library is free software; you can redistribute it and/or modify | ||
| 140 | it under the same terms as Perl itself, either Perl version 5.8.6 or, | ||
| 141 | at your option, any later version of Perl 5 you may have available. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | 3 | 1; | |