| 1 | package OAuth::Lite::ServerUtil; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use base 'Class::ErrorHandler'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 2 | use OAuth::Lite::Util qw( | |||||||||||
| 9 | decode_param | ||||||||||||
| 10 | create_signature_base_string | ||||||||||||
| 11 | 2 | ▶ | ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use OAuth::Lite::Problems qw(:all); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 2 | ▶ | use List::MoreUtils qw(any none); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use UNIVERSAL::require; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 2 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | =head1 NAME | ||||||||||||
| 18 | |||||||||||||
| 19 | OAuth::Lite::ServerUtil - server side utility | ||||||||||||
| 20 | |||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||
| 22 | |||||||||||||
| 23 | my $util = OAuth::Lite::ServerUtil->new; | ||||||||||||
| 24 | $util->support_signature_method('HMAC-SHA1'); | ||||||||||||
| 25 | $util->allow_extra_params(qw/file size/); | ||||||||||||
| 26 | |||||||||||||
| 27 | unless ($util->validate_params($oauth_params)) { | ||||||||||||
| 28 | return $server->error(400, $util->errstr); | ||||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | $util->verify_signature( | ||||||||||||
| 32 | method => $r->method, | ||||||||||||
| 33 | params => $oauth_params, | ||||||||||||
| 34 | url => $request_uri, | ||||||||||||
| 35 | consumer_secret => $consumer->secret, | ||||||||||||
| 36 | ) or return $server->error(401, $util->errstr); | ||||||||||||
| 37 | |||||||||||||
| 38 | And see L<OAuth::Lite::Server::mod_perl2> source code. | ||||||||||||
| 39 | |||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||
| 41 | |||||||||||||
| 42 | This module helps you to implement application that acts as OAuth Service Provider. | ||||||||||||
| 43 | |||||||||||||
| 44 | =head1 PAY ATTENTION | ||||||||||||
| 45 | |||||||||||||
| 46 | If you use OAuth 1.31 or older version, its has invalid way to normalize params. | ||||||||||||
| 47 | (when there are two or more same key and they contain ASCII and non ASCII value) | ||||||||||||
| 48 | |||||||||||||
| 49 | But the many services have already supported deprecated version, | ||||||||||||
| 50 | and the correct way breaks backward compatibility. | ||||||||||||
| 51 | So, from 1.32, supported both correct and deprecated method. | ||||||||||||
| 52 | |||||||||||||
| 53 | use $OAuth::Lite::USE_DEPRECATED_NORMALIZER to switch behaviour. | ||||||||||||
| 54 | Currently 1 is set by default to keep backward compatibility. | ||||||||||||
| 55 | |||||||||||||
| 56 | use OAuth::Lite::ServerUtil; | ||||||||||||
| 57 | use OAuth::Lite; | ||||||||||||
| 58 | |||||||||||||
| 59 | $OAuth::Lite::USE_DEPRECATED_NORMALIZER = 0; | ||||||||||||
| 60 | ... | ||||||||||||
| 61 | |||||||||||||
| 62 | |||||||||||||
| 63 | =head1 METHODS | ||||||||||||
| 64 | |||||||||||||
| 65 | =head2 new | ||||||||||||
| 66 | |||||||||||||
| 67 | Constructor | ||||||||||||
| 68 | |||||||||||||
| 69 | my $util = OAuth::Lite::ServerUtil->new; | ||||||||||||
| 70 | |||||||||||||
| 71 | Set strict true by default, and it judge unsupported param as invalid when validating params. | ||||||||||||
| 72 | You can build ServerUtil as non-strict mode, then it accepts unsupported parameters. | ||||||||||||
| 73 | |||||||||||||
| 74 | my $util = OAuth::Lite::ServerUtil->new( strict => 0 ); | ||||||||||||
| 75 | |||||||||||||
| 76 | =cut | ||||||||||||
| 77 | |||||||||||||
| 78 | sub new { | ||||||||||||
| 79 | 2 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 80 | 2 | my %args = @_; | |||||||||||
| 81 | 2 | ▶ | my $strict = exists $args{strict} ? $args{strict} : 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 2 | my $self = bless { | |||||||||||
| 83 | supported_signature_methods => {}, | ||||||||||||
| 84 | allowed_extra_params => [], | ||||||||||||
| 85 | strict => $strict, | ||||||||||||
| 86 | }, $class; | ||||||||||||
| 87 | 2 | $self; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | =head2 allow_extra_param($param_name); | ||||||||||||
| 91 | |||||||||||||
| 92 | When you validate oauth parameters, if an extra parameter | ||||||||||||
| 93 | is included, the validation will fail. | ||||||||||||
| 94 | |||||||||||||
| 95 | my $params = { | ||||||||||||
| 96 | oauth_version => '1.0', | ||||||||||||
| 97 | ...and other oauth parameters, | ||||||||||||
| 98 | }; | ||||||||||||
| 99 | $params->{file} = "foo.jpg"; | ||||||||||||
| 100 | |||||||||||||
| 101 | # fail! | ||||||||||||
| 102 | unless ($util->validate_params($params)) { | ||||||||||||
| 103 | $your_app->error( $util->errstr ); | ||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | So, if you want allow extra parameter, use this method. | ||||||||||||
| 107 | |||||||||||||
| 108 | $util->allow_extra_param('file'); | ||||||||||||
| 109 | |||||||||||||
| 110 | my $params = { | ||||||||||||
| 111 | oauth_version => '1.0', | ||||||||||||
| 112 | ...and other oauth parameters, | ||||||||||||
| 113 | }; | ||||||||||||
| 114 | $params->{file} = "foo.jpg"; | ||||||||||||
| 115 | |||||||||||||
| 116 | # Now this results successfully. | ||||||||||||
| 117 | unless ($util->validate_params($params)) { | ||||||||||||
| 118 | $your_app->error( $util->errstr ); | ||||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | =cut | ||||||||||||
| 122 | |||||||||||||
| 123 | sub allow_extra_param { | ||||||||||||
| 124 | 3 | ▶ | my ($self, $param) = @_; | ||||||||||
|
Subroutine
allow_extra_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 125 | 3 | push @{ $self->{allowed_extra_params} }, $param; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | =head2 allow_extra_params($param1, $param2, ...) | ||||||||||||
| 129 | |||||||||||||
| 130 | You can allow multiple extra parameters at once. | ||||||||||||
| 131 | |||||||||||||
| 132 | $util->allow_extra_params(qw/file size/); | ||||||||||||
| 133 | |||||||||||||
| 134 | =cut | ||||||||||||
| 135 | |||||||||||||
| 136 | sub allow_extra_params { | ||||||||||||
| 137 | 1 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
allow_extra_params: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 138 | 1 | $self->allow_extra_param($_) for @_; | |||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | =head2 support_signature_method($method_class_name); | ||||||||||||
| 142 | |||||||||||||
| 143 | Set the signature method class's name that your server can supports. | ||||||||||||
| 144 | |||||||||||||
| 145 | $util->support_signature_method('HMAC_SHA1'); | ||||||||||||
| 146 | |||||||||||||
| 147 | This method requires indicated signature method class inside. | ||||||||||||
| 148 | So, you should install OAuth::Lite::SignatureMethod::$method_class_name beforehand. | ||||||||||||
| 149 | For example, when your choise is HMAC_SHA1, you need to have | ||||||||||||
| 150 | OAuth::Lite::SignatureMethod::HMAC_SHA1 installed in your server. | ||||||||||||
| 151 | |||||||||||||
| 152 | =cut | ||||||||||||
| 153 | |||||||||||||
| 154 | sub support_signature_method { | ||||||||||||
| 155 | 5 | ▶ | my ($self, $method_class) = @_; | ||||||||||
|
Subroutine
support_signature_method: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 156 | 5 | $method_class =~ s/-/_/g; | |||||||||||
| 157 | 5 | my $class = join('::', 'OAuth::Lite::SignatureMethod', $method_class); | |||||||||||
| 158 | 5 | ▶ | $class->require or Carp::croak sprintf(q{Couldn't require class, %s}, $class); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 159 | 3 | $self->{supported_signature_methods}{$class->method_name} = $class; | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | =head2 support_signature_methods($method1, $method2, ...); | ||||||||||||
| 163 | |||||||||||||
| 164 | You can set multiple signature method class at once. | ||||||||||||
| 165 | |||||||||||||
| 166 | $util->support_signature_methods(qw/HMAC_SHA1 RSA_SHA1/); | ||||||||||||
| 167 | |||||||||||||
| 168 | =cut | ||||||||||||
| 169 | |||||||||||||
| 170 | sub support_signature_methods { | ||||||||||||
| 171 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
support_signature_methods: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 172 | 2 | $self->support_signature_method($_) for @_; | |||||||||||
| 173 | } | ||||||||||||
| 174 | |||||||||||||
| 175 | =head2 validate_params($params, [$check_token]); | ||||||||||||
| 176 | |||||||||||||
| 177 | Check if the request includes all required params | ||||||||||||
| 178 | and doesn't include unsupported params. | ||||||||||||
| 179 | It doesn't check unsupported params when working on strict mode. | ||||||||||||
| 180 | |||||||||||||
| 181 | unless ($util->validate_params($params)) { | ||||||||||||
| 182 | $your_app->error( $util->errstr ); | ||||||||||||
| 183 | } | ||||||||||||
| 184 | |||||||||||||
| 185 | When the request is to exchange tokens or to access to protected resources, | ||||||||||||
| 186 | pass 1 for second argument. This flag indicates that oauth_token param is needed. | ||||||||||||
| 187 | |||||||||||||
| 188 | unless ($util->validate_params($params, 1)) { | ||||||||||||
| 189 | $your_app->error( $util->errstr ); | ||||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | =cut | ||||||||||||
| 193 | |||||||||||||
| 194 | sub validate_params { | ||||||||||||
| 195 | 6 | ▶ | my ($self, $origin_params, $check_token) = @_; | ||||||||||
|
Subroutine
validate_params: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||
| 196 | 6 | my $params = {%$origin_params}; #copy | |||||||||||
| 197 | 6 | ▶ | delete $params->{oauth_consumer_key} or return $self->error(PARAMETER_ABSENT); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 198 | 6 | ▶ | delete $params->{oauth_nonce} or return $self->error(PARAMETER_ABSENT); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 199 | 6 | ▶ | delete $params->{oauth_timestamp} or return $self->error(PARAMETER_ABSENT); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 200 | 6 | ▶ | delete $params->{oauth_signature_method} or return $self->error(PARAMETER_ABSENT); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 201 | 6 | ▶ | delete $params->{oauth_signature} or return $self->error(PARAMETER_ABSENT); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 202 | 6 | delete $params->{oauth_version}; | |||||||||||
| 203 | 6 | ▶ | if ($check_token) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 204 | 4 | ▶ | delete $params->{oauth_token} or return $self->error(PARAMETER_ABSENT); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 205 | } | ||||||||||||
| 206 | 5 | ▶ | if ( $self->{strict} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | 4 | my @extra_params = keys %$params; | |||||||||||
| 208 | 4 | my @allowed = @{ $self->{allowed_extra_params} }; | |||||||||||
| 209 | 4 | for my $extra ( @extra_params ) { | |||||||||||
| 210 | 10 | ▶ | if (none { $extra eq $_ } @allowed) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 211 | 2 | return $self->error(PARAMETER_REJECTED); | |||||||||||
| 212 | } | ||||||||||||
| 213 | } | ||||||||||||
| 214 | } | ||||||||||||
| 215 | 3 | 1; | |||||||||||
| 216 | } | ||||||||||||
| 217 | |||||||||||||
| 218 | =head2 validate_signature_method($method_name) | ||||||||||||
| 219 | |||||||||||||
| 220 | unless ($util->validate_signature_method('HMAC-SHA1')) { | ||||||||||||
| 221 | |||||||||||||
| 222 | $your_app->error(qq/Unsupported signature method/); | ||||||||||||
| 223 | ... | ||||||||||||
| 224 | } | ||||||||||||
| 225 | |||||||||||||
| 226 | =cut | ||||||||||||
| 227 | |||||||||||||
| 228 | sub validate_signature_method { | ||||||||||||
| 229 | 8 | ▶ | my ($self, $method) = @_; | ||||||||||
|
Subroutine
validate_signature_method: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 230 | 8 | ▶ | return unless $method; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 231 | 8 | ▶ | any { $_ eq $method } keys %{$self->{supported_signature_methods}}; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 232 | } | ||||||||||||
| 233 | |||||||||||||
| 234 | =head2 verify_signature(%args) | ||||||||||||
| 235 | |||||||||||||
| 236 | =over 4 | ||||||||||||
| 237 | |||||||||||||
| 238 | =item method - HTTP request method | ||||||||||||
| 239 | |||||||||||||
| 240 | =item params - parameters hash reference | ||||||||||||
| 241 | |||||||||||||
| 242 | =item url - requested uri | ||||||||||||
| 243 | |||||||||||||
| 244 | =item consumer_secret - consumer secret value(optional) | ||||||||||||
| 245 | |||||||||||||
| 246 | =item token_secret - token secret value(optional) | ||||||||||||
| 247 | |||||||||||||
| 248 | =back | ||||||||||||
| 249 | |||||||||||||
| 250 | # you can omit consumer_secret and token_secret if you don't need them. | ||||||||||||
| 251 | $util->verify_signature( | ||||||||||||
| 252 | method => $r->method, | ||||||||||||
| 253 | params => $params, | ||||||||||||
| 254 | url => $requested_uri, | ||||||||||||
| 255 | consumer_secret => $consumer_secret, | ||||||||||||
| 256 | token_secret => $token_secret, | ||||||||||||
| 257 | ) or die $utl->errstr; | ||||||||||||
| 258 | |||||||||||||
| 259 | =cut | ||||||||||||
| 260 | |||||||||||||
| 261 | sub verify_signature { | ||||||||||||
| 262 | 2 | ▶ | my ($self, %args) = @_; | ||||||||||
|
Subroutine
verify_signature: called CC=8 SCAR=21.8
Pod
documented
| |||||||||||||
| 263 | |||||||||||||
| 264 | 2 | ▶ | my $http_method = $args{method} or Carp::croak(qq/method not found/); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 265 | 2 | ▶ | my $url = $args{url} or Carp::croak(qq/url not found/); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 266 | 2 | ▶ | my $params = $args{params} or Carp::croak(qq/params not found/); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 267 | |||||||||||||
| 268 | 2 | ▶ | my $consumer_secret = $args{consumer_secret} || ''; | ||||||||||
|
Condition50%1/2
$args{'consumer_secret'} || ''
MC/DC0%0/1
$args{'consumer_secret'} || ''
$args{'consumer_secret'}
Truth table50%1/2
A
A= $args{'consumer_secret'}
| |||||||||||||
| 269 | 2 | ▶ | my $token_secret = $args{token_secret} || ''; | ||||||||||
|
Condition50%1/2
$args{'token_secret'} || ''
MC/DC0%0/1
$args{'token_secret'} || ''
$args{'token_secret'}
Truth table50%1/2
A
A= $args{'token_secret'}
| |||||||||||||
| 270 | 2 | my $signature_method = $params->{oauth_signature_method}; | |||||||||||
| 271 | 2 | my $signature = $params->{oauth_signature}; | |||||||||||
| 272 | |||||||||||||
| 273 | 2 | my $base_string = create_signature_base_string($http_method, $url, $params); | |||||||||||
| 274 | 2 | ▶ | unless ($self->validate_signature_method($signature_method)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 275 | 0 | return $self->error(SIGNATURE_METHOD_REJECTED); | |||||||||||
| 276 | } | ||||||||||||
| 277 | 2 | my $method_class = $self->{supported_signature_methods}{$signature_method}; | |||||||||||
| 278 | 2 | my $method = $method_class->new( | |||||||||||
| 279 | consumer_secret => $consumer_secret, | ||||||||||||
| 280 | token_secret => $token_secret, | ||||||||||||
| 281 | ); | ||||||||||||
| 282 | 2 | ▶ | unless ($method->verify($base_string, $signature)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 283 | 1 | return $self->error(SIGNATURE_INVALID); | |||||||||||
| 284 | } | ||||||||||||
| 285 | 1 | 1; | |||||||||||
| 286 | } | ||||||||||||
| 287 | |||||||||||||
| 288 | =head1 SEE ALSO | ||||||||||||
| 289 | |||||||||||||
| 290 | L<OAuth::Lite::Server::mod_perl2> | ||||||||||||
| 291 | |||||||||||||
| 292 | =head1 AUTHOR | ||||||||||||
| 293 | |||||||||||||
| 294 | Lyo Kato, C<lyo.kato _at_ gmail.com> | ||||||||||||
| 295 | |||||||||||||
| 296 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 297 | |||||||||||||
| 298 | This library is free software; you can redistribute it and/or modify | ||||||||||||
| 299 | it under the same terms as Perl itself, either Perl version 5.8.6 or, | ||||||||||||
| 300 | at your option, any later version of Perl 5 you may have available. | ||||||||||||
| 301 | |||||||||||||
| 302 | =cut | ||||||||||||
| 303 | |||||||||||||
| 304 | 2 | 1; | |||||||||||