ente/lib/core/configuration.dart

140 lines
3.8 KiB
Dart
Raw Normal View History

2020-08-15 01:22:14 +00:00
import 'dart:convert';
import 'package:crypto/crypto.dart';
2020-08-17 21:08:23 +00:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'dart:io' as io;
import 'package:path_provider/path_provider.dart';
import 'package:photos/utils/crypto_util.dart';
2020-04-30 15:09:41 +00:00
import 'package:shared_preferences/shared_preferences.dart';
class Configuration {
Configuration._privateConstructor();
static final Configuration instance = Configuration._privateConstructor();
2020-08-09 20:40:55 +00:00
static const endpointKey = "endpoint";
static const userIDKey = "user_id";
2020-08-25 06:00:19 +00:00
static const emailKey = "email";
static const tokenKey = "token";
static const hasOptedForE2EKey = "has_opted_for_e2e_encryption";
2020-08-09 20:40:55 +00:00
static const keyKey = "key";
2020-08-15 01:22:14 +00:00
static const keyEncryptedKey = "encrypted_key";
static final String iv = base64.encode(List.filled(16, 0));
2020-04-30 15:09:41 +00:00
SharedPreferences _preferences;
2020-08-17 21:08:23 +00:00
FlutterSecureStorage _secureStorage;
String _key;
String _documentsDirectory;
String _tempDirectory;
String _thumbnailsDirectory;
2020-04-30 15:09:41 +00:00
Future<void> init() async {
_preferences = await SharedPreferences.getInstance();
2020-08-17 21:08:23 +00:00
_secureStorage = FlutterSecureStorage();
_documentsDirectory = (await getApplicationDocumentsDirectory()).path;
_tempDirectory = _documentsDirectory + "/temp/";
_thumbnailsDirectory = _documentsDirectory + "/thumbnails/";
new io.Directory(_tempDirectory).createSync(recursive: true);
new io.Directory(_thumbnailsDirectory).createSync(recursive: true);
2020-08-17 21:08:23 +00:00
_key = await _secureStorage.read(key: keyKey);
2020-04-30 15:09:41 +00:00
}
2020-08-15 01:22:14 +00:00
Future<void> generateAndSaveKey(String passphrase) async {
final key = CryptoUtil.getBase64EncodedSecureRandomString(length: 32);
await setKey(key);
final hashedPassphrase = sha256.convert(passphrase.codeUnits);
final encryptedKey = CryptoUtil.encryptToBase64(
key, base64.encode(hashedPassphrase.bytes), iv);
await setEncryptedKey(encryptedKey);
}
2020-04-30 15:09:41 +00:00
String getEndpoint() {
2020-08-25 06:00:19 +00:00
return "192.168.0.106";
2020-04-30 15:09:41 +00:00
}
2020-04-30 15:18:26 +00:00
String getHttpEndpoint() {
if (getEndpoint() == null) {
return "";
}
2020-04-30 15:18:26 +00:00
return "http://" + getEndpoint() + ":8080";
}
2020-04-30 15:09:41 +00:00
void setEndpoint(String endpoint) async {
2020-08-09 20:40:55 +00:00
await _preferences.setString(endpointKey, endpoint);
2020-04-30 15:09:41 +00:00
}
String getToken() {
2020-08-09 20:40:55 +00:00
return _preferences.getString(tokenKey);
2020-04-30 15:09:41 +00:00
}
void setToken(String token) async {
2020-08-09 20:40:55 +00:00
await _preferences.setString(tokenKey, token);
2020-04-30 15:09:41 +00:00
}
2020-08-25 06:00:19 +00:00
String getEmail() {
return _preferences.getString(emailKey);
2020-04-30 15:09:41 +00:00
}
2020-08-25 06:00:19 +00:00
void setEmail(String email) async {
await _preferences.setString(emailKey, email);
2020-04-30 15:09:41 +00:00
}
2020-07-31 21:26:53 +00:00
int getUserID() {
2020-08-09 20:40:55 +00:00
return _preferences.getInt(userIDKey);
2020-07-31 21:26:53 +00:00
}
void setUserID(int userID) async {
2020-08-09 20:40:55 +00:00
await _preferences.setInt(userIDKey, userID);
2020-07-31 21:26:53 +00:00
}
void setOptInForE2E(bool hasOptedForE2E) async {
await _preferences.setBool(hasOptedForE2EKey, hasOptedForE2E);
}
bool hasOptedForE2E() {
return true;
// return _preferences.getBool(hasOptedForE2EKey);
}
2020-08-15 01:22:14 +00:00
Future<void> setEncryptedKey(String encryptedKey) async {
await _preferences.setString(keyEncryptedKey, encryptedKey);
}
String getEncryptedKey() {
return _preferences.getString(keyEncryptedKey);
}
Future<void> setKey(String key) async {
2020-08-17 21:08:23 +00:00
await _secureStorage.write(key: keyKey, value: key);
_key = key;
}
2020-08-15 01:22:14 +00:00
Future<void> decryptEncryptedKey(String passphrase) async {
final hashedPassphrase = sha256.convert(passphrase.codeUnits);
final encryptedKey = getEncryptedKey();
final key = CryptoUtil.decryptFromBase64(
encryptedKey, base64.encode(hashedPassphrase.bytes), iv);
await setKey(key);
}
2020-08-11 23:04:16 +00:00
String getKey() {
2020-08-17 21:08:23 +00:00
return _key;
}
String getDocumentsDirectory() {
return _documentsDirectory;
}
String getThumbnailsDirectory() {
return _thumbnailsDirectory;
}
String getTempDirectory() {
return _tempDirectory;
}
bool hasConfiguredAccount() {
return getEndpoint() != null && getToken() != null;
}
2020-04-30 15:09:41 +00:00
}