code.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. String issuerName = uri.queryParameters['issuer']!;
  74. // Handle issuer name with period
  75. // See https://github.com/ente-io/auth/pull/77
  76. if (issuerName.contains("period=")) {
  77. return issuerName.substring(0, issuerName.indexOf("period="));
  78. }
  79. }
  80. final String path = Uri.decodeComponent(uri.path);
  81. return path.split(':')[0].substring(1);
  82. } catch (e) {
  83. return "";
  84. }
  85. }
  86. static int _getDigits(Uri uri) {
  87. try {
  88. return int.parse(uri.queryParameters['digits']!);
  89. } catch (e) {
  90. return defaultDigits;
  91. }
  92. }
  93. static int _getPeriod(Uri uri) {
  94. try {
  95. return int.parse(uri.queryParameters['period']!);
  96. } catch (e) {
  97. return defaultPeriod;
  98. }
  99. }
  100. static Algorithm _getAlgorithm(Uri uri) {
  101. try {
  102. final algorithm =
  103. uri.queryParameters['algorithm'].toString().toLowerCase();
  104. if (algorithm == "sha256") {
  105. return Algorithm.sha256;
  106. } else if (algorithm == "sha512") {
  107. return Algorithm.sha512;
  108. }
  109. } catch (e) {
  110. // nothing
  111. }
  112. return Algorithm.sha1;
  113. }
  114. static Type _getType(Uri uri) {
  115. if (uri.host == "totp") {
  116. return Type.totp;
  117. } else if (uri.host == "hotp") {
  118. return Type.hotp;
  119. }
  120. throw UnsupportedError("Unsupported format with host ${uri.host}");
  121. }
  122. @override
  123. bool operator ==(Object other) {
  124. if (identical(this, other)) return true;
  125. return other is Code &&
  126. other.account == account &&
  127. other.issuer == issuer &&
  128. other.digits == digits &&
  129. other.period == period &&
  130. other.secret == secret &&
  131. other.type == type &&
  132. other.rawData == rawData;
  133. }
  134. @override
  135. int get hashCode {
  136. return account.hashCode ^
  137. issuer.hashCode ^
  138. digits.hashCode ^
  139. period.hashCode ^
  140. secret.hashCode ^
  141. type.hashCode ^
  142. rawData.hashCode;
  143. }
  144. }
  145. enum Type {
  146. totp,
  147. hotp,
  148. }
  149. enum Algorithm {
  150. sha1,
  151. sha256,
  152. sha512,
  153. }