grace/dots

dotfiles/nix configs

git clone https://git.gae.moe/grace/dots

Grace Yodermany changesd144193

main
3.3 KiB90 linesraw
1import os
2from pathlib import Path
3import sys
4import xml.etree.ElementTree as ET
5
6
7config_path = Path(sys.argv[1])
8secret = Path(sys.argv[2]).read_text().strip()
9if not secret or secret.startswith("$pbkdf2-") or secret.startswith("$argon2"):
10    raise ValueError("Jellyfin needs the non-empty plaintext OIDC client secret")
11
12root = ET.parse(config_path).getroot() if config_path.exists() else ET.Element("PluginConfiguration")
13if root.find("SamlConfigs") is None:
14    ET.SubElement(root, "SamlConfigs")
15providers = root.find("OidConfigs")
16if providers is None:
17    providers = ET.SubElement(root, "OidConfigs")
18item = next(
19    (item for item in providers.findall("item") if item.findtext("key/string") == "authelia"),
20    None,
21)
22if item is None:
23    item = ET.SubElement(providers, "item")
24    ET.SubElement(ET.SubElement(item, "key"), "string").text = "authelia"
25    ET.SubElement(ET.SubElement(item, "value"), "PluginConfiguration")
26provider = item.find("value/PluginConfiguration")
27
28# this is so that it doesnt overwrite the links betweena ccounts
29settings = {
30    "OidEndpoint": "https://auth.gae.moe",
31    "OidClientId": "jellyfin",
32    "OidSecret": secret,
33    "Enabled": "true",
34    "EnableAuthorization": "true",
35    "EnableAllFolders": "true",
36    "PreserveAdminPermissions": "true",
37    "EnableFolderRoles": "false",
38    "EnableLiveTvRoles": "false",
39    "EnableLiveTv": "false",
40    "EnableLiveTvManagement": "false",
41    "RoleClaim": "groups",
42    "DefaultUsernameClaim": "preferred_username",
43    "SchemeOverride": "https",
44    "NewPath": "false",
45    "DisableHttps": "false",
46    "DisablePushedAuthorization": "true",
47    "DoNotValidateEndpoints": "false",
48    "DoNotValidateIssuerName": "false",
49    "DoNotLoadProfile": "false",
50    "OverrideDefaultScopes": "false",
51    "OidAuthorizationParameters": "",
52}
53for name, value in settings.items():
54    element = provider.find(name)
55    if element is None:
56        element = ET.SubElement(provider, name)
57    element.text = value
58
59for name, values in {"Roles": ["users"], "AdminRoles": [], "OidScopes": ["groups"]}.items():
60    element = provider.find(name)
61    if element is None:
62        element = ET.SubElement(provider, name)
63    element.clear()
64    for value in values:
65        ET.SubElement(element, "string").text = value
66
67branding_path = Path(sys.argv[3])
68branding = ET.parse(branding_path).getroot() if branding_path.exists() else ET.Element("BrandingOptions")
69disclaimer = branding.find("LoginDisclaimer")
70if disclaimer is None:
71    disclaimer = ET.SubElement(branding, "LoginDisclaimer")
72button = '''<form action="https://fin.gae.moe/sso/OID/start/authelia" id="authelia-signin">
73  <button class="raised block emby-button button-submit">Sign in with auth.gae.moe</button>
74</form>'''
75if 'id="authelia-signin"' not in (disclaimer.text or ""):
76    disclaimer.text = (disclaimer.text or "") + "\n" + button
77css = branding.find("CustomCss")
78if css is None:
79    css = ET.SubElement(branding, "CustomCss")
80rule = ".disclaimerContainer { display: block; }"
81if rule not in (css.text or ""):
82    css.text = (css.text or "") + "\n" + rule
83
84os.umask(0o077)
85for path, document in [(config_path, root), (branding_path, branding)]:
86    temporary = path.with_suffix(".xml.tmp")
87    with temporary.open("wb") as output:
88        os.chmod(temporary, 0o600)
89        ET.ElementTree(document).write(output, encoding="utf-8", xml_declaration=True)
90    temporary.replace(path)