| 1 | package HTML::FormFu::Element::RequestToken; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 2 | our $VERSION = '2.04'; # VERSION | |||||||||||
| 6 | 2 | our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY | |||||||||||
| 7 | |||||||||||||
| 8 | 2 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use MooseX::Attribute::Chained; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 2 | extends 'HTML::FormFu::Element::Text'; | |||||||||||
| 12 | |||||||||||||
| 13 | 2 | ▶ | use HTML::FormFu::Util qw( process_attrs ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use Carp qw( croak ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 2 | has expiration_time => ( is => 'rw', traits => ['Chained'], default => 3600 ); | |||||||||||
| 17 | 2 | has session_key => ( is => 'rw', traits => ['Chained'], default => '__token' ); | |||||||||||
| 18 | 2 | has context => ( is => 'rw', traits => ['Chained'], default => 'context' ); | |||||||||||
| 19 | 2 | has limit => ( is => 'rw', traits => ['Chained'], default => 20 ); | |||||||||||
| 20 | 2 | has message => ( is => 'rw', traits => ['Chained'], default => 'Form submission failed. Please try again.' ); | |||||||||||
| 21 | |||||||||||||
| 22 | 27 | ▶ | after BUILD => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 27 | my $self = shift; | |||||||||||
| 24 | 27 | $self->name('_token'); | |||||||||||
| 25 | 27 | $self->constraints( [qw(RequestToken Required)] ); | |||||||||||
| 26 | 27 | $self->field_type('hidden'); | |||||||||||
| 27 | 2 | }; | |||||||||||
| 28 | |||||||||||||
| 29 | 26 | ▶ | sub process_value { | ||||||||||
|
Subroutine
process_value: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 30 | 26 | my ( $self, $value ) = @_; | |||||||||||
| 31 | |||||||||||||
| 32 | 26 | ▶ | return $self->verify_token($value) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | ? $value | ||||||||||||
| 34 | : $self->value( $self->get_token )->value; | ||||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 34 | ▶ | sub verify_token { | ||||||||||
|
Subroutine
verify_token: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||
| 38 | 34 | my ( $self, $token ) = @_; | |||||||||||
| 39 | |||||||||||||
| 40 | 34 | ▶ | return unless ($token); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | |||||||||||||
| 42 | 10 | my $form = $self->form; | |||||||||||
| 43 | |||||||||||||
| 44 | 10 | ▶ | croak "verify_token() can only be called if form has been submitted" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | if !$form->submitted; | ||||||||||||
| 46 | |||||||||||||
| 47 | 10 | my $field_name = $self->name; | |||||||||||
| 48 | |||||||||||||
| 49 | 10 | my $c = $self->form->stash->{ $self->context }; | |||||||||||
| 50 | |||||||||||||
| 51 | 10 | ▶ | for ( @{ $c->session->{ $self->session_key } || [] } ) { | ||||||||||
|
Condition50%1/2
$c->session->{$self->session_key} || []
MC/DC0%0/1
$c->session->{$self->session_key} || []
$c->session->{$self->session_key}
Truth table50%1/2
A
A= $c->session->{$self->session_key}
| |||||||||||||
| 52 | 48 | ▶ | return 1 if ( $_->[0] eq $token ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 3 | return; | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | 25 | ▶ | sub expire_token { | ||||||||||
|
Subroutine
expire_token: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 59 | 25 | my ($self) = @_; | |||||||||||
| 60 | |||||||||||||
| 61 | 25 | my $c = $self->form->stash->{ $self->context }; | |||||||||||
| 62 | |||||||||||||
| 63 | 25 | my @token; | |||||||||||
| 64 | 25 | ▶ | for ( @{ $c->session->{ $self->session_key } || [] } ) { | ||||||||||
|
Condition100%2/2
$c->session->{$self->session_key} || []
MC/DC100%1/1
$c->session->{$self->session_key} || []
$c->session->{$self->session_key}
Truth table100%2/2
A
A= $c->session->{$self->session_key}
| |||||||||||||
| 65 | 253 | ▶ | push( @token, $_ ) if ( $_->[1] > time ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 25 | ▶ | @token = splice( @token, -$self->limit, $self->limit ) if ( @token > $self->limit ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | |||||||||||||
| 70 | 25 | $c->session->{ $self->session_key } = \@token; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | 25 | ▶ | sub get_token { | ||||||||||
|
Subroutine
get_token: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 74 | 25 | my ($self) = @_; | |||||||||||
| 75 | |||||||||||||
| 76 | 25 | my $token; | |||||||||||
| 77 | 25 | my $c = $self->form->stash->{ $self->context }; | |||||||||||
| 78 | 25 | my @chars = ( 'a' .. 'z', 0 .. 9 ); | |||||||||||
| 79 | |||||||||||||
| 80 | 25 | $token .= $chars[ int( rand() * 36 ) ] for ( 0 .. 15 ); | |||||||||||
| 81 | |||||||||||||
| 82 | 25 | ▶ | $c->session->{ $self->session_key } ||= []; | ||||||||||
|
Condition100%2/2
$c->session->{$self->session_key} ||= []
MC/DC100%1/1
$c->session->{$self->session_key} ||= []
$c->session->{$self->session_key}
Truth table100%2/2
A
A= $c->session->{$self->session_key}
| |||||||||||||
| 83 | |||||||||||||
| 84 | 25 | push @{ $c->session->{ $self->session_key } }, [ $token, time + $self->expiration_time ]; | |||||||||||
| 85 | |||||||||||||
| 86 | 25 | $self->expire_token; | |||||||||||
| 87 | |||||||||||||
| 88 | 25 | return $token; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 2 | 1; | |||||||||||
| 92 | |||||||||||||
| 93 | __END__ | ||||||||||||