authenticate_cram method

Future<bool> authenticate_cram(
  1. dynamic secret
)

Generates digest using from verb response and secret and performs a CRAM authentication to secondary server

Implementation

Future<bool> authenticate_cram(var secret) async {
  secret ??= cramSecret;
  if (secret == null) {
    throw UnAuthenticatedException('Cram secret not passed');
  }
  await _sendCommand('from:$_currentAtSign\n');
  var fromResponse = await messageListener.read();
  logger.info('from result:$fromResponse');
  if (fromResponse == null) {
    return false;
  }
  fromResponse = fromResponse.trim().replaceAll('data:', '');
  var digestInput = '$secret$fromResponse';
  var bytes = utf8.encode(digestInput);
  var digest = sha512.convert(bytes);
  await _sendCommand('cram:$digest\n');
  var cramResponse = await messageListener.read();
  if (cramResponse == 'data:success') {
    logger.info('auth success');
    _isCramAuthenticated = true;
  } else {
    throw UnAuthenticatedException('Auth failed');
  }
  return _isCramAuthenticated;
}