AuthBasicCredentials parse(String authorizationHeader)

Returns a AuthBasicCredentials containing the username and password base64 encoded in authorizationHeader. For example, if the input to this method was 'Basic base64String' it would decode the base64String and return the username and password by splitting that decoded string around the character ':'.

If authorizationHeader is malformed or null, throws an AuthorizationParserException.

Source

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

  var matcher = new RegExp("Basic (.+)");
  var match = matcher.firstMatch(authorizationHeader);
  if (match == null) {
    throw new AuthorizationParserException(
        AuthorizationParserExceptionReason.malformed);
  }

  var base64String = match[1];
  String decodedCredentials;
  try {
    decodedCredentials =
        new String.fromCharCodes(new Base64Decoder().convert(base64String));
  } catch (e) {
    throw new AuthorizationParserException(
        AuthorizationParserExceptionReason.malformed);
  }

  var splitCredentials = decodedCredentials.split(":");
  if (splitCredentials.length != 2) {
    throw new AuthorizationParserException(
        AuthorizationParserExceptionReason.malformed);
  }

  return new AuthBasicCredentials()
    ..username = splitCredentials.first
    ..password = splitCredentials.last;
}