-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.nix
207 lines (206 loc) · 6.59 KB
/
module.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.webauthn-tiny;
passwordFile =
if (cfg.basicAuthFile != null) then
cfg.basicAuthFile
else
(pkgs.runCommand "generated-password-file" { } (
''
touch $out
salt=$(${pkgs.openssl}/bin/openssl rand -hex 16)
''
+ (concatStringsSep ";" (
mapAttrsToList (username: password: ''
echo ${username}:$(printf "${password}" | ${pkgs.libargon2}/bin/argon2 $salt -id -e) >> $out
'') cfg.basicAuth
))
));
sessionSecretFile =
if (cfg.sessionSecretFile != null) then
cfg.sessionSecretFile
else
(pkgs.runCommand "generated-session-secret-file" { }
"${pkgs.openssl}/bin/openssl rand -hex 64 > $out"
);
in
{
options = {
services.webauthn-tiny = {
enable = mkEnableOption "webauthn-tiny server";
package = mkPackageOption pkgs "webauthn-tiny" { };
basicAuth = mkOption {
type = types.attrsOf types.str;
description = ''
A static mapping of usernames to passwords. WARNING: only use this
for testing purposes.
'';
default = { };
example = {
myuser = "mypassword";
};
};
basicAuthFile = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
The path to a password file. This file must contain lines in the form
of "<username>:<argon2_hash>". A valid Argon2 hash can be generated
using the `libargon2` package like so: `argon2 <salt> -id -e`.
'';
};
sessionSecretFile = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
The path to a file containing a session secret (64 or more bytes).
You can use `openssl rand -hex 64` to generate a session secret.
'';
};
relyingParty = {
id = mkOption {
type = types.str;
description = ''
An ID that corresponds to the domain applicable for that Relying
Party.
'';
example = "mywebsite.com";
};
origin = mkOption {
type = types.str;
description = ''
The origin on which registrations for the Relying Party will take
place.
'';
example = "https://mywebsite.com";
};
extraAllowedOrigins = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
Extra allowed origins that will be allowed for redirects and trusted
by the webauthn instance.
'';
example = [ "https://subdomain.mywebsite.com" ];
};
};
nginx = {
enable = mkEnableOption "nginx support";
virtualHost = mkOption {
type = types.str;
description = ''
The virtual host that this service will serve on.
'';
};
protectedVirtualHosts = mkOption {
type = types.listOf types.str;
description = ''
A list of virtual hosts that will be protected by this webauthn
server. This uses nginx's auth_request functionality.
'';
default = [ ];
};
enableACME = mkEnableOption "enable ACME on this virtual host";
useACMEHost = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Whether to ask Let's Encrypt to sign a certificate for this vhost.
Alternately, you can use an existing certificate through
useACMEHost.
'';
};
};
};
};
config = mkIf cfg.enable {
services.nginx = mkIf cfg.nginx.enable {
enable = true;
virtualHosts =
genAttrs cfg.nginx.protectedVirtualHosts (_: {
extraConfig = ''
auth_request /auth;
error_page 401 = @error401;
auth_request_set $set_cookie $upstream_http_set_cookie;
more_set_headers "Set-Cookie: $set_cookie";
'';
locations."= /auth" = {
proxyPass = "http://[::1]:8080/api/validate";
extraConfig = ''
internal;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
'';
};
locations."@error401".return =
"307 $scheme://${cfg.nginx.virtualHost}/authenticate?redirect_url=https://$http_host";
})
// {
${cfg.nginx.virtualHost} = {
inherit (cfg.nginx) enableACME useACMEHost;
forceSSL = true; # webauthn is only available over HTTPS
locations."/" = {
proxyPass = "http://[::1]:8080";
extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
'';
};
};
};
};
systemd.services.webauthn-tiny = {
enable = true;
description = "webauthn-tiny (https://github.com/jmbaur/webauthn-tiny)";
environment.WEBAUTHN_TINY_LOG = "info";
serviceConfig = {
StateDirectory = "webauthn-tiny";
LoadCredential = [
"password-file:${passwordFile}"
"session-secret-file:${sessionSecretFile}"
];
ExecStart = escapeShellArgs (
[
(lib.getExe pkgs.webauthn-tiny)
"--rp-id=${cfg.relyingParty.id}"
"--rp-origin=${cfg.relyingParty.origin}"
"--password-file=\${CREDENTIALS_DIRECTORY}/password-file"
"--session-secret-file=\${CREDENTIALS_DIRECTORY}/session-secret-file"
]
++ (map (origin: "--extra-allowed-origin=${origin}") cfg.relyingParty.extraAllowedOrigins)
);
CapabilityBoundingSet = [ ];
DeviceAllow = [ ];
DynamicUser = true;
LockPersonality = true;
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
PrivateDevices = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectSystem = "strict";
RemoveIPC = true;
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
};
wantedBy = [ "multi-user.target" ];
};
};
}