blib/lib/Mail/AuthenticationResults/Parser.pm untested

filter: Statementstmt 0.0% 0 / 206 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 348 SCAR 79.6
CC
53
Cov
0%
_parse_entry
420.0
_parse_authservid
182.0
tokenise
156.0
CRAP 2862.0
1 package Mail::AuthenticationResults::Parser;
2 # ABSTRACT: Class for parsing Authentication Results Headers
3  
4 require 5.008;
5 use strict;
6 use warnings;
7 our $VERSION = '2.20230112'; # VERSION
8 use Carp;
9  
10 use Mail::AuthenticationResults::Header;
11 use Mail::AuthenticationResults::Header::AuthServID;
12 use Mail::AuthenticationResults::Header::Comment;
13 use Mail::AuthenticationResults::Header::Entry;
14 use Mail::AuthenticationResults::Header::SubEntry;
15 use Mail::AuthenticationResults::Header::Version;
16  
17 use Mail::AuthenticationResults::Token::Assignment;
18 use Mail::AuthenticationResults::Token::Comment;
19 use Mail::AuthenticationResults::Token::QuotedString;
20 use Mail::AuthenticationResults::Token::Separator;
21 use Mail::AuthenticationResults::Token::String;
22  
23 use JSON;
24  
25  
26 sub new {
27 my ( $class, $auth_header ) = @_;
28 my $self = {};
29 bless $self, $class;
30  
31 if ( $auth_header ) {
32 $self->parse( $auth_header );
33 }
34  
35 return $self;
36 }
37  
38  
39 sub parse {
40 my ( $self, $header ) = @_;
41  
42 $self->tokenise( $header );
43  
44 $self->_parse_authservid();
45  
46 while ( @{ $self->{ 'tokenised' } } ) {
47 $self->_parse_entry();
48 }
49  
50 return $self->parsed();
51 }
52  
53  
54 sub from_authentication_results_json {
55 my ( $self, $json ) = @_;
56 my $j = JSON->new();
57 my $hashref = $j->decode( $json );
58 return $self->_from_hashref( $hashref );
59 }
60  
61 sub _from_hashref {
62 my ( $self, $hashref ) = @_;
63 my $type = $hashref->{'type'};
64 my $object
65 = $type eq 'header' ? Mail::AuthenticationResults::Header->new()
66 : $type eq 'authservid' ? Mail::AuthenticationResults::Header::AuthServID->new()
67 : $type eq 'entry' ? Mail::AuthenticationResults::Header::Entry->new()
68 : $type eq 'subentry' ? Mail::AuthenticationResults::Header::SubEntry->new()
69 : $type eq 'comment' ? Mail::AuthenticationResults::Header::Comment->new()
70 : croak "unknown type $type";
71  
72 if ( $type eq 'header' ) {
73 my $authserv_id = $self->_from_hashref( $hashref->{ 'authserv_id' } );
74 $object->set_value( $authserv_id );
75 }
76 else {
77 $object->set_key( $hashref->{'key'} ) if exists $hashref->{'key'};
78 $object->safe_set_value( $hashref->{'value'} ) if exists $hashref->{'value'};
79 }
80  
81 if ( exists $hashref->{'children'} ) {
82 for my $child ( @{ $hashref->{'children'} } ) {
83 my $child_object = $self->_from_hashref( $child );
84 $object->add_child( $child_object );
85 }
86 }
87  
88 return $object;
89 }
90  
91  
92  
93 sub tokenise {
94 my ( $self, $header ) = @_;
95  
96 my @tokenised;
97  
98 $header =~ s/\n/ /g;
99 $header =~ s/\r/ /g;
100 $header =~ s/^\s+//;
101  
102 # Remove Header part if present
103 if ( $header =~ /^Authentication-Results:/i ) {
104 $header =~ s/^Authentication-Results://i;
105 }
106  
107 my $args = {};
108 while ( length($header) > 0 ) {
109  
110 my $token;
111 $header =~ s/^\s+//;
112  
113 my $last_non_comment_type = exists( $args->{ 'last_non_comment_type' } ) ? $args->{ 'last_non_comment_type' }->is() : 'none';
114  
115 if ( length( $header ) == 0 ) {
116 last;
117 }
118 elsif ( $header =~ /^\(/ ) {
119 $token = Mail::AuthenticationResults::Token::Comment->new( $header, $args );
120 }
121 elsif ( $header =~ /^;/ ) {
122 $token = Mail::AuthenticationResults::Token::Separator->new( $header, $args );
123 $args->{ 'last_non_comment_type' } = $token;
124 }
125 elsif ( $header =~ /^"/ ) {
126 $token = Mail::AuthenticationResults::Token::QuotedString->new( $header, $args );
127 $args->{ 'last_non_comment_type' } = $token;
128 }
129 elsif ( $last_non_comment_type ne 'assignment' && $header =~ /^\./ ) {
130 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
131 $args->{ 'last_non_comment_type' } = $token;
132 }
133 elsif ( $last_non_comment_type eq 'assignment' && $header =~ /^\./ ) {
134 # a . after an assignment cannot be another assignment, likely an unquoted string.
135 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
136 $args->{ 'last_non_comment_type' } = $token;
137 }
138 elsif ( $header =~ /^\// ) {
139 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
140 $args->{ 'last_non_comment_type' } = $token;
141 }
142 elsif ( $header =~ /^=/ ) {
143 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
144 $args->{ 'last_non_comment_type' } = $token;
145 }
146 else {
147 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
148 $args->{ 'last_non_comment_type' } = $token;
149 }
150  
151 $header = $token->remainder();
152 push @tokenised, $token;
153 }
154  
155 croak 'Nothing to parse' if ! @tokenised;
156  
157 $self->{ 'tokenised' } = \@tokenised;
158  
159 return;
160 }
161  
162 sub _parse_authservid {
163 my ( $self ) = @_;
164 my $tokenised = $self->{ 'tokenised' };
165 my $token;
166  
167 my $authserv_id = Mail::AuthenticationResults::Header::AuthServID->new();
168  
169 # Find the ServID
170 while ( @$tokenised ) {
171 $token = shift @$tokenised;
172 if ( $token->is() eq 'string' ) {
173 $authserv_id->set_value( $token->value() );
174 last;
175 }
176 elsif ( $token->is() eq 'comment' ) {
177 $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
178 }
179 else {
180 # assignment or separator, both are bogus
181 croak 'Invalid AuthServ-ID';
182 }
183 }
184  
185 my $expecting = 'key';
186 my $key;
187  
188 TOKEN:
189 while ( @$tokenised ) {
190 $token = shift @$tokenised;
191  
192 if ( $token->is() eq 'assignment' ) {
193 if ( $expecting eq 'assignment' ) {
194 if ( $token->value() eq '=' ) {
195 $expecting = 'value';
196 }
197 else {
198 croak 'unexpected token';
199 }
200 }
201 else {
202 croak 'not expecting an assignment';
203 }
204 }
205 elsif ( $token->is() eq 'comment' ) {
206 $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
207 }
208 elsif ( $token->is() eq 'separator' ) {
209 last TOKEN;
210 }
211 if ( $token->is() eq 'string' ) {
212 if ( $expecting eq 'key' ) {
213 $key = $token;
214 $expecting = 'assignment';
215 }
216 elsif ( $expecting eq 'value' ) {
217 $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() )->set_value( $token->value() ) );
218 $expecting = 'key';
219 undef $key;
220 }
221 else {
222 croak 'not expecting a string';
223 }
224 }
225  
226 }
227 if ( $expecting ne 'key' ) {
228 if ( $key->value() =~ /^[0-9]+$/ ) {
229 # Looks like a version
230 $authserv_id->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $key->value() ) );
231 }
232 else {
233 # Probably bogus, but who knows!
234 $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() ) );
235 }
236 }
237  
238 $self->{ 'header' } = Mail::AuthenticationResults::Header->new()->set_value( $authserv_id );
239 $self->{ 'tokenised' } = $tokenised;
240  
241 return;
242 }
243  
244 sub _parse_entry {
245 my ( $self ) = @_;
246 my $tokenised = $self->{ 'tokenised' };
247  
248 my $entry = Mail::AuthenticationResults::Header::Entry->new();
249 my $working_on = $entry;
250  
251 my $expecting = 'key';
252 my $is_subentry = 0;
253 TOKEN:
254 while ( @$tokenised ) {
255 my $token = shift @$tokenised;
256  
257 if ( $token->is() eq 'assignment' ) {
258 if ( $expecting eq 'assignment' ) {
259 if ( $token->value() eq '=' ) {
260 $expecting = 'value';
261 }
262 elsif ( $token->value() eq '.' ) {
263 $expecting = 'keymod';
264 }
265 elsif ( $token->value() eq '/' ) {
266 $expecting = 'version';
267 }
268 }
269 else {
270 croak 'not expecting an assignment';
271 }
272 }
273 elsif ( $token->is() eq 'comment' ) {
274 $working_on->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
275 }
276 elsif ( $token->is() eq 'separator' ) {
277 last TOKEN;
278 }
279 if ( $token->is() eq 'string' ) {
280 if ( $expecting eq 'key' ) {
281 if ( ! $is_subentry ) {
282 if ( $token->value() eq 'none' ) {
283 # Special case the none
284 $expecting = 'no_more_after_none';
285 }
286 else {
287 $entry->set_key( $token->value() );
288 $expecting = 'assignment';
289 }
290 }
291 else {
292 $working_on = Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $token->value() );
293 $expecting = 'assignment';
294 }
295 }
296 elsif ( $expecting eq 'keymod' ) {
297 $working_on->set_key( $working_on->key() . '.' . $token->value() );
298 $expecting = 'assignment';
299 }
300 elsif ( $expecting eq 'version' ) {
301 if ( $token->value() =~ /^[0-9]+$/ ) {
302 # Looks like a version
303 $working_on->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $token->value() ) );
304 }
305 else {
306 croak 'bad version token';
307 }
308 $expecting = 'assignment';
309 }
310 elsif ( $expecting eq 'value' ) {
311 if ( ! $is_subentry ) {
312 $entry->set_value( $token->value() );
313 $is_subentry = 1;
314 }
315 else {
316 $entry->add_child( $working_on->set_value( $token->value() ) );
317 }
318 $expecting = 'key';
319 }
320 else {
321 croak 'not expecting a string';
322 }
323 }
324  
325 }
326  
327 if ( $expecting eq 'no_more_after_none' ) {
328 $self->{ 'tokenised' } = $tokenised;
329 # We may have comment entries, if so add those to the header object
330 foreach my $child ( @{ $entry->children() } ) {
331 delete $child->{ 'parent' };
332 $self->{ 'header' }->add_child( $child );
333 }
334 return;
335 }
336  
337 if ( $expecting ne 'key' ) {
338 if ( $is_subentry ) {
339 $entry->add_child( $working_on );
340 }
341 }
342  
343 $self->{ 'header' }->add_child( $entry );
344 $self->{ 'tokenised' } = $tokenised;
345  
346 return;
347 }
348  
349  
350 sub parsed {
351 my ( $self ) = @_;
352 return $self->{ 'header' };
353 }
354  
355 1;
356  
357 __END__