2021-11-19 17:05:57 +03:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
|
|
|
2022-05-25 19:24:45 +03:00
|
|
|
import '../core/models.dart';
|
|
|
|
|
2021-11-19 17:05:57 +03:00
|
|
|
part 'models.freezed.dart';
|
|
|
|
part 'models.g.dart';
|
|
|
|
|
2022-01-28 19:05:05 +03:00
|
|
|
const defaultPeriod = 30;
|
|
|
|
const defaultDigits = 6;
|
|
|
|
const defaultCounter = 0;
|
|
|
|
const defaultOathType = OathType.totp;
|
|
|
|
const defaultHashAlgorithm = HashAlgorithm.sha1;
|
|
|
|
|
2021-11-19 17:05:57 +03:00
|
|
|
enum HashAlgorithm {
|
|
|
|
@JsonValue(0x01)
|
2022-06-07 15:12:41 +03:00
|
|
|
sha1('SHA-1'),
|
2021-11-19 17:05:57 +03:00
|
|
|
@JsonValue(0x02)
|
2022-06-07 15:12:41 +03:00
|
|
|
sha256('SHA-256'),
|
2021-11-19 17:05:57 +03:00
|
|
|
@JsonValue(0x03)
|
2022-06-07 15:12:41 +03:00
|
|
|
sha512('SHA-512');
|
|
|
|
|
|
|
|
final String displayName;
|
|
|
|
const HashAlgorithm(this.displayName);
|
2021-11-19 17:05:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
enum OathType {
|
|
|
|
@JsonValue(0x10)
|
2022-06-07 15:12:41 +03:00
|
|
|
hotp('Counter based'),
|
2021-11-19 17:05:57 +03:00
|
|
|
@JsonValue(0x20)
|
2022-06-07 15:12:41 +03:00
|
|
|
totp('Time based');
|
|
|
|
|
|
|
|
final String displayName;
|
|
|
|
const OathType(this.displayName);
|
2021-11-19 17:05:57 +03:00
|
|
|
}
|
|
|
|
|
2022-02-22 17:22:41 +03:00
|
|
|
enum KeystoreState { unknown, allowed, failed }
|
|
|
|
|
2021-11-19 17:05:57 +03:00
|
|
|
@freezed
|
|
|
|
class OathCredential with _$OathCredential {
|
|
|
|
factory OathCredential(
|
|
|
|
String deviceId,
|
|
|
|
String id,
|
|
|
|
String? issuer,
|
|
|
|
String name,
|
|
|
|
OathType oathType,
|
|
|
|
int period,
|
|
|
|
bool touchRequired) = _OathCredential;
|
|
|
|
|
|
|
|
factory OathCredential.fromJson(Map<String, dynamic> json) =>
|
|
|
|
_$OathCredentialFromJson(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
class OathCode with _$OathCode {
|
|
|
|
factory OathCode(String value, int validFrom, int validTo) = _OathCode;
|
|
|
|
|
|
|
|
factory OathCode.fromJson(Map<String, dynamic> json) =>
|
|
|
|
_$OathCodeFromJson(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
class OathPair with _$OathPair {
|
|
|
|
factory OathPair(OathCredential credential, OathCode? code) = _OathPair;
|
2022-05-06 15:27:33 +03:00
|
|
|
|
|
|
|
factory OathPair.fromJson(Map<String, dynamic> json) =>
|
|
|
|
_$OathPairFromJson(json);
|
2021-11-19 17:05:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
class OathState with _$OathState {
|
2022-02-08 14:25:36 +03:00
|
|
|
factory OathState(
|
2022-05-25 19:24:45 +03:00
|
|
|
String deviceId,
|
|
|
|
Version version, {
|
2022-02-08 14:25:36 +03:00
|
|
|
required bool hasKey,
|
|
|
|
required bool remembered,
|
|
|
|
required bool locked,
|
2022-02-22 17:22:41 +03:00
|
|
|
required KeystoreState keystore,
|
2022-02-08 14:25:36 +03:00
|
|
|
}) = _OathState;
|
2021-11-19 17:05:57 +03:00
|
|
|
|
|
|
|
factory OathState.fromJson(Map<String, dynamic> json) =>
|
|
|
|
_$OathStateFromJson(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
class CredentialData with _$CredentialData {
|
|
|
|
const CredentialData._();
|
|
|
|
|
|
|
|
factory CredentialData({
|
|
|
|
String? issuer,
|
|
|
|
required String name,
|
|
|
|
required String secret,
|
2022-01-28 19:05:05 +03:00
|
|
|
@Default(defaultOathType) OathType oathType,
|
|
|
|
@Default(defaultHashAlgorithm) HashAlgorithm hashAlgorithm,
|
|
|
|
@Default(defaultDigits) int digits,
|
|
|
|
@Default(defaultPeriod) int period,
|
|
|
|
@Default(defaultCounter) int counter,
|
2021-11-19 17:05:57 +03:00
|
|
|
}) = _CredentialData;
|
|
|
|
|
|
|
|
factory CredentialData.fromJson(Map<String, dynamic> json) =>
|
|
|
|
_$CredentialDataFromJson(json);
|
|
|
|
|
2022-02-11 16:56:35 +03:00
|
|
|
factory CredentialData.fromUri(Uri uri) {
|
|
|
|
if (uri.scheme.toLowerCase() != 'otpauth') {
|
|
|
|
throw ArgumentError('Invalid scheme, must be "otpauth://"');
|
|
|
|
}
|
|
|
|
final oathType = OathType.values.byName(uri.host.toLowerCase());
|
|
|
|
final params = uri.queryParameters;
|
|
|
|
String? issuer;
|
|
|
|
String name = uri.pathSegments.join('/');
|
|
|
|
final nameIndex = name.indexOf(':');
|
|
|
|
if (nameIndex >= 0) {
|
|
|
|
issuer = name.substring(0, nameIndex);
|
|
|
|
name = name.substring(nameIndex + 1);
|
|
|
|
}
|
|
|
|
return CredentialData(
|
|
|
|
issuer: params['issuer'] ?? issuer,
|
|
|
|
name: name,
|
|
|
|
oathType: oathType,
|
2022-02-21 14:38:13 +03:00
|
|
|
hashAlgorithm: HashAlgorithm.values
|
|
|
|
.byName(params['algorithm']?.toLowerCase() ?? 'sha1'),
|
2022-02-11 16:56:35 +03:00
|
|
|
secret: params['secret']!,
|
|
|
|
digits: int.tryParse(params['digits'] ?? '') ?? defaultDigits,
|
|
|
|
period: int.tryParse(params['period'] ?? '') ?? defaultPeriod,
|
|
|
|
counter: int.tryParse(params['counter'] ?? '') ?? defaultCounter,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:12:30 +03:00
|
|
|
Uri toUri() => Uri(
|
|
|
|
scheme: 'otpauth',
|
|
|
|
host: oathType.name,
|
|
|
|
path: issuer != null ? '$issuer:$name' : name,
|
|
|
|
queryParameters: {
|
|
|
|
'secret': secret,
|
|
|
|
if (oathType == OathType.totp) 'period': period.toString(),
|
|
|
|
if (oathType == OathType.hotp) 'counter': counter.toString(),
|
|
|
|
if (issuer != null) 'issuer': issuer!,
|
|
|
|
if (digits != 6) 'digits': digits.toString(),
|
|
|
|
if (hashAlgorithm != HashAlgorithm.sha1)
|
|
|
|
'algorithm': hashAlgorithm.name,
|
|
|
|
},
|
|
|
|
);
|
2021-11-19 17:05:57 +03:00
|
|
|
}
|