ente/lib/core/configuration.dart

92 lines
2.3 KiB
Dart
Raw Normal View History

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 tokenKey = "token";
static const usernameKey = "username";
static const userIDKey = "user_id";
static const passwordKey = "password";
static const hasOptedForE2EKey = "has_opted_for_e2e_encryption";
2020-08-09 20:40:55 +00:00
static const keyKey = "key";
2020-04-30 15:09:41 +00:00
SharedPreferences _preferences;
2020-04-30 15:09:41 +00:00
Future<void> init() async {
_preferences = await SharedPreferences.getInstance();
2020-04-30 15:09:41 +00:00
}
String getEndpoint() {
2020-08-09 20:40:55 +00:00
return _preferences.getString(endpointKey);
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
}
String getUsername() {
2020-08-09 20:40:55 +00:00
return _preferences.getString(usernameKey);
2020-04-30 15:09:41 +00:00
}
void setUsername(String username) async {
2020-08-09 20:40:55 +00:00
await _preferences.setString(usernameKey, username);
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
}
2020-04-30 15:09:41 +00:00
String getPassword() {
2020-08-09 20:40:55 +00:00
return _preferences.getString(passwordKey);
2020-04-30 15:09:41 +00:00
}
void setPassword(String password) async {
2020-08-09 20:40:55 +00:00
await _preferences.setString(passwordKey, password);
2020-04-30 15:09:41 +00:00
}
void setOptInForE2E(bool hasOptedForE2E) async {
await _preferences.setBool(hasOptedForE2EKey, hasOptedForE2E);
}
bool hasOptedForE2E() {
return _preferences.getBool(hasOptedForE2EKey);
}
2020-08-10 23:47:22 +00:00
Future<void> generateAndSaveKey(String passphrase) async {
final key = CryptoUtil.getBase64EncodedSecureRandomString(length: 32);
await _preferences.setString(keyKey, key);
}
2020-08-11 23:04:16 +00:00
// TODO: Encrypt with a passphrase and store in secure storage
String getKey() {
// return "hello";
2020-08-10 23:47:22 +00:00
return _preferences.getString(keyKey);
}
bool hasConfiguredAccount() {
return getEndpoint() != null && getToken() != null;
}
2020-04-30 15:09:41 +00:00
}