Added support for reading auth header from env variable

This commit is contained in:
Marc Ole Bulling 2022-01-13 00:01:48 +01:00
parent 8ec0d9319b
commit d82fafe722
2 changed files with 19 additions and 5 deletions

View File

@ -76,6 +76,8 @@ Setting('AUTH_CLASS', 'Grocy\Middleware\DefaultAuthMiddleware');
// When using ReverseProxyAuthMiddleware,
// the name of the HTTP header which your reverse proxy uses to pass the username (on successful authentication)
Setting('REVERSE_PROXY_AUTH_HEADER', 'REMOTE_USER');
// When using ReverseProxyAuthMiddleware, set to true if the username is passed as environment variable
Setting('REVERSE_PROXY_AUTH_USE_ENV', false);
// LDAP options when using LdapAuthMiddleware
Setting('LDAP_ADDRESS', ''); // Example value "ldap://vm-dc2019.local.berrnd.net"

View File

@ -22,13 +22,25 @@ class ReverseProxyAuthMiddleware extends AuthMiddleware
return $user;
}
$username = $request->getHeader(GROCY_REVERSE_PROXY_AUTH_HEADER);
if (count($username) !== 1)
if (GROCY_REVERSE_PROXY_AUTH_USE_ENV)
{
// Invalid configuration of Proxy
throw new \Exception('ReverseProxyAuthMiddleware: ' . GROCY_REVERSE_PROXY_AUTH_HEADER . ' header is missing or invalid');
$username = $_SERVER[GROCY_REVERSE_PROXY_AUTH_HEADER];
if (strlen($username) === 0)
{
// Invalid configuration of Proxy
throw new \Exception('ReverseProxyAuthMiddleware: ' . GROCY_REVERSE_PROXY_AUTH_HEADER . ' header is missing or invalid');
}
} else {
$username = $request->getHeader(GROCY_REVERSE_PROXY_AUTH_HEADER);
if (count($username) !== 1)
{
// Invalid configuration of Proxy
throw new \Exception('ReverseProxyAuthMiddleware: ' . GROCY_REVERSE_PROXY_AUTH_HEADER . ' header is missing or invalid');
}
$username = $username[0];
}
$username = $username[0];
$user = $db->users()->where('username', $username)->fetch();
if ($user == null)