2018-07-20 20:56:59 +00:00
|
|
|
{ config, lib, ... }:
|
2018-06-29 14:36:03 +00:00
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.oauth2_proxy.nginx;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.oauth2_proxy.nginx = {
|
|
|
|
proxy = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.str;
|
2018-06-29 14:36:03 +00:00
|
|
|
default = config.services.oauth2_proxy.httpAddress;
|
2021-11-26 00:16:05 +00:00
|
|
|
defaultText = literalExpression "config.services.oauth2_proxy.httpAddress";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-06-29 15:23:24 +00:00
|
|
|
The address of the reverse proxy endpoint for oauth2_proxy
|
|
|
|
'';
|
2018-06-29 14:36:03 +00:00
|
|
|
};
|
|
|
|
virtualHosts = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.listOf types.str;
|
2018-06-29 14:36:03 +00:00
|
|
|
default = [];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-06-29 15:23:24 +00:00
|
|
|
A list of nginx virtual hosts to put behind the oauth2 proxy
|
|
|
|
'';
|
2018-06-29 14:36:03 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
config.services.oauth2_proxy = mkIf (cfg.virtualHosts != [] && (hasPrefix "127.0.0.1:" cfg.proxy)) {
|
|
|
|
enable = true;
|
|
|
|
};
|
2021-03-15 16:54:11 +00:00
|
|
|
config.services.nginx = mkIf config.services.oauth2_proxy.enable (mkMerge
|
|
|
|
((optional (cfg.virtualHosts != []) {
|
2018-06-29 14:36:03 +00:00
|
|
|
recommendedProxySettings = true; # needed because duplicate headers
|
|
|
|
}) ++ (map (vhost: {
|
|
|
|
virtualHosts.${vhost} = {
|
|
|
|
locations."/oauth2/" = {
|
|
|
|
proxyPass = cfg.proxy;
|
|
|
|
extraConfig = ''
|
|
|
|
proxy_set_header X-Scheme $scheme;
|
2021-02-08 12:04:23 +00:00
|
|
|
proxy_set_header X-Auth-Request-Redirect $scheme://$host$request_uri;
|
2018-06-29 14:36:03 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
locations."/oauth2/auth" = {
|
|
|
|
proxyPass = cfg.proxy;
|
|
|
|
extraConfig = ''
|
|
|
|
proxy_set_header X-Scheme $scheme;
|
|
|
|
# nginx auth_request includes headers but not body
|
|
|
|
proxy_set_header Content-Length "";
|
|
|
|
proxy_pass_request_body off;
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
locations."/".extraConfig = ''
|
|
|
|
auth_request /oauth2/auth;
|
|
|
|
error_page 401 = /oauth2/sign_in;
|
|
|
|
|
|
|
|
# pass information via X-User and X-Email headers to backend,
|
|
|
|
# requires running with --set-xauthrequest flag
|
|
|
|
auth_request_set $user $upstream_http_x_auth_request_user;
|
|
|
|
auth_request_set $email $upstream_http_x_auth_request_email;
|
|
|
|
proxy_set_header X-User $user;
|
|
|
|
proxy_set_header X-Email $email;
|
|
|
|
|
|
|
|
# if you enabled --cookie-refresh, this is needed for it to work with auth_request
|
|
|
|
auth_request_set $auth_cookie $upstream_http_set_cookie;
|
|
|
|
add_header Set-Cookie $auth_cookie;
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
2021-03-15 16:54:11 +00:00
|
|
|
}) cfg.virtualHosts)));
|
2018-06-29 14:36:03 +00:00
|
|
|
}
|