nginx: factor out the generation of basic auth generation

This commit is contained in:
Graham Christensen 2020-10-20 14:06:37 +00:00
parent fd0b3839b2
commit 33cf4f0e8e
No known key found for this signature in database
GPG Key ID: FE918C3A98C1030F

View File

@ -261,10 +261,7 @@ let
ssl_trusted_certificate ${vhost.sslTrustedCertificate};
''}
${optionalString (vhost.basicAuthFile != null || vhost.basicAuth != {}) ''
auth_basic secured;
auth_basic_user_file ${if vhost.basicAuthFile != null then vhost.basicAuthFile else mkHtpasswd vhostName vhost.basicAuth};
''}
${mkBasicAuth vhostName vhost}
${mkLocations vhost.locations}
@ -295,7 +292,16 @@ let
${optionalString (config.proxyPass != null && cfg.recommendedProxySettings) "include ${recommendedProxyConfig};"}
}
'') (sortProperties (mapAttrsToList (k: v: v // { location = k; }) locations)));
mkHtpasswd = vhostName: authDef: pkgs.writeText "${vhostName}.htpasswd" (
mkBasicAuth = name: zone: optionalString (zone.basicAuthFile != null || zone.basicAuth != {}) (let
auth_file = if zone.basicAuthFile != null
then zone.basicAuthFile
else mkHtpasswd name zone.basicAuth;
in ''
auth_basic secured;
auth_basic_user_file ${auth_file};
'');
mkHtpasswd = name: authDef: pkgs.writeText "${name}.htpasswd" (
concatStringsSep "\n" (mapAttrsToList (user: password: ''
${user}:{PLAIN}${password}
'') authDef)