middleman: Replace nginx-sso with Pocket ID SSO
CI / Check, build and cache nixfiles (push) Canceled after 25m17s
Update docs / update (push) Failing after 1m53s

Gate the vhosts without their own sign-in with oauth2-proxy, which
signs in with Pocket ID and allows members of the `admins` group.
Each gated vhost serves its own `/oauth2/` endpoints with a host-only
session cookie, so other `.nul.ie` sites never receive it. Sessions
last a week, and a refresh token lets oauth2-proxy check back with
Pocket ID every hour without a passkey.

Add `my.pocket-id`, which declares Pocket ID groups and OIDC clients
in Nix; `pocket-id-sync` applies them through the API with a static
API key that nginx keeps from outside requests. This lets apps move
to Pocket ID with their client defined next to them, with secrets
from `slop secret generate`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit was merged in pull request #12.
This commit is contained in:
jackos1998andClaude Opus 5.5 authored and Jack O'Sullivan committed 2026-11-27 00:15:06 +00:00
1 parent a3950b9bb3
commit eeb1f2d15c
14 files changed
+514 -325

No files matched your search

@@ -0,0 +1,105 @@
{ lib, config, ... }:
let
inherit (lib) attrNames filterAttrs;
inherit (lib.my.c) pubDomain;
# Vhosts gated by `ssoServer` in `vhosts.nix`, each with its own `/oauth2/` endpoints
ssoVhosts = attrNames (filterAttrs (_: v: v.locations ? "= /oauth2/auth") config.services.nginx.virtualHosts);
in
{
my = {
secrets.files = {
# Pocket ID's database encryption key; it reads it through `LoadCredential`
"middleman/pocket-id-encryption-key" = { };
# Admin key for `pocket-id-sync`, which only works from inside (`vhosts.nix` strips it)
"middleman/pocket-id-api-key" = { };
# `OAUTH2_PROXY_CLIENT_SECRET` and `OAUTH2_PROXY_COOKIE_SECRET`
"middleman/oauth2-proxy.env" = { };
};
pocket-id = {
enable = true;
apiKeyFile = config.age.secrets."middleman/pocket-id-api-key".path;
groups = {
admins.friendlyName = "Admins";
};
clients = {
# oauth2-proxy, which gates vhosts that have no sign-in of their own (`ssoServer`)
sso = {
name = "nul.ie SSO";
callbackURLs = map (h: "https://${h}/oauth2/callback") ssoVhosts;
pkce = true;
allowedGroups = [ "admins" ];
extraSettings.skipConsent = true;
secret = {
file = config.age.secrets."middleman/oauth2-proxy.env".path;
key = "OAUTH2_PROXY_CLIENT_SECRET";
};
};
};
};
};
# oauth2-proxy talks to Pocket ID through the local nginx rather than out and back in
networking.hosts = {
"127.0.0.1" = [ "id.${pubDomain}" ];
"::1" = [ "id.${pubDomain}" ];
};
services = {
# Passkey-only OpenID Connect provider at `id.nul.ie`, published by nginx
pocket-id = {
settings = {
APP_URL = "https://id.${pubDomain}";
HOST = "127.0.0.1";
PORT = 1411;
TRUST_PROXY = true;
ANALYTICS_DISABLED = true;
};
credentials.ENCRYPTION_KEY = config.age.secrets."middleman/pocket-id-encryption-key".path;
};
# Single sign-on for every `.nul.ie` vhost behind `ssoServer` in `vhosts.nix`
oauth2-proxy = {
enable = true;
provider = "oidc";
oidcIssuerUrl = "https://id.${pubDomain}";
clientID = "sso";
# On the vhost being signed in to
redirectURL = "/oauth2/callback";
keyFile = config.age.secrets."middleman/oauth2-proxy.env".path;
# `offline_access` for a refresh token, so sessions can be checked again without a passkey
scope = "openid email profile groups offline_access";
approvalPrompt = "auto";
# Access is decided by Pocket ID group (`allowed_groups` in `ssoServer`)
email.domains = [ "*" ];
httpAddress = "http://127.0.0.1:4180";
reverseProxy = true;
trustedProxyIP = [ "127.0.0.1" ];
setXauthrequest = true;
cookie = {
# Host-only, so the cookie is only sent to the vhost it was issued for
secure = true;
# Sessions last a week, but Pocket ID checks the user (and their groups) every hour
expire = "168h";
refresh = "1h";
};
extraConfig = {
code-challenge-method = "S256";
skip-provider-button = true;
# Pocket ID doesn't verify email addresses, and they aren't used for access here
insecure-oidc-allow-unverified-email = true;
};
};
};
systemd.services.oauth2-proxy = {
wants = [ "pocket-id-sync.service" ];
after = [ "pocket-id-sync.service" "nginx.service" ];
};
}
@@ -29,7 +29,7 @@ in
inherit (lib.my) networkdAssignment;
in
{
imports = [ ./vhosts.nix ];
imports = [ ./auth.nix ./vhosts.nix ];
config = mkMerge [
{
@@ -54,17 +54,11 @@ in
group = "acme";
mode = "400";
};
"middleman/nginx-sso.yaml" = {
owner = "nginx-sso";
group = "nginx-sso";
};
"middleman/htpasswd" = {
owner = "nginx";
group = "nginx";
};
"librespeed.toml" = { };
# Pocket ID's database encryption key; it reads it through `LoadCredential`
"middleman/pocket-id-encryption-key" = { };
};
};
@@ -79,56 +73,6 @@ in
'';
};
nginx-sso = {
enable = true;
extraConfigFile = config.age.secrets."middleman/nginx-sso.yaml".path;
configuration = {
listen = {
addr = "[::]";
port = 8082;
};
login = {
title = "${pubDomain} login";
default_redirect = "https://${pubDomain}";
default_method = "google_oauth";
names = {
google_oauth = "Google account";
simple = "Username / password";
};
};
cookie = {
domain = ".${pubDomain}";
secure = true;
};
audit_log = {
targets = [ "fd://stdout" ];
events = [
"access_denied"
"login_success"
"login_failure"
"logout"
#"validate"
];
};
providers = {
simple = {
groups = {
admin = [ "dev" ];
};
};
google_oauth = {
client_id = "545475967061-cag4g1qf0pk33g3pdbom4v69562vboc8.apps.googleusercontent.com";
redirect_url = "https://sso.${pubDomain}/login";
user_id_method = "user-id";
};
};
};
includes = {
endpoint = "http://localhost:8082";
baseURL = "https://sso.${pubDomain}";
};
};
librespeed = {
frontend.servers = [
{
@@ -227,19 +171,6 @@ in
};
services = {
# Passkey-only OpenID Connect provider at `id.nul.ie`, published by nginx
pocket-id = {
enable = true;
settings = {
APP_URL = "https://id.${pubDomain}";
HOST = "127.0.0.1";
PORT = 1411;
TRUST_PROXY = true;
ANALYTICS_DISABLED = true;
};
credentials.ENCRYPTION_KEY = config.age.secrets."middleman/pocket-id-encryption-key".path;
};
netdata = {
enable = true;
configDir = {
@@ -9,14 +9,40 @@ let
dualStackListen' = l: map (addr: l // { inherit addr; }) [ "0.0.0.0" "[::]" ];
dualStackListen = ll: flatten (map dualStackListen' ll);
ssoServer = i: {
# Gate a vhost behind oauth2-proxy, for members of any of the given Pocket ID groups. The
# session cookie is host-only, so other `.nul.ie` sites never see it; Pocket ID's own session
# makes signing in to the next gated vhost a redirect.
oauth2Proxy = config.services.oauth2-proxy.httpAddress;
ssoServer = groups: {
locations = {
"^~ /oauth2/" = {
proxyPass = oauth2Proxy;
extraConfig = ''
auth_request off;
${proxyHeaders}
# Where to return after signing in; on a 401 this is still the original request
proxy_set_header X-Auth-Request-Redirect $request_uri;
'';
};
# A literal `proxy_pass`: `proxyPass` sets `$nix_proxy_target`, which this subrequest would
# overwrite for the main request too
"= /oauth2/auth".extraConfig = ''
proxy_pass ${oauth2Proxy}/oauth2/auth?allowed_groups=${concatStringsSep "," groups};
internal;
auth_request off;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
${proxyHeaders}
'';
};
# A location with its own `add_header` would drop the refreshed cookie
extraConfig = ''
include /etc/nginx/includes/sso/server-${i}.conf;
'';
};
ssoLoc = i: {
extraConfig = ''
include /etc/nginx/includes/sso/location-${i}.conf;
auth_request /oauth2/auth;
error_page 401 = /oauth2/start;
# Pass on the session cookie when a check refreshes it
auth_request_set $sso_cookie $upstream_http_set_cookie;
add_header Set-Cookie $sso_cookie;
'';
};
@@ -58,12 +84,6 @@ let
};
in
{
my = {
nginx-sso.includes.instances = {
generic = {};
};
};
services.nginx.virtualHosts =
let
hosts = {
@@ -111,11 +131,6 @@ in
};
};
"sso.${pubDomain}" = {
locations."/".proxyPass = config.my.nginx-sso.includes.endpoint;
useACMEHost = pubDomain;
};
"netdata-colony.${pubDomain}" =
let
hosts = [
@@ -130,26 +145,23 @@ in
locations = {
"= /".return = "301 https://$host/vm/";
"~ /(?<behost>${matchHosts})$".return = "301 https://$host/$behost/";
"~ /(?<behost>${matchHosts})/(?<ndpath>.*)" = mkMerge [
{
proxyPass = "http://$behost.${domain}:19999/$ndpath$is_args$args";
extraConfig = ''
proxy_pass_request_headers on;
${proxyHeaders}
proxy_set_header Connection "keep-alive";
proxy_store off;
"~ /(?<behost>${matchHosts})/(?<ndpath>.*)" = {
proxyPass = "http://$behost.${domain}:19999/$ndpath$is_args$args";
extraConfig = ''
proxy_pass_request_headers on;
${proxyHeaders}
proxy_set_header Connection "keep-alive";
proxy_store off;
gzip on;
gzip_proxied any;
gzip_types *;
'';
}
(ssoLoc "generic")
];
gzip on;
gzip_proxied any;
gzip_types *;
'';
};
};
useACMEHost = pubDomain;
}
(ssoServer "generic")
(ssoServer [ "admins" ])
];
"pass.${pubDomain}" =
@@ -242,56 +254,40 @@ in
"torrents.${pubDomain}" = mkMerge [
{
locations."/" = mkMerge [
{
proxyPass = "http://jackflix-ctr.${domain}:9091";
}
(ssoLoc "generic")
];
locations."/".proxyPass = "http://jackflix-ctr.${domain}:9091";
useACMEHost = pubDomain;
}
(ssoServer "generic")
(ssoServer [ "admins" ])
];
"jackett.${pubDomain}" = mkMerge [
{
locations."/" = mkMerge [
{
proxyPass = "http://jackflix-ctr.${domain}:9117";
}
(ssoLoc "generic")
];
locations."/".proxyPass = "http://jackflix-ctr.${domain}:9117";
useACMEHost = pubDomain;
}
(ssoServer "generic")
(ssoServer [ "admins" ])
];
"radarr.${pubDomain}" = mkMerge [
{
locations."/" = mkMerge [
{
proxyPass = "http://jackflix-ctr.${domain}:7878";
proxyWebsockets = true;
extraConfig = proxyHeaders;
}
(ssoLoc "generic")
];
locations."/" = {
proxyPass = "http://jackflix-ctr.${domain}:7878";
proxyWebsockets = true;
extraConfig = proxyHeaders;
};
useACMEHost = pubDomain;
}
(ssoServer "generic")
(ssoServer [ "admins" ])
];
"sonarr.${pubDomain}" = mkMerge [
{
locations."/" = mkMerge [
{
proxyPass = "http://jackflix-ctr.${domain}:8989";
proxyWebsockets = true;
extraConfig = proxyHeaders;
}
(ssoLoc "generic")
];
locations."/" = {
proxyPass = "http://jackflix-ctr.${domain}:8989";
proxyWebsockets = true;
extraConfig = proxyHeaders;
};
useACMEHost = pubDomain;
}
(ssoServer "generic")
(ssoServer [ "admins" ])
];
"gib.${pubDomain}" = {
locations."/".proxyPass = "http://jackflix-ctr.${domain}:5055";
@@ -427,9 +423,14 @@ in
"id.${pubDomain}" =
let
# API keys (including `pocket-id-sync`'s admin key) only work from inside
pocketIdHeaders = ''
${proxyHeaders}
proxy_set_header X-API-Key "";
'';
pocketId = {
proxyPass = "http://127.0.0.1:${toString config.services.pocket-id.settings.PORT}";
extraConfig = proxyHeaders;
extraConfig = pocketIdHeaders;
};
in
{
@@ -440,7 +441,7 @@ in
extraConfig = ''
${concatStringsSep "\n" (map (n: "allow ${n};") (with lib.my.c.internalNets; v4 ++ v6))}
deny all;
${proxyHeaders}
${pocketIdHeaders}
'';
};
};
@@ -470,24 +471,18 @@ in
"pront.${pubDomain}" = mkMerge [
{
locations."/" = mkMerge [
{
proxyPass = "http://stream-hi.${home.domain}:5000";
proxyWebsockets = true;
extraConfig = proxyHeaders;
}
(ssoLoc "generic")
];
locations."~* ^/webcam/(.*)" = mkMerge [
{
proxyPass = "http://stream-hi.${home.domain}:5050/$1$is_args$args";
extraConfig = proxyHeaders;
}
(ssoLoc "generic")
];
locations."/" = {
proxyPass = "http://stream-hi.${home.domain}:5000";
proxyWebsockets = true;
extraConfig = proxyHeaders;
};
locations."~* ^/webcam/(.*)" = {
proxyPass = "http://stream-hi.${home.domain}:5050/$1$is_args$args";
extraConfig = proxyHeaders;
};
useACMEHost = pubDomain;
}
(ssoServer "generic")
(ssoServer [ "admins" ])
];
"hass.${pubDomain}" = {
locations."/" = {