File Coverage

blib/lib/Mail/Milter/Authentication/Handler/Auth.pm
Criterion Covered Total %
statement 42 44 95.4
branch 4 4 100.0
condition n/a
subroutine 13 13 100.0
pod 1 8 12.5
total 60 69 86.9


line stmt bran cond sub pod time code
1             package Mail::Milter::Authentication::Handler::Auth;
2 31     31   19547 use 5.20.0;
  31         155  
3 31     31   238 use strict;
  31         94  
  31         796  
4 31     31   223 use warnings;
  31         116  
  31         996  
5 31     31   329 use Mail::Milter::Authentication::Pragmas;
  31         79  
  31         365  
6             # ABSTRACT: Handler class for SMTP Auth
7             our $VERSION = '3.20230629'; # VERSION
8 31     31   8281 use base 'Mail::Milter::Authentication::Handler';
  31         131  
  31         23621  
9              
10             sub default_config {
11 1     1 0 1745 return {};
12             }
13              
14             sub grafana_rows {
15 1     1 0 3964 my ( $self ) = @_;
16 1         3 my @rows;
17 1         9 push @rows, $self->get_json( 'Auth_metrics' );
18 1         6 return \@rows;
19             }
20              
21             sub register_metrics {
22             return {
23 30     30 1 378 'authenticated_connect_total' => 'The number of connections from an authenticated host',
24             };
25             }
26              
27             sub pre_loop_setup {
28 30     30 0 97 my ( $self ) = @_;
29 30         163 my $protocol = Mail::Milter::Authentication::Config::get_config()->{'protocol'};
30 30 100       178 if ( $protocol ne 'milter' ) {
31 16         412 warn 'The Auth handler only works with the milter protocol';
32             }
33             }
34              
35             sub get_auth_name {
36 74     74 0 228 my ($self) = @_;
37 74         425 my $name = $self->get_symbol('{auth_authen}');
38 74         391 return $name;
39             }
40              
41             sub connect_callback {
42 74     74 0 396 my ( $self, $hostname, $ip ) = @_;
43 74         553 $self->{'is_authenticated'} = 0;
44             }
45              
46             sub envfrom_callback {
47 74     74 0 315 my ( $self, $env_from ) = @_;
48 74         500 my $auth_name = $self->get_auth_name();
49 74 100       473 if ($auth_name) {
50 1         10 $self->dbgout( 'AuthenticatedAs', $auth_name, LOG_INFO );
51             # Clear the current auth headers ( iprev and helo may already be added )
52             # ToDo is this a good idea?
53 1         4 my $top_handler = $self->get_top_handler();
54 1         12 for my $type ( sort keys $top_handler->{'c_auth_headers'}->%* ) {
55 0         0 $top_handler->{'c_auth_headers'}->{$type} = [];
56             }
57 1         7 for my $type ( sort keys $top_handler->{'auth_headers'}->%* ) {
58 0         0 $top_handler->{'auth_headers'}->{$type} = [];
59             }
60 1         5 $self->{'is_authenticated'} = 1;
61 1         13 $self->metric_count( 'authenticated_connect_total' );
62 1         14 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'auth' )->safe_set_value( 'pass' );
63 1         144 $self->add_auth_header( $header );
64             }
65             }
66              
67             sub close_callback {
68 104     104 0 324 my ( $self ) = @_;
69 104         450 delete $self->{'is_authenticated'};
70             }
71              
72             1;
73              
74             __END__
75              
76             =pod
77              
78             =encoding UTF-8
79              
80             =head1 NAME
81              
82             Mail::Milter::Authentication::Handler::Auth - Handler class for SMTP Auth
83              
84             =head1 VERSION
85              
86             version 3.20230629
87              
88             =head1 DESCRIPTION
89              
90             Module which identifies email that was sent via an authenticated connection.
91              
92             =head1 CONFIGURATION
93              
94             No configuration options exist for this handler.
95              
96             =head1 AUTHOR
97              
98             Marc Bradshaw <marc@marcbradshaw.net>
99              
100             =head1 COPYRIGHT AND LICENSE
101              
102             This software is copyright (c) 2020 by Marc Bradshaw.
103              
104             This is free software; you can redistribute it and/or modify it under
105             the same terms as the Perl 5 programming language system itself.
106              
107             =cut