ente/lib/models/code.dart

157 lines
3.1 KiB
Dart
Raw Normal View History

2022-11-11 14:21:54 +00:00
import 'package:ente_auth/utils/totp_util.dart';
2022-11-01 06:13:06 +00:00
class Code {
static const defaultDigits = 6;
static const defaultPeriod = 30;
2022-11-22 08:07:09 +00:00
int? generatedID;
2022-11-01 06:13:06 +00:00
final String account;
final String issuer;
final int digits;
final int period;
final String secret;
final Algorithm algorithm;
final Type type;
final String rawData;
bool? hasSynced;
2022-11-01 06:13:06 +00:00
Code(
this.account,
this.issuer,
this.digits,
this.period,
this.secret,
this.algorithm,
this.type,
this.rawData, {
2022-11-22 08:07:09 +00:00
this.generatedID,
2022-11-01 06:13:06 +00:00
});
2022-11-22 08:07:09 +00:00
static Code fromAccountAndSecret(
String account,
String issuer,
String secret,
) {
2022-11-01 06:13:06 +00:00
return Code(
account,
issuer,
2022-11-01 06:13:06 +00:00
defaultDigits,
defaultPeriod,
secret,
Algorithm.sha1,
Type.totp,
"otpauth://totp/" +
issuer +
2022-11-01 06:13:06 +00:00
":" +
account +
"?algorithm=SHA1&digits=6&issuer=" +
issuer +
2022-11-01 06:13:06 +00:00
"period=30&secret=" +
secret,
);
}
static Code fromRawData(String rawData) {
Uri uri = Uri.parse(rawData);
return Code(
_getAccount(uri),
_getIssuer(uri),
_getDigits(uri),
_getPeriod(uri),
2022-11-11 14:21:54 +00:00
getSanitizedSecret(uri.queryParameters['secret']!),
2022-11-01 06:13:06 +00:00
_getAlgorithm(uri),
_getType(uri),
rawData,
);
}
static String _getAccount(Uri uri) {
try {
2023-01-28 12:57:59 +00:00
final String path = Uri.decodeComponent(uri.path);
return path.split(':')[1];
2022-11-01 06:13:06 +00:00
} catch (e) {
return "";
}
}
static String _getIssuer(Uri uri) {
try {
2023-01-28 12:57:59 +00:00
final String path = Uri.decodeComponent(uri.path);
return path.split(':')[0].substring(1);
2022-11-01 06:13:06 +00:00
} catch (e) {
return "";
}
}
static int _getDigits(Uri uri) {
try {
return int.parse(uri.queryParameters['digits']!);
} catch (e) {
return defaultDigits;
}
}
static int _getPeriod(Uri uri) {
try {
return int.parse(uri.queryParameters['period']!);
} catch (e) {
return defaultPeriod;
}
}
static Algorithm _getAlgorithm(Uri uri) {
try {
final algorithm =
uri.queryParameters['algorithm'].toString().toLowerCase();
if (algorithm == "sha256") {
return Algorithm.sha256;
} else if (algorithm == "sha512") {
return Algorithm.sha512;
}
} catch (e) {
// nothing
}
return Algorithm.sha1;
}
static Type _getType(Uri uri) {
return uri.host == "totp" ? Type.totp : Type.hotp;
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Code &&
other.account == account &&
other.issuer == issuer &&
other.digits == digits &&
other.period == period &&
other.secret == secret &&
other.type == type &&
other.rawData == rawData;
}
@override
int get hashCode {
return account.hashCode ^
issuer.hashCode ^
digits.hashCode ^
period.hashCode ^
secret.hashCode ^
type.hashCode ^
rawData.hashCode;
}
}
enum Type {
totp,
hotp,
}
enum Algorithm {
sha1,
sha256,
sha512,
}