| 1 | package MVC::Neaf::X::Form::Data; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 6 | our $VERSION = '0.2901'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | MVC::Neaf::X::Form::Data - Form validation result object. | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 CAUTION | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | This module should be moved into a separate distribution or (ideally) | ||||||||||||||||||||
| 14 | merged with an existing module with similar functionality. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | Possible candidates include L<Validator::LIVR>, L<Data::FormValidator>, | ||||||||||||||||||||
| 17 | L<Data::CGIForm>, and more. | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | See L<MVC::Neaf::X::Form>. | ||||||||||||||||||||
| 22 | This class is not expected to be created and used directly. | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 METHODS | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 6 | ▶ | use Digest::SHA qw(sha1); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 6 | ▶ | use MVC::Neaf::Util qw( encode_b64 ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 6 | ▶ | use URI::Escape; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 6 | ▶ | use parent qw(MVC::Neaf::X); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head2 new( %options ) | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | %options may include: | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =over | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =item * data - data that passed validation | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =item * error - fields that failed validation with correspondent error messages. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =item * raw - data as it was before validation. | ||||||||||||||||||||
| 45 | This should in theory match data + error, but isn't check in any way. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =back | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head2 fields() | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | Return fields currently in either data or raw hashes. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 1 | ▶ | sub fields { | ||||||||||||||||||
|
Subroutine
fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 58 | 1 | my $self = shift; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 1 | my %megahash = (%{ $self->raw }, %{ $self->data }); | |||||||||||||||||||
| 61 | 1 | return keys %megahash; | |||||||||||||||||||
| 62 | }; | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =head2 is_valid() | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | Returns true if data passed validation, false otherwise. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 9 | ▶ | sub is_valid { | ||||||||||||||||||
|
Subroutine
is_valid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 71 | 9 | my $self = shift; | |||||||||||||||||||
| 72 | 9 | return !%{ $self->error }; | |||||||||||||||||||
| 73 | }; | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =head2 data | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | Returns data that passed validation as hashref. | ||||||||||||||||||||
| 78 | This MAY be incomplete, check is_valid() first. | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =head2 data( "key" ) | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Get specific data item. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head2 data( key => $newvalue ) | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | Set specific data item. | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head2 error | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | Returns errors that occurred during validation. | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head2 error( "key" ) | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | Get specific error item. | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head2 error( key => $newvalue ) | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | Set specific error item. This may be used to invalidate a value | ||||||||||||||||||||
| 99 | after additional checks, and will also reset is_valid. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =head2 raw | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | Returns raw input values as hashref. | ||||||||||||||||||||
| 104 | Only keys subject to validation will be retained. | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | This may be useful for sending the data back for resubmission. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head2 raw( "key" ) | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | Get specific raw item. | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head2 raw( key => $newvalue ) | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | Set specific raw item. | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 6 | foreach (qw(data error raw)) { | |||||||||||||||||||
| 119 | 18 | my $method = $_; | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 35 | ▶ | my $code = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 122 | 35 | my $self = shift; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 35 | ▶ | my $hash = $self->{$method} ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$self->{$method} ||= {}
MC/DC100%1/1
$self->{$method} ||= {}
$self->{$method}
Truth table100%2/2
A
A= $self->{$method}
| |||||||||||||||||||||
| 125 | 35 | ▶ | return $hash unless @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 1 | my $param = shift; | |||||||||||||||||||
| 128 | 1 | ▶ | return $hash->{param} unless @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 1 | $hash->{$param} = shift; | |||||||||||||||||||
| 131 | 1 | return $self; | |||||||||||||||||||
| 132 | 18 | }; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 6 | ▶ | no strict 'refs'; ## no critic | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 135 | 18 | *$method = $code; | |||||||||||||||||||
| 136 | }; | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =head2 as_url( %override ) | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | Return the cleansed form data as one url-encoded line. | ||||||||||||||||||||
| 141 | The keys are sorted, and empty/undef values are discarded. | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | Arrays are NOT supported (yet). This may change in the future. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 3 | ▶ | sub as_url { | ||||||||||||||||||
|
Subroutine
as_url: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||
| 148 | 3 | my ($self, %override) = @_; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 3 | ▶ | my %data = ( %{ $self->{data} || {} }, %override ); | ||||||||||||||||||
|
Condition50%1/2
$self->{'data'} || {}
MC/DC0%0/1
$self->{'data'} || {}
$self->{'data'}
Truth table50%1/2
A
A= $self->{'data'}
| |||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | return join '&' | ||||||||||||||||||||
| 153 | 3 | , map { uri_escape_utf8( $_ ). "=". uri_escape_utf8( $data{$_} ) } | |||||||||||||||||||
| 154 | 3 | ▶ | grep { defined $data{$_} and length $data{$_} } | ||||||||||||||||||
|
Condition33%1/3
defined $data{$_} && length $data{$_}
MC/DC0%0/2
defined $data{$_} && length $data{$_}
defined $data{$_}
length $data{$_}
Truth table33%1/3
A && B
A= defined $data{$_}B= length $data{$_}
| |||||||||||||||||||||
| 155 | sort keys %data; | ||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head2 sign( %options ) | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | Sign data with a key. | ||||||||||||||||||||
| 161 | Empty values are discarded. | ||||||||||||||||||||
| 162 | The same data set with the same key is guaranteed to produce the same signature, | ||||||||||||||||||||
| 163 | at least in the same module version. | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | Options may include: | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =over | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =item * key (required) - the encryption key. If unsure, run pwgen(1) and | ||||||||||||||||||||
| 170 | hardcode something from its output. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =item * crypt = CODE($data, $key) - use that function for encryption. | ||||||||||||||||||||
| 173 | The default is simple sha1-based hash. | ||||||||||||||||||||
| 174 | You may need a more secure alternative. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =item * override = %hash - override these values. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =item * discard = @list - discard these values. This takes over override. | ||||||||||||||||||||
| 179 | May be needed e.g. to check if the form matches signature that comes with the | ||||||||||||||||||||
| 180 | form itself. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =back | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 2 | ▶ | sub sign { | ||||||||||||||||||
|
Subroutine
sign: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||||||||||||
| 187 | 2 | my ($self, %opt) = @_; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | $self->my_croak( "key parameter is required" ) | ||||||||||||||||||||
| 190 | 2 | ▶ | unless $opt{key}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 2 | ▶ | my %override = ( %{ $opt{override} || {} } | ||||||||||||||||||
|
Condition50%1/2
$opt{'override'} || {}
MC/DC0%0/1
$opt{'override'} || {}
$opt{'override'}
Truth table50%1/2
A
A= $opt{'override'}
| |||||||||||||||||||||
| 193 | 0 | ▶ | , map { $_ => '' } @{ $opt{exclude} || [] } ); | ||||||||||||||||||
|
Condition50%1/2
$opt{'exclude'} || []
MC/DC0%0/1
$opt{'exclude'} || []
$opt{'exclude'}
Truth table50%1/2
A
A= $opt{'exclude'}
| |||||||||||||||||||||
| 194 | 2 | ▶ | $opt{crypt} ||= \&_default_sign; | ||||||||||||||||||
|
Condition50%1/2
$opt{"crypt"} ||= \&MVC::Neaf::X::Form::Data::_default_sign
MC/DC0%0/1
$opt{"crypt"} ||= \&MVC::Neaf::X::Form::Data::_default_sign
$opt{"crypt"}
Truth table50%1/2
A
A= $opt{"crypt"}
| |||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 2 | return $opt{crypt}->( $self->as_url( %override ), $opt{key}); | |||||||||||||||||||
| 197 | }; | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | # A weak ad-hoc HMAC. Use a better one... | ||||||||||||||||||||
| 200 | 2 | ▶ | sub _default_sign { | ||||||||||||||||||
|
Subroutine
_default_sign: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 201 | 2 | my ($data, $key) = @_; | |||||||||||||||||||
| 202 | 2 | return encode_b64( sha1( join "?", $key, $data, $key ) ); | |||||||||||||||||||
| 203 | }; | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | This module is part of L<MVC::Neaf> suite. | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>. | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 212 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 213 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 6 | 1; | |||||||||||||||||||