New Version

This commit is contained in:
Madzone
2026-09-09 14:13:44 +02:00
parent 23312854fc
commit efba066b51
11 changed files with 170 additions and 31 deletions
+32 -8
View File
@@ -7,7 +7,7 @@ import os
from pathlib import Path
import re
import tempfile
from urllib.parse import urlsplit
from urllib.parse import urlsplit, urlencode
MARK = '/* PVE-IDM LOGOUT v1 */'
ANCHOR = re.compile(r"(text:\s*gettext\('Logout'\),\s*handler:\s*function\s*\(\)\s*\{)(\s*PVE\.data\.ResourceStore\.loadData\(\[\],\s*false\);\s*me\.showLogin\(\);)")
@@ -33,28 +33,50 @@ def write(path, data, mode=0o600, owner=None):
os.unlink(tmp)
def render(text, realm, endpoint, client, redirect):
def logout_plan(endpoints, client, redirect):
if not isinstance(endpoints, list) or not 1 <= len(endpoints) <= 2 or len(set(endpoints)) != len(endpoints):
raise ValueError('Ein oder zwei unterschiedliche Keycloak-Logout-Endpunkte erforderlich.')
for url in endpoints + [redirect]:
u = urlsplit(url)
if (u.scheme != 'https' or not u.hostname or u.username or u.password or
u.fragment or u.query or any(c.isspace() for c in url) or '*' in url):
raise ValueError('Nur exakte HTTPS-Adressen ohne Query oder Zugangsdaten verwenden.')
for endpoint in endpoints:
if not urlsplit(endpoint).path.endswith('/protocol/openid-connect/logout'):
raise ValueError('Keycloak-Logout-Endpunkt erwartet.')
result = redirect
allowed = []
for endpoint in reversed(endpoints):
allowed.append(result)
result = endpoint + '?' + urlencode({'client_id': client, 'post_logout_redirect_uri': result})
return {'url': result, 'allowed_redirects': allowed, 'endpoints': endpoints}
def render(text, realm, endpoint, client, redirect, endpoints=None):
if MARK in text or len(list(ANCHOR.finditer(text))) != 1:
raise ValueError('Unbekannter oder mehrdeutiger Logout-Code; keine Änderung.')
for url in (endpoint, redirect):
u = urlsplit(url)
if u.scheme != 'https' or not u.hostname or u.username or u.password or u.fragment or u.query:
raise ValueError('Nur feste HTTPS-URLs ohne Query/Zugangsdaten erlaubt.')
cfg = json.dumps(dict(realm=realm, endpoint=endpoint, client=client, redirect=redirect), ensure_ascii=True)
if endpoints:
plan = logout_plan(endpoints, client, redirect)
logout_url = plan['url']
else:
logout_url = endpoint + '?' + urlencode({'client_id': client, 'post_logout_redirect_uri': redirect})
cfg = json.dumps(dict(realm=realm, endpoint=endpoint, client=client, redirect=redirect, logoutUrl=logout_url), ensure_ascii=True)
def replace(match):
return (match[1] + '\n' + MARK + '\n'
'const pveIdmUser = Proxmox.UserName || "";\n'
'const pveIdmConfig = ' + cfg + ';\n' + match[2] + '\n'
'if (pveIdmUser.endsWith("@" + pveIdmConfig.realm)) {\n'
' const pveIdmUrl = new URL(pveIdmConfig.endpoint);\n'
' pveIdmUrl.searchParams.set("client_id", pveIdmConfig.client);\n'
' pveIdmUrl.searchParams.set("post_logout_redirect_uri", pveIdmConfig.redirect);\n'
' const pveIdmUrl = new URL(pveIdmConfig.logoutUrl);\n'
' window.setTimeout(function () { window.location.assign(pveIdmUrl.href); }, 0);\n'
'}\n/* END PVE-IDM LOGOUT v1 */\n')
return ANCHOR.sub(replace, text)
def apply(target, directory, enabled, version='', realm='', endpoint='', client='', redirect=''):
def apply(target, directory, enabled, version='', realm='', endpoint='', client='', redirect='', endpoints=None):
if target.is_symlink() or not target.is_file():
raise ValueError('JavaScript-Ziel muss eine reguläre Datei sein.')
current = target.read_bytes()
@@ -75,7 +97,7 @@ def apply(target, directory, enabled, version='', realm='', endpoint='', client=
for suffix in ('.gz', '.br'):
if Path(str(target) + suffix).exists():
raise ValueError('Vorkomprimiertes JS gefunden; Auslieferung zuerst prüfen.')
desired = render(base.decode(), realm, endpoint, client, redirect).encode()
desired = render(base.decode(), realm, endpoint, client, redirect, endpoints).encode()
else:
desired = base
if desired == current:
@@ -103,7 +125,9 @@ if __name__ == '__main__':
parser.add_argument('--enabled', choices=['true', 'false'], required=True)
for option in ('version', 'realm', 'endpoint', 'client', 'redirect'):
parser.add_argument('--' + option, default='')
parser.add_argument('--endpoints-json', default='[]', help='JSON-Liste mit maximal zwei Logout-Endpunkten')
args = vars(parser.parse_args())
args['endpoints'] = json.loads(args.pop('endpoints_json'))
args['target'] = Path(args['target'])
args['directory'] = Path(args.pop('state_dir'))
args['enabled'] = args['enabled'] == 'true'