code.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import 'package:ente_auth/models/code_display.dart';
  2. import 'package:ente_auth/utils/totp_util.dart';
  3. import 'package:flutter/foundation.dart';
  4. class Code {
  5. static const defaultDigits = 6;
  6. static const defaultPeriod = 30;
  7. int? generatedID;
  8. final String account;
  9. final String issuer;
  10. final int digits;
  11. final int period;
  12. final String secret;
  13. final Algorithm algorithm;
  14. final Type type;
  15. /// otpauth url in the code
  16. final String rawData;
  17. final int counter;
  18. bool? hasSynced;
  19. final CodeDisplay? display;
  20. bool get isPinned => display?.pinned ?? false;
  21. Code(
  22. this.account,
  23. this.issuer,
  24. this.digits,
  25. this.period,
  26. this.secret,
  27. this.algorithm,
  28. this.type,
  29. this.counter,
  30. this.rawData, {
  31. this.generatedID,
  32. this.display,
  33. });
  34. Code copyWith({
  35. String? account,
  36. String? issuer,
  37. int? digits,
  38. int? period,
  39. String? secret,
  40. Algorithm? algorithm,
  41. Type? type,
  42. int? counter,
  43. CodeDisplay? display,
  44. }) {
  45. final String updateAccount = account ?? this.account;
  46. final String updateIssuer = issuer ?? this.issuer;
  47. final int updatedDigits = digits ?? this.digits;
  48. final int updatePeriod = period ?? this.period;
  49. final String updatedSecret = secret ?? this.secret;
  50. final Algorithm updatedAlgo = algorithm ?? this.algorithm;
  51. final Type updatedType = type ?? this.type;
  52. final int updatedCounter = counter ?? this.counter;
  53. final CodeDisplay? updatedDisplay = display ?? this.display;
  54. return Code(
  55. updateAccount,
  56. updateIssuer,
  57. updatedDigits,
  58. updatePeriod,
  59. updatedSecret,
  60. updatedAlgo,
  61. updatedType,
  62. updatedCounter,
  63. "otpauth://${updatedType.name}/$updateIssuer:$updateAccount?algorithm=${updatedAlgo.name}&digits=$updatedDigits&issuer=$updateIssuer&period=$updatePeriod&secret=$updatedSecret${updatedType == Type.hotp ? "&counter=$updatedCounter" : ""}",
  64. generatedID: generatedID,
  65. display: updatedDisplay,
  66. );
  67. }
  68. static Code fromAccountAndSecret(
  69. String account,
  70. String issuer,
  71. String secret,
  72. ) {
  73. return Code(
  74. account,
  75. issuer,
  76. defaultDigits,
  77. defaultPeriod,
  78. secret,
  79. Algorithm.sha1,
  80. Type.totp,
  81. 0,
  82. "otpauth://totp/$issuer:$account?algorithm=SHA1&digits=6&issuer=$issuer&period=30&secret=$secret",
  83. );
  84. }
  85. static Code fromOTPAuthUrl(String rawData, {CodeDisplay? display}) {
  86. Uri uri = Uri.parse(rawData);
  87. try {
  88. return Code(
  89. _getAccount(uri),
  90. _getIssuer(uri),
  91. _getDigits(uri),
  92. _getPeriod(uri),
  93. getSanitizedSecret(uri.queryParameters['secret']!),
  94. _getAlgorithm(uri),
  95. _getType(uri),
  96. _getCounter(uri),
  97. rawData,
  98. );
  99. } catch (e) {
  100. // if account name contains # without encoding,
  101. // rest of the url are treated as url fragment
  102. if (rawData.contains("#")) {
  103. return Code.fromOTPAuthUrl(rawData.replaceAll("#", '%23'));
  104. } else {
  105. rethrow;
  106. }
  107. }
  108. }
  109. static String _getAccount(Uri uri) {
  110. try {
  111. String path = Uri.decodeComponent(uri.path);
  112. if (path.startsWith("/")) {
  113. path = path.substring(1, path.length);
  114. }
  115. // Parse account name from documented auth URI
  116. // otpauth://totp/ACCOUNT?secret=SUPERSECRET&issuer=SERVICE
  117. if (uri.queryParameters.containsKey("issuer") && !path.contains(":")) {
  118. return path;
  119. }
  120. return path.split(':')[1];
  121. } catch (e) {
  122. return "";
  123. }
  124. }
  125. static Code fromExportJson(Map rawJson) {
  126. try {
  127. Code resultCode = Code.fromOTPAuthUrl(
  128. rawJson['rawData'],
  129. display: CodeDisplay.fromJson(rawJson['display']),
  130. );
  131. return resultCode;
  132. } catch (e) {
  133. debugPrint("Failed to parse code from export json $e");
  134. rethrow;
  135. }
  136. }
  137. Map<String, dynamic> toExportJson() {
  138. return {
  139. 'rawData': rawData,
  140. 'display': display?.toJson(),
  141. };
  142. }
  143. static String _getIssuer(Uri uri) {
  144. try {
  145. if (uri.queryParameters.containsKey("issuer")) {
  146. String issuerName = uri.queryParameters['issuer']!;
  147. // Handle issuer name with period
  148. // See https://github.com/ente-io/ente/pull/77
  149. if (issuerName.contains("period=")) {
  150. return issuerName.substring(0, issuerName.indexOf("period="));
  151. }
  152. return issuerName;
  153. }
  154. final String path = Uri.decodeComponent(uri.path);
  155. return path.split(':')[0].substring(1);
  156. } catch (e) {
  157. return "";
  158. }
  159. }
  160. static int _getDigits(Uri uri) {
  161. try {
  162. return int.parse(uri.queryParameters['digits']!);
  163. } catch (e) {
  164. return defaultDigits;
  165. }
  166. }
  167. static int _getPeriod(Uri uri) {
  168. try {
  169. return int.parse(uri.queryParameters['period']!);
  170. } catch (e) {
  171. return defaultPeriod;
  172. }
  173. }
  174. static int _getCounter(Uri uri) {
  175. try {
  176. final bool hasCounterKey = uri.queryParameters.containsKey('counter');
  177. if (!hasCounterKey) {
  178. return 0;
  179. }
  180. return int.parse(uri.queryParameters['counter']!);
  181. } catch (e) {
  182. return defaultPeriod;
  183. }
  184. }
  185. static Algorithm _getAlgorithm(Uri uri) {
  186. try {
  187. final algorithm =
  188. uri.queryParameters['algorithm'].toString().toLowerCase();
  189. if (algorithm == "sha256") {
  190. return Algorithm.sha256;
  191. } else if (algorithm == "sha512") {
  192. return Algorithm.sha512;
  193. }
  194. } catch (e) {
  195. // nothing
  196. }
  197. return Algorithm.sha1;
  198. }
  199. static Type _getType(Uri uri) {
  200. if (uri.host == "totp" || uri.host == "steam") {
  201. return Type.totp;
  202. } else if (uri.host == "hotp") {
  203. return Type.hotp;
  204. }
  205. throw UnsupportedError("Unsupported format with host ${uri.host}");
  206. }
  207. @override
  208. bool operator ==(Object other) {
  209. if (identical(this, other)) return true;
  210. return other is Code &&
  211. other.account == account &&
  212. other.issuer == issuer &&
  213. other.digits == digits &&
  214. other.period == period &&
  215. other.secret == secret &&
  216. other.counter == counter &&
  217. other.type == type &&
  218. other.rawData == rawData;
  219. }
  220. @override
  221. int get hashCode {
  222. return account.hashCode ^
  223. issuer.hashCode ^
  224. digits.hashCode ^
  225. period.hashCode ^
  226. secret.hashCode ^
  227. type.hashCode ^
  228. counter.hashCode ^
  229. rawData.hashCode ^
  230. display.hashCode;
  231. }
  232. }
  233. enum Type {
  234. totp,
  235. hotp,
  236. }
  237. enum Algorithm {
  238. sha1,
  239. sha256,
  240. sha512,
  241. }