extract method

  1. @override
Future<Result<BasicCredentials, Rejection>> extract(
  1. Request request
)
override

Produces the value, or the rejection that stops the handler.

Implementation

@override
Future<Result<BasicCredentials, Rejection>> extract(Request request) async {
  final header = Authorization.of(request);
  if (header == null || !header.isScheme('basic')) return Err(_challenge);

  final String decoded;
  try {
    decoded = utf8.decode(base64.decode(header.credentials));
  } on FormatException {
    return Err(_challenge);
  }

  // Only the first colon separates; a password may contain more.
  final separator = decoded.indexOf(':');
  if (separator < 1) return Err(_challenge);

  return Ok(
    BasicCredentials(
      decoded.substring(0, separator),
      decoded.substring(separator + 1),
    ),
  );
}