authenticate method

  1. @override
Future<Principal?> authenticate(
  1. HubRequest request
)
override

Authenticates request. See the class contract for the return/throw semantics.

Implementation

@override
Future<Principal?> authenticate(HubRequest request) async {
  final header = request.header('authorization');
  if (header == null) return null;
  final parts = header.split(' ');
  if (parts.length != 2 || parts[0].toLowerCase() != 'basic') return null;

  final String decoded;
  try {
    decoded = utf8.decode(base64.decode(parts[1]));
  } on Object {
    throw const UnauthorizedException('Malformed basic credentials');
  }
  final sep = decoded.indexOf(':');
  if (sep < 0) {
    throw const UnauthorizedException('Malformed basic credentials');
  }

  final principal = await _resolve(
    decoded.substring(0, sep),
    decoded.substring(sep + 1),
  );
  if (principal == null) {
    throw const UnauthorizedException('Invalid credentials');
  }
  return principal;
}