authenticate method
Generates digest using from verb response and privateKey
and performs a PKAM authentication to
secondary server. This method is executed for all verbs that requires authentication.
Implementation
Future<bool> authenticate(String? privateKey) async {
if (privateKey == null) {
throw UnAuthenticatedException('Private key not passed');
}
await _sendCommand('from:$_currentAtSign\n');
var fromResponse = await (messageListener.read());
logger.finer('from result:$fromResponse');
if (fromResponse == null) {
return false;
}
fromResponse = fromResponse.trim().replaceAll('data:', '');
logger.finer('fromResponse $fromResponse');
var key = RSAPrivateKey.fromString(privateKey);
var sha256signature =
key.createSHA256Signature(utf8.encode(fromResponse) as Uint8List);
var signature = base64Encode(sha256signature);
logger.finer('Sending command pkam:$signature');
await _sendCommand('pkam:$signature\n');
var pkamResponse = await messageListener.read();
if (pkamResponse == 'data:success') {
logger.info('auth success');
_isPkamAuthenticated = true;
} else {
throw UnAuthenticatedException('Auth failed');
}
return _isPkamAuthenticated;
}