common: Reflect new account API

This commit is contained in:
Elias Projahn 2020-05-13 13:49:19 +02:00
parent ea1469c0dd
commit b2d8dd1cfc
2 changed files with 13 additions and 54 deletions

View file

@ -141,16 +141,8 @@ class MusicusBackendState extends State<MusicusBackend> {
_updateClient(serverSettings); _updateClient(serverSettings);
}); });
settings.account.listen((accountSettings) { settings.account.listen((credentials) {
if (accountSettings != null) { client.credentials = credentials;
client.user = User(
name: accountSettings.username,
email: accountSettings.email,
password: accountSettings.password,
);
} else {
client.user = null;
}
}); });
// This will also check for existing account settings. // This will also check for existing account settings.
@ -184,23 +176,11 @@ class MusicusBackendState extends State<MusicusBackend> {
/// Create a new client based on [serverSettings]. /// Create a new client based on [serverSettings].
void _updateClient(MusicusServerSettings serverSettings) { void _updateClient(MusicusServerSettings serverSettings) {
client?.dispose(); client?.dispose();
User user;
final accountSettings = settings.account.value;
if (accountSettings != null) {
user = User(
name: accountSettings.username,
email: accountSettings.email,
password: accountSettings.password,
);
}
client = MusicusClient( client = MusicusClient(
host: serverSettings.host, host: serverSettings.host,
port: serverSettings.port, port: serverSettings.port,
basePath: serverSettings.apiPath, basePath: serverSettings.apiPath,
user: user, credentials: settings.account.value,
); );
} }

View file

@ -1,6 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:musicus_client/musicus_client.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
/// Interface for persisting settings. /// Interface for persisting settings.
@ -38,24 +39,6 @@ class MusicusServerSettings {
}); });
} }
/// Settings concerning the Musicus account.
class MusicusAccountSettings {
/// The user name to login as.
final String username;
/// An optional email address.
final String email;
/// The password for authentication.
final String password;
MusicusAccountSettings({
this.username,
this.email,
this.password,
});
}
/// Manager for all settings that are persisted. /// Manager for all settings that are persisted.
class MusicusSettings { class MusicusSettings {
static const defaultHost = 'musicus.johrpan.de'; static const defaultHost = 'musicus.johrpan.de';
@ -74,8 +57,8 @@ class MusicusSettings {
/// Musicus server to connect to. /// Musicus server to connect to.
final server = BehaviorSubject<MusicusServerSettings>(); final server = BehaviorSubject<MusicusServerSettings>();
/// Musicus account to login with. /// Credentials for the Musicus account to login as.
final account = BehaviorSubject<MusicusAccountSettings>(); final account = BehaviorSubject<MusicusAccountCredentials>();
/// Create a settings instance. /// Create a settings instance.
MusicusSettings(this.storage); MusicusSettings(this.storage);
@ -100,13 +83,11 @@ class MusicusSettings {
)); ));
final username = await storage.getString('accountUsername'); final username = await storage.getString('accountUsername');
final email = await storage.getString('accountEmail');
final passwordBase64 = await storage.getString('accountPassword'); final passwordBase64 = await storage.getString('accountPassword');
if (username != null) { if (username != null) {
account.add(MusicusAccountSettings( account.add(MusicusAccountCredentials(
username: username, username: username,
email: email,
password: utf8.decode(base64Decode(passwordBase64)), password: utf8.decode(base64Decode(passwordBase64)),
)); ));
} }
@ -139,28 +120,26 @@ class MusicusSettings {
)); ));
} }
/// Update the account settings. /// Update the account credentials.
/// ///
/// This will persist the new values and update the stream. /// This will persist the new values and update the stream.
Future<void> setAccount(MusicusAccountSettings accountSettings) async { Future<void> setAccount(MusicusAccountCredentials credentials) async {
await storage.setString('accountUsername', accountSettings.username); await storage.setString('accountUsername', credentials.username);
await storage.setString('accountEmail', accountSettings.email);
// IMPORTANT NOTE: We encode the password using Base64 to defend just the // IMPORTANT NOTE: We encode the password using Base64 to defend just the
// simplest of simplest attacks. This provides no additional security // simplest of simplest attacks. This provides no additional security
// besides the fact that the password looks a little bit encrypted. // besides the fact that the password looks a little bit encrypted.
await storage.setString( await storage.setString(
'accountPassword', 'accountPassword',
base64Encode(utf8.encode(accountSettings.password)), base64Encode(utf8.encode(credentials.password)),
); );
account.add(accountSettings); account.add(credentials);
} }
/// Delete the current account settings. /// Delete the current account credentials.
Future<void> clearAccount() async { Future<void> clearAccount() async {
await storage.setString('accountUsername', null); await storage.setString('accountUsername', null);
await storage.setString('accountEmail', null);
await storage.setString('accountPassword', null); await storage.setString('accountPassword', null);
account.add(null); account.add(null);