2022-11-01 06:13:06 +00:00
|
|
|
import 'package:ente_auth/models/code.dart';
|
2023-08-10 06:29:19 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-11-01 06:13:06 +00:00
|
|
|
import 'package:otp/otp.dart' as otp;
|
|
|
|
|
2023-08-01 12:04:58 +00:00
|
|
|
String getOTP(Code code) {
|
|
|
|
if(code.type == Type.hotp) {
|
|
|
|
return _getHOTPCode(code);
|
|
|
|
}
|
2022-11-01 06:13:06 +00:00
|
|
|
return otp.OTP.generateTOTPCodeString(
|
2022-11-11 14:21:54 +00:00
|
|
|
getSanitizedSecret(code.secret),
|
2022-11-01 06:13:06 +00:00
|
|
|
DateTime.now().millisecondsSinceEpoch,
|
|
|
|
length: code.digits,
|
|
|
|
interval: code.period,
|
|
|
|
algorithm: _getAlgorithm(code),
|
|
|
|
isGoogle: true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:04:58 +00:00
|
|
|
String _getHOTPCode(Code code) {
|
|
|
|
return otp.OTP.generateHOTPCodeString(
|
|
|
|
getSanitizedSecret(code.secret),
|
|
|
|
code.counter,
|
|
|
|
length: code.digits,
|
|
|
|
algorithm: _getAlgorithm(code),
|
|
|
|
isGoogle: true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-01 06:13:06 +00:00
|
|
|
String getNextTotp(Code code) {
|
|
|
|
return otp.OTP.generateTOTPCodeString(
|
2022-11-11 14:21:54 +00:00
|
|
|
getSanitizedSecret(code.secret),
|
2022-11-01 06:13:06 +00:00
|
|
|
DateTime.now().millisecondsSinceEpoch + code.period * 1000,
|
|
|
|
length: code.digits,
|
|
|
|
interval: code.period,
|
|
|
|
algorithm: _getAlgorithm(code),
|
|
|
|
isGoogle: true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
otp.Algorithm _getAlgorithm(Code code) {
|
|
|
|
switch (code.algorithm) {
|
|
|
|
case Algorithm.sha256:
|
|
|
|
return otp.Algorithm.SHA256;
|
|
|
|
case Algorithm.sha512:
|
|
|
|
return otp.Algorithm.SHA512;
|
|
|
|
default:
|
|
|
|
return otp.Algorithm.SHA1;
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 14:21:54 +00:00
|
|
|
|
|
|
|
String getSanitizedSecret(String secret) {
|
2023-06-09 09:27:31 +00:00
|
|
|
return secret.toUpperCase().trim().replaceAll(' ', '');
|
2022-11-11 14:21:54 +00:00
|
|
|
}
|
2023-08-10 06:29:19 +00:00
|
|
|
|
|
|
|
String safeDecode(String value) {
|
|
|
|
try {
|
|
|
|
return Uri.decodeComponent(value);
|
|
|
|
} catch (e) {
|
|
|
|
// note: don't log the value, it might contain sensitive information
|
|
|
|
debugPrint("Failed to decode $e");
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|