Skip to content

Commit

Permalink
Pass JSON request body as a dictionary input to LoginForm
Browse files Browse the repository at this point in the history
This allows to use the consume the form validation logic when
the login request is sent as JSON
  • Loading branch information
sbesson committed Feb 17, 2025
1 parent 00fe304 commit 42790d5
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions omeroweb/webgateway/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3474,31 +3474,21 @@ def post(self, request, api_version=None):
and store that on the request.session OR handling login failures
"""
error = None
username = None
username = None
server_id = None
new_connection = False
if len(request.POST) > 0:
# Authentication posted using form data
form = self.form_class(request.POST.copy())
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
server_id = form.cleaned_data["server"]
new_connection = True
else:
# Authentication posted using JSON data
if request.content_type == "application/json":
try:
payload = json.loads(request.body)
username = payload.get("username")
password = payload.get("password")
server_id = payload.get("server")
new_connection = True
form = self.form_class(dict(payload))
except Exception:
logger.debug(f"Invalid JSON data: {request.body}")
form = self.form_class(dict(payload))
else:
form = self.form_class(request.POST.copy())

userip = get_client_ip(request)
if new_connection:
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
server_id = form.cleaned_data["server"]
is_secure = settings.SECURE

connector = Connector(server_id, is_secure)
Expand Down

0 comments on commit 42790d5

Please sign in to comment.