parse method Null safety

  1. @override
String? parse(
  1. String? authorizationHeader
)
override

Parses a Bearer token from authorizationHeader. If the header is malformed or doesn't exist, throws an AuthorizationParserException. Otherwise, returns the String representation of the bearer token.

For example, if the input to this method is "Bearer token" it would return 'token'.

If authorizationHeader is malformed or null, throws an AuthorizationParserException.

Implementation

@override
String? parse(String? authorizationHeader) {
  if (authorizationHeader == null) {
    throw AuthorizationParserException(
        AuthorizationParserExceptionReason.missing);
  }

  final matcher = RegExp("Bearer (.+)");
  final match = matcher.firstMatch(authorizationHeader);
  if (match == null) {
    throw AuthorizationParserException(
        AuthorizationParserExceptionReason.malformed);
  }
  return match[1];
}