code.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import 'package:ente_auth/utils/totp_util.dart';
  2. class Code {
  3. static const defaultDigits = 6;
  4. static const defaultPeriod = 30;
  5. int? generatedID;
  6. final String account;
  7. final String issuer;
  8. final int digits;
  9. final int period;
  10. final String secret;
  11. final Algorithm algorithm;
  12. final Type type;
  13. final String rawData;
  14. bool? hasSynced;
  15. Code(
  16. this.account,
  17. this.issuer,
  18. this.digits,
  19. this.period,
  20. this.secret,
  21. this.algorithm,
  22. this.type,
  23. this.rawData, {
  24. this.generatedID,
  25. });
  26. static Code fromAccountAndSecret(
  27. String account,
  28. String issuer,
  29. String secret,
  30. ) {
  31. return Code(
  32. account,
  33. issuer,
  34. defaultDigits,
  35. defaultPeriod,
  36. secret,
  37. Algorithm.sha1,
  38. Type.totp,
  39. "otpauth://totp/" +
  40. issuer +
  41. ":" +
  42. account +
  43. "?algorithm=SHA1&digits=6&issuer=" +
  44. issuer +
  45. "&period=30&secret=" +
  46. secret,
  47. );
  48. }
  49. static Code fromRawData(String rawData) {
  50. Uri uri = Uri.parse(rawData);
  51. return Code(
  52. _getAccount(uri),
  53. _getIssuer(uri),
  54. _getDigits(uri),
  55. _getPeriod(uri),
  56. getSanitizedSecret(uri.queryParameters['secret']!),
  57. _getAlgorithm(uri),
  58. _getType(uri),
  59. rawData,
  60. );
  61. }
  62. static String _getAccount(Uri uri) {
  63. try {
  64. final String path = Uri.decodeComponent(uri.path);
  65. return path.split(':')[1];
  66. } catch (e) {
  67. return "";
  68. }
  69. }
  70. static String _getIssuer(Uri uri) {
  71. try {
  72. if (uri.queryParameters.containsKey("issuer")) {
  73. return uri.queryParameters['issuer']!;
  74. }
  75. final String path = Uri.decodeComponent(uri.path);
  76. return path.split(':')[0].substring(1);
  77. } catch (e) {
  78. return "";
  79. }
  80. }
  81. static int _getDigits(Uri uri) {
  82. try {
  83. return int.parse(uri.queryParameters['digits']!);
  84. } catch (e) {
  85. return defaultDigits;
  86. }
  87. }
  88. static int _getPeriod(Uri uri) {
  89. try {
  90. return int.parse(uri.queryParameters['period']!);
  91. } catch (e) {
  92. return defaultPeriod;
  93. }
  94. }
  95. static Algorithm _getAlgorithm(Uri uri) {
  96. try {
  97. final algorithm =
  98. uri.queryParameters['algorithm'].toString().toLowerCase();
  99. if (algorithm == "sha256") {
  100. return Algorithm.sha256;
  101. } else if (algorithm == "sha512") {
  102. return Algorithm.sha512;
  103. }
  104. } catch (e) {
  105. // nothing
  106. }
  107. return Algorithm.sha1;
  108. }
  109. static Type _getType(Uri uri) {
  110. if (uri.host == "totp") {
  111. return Type.totp;
  112. } else if (uri.host == "hotp") {
  113. return Type.hotp;
  114. }
  115. throw UnsupportedError("Unsupported format with host ${uri.host}");
  116. }
  117. @override
  118. bool operator ==(Object other) {
  119. if (identical(this, other)) return true;
  120. return other is Code &&
  121. other.account == account &&
  122. other.issuer == issuer &&
  123. other.digits == digits &&
  124. other.period == period &&
  125. other.secret == secret &&
  126. other.type == type &&
  127. other.rawData == rawData;
  128. }
  129. @override
  130. int get hashCode {
  131. return account.hashCode ^
  132. issuer.hashCode ^
  133. digits.hashCode ^
  134. period.hashCode ^
  135. secret.hashCode ^
  136. type.hashCode ^
  137. rawData.hashCode;
  138. }
  139. }
  140. enum Type {
  141. totp,
  142. hotp,
  143. }
  144. enum Algorithm {
  145. sha1,
  146. sha256,
  147. sha512,
  148. }