-
Notifications
You must be signed in to change notification settings - Fork 0
/
authelia.nix
331 lines (314 loc) · 11 KB
/
authelia.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
{
lib,
config,
hostName,
mkSecrets,
pkgs,
inputs,
...
}: let
inherit (lib) mkEnableOption mkOption mkIf optionals types;
autheliaInstance = "main";
mkUserRule = {
appName,
two_factor ? true,
}:
optionals config."${appName}".enable [
{
domain = config."${appName}".hostName;
policy = "one_factor";
networks = ["local"];
}
{
domain = config."${appName}".hostName;
policy =
if two_factor
then "two_factor"
else "one_factor";
}
];
in {
options = {
authelia = {
enable = mkEnableOption "enable Authelia";
hostName = mkOption {
type = types.str;
default = "authelia.${hostName}";
description = "The hostname of the Authelia instance";
};
port = mkOption {
type = types.int;
default = 9091;
description = "The port on which Authelia will listen";
};
};
};
disabledModules = [
"services/security/authelia.nix"
];
imports = [
"${inputs.authelia}/nixos/modules/services/security/authelia.nix"
];
config = mkIf config.authelia.enable {
age.secrets = let
cfg = {
owner = "authelia";
group = "authelia";
};
in
mkSecrets {
"authelia/storageKey" = cfg;
"authelia/jwtKey" = cfg;
"authelia/authBackend" = cfg;
"authelia/oAuth2PrivateKey" = cfg;
};
users = {
users.authelia = {
isSystemUser = true;
group = "authelia";
extraGroups = ["smtp"];
};
groups.authelia = {};
};
systemd.services.authelia = {
after = ["postgresql.service"];
};
systemd.services."authelia-${autheliaInstance}" = {
environment = {
# needed to set the secrets using agenix see: https://www.authelia.com/configuration/methods/files/#file-filters
X_AUTHELIA_CONFIG_FILTERS = "template";
AUTHELIA_NOTIFIER_SMTP_PASSWORD_FILE = config.age.secrets.smtpPassword.path;
};
};
services = {
authelia.instances = {
"${autheliaInstance}" = {
enable = true;
package = pkgs.prs.authelia;
user = "authelia";
group = "authelia";
secrets = {
storageEncryptionKeyFile = config.age.secrets."authelia/storageKey".path;
jwtSecretFile = config.age.secrets."authelia/jwtKey".path;
};
settingsFiles = [
# neet to write this in plain text because nix to yaml is doing some weird stuff
# see https://github.com/NixOS/nixpkgs/pull/299309 for details
(builtins.toFile
"authelia_id_provider_key.yaml"
'' identity_providers:
oidc:
jwks:
- key: {{ secret "/run/agenix/authelia/oAuth2PrivateKey" | mindent 10 "|" | msquote }}'')
];
settings = {
theme = "auto";
default_2fa_method = "webauthn";
webauthn = {
disable = false;
display_name = "Authelia";
attestation_conveyance_preference = "indirect";
user_verification = "preferred";
timeout = "60s";
};
totp = {
disable = false;
issuer = "authelia.com";
algorithm = "sha1";
digits = 6;
period = 30;
skew = 1;
secret_size = 32;
allowed_algorithms = ["SHA1"];
allowed_digits = [6];
allowed_periods = [30];
disable_reuse_security_policy = false;
};
# TODO add duo configuration later
server = {
disable_healthcheck = true;
address = "tcp://:${toString config.authelia.port}/";
endpoints = {
authz = {
auth-request = {
implementation = "AuthRequest";
};
};
};
};
log = {
format = "text"; # for fail2ban better integration
file_path = "/var/log/authelia/authelia.log";
keep_stdout = true;
level = "trace";
};
storage = {
postgres = {
address = "/run/postgresql";
database = "authelia";
username = "authelia";
password = "anUnus3dP@ssw0rd"; # thanks copilot for this beautiful password
};
};
# notifier needed for 2FA and email
# https://www.authelia.com/configuration/notifications/introduction/
notifier = {
disable_startup_check = false;
smtp = {
# using 587 port which is unencrypted I know but did not manage to make it working with 465
# however this is very unprobable that someone will sniff the network
address = "smtp://smtp.free.fr:587";
timeout = "60s";
username = "eymeric.monitoring";
sender = "Authelia <authelia@onyx.ovh>";
subject = "[Authelia] {title}";
startup_check_address = "eymericdechelette@gmail.com";
disable_require_tls = false;
disable_starttls = false;
disable_html_emails = false;
};
};
access_control = {
default_policy = "deny";
networks = [
{
name = "local";
networks = ["192.168.0.0/16"];
}
{
name = "internal";
networks = ["127.0.0.1/32"];
}
];
rules =
[
# be careful with the order of the rules it is important
# https://www.authelia.com/configuration/security/access-control/#rule-matching
{
domain_regex = ".*\.${hostName}";
policy = "bypass";
networks = ["internal"];
}
{
domain_regex = ".*\.${hostName}";
policy = "one_factor";
networks = ["local"];
subject = [
["group:admin"]
];
}
{
domain_regex = ".*\.${hostName}";
policy = "two_factor";
subject = [
["group:admin"]
];
}
]
++ mkUserRule {
appName = "homepage";
two_factor = false;
};
};
authentication_backend = {
file = {
# a agenix managed yaml doc : https://www.authelia.com/reference/guides/passwords/#yaml-format
path = config.age.secrets."authelia/authBackend".path;
# letting password hashing settings to the default (argon2id)
};
};
session = {
cookies = [
{
domain = hostName;
authelia_url = "https://${config.authelia.hostName}";
default_redirection_url = "https://${hostName}";
}
];
};
identity_providers.oidc = {
# enable to make it working so using settingsFiles (look above)
# jwks = [
# {
# key_id = "main";
# key = ''{{ secret "${config.age.secrets."authelia/oAuth2PrivateKey".path}" | mindent 10 "|" | msquote }}'';
# }
# ];
clients =
[]
++ optionals config.nextcloud.enable [
{
client_name = "NextCloud";
client_id = "nextcloud";
# the client secret is a random hash so don't worry about it
client_secret = "$pbkdf2-sha512$310000$NqCsT52TLWKH2GOq1c7vyw$ObxsUBEcwK53BY8obKj7fjmk1xp4MnTYCc2kS9UKpKifVGOQczt4rQx0bWt5pInqpAKxGHXo/RGa7DolDugz2A";
public = false;
authorization_policy = "two_factor";
require_pkce = true;
pkce_challenge_method = "S256";
redirect_uris = ["https://${config.nextcloud.hostName}/apps/oidc_login/oidc"];
scopes = [
"openid"
"profile"
"email"
"groups"
];
userinfo_signed_response_alg = "none";
token_endpoint_auth_method = "client_secret_basic";
}
]
++ optionals config.gitlab.enable [
{
client_name = "GitLab";
client_id = "gitlab";
# the client secret is a random hash so don't worry about it
client_secret = "$pbkdf2-sha512$310000$rSqyDXxdbAKVa46e7tdWWg$Go0EtABXJpe9oJuJboomLc/g31Dho5QqT3Hs954WPAYLKv2GKmPlclvPZb.0tq1dLVQHBbOG66hQvFh1kpOt7g";
public = false;
authorization_policy = "two_factor";
require_pkce = true;
pkce_challenge_method = "S256";
redirect_uris = ["https://${config.gitlab.hostName}/users/auth/openid_connect/callback"];
scopes = [
"openid"
"profile"
"groups"
"email"
];
userinfo_signed_response_alg = "none";
token_endpoint_auth_method = "client_secret_basic";
}
]
++ optionals config.matrix.enable [
{
client_name = "Synapse";
client_id = "synapse";
# the client secret is a random hash so don't worry about it
client_secret = "$pbkdf2-sha512$310000$76fMoD2cZWbBuahpGNUyNg$w5bAvbBpTU1fXMO41FdkYXbTafvDfYnrOZxRiOwOmfXe.sCZZ9pvL6nboA8/oDSXMUsJDGd3hxpiZL7x5c/vgQ";
public = false;
authorization_policy = "two_factor";
redirect_uris = ["https://${config.matrix.hostName}/_synapse/client/oidc/callback"];
scopes = [
"openid"
"profile"
"email"
];
userinfo_signed_response_alg = "none";
}
];
};
};
};
};
postgresql = {
enable = true;
ensureDatabases = ["authelia"];
ensureUsers = [
{
name = "authelia";
ensureDBOwnership = true;
}
];
};
};
};
}