@@ -0,0 +1,58 @@
1+{ pkgs, config, lib, ... }:
2+with lib;
3+let
4+ cfg = config.services.oauth2_proxy.nginx;
5+in
6+{
7+ options.services.oauth2_proxy.nginx = {
8+ proxy = mkOption {
9+ type = types.string;
10+ default = config.services.oauth2_proxy.httpAddress;
11+ };
12+ virtualHosts = mkOption {
13+ type = types.listOf types.string;
14+ default = [];
15+ };
16+ };
17+ config.services.oauth2_proxy = mkIf (cfg.virtualHosts != [] && (hasPrefix "127.0.0.1:" cfg.proxy)) {
18+ enable = true;
19+ };
20+ config.services.nginx = mkMerge ((optional (cfg.virtualHosts != []) {
21+ recommendedProxySettings = true; # needed because duplicate headers
22+ }) ++ (map (vhost: {
23+ virtualHosts.${vhost} = {
24+ locations."/oauth2/" = {
25+ proxyPass = cfg.proxy;
26+ extraConfig = ''
27+ proxy_set_header X-Scheme $scheme;
28+ proxy_set_header X-Auth-Request-Redirect $request_uri;
29+ '';
30+ };
31+ locations."/oauth2/auth" = {
32+ proxyPass = cfg.proxy;
33+ extraConfig = ''
34+ proxy_set_header X-Scheme $scheme;
35+ # nginx auth_request includes headers but not body
36+ proxy_set_header Content-Length "";
37+ proxy_pass_request_body off;
38+ '';
39+ };
40+ locations."/".extraConfig = ''
41+ auth_request /oauth2/auth;
42+ error_page 401 = /oauth2/sign_in;
43+
44+ # pass information via X-User and X-Email headers to backend,
45+ # requires running with --set-xauthrequest flag
46+ auth_request_set $user $upstream_http_x_auth_request_user;
47+ auth_request_set $email $upstream_http_x_auth_request_email;
48+ proxy_set_header X-User $user;
49+ proxy_set_header X-Email $email;
50+
51+ # if you enabled --cookie-refresh, this is needed for it to work with auth_request
52+ auth_request_set $auth_cookie $upstream_http_set_cookie;
53+ add_header Set-Cookie $auth_cookie;
54+ '';
55+
56+ };
57+ }) cfg.virtualHosts));
58+}