ente/lib/core/configuration.dart

168 lines
5.2 KiB
Dart
Raw Normal View History

import 'dart:io' as io;
2020-08-15 01:22:14 +00:00
2020-08-17 21:08:23 +00:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:path_provider/path_provider.dart';
import 'package:photos/models/key_attributes.dart';
2020-04-30 15:09:41 +00:00
import 'package:shared_preferences/shared_preferences.dart';
import 'package:photos/utils/crypto_util.dart';
2020-04-30 15:09:41 +00:00
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 const keyKekSalt = "kek_salt";
static const keyKekHash = "kek_hash";
static const keyKekHashSalt = "kek_hash_salt";
static const keyEncryptedKeyIV = "encrypted_key_iv";
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
}
Future<KeyAttributes> generateAndSaveKey(String passphrase) async {
2020-08-15 01:22:14 +00:00
final key = CryptoUtil.getBase64EncodedSecureRandomString(length: 32);
final kekSalt = CryptoUtil.getBase64EncodedSecureRandomString(length: 32);
2020-09-06 06:30:26 +00:00
final kek = CryptoUtil.scrypt(passphrase, kekSalt, 32);
final kekHashSalt =
CryptoUtil.getBase64EncodedSecureRandomString(length: 32);
final kekHash = CryptoUtil.scrypt(kek, kekHashSalt, 32);
2020-09-06 06:30:26 +00:00
final iv = CryptoUtil.getBase64EncodedSecureRandomString(length: 16);
final encryptedKey = CryptoUtil.encryptToBase64(key, kek, iv);
final attributes =
KeyAttributes(kekSalt, kekHash, kekHashSalt, encryptedKey, iv);
await setKey(key);
await setKeyAttributes(attributes);
return attributes;
}
Future<void> decryptAndSaveKey(
String passphrase, KeyAttributes attributes) async {
2020-09-06 06:30:26 +00:00
final kek = CryptoUtil.scrypt(passphrase, attributes.kekSalt, 32);
bool correctPassphrase =
CryptoUtil.compareHash(kek, attributes.kekHash, attributes.kekHashSalt);
if (!correctPassphrase) {
throw Exception("Incorrect passphrase");
}
final key = CryptoUtil.decryptFromBase64(
attributes.encryptedKey, kek, attributes.encryptedKeyIV);
2020-08-15 01:22:14 +00:00
await setKey(key);
}
2020-04-30 15:18:26 +00:00
String getHttpEndpoint() {
2020-09-06 08:26:55 +00:00
return "http://api.staging.ente.io";
2020-04-30 15:18:26 +00:00
}
2020-09-06 06:30:26 +00:00
Future<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
}
2020-09-06 06:30:26 +00:00
Future<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-09-06 06:30:26 +00:00
Future<void> setEmail(String email) async {
2020-08-25 06:00:19 +00:00
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
}
2020-09-06 06:30:26 +00:00
Future<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
}
2020-09-06 06:30:26 +00:00
Future<void> setOptInForE2E(bool hasOptedForE2E) async {
await _preferences.setBool(hasOptedForE2EKey, hasOptedForE2E);
}
bool hasOptedForE2E() {
return true;
// return _preferences.getBool(hasOptedForE2EKey);
}
Future<void> setKeyAttributes(KeyAttributes attributes) async {
await _preferences.setString(
keyKekSalt, attributes == null ? null : attributes.kekSalt);
await _preferences.setString(
keyKekHash, attributes == null ? null : attributes.kekHash);
await _preferences.setString(
keyKekHashSalt, attributes == null ? null : attributes.kekHashSalt);
await _preferences.setString(
keyEncryptedKey, attributes == null ? null : attributes.encryptedKey);
await _preferences.setString(keyEncryptedKeyIV,
attributes == null ? null : attributes.encryptedKeyIV);
}
KeyAttributes getKeyAttributes() {
if (_preferences.getString(keyEncryptedKey) == null) {
return null;
}
return KeyAttributes(
_preferences.getString(keyKekSalt),
_preferences.getString(keyKekHash),
_preferences.getString(keyKekHashSalt),
_preferences.getString(keyEncryptedKey),
_preferences.getString(keyEncryptedKeyIV));
2020-08-15 01:22:14 +00:00
}
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-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 getToken() != null && getKey() != null;
}
2020-04-30 15:09:41 +00:00
}