configuration.dart 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import 'package:photos/utils/crypto_util.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. class Configuration {
  4. Configuration._privateConstructor();
  5. static final Configuration instance = Configuration._privateConstructor();
  6. static const endpointKey = "endpoint";
  7. static const tokenKey = "token";
  8. static const usernameKey = "username";
  9. static const userIDKey = "user_id";
  10. static const passwordKey = "password";
  11. static const hasOptedForE2EKey = "has_opted_for_e2e_encryption";
  12. static const keyKey = "key";
  13. SharedPreferences _preferences;
  14. Future<void> init() async {
  15. _preferences = await SharedPreferences.getInstance();
  16. }
  17. String getEndpoint() {
  18. return _preferences.getString(endpointKey);
  19. }
  20. String getHttpEndpoint() {
  21. if (getEndpoint() == null) {
  22. return "";
  23. }
  24. return "http://" + getEndpoint() + ":8080";
  25. }
  26. void setEndpoint(String endpoint) async {
  27. await _preferences.setString(endpointKey, endpoint);
  28. }
  29. String getToken() {
  30. return _preferences.getString(tokenKey);
  31. }
  32. void setToken(String token) async {
  33. await _preferences.setString(tokenKey, token);
  34. }
  35. String getUsername() {
  36. return _preferences.getString(usernameKey);
  37. }
  38. void setUsername(String username) async {
  39. await _preferences.setString(usernameKey, username);
  40. }
  41. int getUserID() {
  42. return _preferences.getInt(userIDKey);
  43. }
  44. void setUserID(int userID) async {
  45. await _preferences.setInt(userIDKey, userID);
  46. }
  47. String getPassword() {
  48. return _preferences.getString(passwordKey);
  49. }
  50. void setPassword(String password) async {
  51. await _preferences.setString(passwordKey, password);
  52. }
  53. void setOptInForE2E(bool hasOptedForE2E) async {
  54. await _preferences.setBool(hasOptedForE2EKey, hasOptedForE2E);
  55. }
  56. bool hasOptedForE2E() {
  57. return _preferences.getBool(hasOptedForE2EKey);
  58. }
  59. void generateAndSaveKey(String passphrase) async {
  60. final key = CryptoUtil.createCryptoRandomString();
  61. await _preferences.setString(keyKey, CryptoUtil.encrypt(key, passphrase));
  62. }
  63. String getKey(String passphrase) {
  64. return CryptoUtil.decrypt(_preferences.getString(keyKey), passphrase);
  65. }
  66. bool hasConfiguredAccount() {
  67. return getEndpoint() != null && getToken() != null;
  68. }
  69. }