handle method Null safety

  1. @override
FutureOr<RequestOrResponse> handle(
  1. Request request
)
override

The primary request handling method of this object.

Subclasses implement this method to provide their request handling logic.

If this method returns a Response, it will be sent as the response for request linked controllers will not handle it.

If this method returns request, the linked controller handles the request.

If this method returns null, request is not passed to any other controller and is not responded to. You must respond to request through Request.raw.

Implementation

@override
FutureOr<RequestOrResponse> handle(Request request) async {
  final authData = request.raw.headers.value(HttpHeaders.authorizationHeader);
  if (authData == null) {
    return Response.unauthorized();
  }

  try {
    final value = parser.parse(authData);
    request.authorization =
        await validator!.validate(parser, value, requiredScope: scopes);
    if (request.authorization == null) {
      return Response.unauthorized();
    }

    _addScopeRequirementModifier(request);
  } on AuthorizationParserException catch (e) {
    return _responseFromParseException(e);
  } on AuthServerException catch (e) {
    if (e.reason == AuthRequestError.invalidScope) {
      return Response.forbidden(
        body: {
          "error": "insufficient_scope",
          "scope": scopes!.map((s) => s.toString()).join(" ")
        },
      );
    }

    return Response.unauthorized();
  }

  return request;
}