willSendResponse method Null safety

  1. @override
void willSendResponse(
  1. Response response
)
override

Executed prior to Response being sent.

This method is used to post-process response just before it is sent. By default, does nothing. The response may be altered prior to being sent. This method will be executed for all requests, including server errors.

Implementation

@override
void willSendResponse(Response response) {
  if (response.statusCode == 400) {
    // This post-processes the response in the case that duplicate parameters
    // were in the request, which violates oauth2 spec. It just adjusts the error message.
    // This could be hardened some.
    final body = response.body;
    if (body != null && body["error"] is String) {
      final errorMessage = body["error"] as String;
      if (errorMessage.startsWith("multiple values")) {
        response.body = {
          "error":
              AuthServerException.errorString(AuthRequestError.invalidRequest)
        };
      }
    }
  }
}