The Auth Handler

Step 2: The Auth Handler

The first real stage of the pipeline is authentication. AuthHandler inspects the request and decides whether it may continue.

Implement AuthHandler.handle(request):

  • If request.get("user") is None, the request is unauthenticated. Return {"status": 401, "body": "unauthorized"} and do NOT delegate. This short-circuit is the whole point of the pattern: a handler can stop the chain.
  • Otherwise the user is present, so pass the request down the chain by returning super().handle(request).
Hints
Starter code
class Handler:
    def __init__(self):
        self._next = None

    def set_next(self, handler):
        self._next = handler
        return handler

    def handle(self, request):
        if self._next is not None:
            return self._next.handle(request)
        return None


class AuthHandler(Handler):
    def handle(self, request):
        """Reject when no user, otherwise pass down the chain."""
        # TODO: if request.get("user") is None: return {"status": 401, "body": "unauthorized"}
        # TODO: return super().handle(request)
        pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.