auth method
Implementation
Future<String> auth(UdpAddress? remoteConnectionPoint) async {
if (authProcessing) {
return "auth processing ...";
}
try {
authProcessing = true;
var getNonceResult = await regularCall(
remoteConnectionPoint, "/xchg-get-nonce", Uint8List(0), Uint8List(0));
if (getNonceResult.isError()) {
authProcessing = false;
return "get nonce error:" + getNonceResult.error;
}
if (getNonceResult.data.length != 16) {
authProcessing = false;
return "nonce != 16";
}
if (remotePublicKey == null) {
authProcessing = false;
return "remotePublicKey == null";
}
var authDataBS = utf8.encode(authData);
var localPublicKeyBS = encodePublicKeyToPKIX(keyPair.publicKey);
// Prepare auth frame
Uint8List authFrameSecret = Uint8List(16 + authDataBS.length);
copyBytes(authFrameSecret, 0, getNonceResult.data);
copyBytes(authFrameSecret, 16, Uint8List.fromList(authDataBS));
// Encrypt auth frame
Uint8List encryptedAuthFrame =
await rsaEncrypt(remotePublicKey!, authFrameSecret);
Uint8List authFrame =
Uint8List(4 + localPublicKeyBS.length + encryptedAuthFrame.length);
authFrame.buffer.asUint32List(0)[0] =
localPublicKeyBS.buffer.lengthInBytes;
copyBytes(authFrame, 4, localPublicKeyBS);
copyBytes(authFrame, 4 + localPublicKeyBS.length, encryptedAuthFrame);
CallResult authResult = await regularCall(
remoteConnectionPoint, "/xchg-auth", authFrame, Uint8List(0));
if (authResult.isError()) {
authProcessing = false;
return "auth error-:" + authResult.error;
}
Uint8List authResultDecrypted =
await rsaDecrypt(keyPair.privateKey, authResult.data);
if (authResultDecrypted.length != 8 + 32) {
authProcessing = false;
return "authResultDecrypted.length != 8 + 32";
}
sessionId = authResultDecrypted.buffer.asInt64List(0)[0];
aesKey = authResultDecrypted.sublist(8);
} catch (ex) {
print("******************** auth Exception ee: $ex");
authProcessing = false;
}
authProcessing = false;
return "";
}