| 1 | package Net::Braintree::TransparentRedirect::QueryString; | ||
| 2 | 0 | ▶ | use CGI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use Net::Braintree::Digest qw(hexdigest); | ||
| 4 | use Moose; | ||
| 5 | use Carp qw(confess); | ||
| 6 | |||
| 7 | has 'config' => ( is => 'rw', default => sub { Net::Braintree->configuration}); | ||
| 8 | |||
| 9 | sub validate { | ||
| 10 | my($self, $query_string) = @_; | ||
| 11 | $self->check_http_status($query_string); | ||
| 12 | confess "ForgedQueryString" if $self->hash_is_forged($query_string); | ||
| 13 | return 1; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub parse { | ||
| 17 | my ($self, $query_string) = @_; | ||
| 18 | my $query = CGI->new($query_string); | ||
| 19 | return $query->Vars; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub hash_is_forged { | ||
| 23 | my ($self, $query_string) = @_; | ||
| 24 | if ($query_string =~ /(.*)(&|;)hash=(.*)/) { | ||
| 25 | my $query_string_without_hash = $1; | ||
| 26 | my $hash = $3; | ||
| 27 | return $hash ne hexdigest($self->config->private_key, $query_string_without_hash); | ||
| 28 | } | ||
| 29 | return 1; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub check_http_status { | ||
| 33 | my($self, $query_string) = @_; | ||
| 34 | my $params = $self->parse($query_string); | ||
| 35 | confess "UnexpectedError: expected query string to have an http_status param" unless $params->{'http_status'}; | ||
| 36 | confess "AuthenticationError" if $params->{'http_status'} eq '401'; | ||
| 37 | confess "AuthorizationError" if $params->{'http_status'} eq '403'; | ||
| 38 | confess "ServerError" if $params->{'http_status'} eq '500'; | ||
| 39 | confess "DownForMaintenance" if $params->{'http_status'} eq '503'; | ||
| 40 | confess "UnexpectedError" if $params->{'http_status'} ne '200'; | ||
| 41 | } | ||
| 42 | |||
| 43 | 1; | ||