1 Commits
Author SHA1 Message Date
claudeandClaude Opus 5.5 b8978221a3 Option to turn off local login (FUNKWHALE_OIDC_LOCAL_LOGIN)
With it false, a middleware rejects password login, sign-up and password
reset/change in the API, allauth's pages and the Django admin, and the
web app shows only the provider button. /login and /signup go straight
to the provider, and the post-login redirect never points back at them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:41:28 -05:00
9 changed files with 91 additions and 17 deletions
+8 -2
View File
@@ -8,6 +8,10 @@ Funkwhale already ships django-allauth. This plugin turns on allauth's generic `
- **Admins** can be granted from a list of usernames or from a groups claim.
- **Existing local accounts** can optionally be linked by verified e-mail address.
- The **web app** gets a "Log in with …" button on every login form. It can also send `/login` straight to the provider; `/login?local=1` still shows the password form.
- **Local login** can be turned off entirely (`FUNKWHALE_OIDC_LOCAL_LOGIN=false`).
- The server rejects password login, sign-up and password reset/change, in the API, in allauth's pages and in the Django admin.
- The web app shows only the provider button and sends `/login` and `/signup` to the provider.
- Subsonic app tokens and OAuth apps keep working.
Tested with Funkwhale 2.0.13 (django-allauth 65.14.1) and Dex v2.44.0.
@@ -21,7 +25,7 @@ Install the package and PyJWT into `FUNKWHALE_PLUGINS_PATH` (default `/srv/funkw
```sh
pip install --target /srv/funkwhale/plugins \
https://gitea.eom.dev/DevOps/funkwhale-oidc/archive/v0.1.0.tar.gz
https://gitea.eom.dev/DevOps/funkwhale-oidc/archive/v0.2.0.tar.gz
```
In containers, run this as an init step into a shared volume. `tests/compose.yaml` shows how.
@@ -41,6 +45,7 @@ Then set:
| `FUNKWHALE_OIDC_USERNAME_CLAIM` | `preferred_username` | becomes the username and `@user@host` handle, so it must be ASCII |
| `FUNKWHALE_OIDC_SIGNUP` | `true` | create accounts for new provider users |
| `FUNKWHALE_OIDC_LINK_BY_EMAIL` | `false` | log into an existing local account with the same verified e-mail; only enable this for a provider you trust |
| `FUNKWHALE_OIDC_LOCAL_LOGIN` | `true` | `false`: the provider is the only way in. Recover with `funkwhale-manage` in the API container |
| `FUNKWHALE_OIDC_ADMIN_USERS` | | comma-separated usernames made superuser at login |
| `FUNKWHALE_OIDC_ADMIN_GROUPS` | | comma-separated groups (from `FUNKWHALE_OIDC_GROUPS_CLAIM`, default `groups`) that grant superuser |
@@ -65,6 +70,7 @@ The files are in the package under `funkwhale_oidc/front/`, so the same `pip ins
3. Set these on the front container:
- `FUNKWHALE_OIDC_PROVIDER_ID` and `FUNKWHALE_OIDC_NAME`, with the same values as on the API.
- `FUNKWHALE_OIDC_AUTO_REDIRECT`: optional, `true` to skip the password form on `/login`.
- `FUNKWHALE_OIDC_LOCAL_LOGIN`: the same value as on the API.
If you use your own nginx in front of the API, add this instead:
@@ -83,7 +89,7 @@ Set `FUNKWHALE_SPA_HTML_ROOT` to the front's internal URL (e.g. `http://funkwhal
## Test
```sh
docker compose -f tests/compose.yaml up -d
docker compose -f tests/compose.yaml up -d # LOCAL_LOGIN=false to test that mode
# http://funkwhale.localhost/login → "Log in with Dex" → admin@example.com / password
docker compose -f tests/compose.yaml down -v
```
+1 -1
View File
@@ -1,4 +1,4 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""OpenID Connect login for Funkwhale, as a Funkwhale plugin."""
__version__ = "0.1.0"
__version__ = "0.2.0"
+3
View File
@@ -35,6 +35,9 @@ def get():
# (provider-verified) e-mail. Only enable for a provider you trust
# to verify e-mail addresses.
"link_by_email": e("LINK_BY_EMAIL", False, env.bool),
# false: no password login, sign-up or password reset; the provider
# is the only way in. Recovery: funkwhale-manage in the API pod.
"local_login": e("LOCAL_LOGIN", True, env.bool),
# Superuser on every login if the username is listed, or if the
# groups claim contains one of the groups. Never revoked here.
"admin_users": e("ADMIN_USERS", [], env.list),
+3 -2
View File
@@ -3,13 +3,14 @@
# Funkwhale front image hook: copy into /docker-entrypoint.d/ (e.g. as
# 98-funkwhale-oidc.sh). Adds oidc-login.{js,css} to index.html once.
# Expects the two files under /usr/share/nginx/html/oidc/ and reads
# FUNKWHALE_OIDC_PROVIDER_ID, FUNKWHALE_OIDC_NAME, FUNKWHALE_OIDC_AUTO_REDIRECT.
# FUNKWHALE_OIDC_PROVIDER_ID, FUNKWHALE_OIDC_NAME, FUNKWHALE_OIDC_AUTO_REDIRECT
# and FUNKWHALE_OIDC_LOCAL_LOGIN.
set -eu
index=/usr/share/nginx/html/index.html
grep -q 'oidc-login.js' "$index" && exit 0
esc() { printf '%s' "$1" | sed -e 's/&/\&amp;/g' -e 's/"/\&quot;/g' -e 's/</\&lt;/g' -e 's/[|\\]/\\&/g'; }
url="/accounts/oidc/$(esc "${FUNKWHALE_OIDC_PROVIDER_ID:-oidc}")/login/"
tag="<link rel=\"stylesheet\" href=\"/oidc/oidc-login.css\"><script src=\"/oidc/oidc-login.js\" defer data-login-url=\"$url\" data-name=\"$(esc "${FUNKWHALE_OIDC_NAME:-Single sign-on}")\" data-auto-redirect=\"$(esc "${FUNKWHALE_OIDC_AUTO_REDIRECT:-false}")\"></script>"
tag="<link rel=\"stylesheet\" href=\"/oidc/oidc-login.css\"><script src=\"/oidc/oidc-login.js\" defer data-login-url=\"$url\" data-name=\"$(esc "${FUNKWHALE_OIDC_NAME:-Single sign-on}")\" data-auto-redirect=\"$(esc "${FUNKWHALE_OIDC_AUTO_REDIRECT:-false}")\" data-local-login=\"$(esc "${FUNKWHALE_OIDC_LOCAL_LOGIN:-true}")\"></script>"
sed -i "s|</head>|$tag</head>|" "$index"
echo "funkwhale-oidc: login button added to $index"
+4
View File
@@ -8,3 +8,7 @@
.funkwhale-oidc-divider::before, .funkwhale-oidc-divider::after {
content: ""; flex: 1; border-top: 1px solid currentColor; opacity: 0.4;
}
/* FUNKWHALE_OIDC_LOCAL_LOGIN=false: only the provider button remains. */
.funkwhale-oidc-only form[data-oidc] > :not(.funkwhale-oidc),
.funkwhale-oidc-only form[data-oidc] ~ *,
.funkwhale-oidc-only a[href="/signup"] { display: none !important; }
+27 -12
View File
@@ -5,20 +5,28 @@
// data-login-url="/accounts/oidc/oidc/login/" data-name="Single sign-on"
// data-auto-redirect="false"></script>
// With data-auto-redirect="true", /login goes straight to the provider;
// /login?local=1 still shows the password form.
// /login?local=1 still shows the password form, unless
// data-local-login="false", which hides it everywhere and sends /login and
// /signup straight to the provider.
(() => {
const script = document.currentScript
const cfg = {
loginUrl: script?.dataset.loginUrl || '/accounts/oidc/oidc/login/',
name: script?.dataset.name || 'Single sign-on',
autoRedirect: script?.dataset.autoRedirect === 'true'
autoRedirect: script?.dataset.autoRedirect === 'true',
localLogin: script?.dataset.localLogin !== 'false'
}
if (!cfg.localLogin) {
document.documentElement.classList.add('funkwhale-oidc-only')
}
const nextPath = () => {
const params = new URLSearchParams(location.search)
const next = params.get('next') || (location.pathname === '/login' ? '/library' : location.pathname + location.search)
// Only same-site paths; allauth checks this again server-side.
return next.startsWith('/') && !next.startsWith('//') ? next : '/library'
const onAuthPage = /^\/(login|signup)\/?$/.test(location.pathname)
const next = params.get('next') || (onAuthPage ? '/library' : location.pathname + location.search)
// Same-site paths only (allauth checks this again), and never back to
// /login or /signup, which would redirect to the provider again.
return next.startsWith('/') && !next.startsWith('//') && !/^\/(login|signup)\b/.test(next) ? next : '/library'
}
const go = () => {
@@ -26,8 +34,13 @@
}
const maybeRedirect = () => {
if (!cfg.autoRedirect || location.pathname !== '/login') return false
if (new URLSearchParams(location.search).has('local')) return false
const path = location.pathname.replace(/\/$/, '')
if (!cfg.localLogin) {
if (path !== '/login' && path !== '/signup') return false
} else {
if (!cfg.autoRedirect || path !== '/login') return false
if (new URLSearchParams(location.search).has('local')) return false
}
go()
return true
}
@@ -46,13 +59,15 @@
button.textContent = `Log in with ${cfg.name}`
button.addEventListener('click', go)
const divider = document.createElement('p')
divider.className = 'funkwhale-oidc-divider'
divider.textContent = 'or use a local account'
const wrap = document.createElement('div')
wrap.className = 'funkwhale-oidc'
wrap.append(button, divider)
wrap.append(button)
if (cfg.localLogin) {
const divider = document.createElement('p')
divider.className = 'funkwhale-oidc-divider'
divider.textContent = 'or use a local account'
wrap.append(divider)
}
form.prepend(wrap)
}
+5
View File
@@ -22,3 +22,8 @@ def add_apps(apps, **kwargs):
"allauth.socialaccount.providers.openid_connect",
"funkwhale_oidc.apps.FunkwhaleOIDCConfig",
]
@plugins.register_filter(plugins.MIDDLEWARES_BEFORE, PLUGIN)
def add_middlewares(middlewares, **kwargs):
return middlewares + ["funkwhale_oidc.middleware.LocalLoginMiddleware"]
+38
View File
@@ -0,0 +1,38 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Turns off password login when FUNKWHALE_OIDC_LOCAL_LOGIN=false."""
import json
import re
from django import http
from . import conf
# Password login, sign-up and password reset/change, in the API, allauth's
# HTML views and the Django admin. The OIDC routes (/accounts/oidc/,
# allauth's /accounts/3rdparty/ sign-up step) and logout stay open.
BLOCKED = re.compile(
r"^/api/v\d+/(users/)?users/login/?$"
r"|^/api/v\d+/auth/(password|registration)/"
r"|^/api/admin/login/"
r"|^/accounts/(?!oidc/|3rdparty/|social/|logout/)"
)
class LocalLoginMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if not conf.get()["local_login"] and BLOCKED.match(request.path):
if request.path.startswith("/api/") and request.method != "GET":
return http.HttpResponse(
json.dumps({"detail": "Local login is disabled; log in with " + conf.get()["name"] + "."}),
status=403,
content_type="application/json",
)
# Browsers landing on a password page go to the provider instead.
return http.HttpResponseRedirect(
f"/accounts/oidc/{conf.get()['provider_id']}/login/?process=login&next=/"
)
return self.get_response(request)
+2
View File
@@ -33,6 +33,7 @@ x-api: &api
FUNKWHALE_OIDC_NAME: Dex
FUNKWHALE_OIDC_ADMIN_USERS: alice
FUNKWHALE_OIDC_LINK_BY_EMAIL: "true"
FUNKWHALE_OIDC_LOCAL_LOGIN: ${LOCAL_LOGIN:-true}
services:
postgres:
image: postgres:17.11-alpine
@@ -82,6 +83,7 @@ services:
FUNKWHALE_OIDC_PROVIDER_ID: dex
FUNKWHALE_OIDC_NAME: Dex
FUNKWHALE_OIDC_AUTO_REDIRECT: "false"
FUNKWHALE_OIDC_LOCAL_LOGIN: ${LOCAL_LOGIN:-true}
volumes:
- ../funkwhale_oidc/front/inject.sh:/docker-entrypoint.d/98-funkwhale-oidc.sh:ro
- ../funkwhale_oidc/front/oidc-login.js:/usr/share/nginx/html/oidc/oidc-login.js:ro