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>
39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
# 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)
|