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