| 1 |
package HTML::FormHandler::Field::RequestToken; |
| 2 |
# ABSTRACT: Hidden text field which contains a unique time-stamped token |
| 3 |
$HTML::FormHandler::Field::RequestToken::VERSION = '0.410002'; |
| 4 |
use HTML::FormHandler::Moose; |
| 5 |
extends 'HTML::FormHandler::Field::Hidden'; |
| 6 |
|
| 7 |
use namespace::autoclean; |
| 8 |
|
| 9 |
use Crypt::CBC; |
| 10 |
use MIME::Base64 qw(decode_base64 encode_base64); |
| 11 |
use Moose::Util::TypeConstraints qw(class_type); |
| 12 |
use Try::Tiny; |
| 13 |
|
| 14 |
has '+required' => ( default => 1 ); |
| 15 |
|
| 16 |
has '+default_method' => ( default => sub { \&get_token }); |
| 17 |
|
| 18 |
|
| 19 |
has 'expiration_time' => ( |
| 20 |
is => 'rw', |
| 21 |
default => 3600 |
| 22 |
); |
| 23 |
|
| 24 |
|
| 25 |
has 'token_prefix' => ( |
| 26 |
is => 'rw', |
| 27 |
default => '' |
| 28 |
); |
| 29 |
|
| 30 |
|
| 31 |
has 'crypto_key' => ( |
| 32 |
is => 'rw', |
| 33 |
default => 'rEpLaCeMe', |
| 34 |
); |
| 35 |
|
| 36 |
|
| 37 |
has 'crypto_cipher_type' => ( |
| 38 |
is => 'rw', |
| 39 |
default => 'Blowfish', |
| 40 |
); |
| 41 |
|
| 42 |
|
| 43 |
has 'message' => ( |
| 44 |
is => 'rw', |
| 45 |
default => 'Form submission failed. Please try again.' |
| 46 |
); |
| 47 |
|
| 48 |
|
| 49 |
has 'cipher' => ( |
| 50 |
is => 'ro', |
| 51 |
isa => class_type('Crypt::CBC'), |
| 52 |
lazy => 1, |
| 53 |
builder => '_build_cipher', |
| 54 |
); |
| 55 |
|
| 56 |
sub _build_cipher { |
| 57 |
my ($self) = @_; |
| 58 |
return Crypt::CBC->new( |
| 59 |
-key => $self->crypto_key, |
| 60 |
-cipher => $self->crypto_cipher_type, |
| 61 |
-salt => 1, |
| 62 |
-header => 'salt', |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
sub validate { |
| 67 |
my ($self, $value) = @_; |
| 68 |
|
| 69 |
# If it's good, return it |
| 70 |
unless ( $self->verify_token($value) ) { |
| 71 |
$self->add_error(); |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
sub verify_token { |
| 78 |
my ($self, $token) = @_; |
| 79 |
|
| 80 |
return undef unless($token); |
| 81 |
|
| 82 |
my $form = $self->form; |
| 83 |
|
| 84 |
my $value = undef; |
| 85 |
try { |
| 86 |
$value = $self->cipher->decrypt(decode_base64($token)); |
| 87 |
if ( my $prefix = $self->token_prefix ) { |
| 88 |
return undef unless ($value =~ s/^\Q$prefix\E//); |
| 89 |
} |
| 90 |
} catch {}; |
| 91 |
|
| 92 |
return undef unless defined($value); |
| 93 |
return undef unless ( $value =~ /^\d+$/ ); |
| 94 |
return undef if ( time() > $value ); |
| 95 |
|
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
sub get_token { |
| 101 |
my $self = shift; |
| 102 |
|
| 103 |
my $value = $self->token_prefix . (time() + $self->expiration_time); |
| 104 |
my $token = encode_base64($self->cipher->encrypt($value)); |
| 105 |
$token =~ s/[\s\r\n]+//g; |
| 106 |
return $token; |
| 107 |
} |
| 108 |
|
| 109 |
__PACKAGE__->meta->make_immutable; |
| 110 |
|
| 111 |
1; |
| 112 |
|
| 113 |
__END__ |
| 114 |
|
| 115 |
=pod |
| 116 |
|
| 117 |
=encoding UTF-8 |
| 118 |
|
| 119 |
=head1 NAME |
| 120 |
|
| 121 |
HTML::FormHandler::Field::RequestToken - Hidden text field which contains a unique time-stamped token |
| 122 |
|
| 123 |
=head1 VERSION |
| 124 |
|
| 125 |
version 0.410002 |
| 126 |
|
| 127 |
=head1 SYNOPSIS |
| 128 |
|
| 129 |
with 'HTML::FormHandler::Field::Role::RequestToken'; |
| 130 |
... |
| 131 |
has_field '_token' => ( |
| 132 |
type => 'RequestToken', |
| 133 |
); |
| 134 |
|
| 135 |
=head1 DESCRIPTION |
| 136 |
|
| 137 |
This field is for preventing CSRF attacks. It contains |
| 138 |
an encrypted token containing an expiration time for the form. |
| 139 |
No data needs to be persisted in the user's session or on the |
| 140 |
server. |
| 141 |
|
| 142 |
=head1 ATTRIBUTES |
| 143 |
|
| 144 |
=head2 expiration_time |
| 145 |
|
| 146 |
Length of time (in seconds) that token will be accepted as valid from |
| 147 |
the time it is initially generated. Defaults to C<3600>. |
| 148 |
|
| 149 |
=head2 token_prefix |
| 150 |
|
| 151 |
An optional string to prepend to the token value before encrypting it. |
| 152 |
If specified, any received tokens must begin with this value to be |
| 153 |
accepted as valid. Defaults to an empty string. |
| 154 |
|
| 155 |
Passed on form process. C<< $c->sessionid . '|' >> |
| 156 |
|
| 157 |
=head2 crypto_key |
| 158 |
|
| 159 |
Key to use to encrypt/decrypt the token payload. |
| 160 |
|
| 161 |
=head2 crypto_cipher_type |
| 162 |
|
| 163 |
The C<Crypt::CBC> cipher to use to encrypt/decrypt the token payload. |
| 164 |
Defaults to C<Blowfish>. |
| 165 |
|
| 166 |
=head2 message |
| 167 |
|
| 168 |
Error message if token is missing/invalid. |
| 169 |
|
| 170 |
=head2 cipher |
| 171 |
|
| 172 |
A C<Crypt::CBC> object to handle encrypting/decrypting the token payload. |
| 173 |
If not specified, L</crypto_key> and L</crypto_cipher_type> will be |
| 174 |
used to construct one. |
| 175 |
|
| 176 |
=head2 verify_token |
| 177 |
|
| 178 |
Validates whether the specified token is currently valid for this form. |
| 179 |
|
| 180 |
=head2 get_token |
| 181 |
|
| 182 |
Generates a new token and returns it. |
| 183 |
|
| 184 |
=head1 AUTHOR |
| 185 |
|
| 186 |
FormHandler Contributors - see HTML::FormHandler |
| 187 |
|
| 188 |
=head1 COPYRIGHT AND LICENSE |
| 189 |
|
| 190 |
This software is copyright (c) 2017 by Gerda Shank. |
| 191 |
|
| 192 |
This is free software; you can redistribute it and/or modify it under |
| 193 |
the same terms as the Perl 5 programming language system itself. |
| 194 |
|
| 195 |
=cut |