| 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.20260216'; # 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 ( $last_non_comment_type ne 'assignment' && $header =~ /^\// ) { | ||
| 139 | $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args ); | ||
| 140 | $args->{ 'last_non_comment_type' } = $token; | ||
| 141 | } | ||
| 142 | elsif ( $last_non_comment_type eq 'assignment' && $header =~ /^\// ) { | ||
| 143 | # a / after an assignment cannot be another assignment, likely an unquoted string. | ||
| 144 | $token = Mail::AuthenticationResults::Token::String->new( $header, $args ); | ||
| 145 | $args->{ 'last_non_comment_type' } = $token; | ||
| 146 | } | ||
| 147 | elsif ( $header =~ /^=/ ) { | ||
| 148 | $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args ); | ||
| 149 | $args->{ 'last_non_comment_type' } = $token; | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | $token = Mail::AuthenticationResults::Token::String->new( $header, $args ); | ||
| 153 | $args->{ 'last_non_comment_type' } = $token; | ||
| 154 | } | ||
| 155 | |||
| 156 | $header = $token->remainder(); | ||
| 157 | push @tokenised, $token; | ||
| 158 | } | ||
| 159 | |||
| 160 | croak 'Nothing to parse' if ! @tokenised; | ||
| 161 | |||
| 162 | $self->{ 'tokenised' } = \@tokenised; | ||
| 163 | |||
| 164 | return; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _parse_authservid { | ||
| 168 | my ( $self ) = @_; | ||
| 169 | my $tokenised = $self->{ 'tokenised' }; | ||
| 170 | my $token; | ||
| 171 | |||
| 172 | my $authserv_id = Mail::AuthenticationResults::Header::AuthServID->new(); | ||
| 173 | |||
| 174 | # Find the ServID | ||
| 175 | while ( @$tokenised ) { | ||
| 176 | $token = shift @$tokenised; | ||
| 177 | if ( $token->is() eq 'string' ) { | ||
| 178 | $authserv_id->set_value( $token->value() ); | ||
| 179 | last; | ||
| 180 | } | ||
| 181 | elsif ( $token->is() eq 'comment' ) { | ||
| 182 | $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) ); | ||
| 183 | } | ||
| 184 | else { | ||
| 185 | # assignment or separator, both are bogus | ||
| 186 | croak 'Invalid AuthServ-ID'; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | my $expecting = 'key'; | ||
| 191 | my $key; | ||
| 192 | |||
| 193 | TOKEN: | ||
| 194 | while ( @$tokenised ) { | ||
| 195 | $token = shift @$tokenised; | ||
| 196 | |||
| 197 | if ( $token->is() eq 'assignment' ) { | ||
| 198 | if ( $expecting eq 'assignment' ) { | ||
| 199 | if ( $token->value() eq '=' ) { | ||
| 200 | $expecting = 'value'; | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | croak 'unexpected token'; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | croak 'not expecting an assignment'; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | elsif ( $token->is() eq 'comment' ) { | ||
| 211 | $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) ); | ||
| 212 | } | ||
| 213 | elsif ( $token->is() eq 'separator' ) { | ||
| 214 | last TOKEN; | ||
| 215 | } | ||
| 216 | if ( $token->is() eq 'string' ) { | ||
| 217 | if ( $expecting eq 'key' ) { | ||
| 218 | $key = $token; | ||
| 219 | $expecting = 'assignment'; | ||
| 220 | } | ||
| 221 | elsif ( $expecting eq 'value' ) { | ||
| 222 | $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() )->set_value( $token->value() ) ); | ||
| 223 | $expecting = 'key'; | ||
| 224 | undef $key; | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | croak 'not expecting a string'; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | } | ||
| 232 | if ( $expecting ne 'key' ) { | ||
| 233 | if ( $key->value() =~ /^[0-9]+$/ ) { | ||
| 234 | # Looks like a version | ||
| 235 | $authserv_id->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $key->value() ) ); | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | # Probably bogus, but who knows! | ||
| 239 | $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() ) ); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | $self->{ 'header' } = Mail::AuthenticationResults::Header->new()->set_value( $authserv_id ); | ||
| 244 | $self->{ 'tokenised' } = $tokenised; | ||
| 245 | |||
| 246 | return; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub _parse_entry { | ||
| 250 | my ( $self ) = @_; | ||
| 251 | my $tokenised = $self->{ 'tokenised' }; | ||
| 252 | |||
| 253 | my $entry = Mail::AuthenticationResults::Header::Entry->new(); | ||
| 254 | my $working_on = $entry; | ||
| 255 | |||
| 256 | my $expecting = 'key'; | ||
| 257 | my $is_subentry = 0; | ||
| 258 | TOKEN: | ||
| 259 | while ( @$tokenised ) { | ||
| 260 | my $token = shift @$tokenised; | ||
| 261 | |||
| 262 | if ( $token->is() eq 'assignment' ) { | ||
| 263 | if ( $expecting eq 'assignment' ) { | ||
| 264 | if ( $token->value() eq '=' ) { | ||
| 265 | $expecting = 'value'; | ||
| 266 | } | ||
| 267 | elsif ( $token->value() eq '.' ) { | ||
| 268 | $expecting = 'keymod'; | ||
| 269 | } | ||
| 270 | elsif ( $token->value() eq '/' ) { | ||
| 271 | $expecting = 'version'; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | else { | ||
| 275 | croak 'not expecting an assignment'; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | elsif ( $token->is() eq 'comment' ) { | ||
| 279 | $working_on->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) ); | ||
| 280 | } | ||
| 281 | elsif ( $token->is() eq 'separator' ) { | ||
| 282 | last TOKEN; | ||
| 283 | } | ||
| 284 | if ( $token->is() eq 'string' ) { | ||
| 285 | if ( $expecting eq 'key' ) { | ||
| 286 | if ( ! $is_subentry ) { | ||
| 287 | if ( $token->value() eq 'none' ) { | ||
| 288 | # Special case the none | ||
| 289 | $expecting = 'no_more_after_none'; | ||
| 290 | } | ||
| 291 | else { | ||
| 292 | $entry->set_key( $token->value() ); | ||
| 293 | $expecting = 'assignment'; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | else { | ||
| 297 | $working_on = Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $token->value() ); | ||
| 298 | $expecting = 'assignment'; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | elsif ( $expecting eq 'keymod' ) { | ||
| 302 | $working_on->set_key( $working_on->key() . '.' . $token->value() ); | ||
| 303 | $expecting = 'assignment'; | ||
| 304 | } | ||
| 305 | elsif ( $expecting eq 'version' ) { | ||
| 306 | if ( $token->value() =~ /^[0-9]+$/ ) { | ||
| 307 | # Looks like a version | ||
| 308 | $working_on->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $token->value() ) ); | ||
| 309 | } | ||
| 310 | else { | ||
| 311 | croak 'bad version token'; | ||
| 312 | } | ||
| 313 | $expecting = 'assignment'; | ||
| 314 | } | ||
| 315 | elsif ( $expecting eq 'value' ) { | ||
| 316 | if ( ! $is_subentry ) { | ||
| 317 | $entry->set_value( $token->value() ); | ||
| 318 | $is_subentry = 1; | ||
| 319 | } | ||
| 320 | else { | ||
| 321 | $entry->add_child( $working_on->set_value( $token->value() ) ); | ||
| 322 | } | ||
| 323 | $expecting = 'key'; | ||
| 324 | } | ||
| 325 | else { | ||
| 326 | croak 'not expecting a string'; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | } | ||
| 331 | |||
| 332 | if ( $expecting eq 'no_more_after_none' ) { | ||
| 333 | $self->{ 'tokenised' } = $tokenised; | ||
| 334 | # We may have comment entries, if so add those to the header object | ||
| 335 | foreach my $child ( @{ $entry->children() } ) { | ||
| 336 | delete $child->{ 'parent' }; | ||
| 337 | $self->{ 'header' }->add_child( $child ); | ||
| 338 | } | ||
| 339 | return; | ||
| 340 | } | ||
| 341 | |||
| 342 | if ( $expecting ne 'key' ) { | ||
| 343 | if ( $is_subentry ) { | ||
| 344 | $entry->add_child( $working_on ); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | $self->{ 'header' }->add_child( $entry ); | ||
| 349 | $self->{ 'tokenised' } = $tokenised; | ||
| 350 | |||
| 351 | return; | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | sub parsed { | ||
| 356 | my ( $self ) = @_; | ||
| 357 | return $self->{ 'header' }; | ||
| 358 | } | ||
| 359 | |||
| 360 | 1; | ||
| 361 | |||
| 362 | __END__ |