| 1 | package HTML::FormHandler::Field::RequestToken; | ||||||||||
| 2 | # ABSTRACT: Hidden text field which contains a unique time-stamped token | ||||||||||
| 3 | 1 | $HTML::FormHandler::Field::RequestToken::VERSION = '0.410000'; | |||||||||
| 4 | 1 | ▶ | use HTML::FormHandler::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | extends 'HTML::FormHandler::Field::Hidden'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Crypt::CBC; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use MIME::Base64 qw(decode_base64 encode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Moose::Util::TypeConstraints qw(class_type); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | has '+required' => ( default => 1 ); | |||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | has '+default_method' => ( default => sub { \&get_token }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | 1 | has 'expiration_time' => ( | |||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | default => 3600 | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 1 | has 'token_prefix' => ( | |||||||||
| 26 | is => 'rw', | ||||||||||
| 27 | default => '' | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | |||||||||||
| 31 | 1 | has 'crypto_key' => ( | |||||||||
| 32 | is => 'rw', | ||||||||||
| 33 | default => 'rEpLaCeMe', | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | 1 | has 'crypto_cipher_type' => ( | |||||||||
| 38 | is => 'rw', | ||||||||||
| 39 | default => 'Blowfish', | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | 1 | has 'message' => ( | |||||||||
| 44 | is => 'rw', | ||||||||||
| 45 | default => 'Form submission failed. Please try again.' | ||||||||||
| 46 | ); | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | 1 | 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 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build_cipher: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | 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 | 1 | ▶ | my ($self, $value) = @_; | ||||||||
|
Subroutine
validate: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 68 | |||||||||||
| 69 | # If it's good, return it | ||||||||||
| 70 | 1 | ▶ | unless ( $self->verify_token($value) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1 | $self->add_error(); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | sub verify_token { | ||||||||||
| 78 | 1 | ▶ | my ($self, $token) = @_; | ||||||||
|
Subroutine
verify_token: called CC=5 SCAR=24.1
Pod
documented
| |||||||||||
| 79 | |||||||||||
| 80 | 1 | ▶ | return undef unless($token); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | |||||||||||
| 82 | 1 | my $form = $self->form; | |||||||||
| 83 | |||||||||||
| 84 | 1 | my $value = undef; | |||||||||
| 85 | try { | ||||||||||
| 86 | 1 | ▶ | $value = $self->cipher->decrypt(decode_base64($token)); | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=21.6
| |||||||||||
| 87 | 0 | ▶ | if ( my $prefix = $self->token_prefix ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | ▶ | return undef unless ($value =~ s/^\Q$prefix\E//); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | } | ||||||||||
| 90 | 1 | ▶ | } catch {}; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 91 | |||||||||||
| 92 | 1 | ▶ | return undef unless defined($value); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | ▶ | return undef unless ( $value =~ /^\d+$/ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | ▶ | return undef if ( time() > $value ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | |||||||||||
| 96 | 0 | return 1; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | sub get_token { | ||||||||||
| 101 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 102 | |||||||||||
| 103 | 1 | my $value = $self->token_prefix . (time() + $self->expiration_time); | |||||||||
| 104 | 1 | my $token = encode_base64($self->cipher->encrypt($value)); | |||||||||
| 105 | 1 | $token =~ s/[\s\r\n]+//g; | |||||||||
| 106 | 1 | return $token; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 110 | |||||||||||
| 111 | 1 | 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.410000 | ||||||||||
| 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 | ||||||||||