authenticate static method
Future<ActivateDeviceResponse>
authenticate(
- AuthConfig authConfig,
- ActivateDeviceConfig activateDeviceConfig,
- dynamic onComplete()
Implementation
static Future<ActivateDeviceResponse> authenticate(
AuthConfig authConfig,
ActivateDeviceConfig activateDeviceConfig,
Function(bool) onComplete) async {
try {
final prefs = await SharedPreferences.getInstance();
final activateResp = prefs.getString('activateResp') ?? '';
if (activateResp.trim().isEmpty) {
AuthResponse authResponse = await _authenticate(authConfig);
await prefs.setString('authResp', json.encode(authResponse.toJson()));
ActivateDeviceResponse activateDeviceResponse = await _activateDevice(
activateDeviceConfig, authResponse.accessToken!);
await prefs.setString(
'activateResp', json.encode(activateDeviceResponse.toJson()));
final now = DateTime.now();
String totpCode = OTP.generateTOTPCodeString(
activateDeviceResponse.key!, now.millisecondsSinceEpoch,
algorithm: Algorithm.SHA1,
length: activateDeviceResponse.length!,
interval: activateDeviceResponse.duration!,
isGoogle: true);
bool confirmDevice = await _confirmDevice(
activateDeviceConfig.uid ?? "",
activateDeviceConfig.username!,
totpCode,
authResponse.accessToken!);
await prefs.setBool('confirmDevice', confirmDevice);
if (confirmDevice) {
onComplete(true);
return activateDeviceResponse;
} else {
onComplete(false);
throw Exception("Service failure authenticate()");
}
} else {
onComplete(true);
return ActivateDeviceResponse.fromJson(json.decode(activateResp));
}
} catch (e) {
onComplete(false);
throw Exception("Service failure authenticate()");
}
}