Let apps describe their sign-in client: [sso] callback, pkce, token_auth
A rehearsal with Vikunja showed apps with their own redirect path or no PKCE needed a bridge in the container. coreapp.toml's [sso] now sets the Authelia client's callback, PKCE and token method; changes re-register the client, and a failed update restores it. The skill covers [sso], images without a shell and upstream ENV defaults; coreapp shell and status explain rough edges the rehearsal hit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+55
-1
@@ -86,9 +86,17 @@ FIELDS = {
|
||||
"health": r"/[A-Za-z0-9._~/-]{0,100}",
|
||||
"docs": r"(?:https://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[A-Za-z0-9._~/%+-]*)?)?",
|
||||
"needs": None,
|
||||
"sso": None,
|
||||
}
|
||||
REQUIRED = ["name", "title", "version", "image", "port"]
|
||||
|
||||
# [sso]: how the app's OpenID Connect client in Authelia differs from the
|
||||
# default, for apps whose sign-in can't be configured to match it
|
||||
SSO_DEFAULTS = {"callback": "/auth/callback", "pkce": True,
|
||||
"token_auth": "client_secret_basic"}
|
||||
SSO_CALLBACK = r"/[A-Za-z0-9._~/-]{0,200}"
|
||||
SSO_TOKEN_AUTH = ["client_secret_basic", "client_secret_post"]
|
||||
|
||||
# Repositories apps are installed from: https only, no user or password in
|
||||
# the URL. Tags: v + a version; releases (picked by default) are numeric.
|
||||
REPO_URL = r"https://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[A-Za-z0-9._-]+){2,}?(?:\.git)?/?"
|
||||
@@ -118,6 +126,25 @@ def say(*args):
|
||||
|
||||
# --- coreapp.toml -------------------------------------------------------------
|
||||
|
||||
def read_sso(value):
|
||||
"""coreapp.toml's [sso] table, checked, with defaults filled in."""
|
||||
if not isinstance(value, dict):
|
||||
raise Refused("coreapp.toml: sso must be a table: [sso] with callback, pkce, token_auth.")
|
||||
unknown = sorted(set(value) - set(SSO_DEFAULTS))
|
||||
if unknown:
|
||||
raise Refused(f"coreapp.toml: [sso]: unknown setting(s) {', '.join(unknown)} "
|
||||
f"(it knows {', '.join(SSO_DEFAULTS)}).")
|
||||
sso = dict(SSO_DEFAULTS, **value)
|
||||
if not isinstance(sso["callback"], str) or not re.fullmatch(SSO_CALLBACK, sso["callback"]):
|
||||
raise Refused(f"coreapp.toml: [sso] callback = {sso['callback']!r} isn't allowed: "
|
||||
"a path on the app's own address, like \"/auth/callback\".")
|
||||
if not isinstance(sso["pkce"], bool):
|
||||
raise Refused("coreapp.toml: [sso] pkce must be true or false.")
|
||||
if sso["token_auth"] not in SSO_TOKEN_AUTH:
|
||||
raise Refused(f"coreapp.toml: [sso] token_auth must be {' or '.join(SSO_TOKEN_AUTH)}.")
|
||||
return sso
|
||||
|
||||
|
||||
def read_manifest(folder):
|
||||
"""coreapp.toml in folder, checked, with defaults filled in."""
|
||||
path = os.path.join(folder, "coreapp.toml")
|
||||
@@ -139,7 +166,7 @@ def read_manifest(folder):
|
||||
if missing:
|
||||
raise Refused(f"coreapp.toml needs {', '.join(missing)}.")
|
||||
|
||||
m = {"description": "", "health": "/", "docs": "", "needs": []}
|
||||
m = {"description": "", "health": "/", "docs": "", "needs": [], "sso": dict(SSO_DEFAULTS)}
|
||||
for key, value in data.items():
|
||||
if key == "port":
|
||||
if not isinstance(value, int) or isinstance(value, bool) or not 1 <= value <= 65535:
|
||||
@@ -152,6 +179,8 @@ def read_manifest(folder):
|
||||
raise Refused(f"coreapp.toml: needs: unknown {', '.join(bad)} "
|
||||
f"(it knows {', '.join(NEEDS)}).")
|
||||
value = [n for n in NEEDS if n in value]
|
||||
elif key == "sso":
|
||||
value = read_sso(value)
|
||||
if "postgres" in value and "mysql" in value:
|
||||
raise Refused("coreapp.toml: needs: postgres or mysql, not both "
|
||||
"(both give the app DB_* settings).")
|
||||
@@ -160,6 +189,8 @@ def read_manifest(folder):
|
||||
raise Refused(f"coreapp.toml: {key} = {value!r} isn't allowed. "
|
||||
+ HINTS.get(key, ""))
|
||||
m[key] = value
|
||||
if "sso" in data and "sso" not in m["needs"]:
|
||||
raise Refused('coreapp.toml: [sso] is for apps with "sso" in needs.')
|
||||
if m["name"] in RESERVED:
|
||||
raise Refused(f"'{m['name']}' is one of the Core's own hostnames. Pick another name.")
|
||||
m["dbname"] = "coreapp_" + m["name"].replace("-", "_")
|
||||
@@ -604,6 +635,9 @@ def make_service_folder(m, p, source, origin):
|
||||
# Without docs, Superpanel links a search of Federated's documentation
|
||||
"DOCS": m["docs"] or (origin["source"] if origin else ""), "IMAGE": m["image"], "VERSION": m["version"],
|
||||
"IP": p["address"], "PORT": str(m["port"]), "DBNAME": m["dbname"],
|
||||
"SSO_CALLBACK": m["sso"]["callback"], "SSO_TOKEN_AUTH": m["sso"]["token_auth"],
|
||||
"SSO_PKCE": ("require_pkce: true\n pkce_challenge_method: 'S256'"
|
||||
if m["sso"]["pkce"] else "require_pkce: false"),
|
||||
"DEPENDS": " ".join(read_text(NEEDS_DIR, n, "depends").strip() for n in m["needs"]),
|
||||
}
|
||||
compose_extra = "".join(read_text(NEEDS_DIR, n, "compose") for n in m["needs"])
|
||||
@@ -920,6 +954,18 @@ for need in "$@"; do
|
||||
done
|
||||
'''
|
||||
|
||||
# After the old service folder is back: its needs' update steps, so what
|
||||
# they set up matches it again (e.g. an Authelia client the update changed)
|
||||
RESYNC_SCRIPT = r'''
|
||||
cd /federated
|
||||
. /etc/federated
|
||||
. lib/functions.sh
|
||||
. lib/helpers.sh
|
||||
for step in services/$1/needs/*/update; do
|
||||
if [ -e "$step" ]; then . "$step"; fi
|
||||
done
|
||||
'''
|
||||
|
||||
|
||||
def roll_back(m, p, backup, log):
|
||||
"""Put back the version that ran before a failed update, with its
|
||||
@@ -952,6 +998,11 @@ def roll_back(m, p, backup, log):
|
||||
for f in ROLLBACK_FILES:
|
||||
if os.path.exists(os.path.join(backup, f)):
|
||||
shutil.copy2(os.path.join(backup, f), os.path.join(APPS, name, f))
|
||||
with open(log, "a") as f:
|
||||
result = subprocess.run(["bash", "-c", RESYNC_SCRIPT, "resync", name],
|
||||
stdout=f, stderr=subprocess.STDOUT)
|
||||
if result.returncode != 0:
|
||||
problems.append("putting back what its needs set up")
|
||||
try:
|
||||
old = read_manifest(final)
|
||||
except Refused:
|
||||
@@ -1126,6 +1177,9 @@ def main(args):
|
||||
"health", "docs", "dbname"):
|
||||
print(f"{key}={m[key]}")
|
||||
print(f"needs={' '.join(m['needs'])}")
|
||||
if "sso" in m["needs"]:
|
||||
for key, value in m["sso"].items():
|
||||
print(f"sso_{key}={str(value).lower() if isinstance(value, bool) else value}")
|
||||
state, note = icon_check(rest[0])
|
||||
print(f"icon={state}")
|
||||
print(f"icon_note={note}")
|
||||
|
||||
@@ -15,10 +15,9 @@ cat >> "$IDP" <<OIDC
|
||||
consent_mode: 'implicit'
|
||||
public: false
|
||||
authorization_policy: 'one_factor'
|
||||
require_pkce: true
|
||||
pkce_challenge_method: 'S256'
|
||||
{{SSO_PKCE}}
|
||||
redirect_uris:
|
||||
- 'https://{{NAME}}.$DOMAIN/auth/callback'
|
||||
- 'https://{{NAME}}.$DOMAIN{{SSO_CALLBACK}}'
|
||||
scopes:
|
||||
- 'openid'
|
||||
- 'profile'
|
||||
@@ -28,7 +27,7 @@ cat >> "$IDP" <<OIDC
|
||||
- 'code'
|
||||
grant_types:
|
||||
- 'authorization_code'
|
||||
token_endpoint_auth_method: 'client_secret_basic'
|
||||
token_endpoint_auth_method: '{{SSO_TOKEN_AUTH}}'
|
||||
userinfo_signed_response_alg: 'none'
|
||||
### coreapp-{{NAME}} end
|
||||
OIDC
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
# Register the client again if coreapp.toml's [sso] changed it (its
|
||||
# callback, PKCE, or how it sends its secret); Authelia restarts only then
|
||||
IDP=/federated/apps/authelia/data/config/idproviders.yml
|
||||
BLOCK="$(sed -n '/^ ### coreapp-{{NAME}} begin$/,/^ ### coreapp-{{NAME}} end$/p' "$IDP")"
|
||||
if ! grep -qxF " - 'https://{{NAME}}.$DOMAIN{{SSO_CALLBACK}}'" <<< "$BLOCK" \
|
||||
|| ! grep -qxF " token_endpoint_auth_method: '{{SSO_TOKEN_AUTH}}'" <<< "$BLOCK" \
|
||||
|| ! grep -qxF " $(echo "{{SSO_PKCE}}" | head -1)" <<< "$BLOCK" \
|
||||
|| [ "$(grep -c pkce_challenge_method <<< "$BLOCK")" != "$(echo "{{SSO_PKCE}}" | grep -c pkce_challenge_method)" ]; then
|
||||
. /federated/services/{{NAME}}/needs/sso/setup
|
||||
fi
|
||||
unset BLOCK
|
||||
# Keep the copy of its Authelia client current (apps installed before
|
||||
# copies were kept get one); Authelia's start restores clients from these
|
||||
IDP=/federated/apps/authelia/data/config/idproviders.yml
|
||||
|
||||
Reference in New Issue
Block a user