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 | 8abe202 | 2023-11-11 16:24:43 +0100 | [diff] [blame] | 19 | provider-display-name = "sso.hackerspace.pl"; |
| 20 | |
| 21 | # We use HTTP basic authentication for programmatic access to LAN services |
Piotr Dobrowolski | a7af16d | 2023-09-19 20:10:57 +0200 | [diff] [blame] | 22 | htpasswd-file = "/var/beyondspace.htpasswd"; |
Piotr Dobrowolski | 8abe202 | 2023-11-11 16:24:43 +0100 | [diff] [blame] | 23 | display-htpasswd-form = false; |
| 24 | |
| 25 | custom-sign-in-logo = builtins.path { path = ./default-vhost/beyondspace.png; }; |
| 26 | footer = "This page is only accessible to <a href='https://hackerspace.pl'>Warsaw Hackerspace</a> members (or directly from within Warsaw Hackerspace LAN)."; |
Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 27 | }; |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | services.nginx.commonHttpConfig = '' |
| 32 | map $http_host $beyondspace_upstream_proto { |
| 33 | hostnames; |
| 34 | |
| 35 | default http; |
| 36 | |
| 37 | ${concatStringsSep "\n" (mapAttrsToList (key: value: "${key} ${value};") beyondspaceDomains)} |
| 38 | } |
| 39 | ''; |
| 40 | |
| 41 | services.nginx.virtualHosts."beyond.waw.hackerspace.pl" = { |
Piotr Dobrowolski | 13e6052 | 2023-11-12 01:07:46 +0100 | [diff] [blame] | 42 | # NOTE: we *can't* use forceSSL here for services that do not use HTTPS in |
| 43 | # local network setups, since this will pollute browser's redirect cache... |
| 44 | addSSL = true; |
Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 45 | enableACME = true; |
| 46 | |
| 47 | serverAliases = attrNames beyondspaceDomains; |
| 48 | |
| 49 | locations."/oauth2/" = { |
| 50 | extraConfig = '' |
Piotr Dobrowolski | 13e6052 | 2023-11-12 01:07:46 +0100 | [diff] [blame] | 51 | if ($scheme != https) { |
| 52 | return 302 https://$host$request_uri; |
| 53 | } |
| 54 | |
Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 55 | proxy_pass http://127.0.0.1:4180; |
| 56 | proxy_set_header Host $host; |
| 57 | proxy_set_header X-Real-IP $remote_addr; |
| 58 | proxy_set_header X-Scheme $scheme; |
| 59 | proxy_set_header X-Auth-Request-Redirect $request_uri; |
| 60 | ''; |
| 61 | }; |
| 62 | |
| 63 | locations."= /oauth2/auth" = { |
| 64 | extraConfig = '' |
Piotr Dobrowolski | 13e6052 | 2023-11-12 01:07:46 +0100 | [diff] [blame] | 65 | if ($scheme != https) { |
| 66 | return 302 https://$host$request_uri; |
| 67 | } |
| 68 | |
Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 69 | proxy_pass http://127.0.0.1:4180; |
| 70 | proxy_set_header Host $host; |
| 71 | proxy_set_header X-Real-IP $remote_addr; |
| 72 | proxy_set_header X-Scheme $scheme; |
| 73 | |
| 74 | # nginx auth_request includes headers but not body |
| 75 | proxy_set_header Content-Length ""; |
| 76 | proxy_pass_request_body off; |
| 77 | ''; |
| 78 | }; |
| 79 | |
| 80 | locations."/" = { |
| 81 | extraConfig = '' |
Piotr Dobrowolski | 13e6052 | 2023-11-12 01:07:46 +0100 | [diff] [blame] | 82 | if ($scheme != https) { |
| 83 | return 302 https://$host$request_uri; |
| 84 | } |
| 85 | |
Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 86 | auth_request /oauth2/auth; |
| 87 | error_page 401 = /oauth2/sign_in; |
| 88 | |
| 89 | # if you enabled --cookie-refresh, this is needed for it to work with auth_request |
| 90 | auth_request_set $auth_cookie $upstream_http_set_cookie; |
| 91 | add_header Set-Cookie $auth_cookie; |
| 92 | |
| 93 | proxy_pass $beyondspace_upstream_proto://$host$request_uri; |
| 94 | ''; |
| 95 | }; |
| 96 | }; |
Piotr Dobrowolski | 8abe202 | 2023-11-11 16:24:43 +0100 | [diff] [blame] | 97 | |
| 98 | services.nginx.virtualHosts."*.waw.hackerspace.pl" = { |
| 99 | default = true; |
| 100 | locations."/".root = ./default-vhost; |
| 101 | }; |
Piotr Dobrowolski | 9c5d866 | 2022-05-08 02:17:41 +0200 | [diff] [blame] | 102 | } |