grace/dots
dotfiles/nix configs
git clone https://git.gae.moe/grace/dots
d144193
main
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" ) : 10raise 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 : 14ET . SubElement ( root , "SamlConfigs" ) 15providers = root . find ( "OidConfigs" ) 16if providers is None : 17providers = ET . SubElement ( root , "OidConfigs" ) 18item = next ( 19( item for item in providers . findall ( "item" ) if item . findtext ( "key/string" ) == "authelia" ) , 20None , 21) 22if item is None : 23item = ET . SubElement ( providers , "item" ) 24ET . SubElement ( ET . SubElement ( item , "key" ) , "string" ) . text = "authelia" 25ET . 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 () : 54element = provider . find ( name ) 55if element is None : 56element = ET . SubElement ( provider , name ) 57element . text = value 58 59for name , values in { "Roles" : [ "users" ] , "AdminRoles" : [] , "OidScopes" : [ "groups" ]} . items () : 60element = provider . find ( name ) 61if element is None : 62element = ET . SubElement ( provider , name ) 63element . clear () 64for value in values : 65ET . 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 : 71disclaimer = 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 "" ) : 76disclaimer . text = ( disclaimer . text or "" ) + " \n " + button 77css = branding . find ( "CustomCss" ) 78if css is None : 79css = ET . SubElement ( branding , "CustomCss" ) 80rule = ".disclaimerContainer { display: block; }" 81if rule not in ( css . text or "" ) : 82css . text = ( css . text or "" ) + " \n " + rule 83 84os . umask ( 0o077 ) 85for path , document in [( config_path , root ) , ( branding_path , branding )] : 86temporary = path . with_suffix ( ".xml.tmp" ) 87with temporary . open ( "wb" ) as output : 88os . chmod ( temporary , 0o600 ) 89ET . ElementTree ( document ) . write ( output , encoding = "utf-8" , xml_declaration = True ) 90temporary . replace ( path )