Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 1 | { config, pkgs, lib, ... }: |
| 2 | |
| 3 | let |
| 4 | beyondspaceDomains = { |
| 5 | "inventory.waw.hackerspace.pl" = "https"; |
| 6 | "vending.waw.hackerspace.pl" = "https"; |
Piotr Dobrowolski | a7af16d | 2023-09-19 20:10:57 +0200 | [diff] [blame^] | 7 | "label.waw.hackerspace.pl" = "http"; |
Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 8 | }; |
| 9 | |
| 10 | in with lib; { |
| 11 | services.oauth2_proxy = { |
| 12 | enable = true; |
| 13 | provider = "oidc"; |
| 14 | keyFile = "/var/beyondspace.secrets"; |
| 15 | clientID = "1e0a7ba0-5a15-477a-8d96-690ebbe6e720"; |
| 16 | extraConfig = { |
| 17 | oidc-issuer-url = "https://sso.hackerspace.pl"; |
| 18 | email-domain = "*"; |
Piotr Dobrowolski | a7af16d | 2023-09-19 20:10:57 +0200 | [diff] [blame^] | 19 | htpasswd-file = "/var/beyondspace.htpasswd"; |
Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 20 | }; |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | services.nginx.commonHttpConfig = '' |
| 25 | map $http_host $beyondspace_upstream_proto { |
| 26 | hostnames; |
| 27 | |
| 28 | default http; |
| 29 | |
| 30 | ${concatStringsSep "\n" (mapAttrsToList (key: value: "${key} ${value};") beyondspaceDomains)} |
| 31 | } |
| 32 | ''; |
| 33 | |
| 34 | services.nginx.virtualHosts."beyond.waw.hackerspace.pl" = { |
| 35 | forceSSL = true; |
| 36 | enableACME = true; |
| 37 | |
| 38 | serverAliases = attrNames beyondspaceDomains; |
| 39 | |
| 40 | locations."/oauth2/" = { |
| 41 | extraConfig = '' |
| 42 | proxy_pass http://127.0.0.1:4180; |
| 43 | proxy_set_header Host $host; |
| 44 | proxy_set_header X-Real-IP $remote_addr; |
| 45 | proxy_set_header X-Scheme $scheme; |
| 46 | proxy_set_header X-Auth-Request-Redirect $request_uri; |
| 47 | ''; |
| 48 | }; |
| 49 | |
| 50 | locations."= /oauth2/auth" = { |
| 51 | extraConfig = '' |
| 52 | proxy_pass http://127.0.0.1:4180; |
| 53 | proxy_set_header Host $host; |
| 54 | proxy_set_header X-Real-IP $remote_addr; |
| 55 | proxy_set_header X-Scheme $scheme; |
| 56 | |
| 57 | # nginx auth_request includes headers but not body |
| 58 | proxy_set_header Content-Length ""; |
| 59 | proxy_pass_request_body off; |
| 60 | ''; |
| 61 | }; |
| 62 | |
| 63 | locations."/" = { |
| 64 | extraConfig = '' |
| 65 | auth_request /oauth2/auth; |
| 66 | error_page 401 = /oauth2/sign_in; |
| 67 | |
| 68 | # if you enabled --cookie-refresh, this is needed for it to work with auth_request |
| 69 | auth_request_set $auth_cookie $upstream_http_set_cookie; |
| 70 | add_header Set-Cookie $auth_cookie; |
| 71 | |
| 72 | proxy_pass $beyondspace_upstream_proto://$host$request_uri; |
| 73 | ''; |
| 74 | }; |
| 75 | }; |
| 76 | } |