Skip to content

Commit

Permalink
Implement back-channel logout endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
boehlke committed Sep 27, 2024
1 parent 54c3b2c commit 27594ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
26 changes: 25 additions & 1 deletion openslides_backend/http/views/action_view.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import binascii
import json
from base64 import b64decode
from pathlib import Path

Expand Down Expand Up @@ -91,7 +92,30 @@ def info_route(self, request: Request) -> RouteResponse:

@route("logout", method="POST", json=False)
def backchannel_logout(self, request: Request) -> RouteResponse:
return {}, None
# topic '<logout>', field 'sessionId', value sessionId
self.logger.debug("Received logout request")
try:
logout_token = request.form.get("logout_token")
if not logout_token:
self.logger.error("Missing logout_token")
raise ServerError("Missing logout_token")

# Verify and decode the logout token
decoded_token = self.services.authentication().auth_handler.verify_logout_token(logout_token)
if decoded_token is None:
return AuthenticationException("Invalid logout token")

# Extract the session ID (sid) from the token
session_id = decoded_token.get("sid")
if not session_id:
return AuthenticationException("Missing session ID (sid) in logout token")

self.logger.debug(f"Session ID to terminate: {session_id}")
self.message_bus.redis.xadd("logout", {"sessionId": session_id})

return { "success": True }, None
except json.JSONDecodeError:
return ServerError("Invalid JSON payload", status=400)

@route("version", method="GET", json=False)
def version_route(self, _: Request) -> RouteResponse:
Expand Down
4 changes: 4 additions & 0 deletions openslides_backend/http/views/base_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ def dispatch(self, request: Request) -> RouteResponse:
if route_options["json"]:
# Check mimetype and parse JSON body. The result is cached in request.json
if not request.is_json:
self.logger.debug(f"Wrong media type {request.content_type}. Use 'Content-Type: application/json' instead.")
raise View400Exception(
"Wrong media type. Use 'Content-Type: application/json' instead."
)
try:
self.logger.debug(f"Unpacking JSON.")
request_body = request.get_json()
except WerkzeugBadRequest as exception:
self.logger.debug(f"Request contains invalid JSON.")
raise View400Exception(exception.description)
self.logger.debug(f"Request contains JSON: {request_body}.")

self.logger.debug(f"Executing handler.")
return func(request)
raise NotFound()

0 comments on commit 27594ea

Please sign in to comment.