configuration.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import 'dart:convert';
  2. import 'package:crypto/crypto.dart';
  3. import 'dart:io' as io;
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:photos/utils/crypto_util.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. class Configuration {
  8. Configuration._privateConstructor();
  9. static final Configuration instance = Configuration._privateConstructor();
  10. static const endpointKey = "endpoint";
  11. static const tokenKey = "token";
  12. static const usernameKey = "username";
  13. static const userIDKey = "user_id";
  14. static const passwordKey = "password";
  15. static const hasOptedForE2EKey = "has_opted_for_e2e_encryption";
  16. static const keyKey = "key";
  17. static const keyEncryptedKey = "encrypted_key";
  18. static final String iv = base64.encode(List.filled(16, 0));
  19. SharedPreferences _preferences;
  20. String _documentsDirectory;
  21. String _tempDirectory;
  22. String _thumbnailsDirectory;
  23. Future<void> init() async {
  24. _preferences = await SharedPreferences.getInstance();
  25. _documentsDirectory = (await getApplicationDocumentsDirectory()).path;
  26. _tempDirectory = _documentsDirectory + "/temp/";
  27. _thumbnailsDirectory = _documentsDirectory + "/thumbnails/";
  28. new io.Directory(_tempDirectory).createSync(recursive: true);
  29. new io.Directory(_thumbnailsDirectory).createSync(recursive: true);
  30. }
  31. Future<void> generateAndSaveKey(String passphrase) async {
  32. final key = CryptoUtil.getBase64EncodedSecureRandomString(length: 32);
  33. await setKey(key);
  34. final hashedPassphrase = sha256.convert(passphrase.codeUnits);
  35. final encryptedKey = CryptoUtil.encryptToBase64(
  36. key, base64.encode(hashedPassphrase.bytes), iv);
  37. await setEncryptedKey(encryptedKey);
  38. }
  39. String getEndpoint() {
  40. return _preferences.getString(endpointKey);
  41. }
  42. String getHttpEndpoint() {
  43. if (getEndpoint() == null) {
  44. return "";
  45. }
  46. return "http://" + getEndpoint() + ":8080";
  47. }
  48. void setEndpoint(String endpoint) async {
  49. await _preferences.setString(endpointKey, endpoint);
  50. }
  51. String getToken() {
  52. return _preferences.getString(tokenKey);
  53. }
  54. void setToken(String token) async {
  55. await _preferences.setString(tokenKey, token);
  56. }
  57. String getUsername() {
  58. return _preferences.getString(usernameKey);
  59. }
  60. void setUsername(String username) async {
  61. await _preferences.setString(usernameKey, username);
  62. }
  63. int getUserID() {
  64. return _preferences.getInt(userIDKey);
  65. }
  66. void setUserID(int userID) async {
  67. await _preferences.setInt(userIDKey, userID);
  68. }
  69. String getPassword() {
  70. return _preferences.getString(passwordKey);
  71. }
  72. void setPassword(String password) async {
  73. await _preferences.setString(passwordKey, password);
  74. }
  75. void setOptInForE2E(bool hasOptedForE2E) async {
  76. await _preferences.setBool(hasOptedForE2EKey, hasOptedForE2E);
  77. }
  78. bool hasOptedForE2E() {
  79. return true;
  80. // return _preferences.getBool(hasOptedForE2EKey);
  81. }
  82. Future<void> setEncryptedKey(String encryptedKey) async {
  83. await _preferences.setString(keyEncryptedKey, encryptedKey);
  84. }
  85. String getEncryptedKey() {
  86. return _preferences.getString(keyEncryptedKey);
  87. }
  88. Future<void> setKey(String key) async {
  89. await _preferences.setString(keyKey, key);
  90. }
  91. Future<void> decryptEncryptedKey(String passphrase) async {
  92. final hashedPassphrase = sha256.convert(passphrase.codeUnits);
  93. final encryptedKey = getEncryptedKey();
  94. final key = CryptoUtil.decryptFromBase64(
  95. encryptedKey, base64.encode(hashedPassphrase.bytes), iv);
  96. await setKey(key);
  97. }
  98. // TODO: Store in secure storage
  99. String getKey() {
  100. // return "8qD++K3xkgjIl3dIsGiTze5PhYtxiS5AtOeZw+Bl1z0=";
  101. return _preferences.getString(keyKey);
  102. }
  103. String getDocumentsDirectory() {
  104. return _documentsDirectory;
  105. }
  106. String getThumbnailsDirectory() {
  107. return _thumbnailsDirectory;
  108. }
  109. String getTempDirectory() {
  110. return _tempDirectory;
  111. }
  112. bool hasConfiguredAccount() {
  113. return getEndpoint() != null && getToken() != null;
  114. }
  115. }