Files
claudeandClaude Opus 5.5 fc3120ee3f OpenID Connect login for Funkwhale as a plugin
Turns on django-allauth's generic openid_connect provider, which
Funkwhale already ships, and wires it to Funkwhale accounts: actor and
libraries on first login, admin by username or group, optional link by
e-mail. The front assets add a login button to the web app and proxy
/accounts/ to the API. Tested end to end against Funkwhale 2.0.13 and
Dex v2.44.0 (tests/compose.yaml).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:14:33 -05:00

83 lines
3.0 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
import logging
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from django.conf import settings
from funkwhale_api.users import models as users_models
from funkwhale_api.users.adapters import FunkwhaleAccountAdapter
from . import conf
logger = logging.getLogger("funkwhale_oidc")
def _oidc_path():
from allauth.socialaccount import app_settings
return f"/accounts/{app_settings.OPENID_CONNECT_URL_PREFIX or ''}".rstrip("/") + "/"
class AccountAdapter(FunkwhaleAccountAdapter):
# Funkwhale answers "noop" so its JSON login API never redirects. After
# the provider's callback there is a browser to send home instead.
def get_login_redirect_url(self, request):
if request.path.startswith(_oidc_path()):
return "/"
return super().get_login_redirect_url(request)
def get_signup_redirect_url(self, request):
if request.path.startswith(_oidc_path()):
return "/"
return super().get_signup_redirect_url(request)
def _claims(sociallogin):
data = sociallogin.account.extra_data or {}
return data.get("userinfo") or data.get("id_token") or data
class SocialAccountAdapter(DefaultSocialAccountAdapter):
def is_open_for_signup(self, request, sociallogin):
return conf.get()["signup"]
def populate_user(self, request, sociallogin, data):
user = super().populate_user(request, sociallogin, data)
claim = conf.get()["username_claim"]
username = _claims(sociallogin).get(claim)
if username:
user.username = username
return user
def save_user(self, request, sociallogin, form=None):
user = super().save_user(request, sociallogin, form)
# Same steps as Funkwhale's own sign-up (users/serializers.py): a
# federation actor and the default libraries.
if not user.actor:
users_models.create_actor(user)
users_models.create_user_libraries(user)
logger.info("Created Funkwhale account %s from OIDC", user.username)
self._grant_admin(user, sociallogin)
return user
def pre_social_login(self, request, sociallogin):
super().pre_social_login(request, sociallogin)
if sociallogin.is_existing:
self._grant_admin(sociallogin.user, sociallogin)
def _grant_admin(self, user, sociallogin):
c = conf.get()
groups = _claims(sociallogin).get(c["groups_claim"]) or []
if isinstance(groups, str):
groups = [groups]
if user.username not in c["admin_users"] and not set(groups) & set(c["admin_groups"]):
return
fields = {"is_staff": True, "is_superuser": True}
fields.update({f"permission_{p}": True for p in users_models.PERMISSIONS})
changed = [k for k, v in fields.items() if getattr(user, k) != v]
if changed:
for k in changed:
setattr(user, k, fields[k])
user.save(update_fields=changed)
logger.info("Granted admin to %s from OIDC", user.username)