]> wagnertech.de Git - mfinanz.git/blob - SL/Dispatcher/AuthHandler/Base.pm
restart apache2 in postinst
[mfinanz.git] / SL / Dispatcher / AuthHandler / Base.pm
1 package SL::Dispatcher::AuthHandler::Base;
2
3 use strict;
4 use parent qw(Rose::Object);
5
6 use Encode ();
7 use MIME::Base64 ();
8
9 use SL::Layout::Dispatcher;
10
11 sub _env_var_for_header {
12   my ($header) = @_;
13
14   $header =~ s{-}{_}g;
15   return $ENV{'HTTP_' . uc($header)};
16 }
17
18 sub _parse_http_basic_auth {
19   my ($self) = @_;
20
21   my $cfg = $::lx_office_conf{'authentication/http_basic'};
22
23   return unless $cfg && $cfg->{enabled};
24
25   # See RFC 7617.
26
27   # Requires that the server passes the 'Authorization' header as the
28   # environment variable 'HTTP_AUTHORIZATION'. Example code for
29   # Apache:
30
31   # SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
32
33   my $data = _env_var_for_header('Authorization');
34
35   return unless ($data // '') =~ m{^basic +(.+)}i;
36
37   $data = Encode::decode('utf-8', MIME::Base64::decode($1));
38
39   return unless $data =~ m{(.+?):(.+)};
40
41   return ($1, $2);
42 }
43
44 sub _parse_http_headers_auth {
45   my ($self) = @_;
46
47   my $cfg = $::lx_office_conf{'authentication/http_headers'};
48
49   return unless $cfg && ($::lx_office_conf{'authentication'}->{module} =~ m{HTTPHeaders});
50
51   foreach (qw(secret_header secret user_header client_id_header)) {
52     next if $cfg->{$_};
53     die 'config/kivitendo.conf: Missing parameter in "authentication/http_headers": ' . $_;
54   }
55
56   my $secret = _env_var_for_header($cfg->{secret_header}) // '';
57   if ($secret ne $cfg->{secret}) {
58     $::lxdebug->message(LXDebug->DEBUG2(), "_parse_http_headers_auth: bad secret sent by upstream server: $secret");
59     return;
60   }
61
62   my $client_id = _env_var_for_header($cfg->{client_id_header});
63   if (!$client_id) {
64     $::lxdebug->message(LXDebug->DEBUG2(), "_parse_http_headers_auth: no client ID header found");
65     return;
66   }
67
68   # $::auth->set_client();
69
70   my $user = _env_var_for_header($cfg->{user_header});
71   if (!$user) {
72     $::lxdebug->message(LXDebug->DEBUG2(), "_parse_http_headers_auth: no user name header found");
73     return;
74   }
75
76   $::lxdebug->message(LXDebug->DEBUG2(), "_parse_http_headers_auth: OK client $client_id user $user");
77
78   return ($client_id, $user);
79 }
80
81 1;
82 __END__
83
84 =pod
85
86 =encoding utf8
87
88 =head1 NAME
89
90 SL::Dispatcher::AuthHandler::Base - Base class for different modules
91 handling user authentication
92
93 =head1 OVERVIEW
94
95 This module acts as a base class for the modules in the
96 C<SL::Dispatcher::AuthHandler> namespace. It contains a couple of
97 shared helper functions.
98
99 =head1 FUNCTIONS
100
101 =over 4
102
103 =item C<_env_var_for_header $header_name>
104
105 Static method returning the name of an environment variable that holds
106 the value of the HTTP request header named C<$header_name> according
107 to the CGI specifications.
108
109 =item C<_parse_http_headers_auth>
110
111 When HTTP headers authentication is enabled in the configuration file,
112 this function parses the content of the configured HTTP request
113 headers. First, it ensures that the configuration is complete; if not,
114 an exception is thrown.
115
116 Next, it ensures that the shared secret was sent and equals the
117 expected value.
118
119 Lastly it fetches the name of the logged in user & the client ID sent
120 by the upstream servers & returns both as a two-element list.
121
122 This function returns an empty list if either the authentication
123 module is not C<HTTPHeaders> or if the configuration option
124 C<authentication/http_headers.enabled> is not true.
125
126 =item C<_parse_http_basic_auth>
127
128 When HTTP Basic Authentication is enabled in the configuration file,
129 this function parses the content of the C<Authorization> HTTP request
130 header via the corresponding environment variable according to the CGI
131 spec. It extracts the user name & password from the header & returns
132 both as a two-element list.
133
134 This function returns an empty list if either the authentication
135 module is not C<HTTPHeaders> or if the configuration option
136 C<authentication/http_basic.enabled> is not true.
137
138 =back
139
140 =head1 AUTHOR
141
142 Moritz Bunkus E<lt>m.bunkus@linet.deE<gt>
143
144 =cut