String parse(String authorizationHeader)

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.

Source

static String parse(String authorizationHeader) {
  if (authorizationHeader == null) {
    throw new AuthorizationParserException(
        AuthorizationParserExceptionReason.missing);
  }

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