Fixed saving password on logging in unsecured mode

This commit is contained in:
CappielloAntonio 2022-01-11 15:50:22 +01:00
parent 2e8f8bd7b6
commit 1fce57e119
7 changed files with 31 additions and 16 deletions

View file

@ -87,7 +87,7 @@ public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ViewHolder
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Server server = servers.get(getBindingAdapterPosition()); Server server = servers.get(getBindingAdapterPosition());
saveServerPreference(server.getAddress(), server.getUsername(), server.getToken(), server.getSalt()); saveServerPreference(server.getServerId(), server.getAddress(), server.getUsername(), server.isLowSecurity() ? server.getPassword() : null, server.getToken(), server.getSalt());
refreshSubsonicClientInstance(); refreshSubsonicClientInstance();
SystemRepository systemRepository = new SystemRepository(App.getInstance()); SystemRepository systemRepository = new SystemRepository(App.getInstance());
@ -98,8 +98,8 @@ public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ViewHolder
} }
@Override @Override
public void onSuccess(String token, String salt) { public void onSuccess(String password, String token, String salt) {
saveServerPreference(null, null, token, salt); saveServerPreference(null, null, null, password, token, salt);
enter(); enter();
} }
}); });
@ -121,11 +121,14 @@ public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ViewHolder
mainActivity.goFromLogin(); mainActivity.goFromLogin();
} }
private void saveServerPreference(String server, String user, String token, String salt) { private void saveServerPreference(String serverId, String server, String user, String password, String token, String salt) {
if (user != null) PreferenceUtil.getInstance(context).setUser(user); if (user != null) PreferenceUtil.getInstance(context).setUser(user);
if (serverId != null) PreferenceUtil.getInstance(context).setServerId(serverId);
if (server != null) PreferenceUtil.getInstance(context).setServer(server); if (server != null) PreferenceUtil.getInstance(context).setServer(server);
if (password != null) PreferenceUtil.getInstance(context).setPassword(password);
if (token != null && salt != null) { if (token != null && salt != null) {
PreferenceUtil.getInstance(context).setPassword(password);
PreferenceUtil.getInstance(context).setToken(token); PreferenceUtil.getInstance(context).setToken(token);
PreferenceUtil.getInstance(context).setSalt(salt); PreferenceUtil.getInstance(context).setSalt(salt);
} }

View file

@ -21,9 +21,9 @@ import com.cappielloantonio.play.model.Server;
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
@Database( @Database(
version = 31, version = 32,
entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Playlist.class}, entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Playlist.class},
autoMigrations = { @AutoMigration(from = 30, to = 31) } autoMigrations = { @AutoMigration(from = 31, to = 32) }
) )
public abstract class AppDatabase extends RoomDatabase { public abstract class AppDatabase extends RoomDatabase {
private static final String TAG = "AppDatabase"; private static final String TAG = "AppDatabase";

View file

@ -4,5 +4,5 @@ public interface SystemCallback {
void onError(Exception exception); void onError(Exception exception);
void onSuccess(String token, String salt); void onSuccess(String password, String token, String salt);
} }

View file

@ -21,6 +21,9 @@ public class Server implements Parcelable {
@ColumnInfo(name = "username") @ColumnInfo(name = "username")
private String username; private String username;
@ColumnInfo(name = "password")
private String password;
@ColumnInfo(name = "address") @ColumnInfo(name = "address")
private String address; private String address;
@ -36,10 +39,11 @@ public class Server implements Parcelable {
@ColumnInfo(name = "low_security", defaultValue = "false") @ColumnInfo(name = "low_security", defaultValue = "false")
private boolean lowSecurity; private boolean lowSecurity;
public Server(@NonNull String serverId, String serverName, String username, String address, String token, String salt, long timestamp, boolean lowSecurity) { public Server(@NonNull String serverId, String serverName, String username, String password, String address, String token, String salt, long timestamp, boolean lowSecurity) {
this.serverId = serverId; this.serverId = serverId;
this.serverName = serverName; this.serverName = serverName;
this.username = username; this.username = username;
this.password = password;
this.address = address; this.address = address;
this.token = token; this.token = token;
this.salt = salt; this.salt = salt;
@ -72,6 +76,14 @@ public class Server implements Parcelable {
this.username = username; this.username = username;
} }
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() { public String getAddress() {
return address; return address;
} }

View file

@ -34,9 +34,10 @@ public class SystemRepository {
if (response.body().getStatus().getValue().equals(ResponseStatus.FAILED)) { if (response.body().getStatus().getValue().equals(ResponseStatus.FAILED)) {
callback.onError(new Exception(response.body().getError().getCode().getValue() + " - " + response.body().getError().getMessage())); callback.onError(new Exception(response.body().getError().getCode().getValue() + " - " + response.body().getError().getMessage()));
} else if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) { } else if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) {
String salt = response.raw().request().url().queryParameter("s"); String password = response.raw().request().url().queryParameter("p");
String token = response.raw().request().url().queryParameter("t"); String token = response.raw().request().url().queryParameter("t");
callback.onSuccess(token, salt); String salt = response.raw().request().url().queryParameter("s");
callback.onSuccess(password, token, salt);
} else { } else {
callback.onError(new Exception("Empty response")); callback.onError(new Exception("Empty response"));
} }

View file

@ -96,7 +96,7 @@ public class MainActivity extends BaseActivity {
initBottomSheet(); initBottomSheet();
initNavigation(); initNavigation();
if (PreferenceUtil.getInstance(this).getToken() != null) { if (PreferenceUtil.getInstance(this).getToken() != null && PreferenceUtil.getInstance(this).getSalt() != null) {
goFromLogin(); goFromLogin();
} else { } else {
goToLogin(); goToLogin();

View file

@ -148,8 +148,8 @@ public class ServerSignupDialog extends DialogFragment {
} }
@Override @Override
public void onSuccess(String token, String salt) { public void onSuccess(String password, String token, String salt) {
saveServerPreference(null, null, null, token, salt); saveServerPreference(null, null, password, token, salt);
if (directAccess) enter(); if (directAccess) enter();
} }
}); });
@ -167,13 +167,12 @@ public class ServerSignupDialog extends DialogFragment {
if (token != null && salt != null) { if (token != null && salt != null) {
String serverID = loginViewModel.getServerToEdit() != null ? loginViewModel.getServerToEdit().getServerId() : UUID.randomUUID().toString(); String serverID = loginViewModel.getServerToEdit() != null ? loginViewModel.getServerToEdit().getServerId() : UUID.randomUUID().toString();
PreferenceUtil.getInstance(context).setPassword(this.lowSecurity ? password : null);
PreferenceUtil.getInstance(context).setToken(token); PreferenceUtil.getInstance(context).setToken(token);
PreferenceUtil.getInstance(context).setSalt(salt); PreferenceUtil.getInstance(context).setSalt(salt);
PreferenceUtil.getInstance(context).setServerId(serverID); PreferenceUtil.getInstance(context).setServerId(serverID);
if (!lowSecurity) PreferenceUtil.getInstance(context).setPassword(null); loginViewModel.addServer(new Server(serverID, this.serverName, this.username, this.lowSecurity ? password : null, this.server, token, salt, System.currentTimeMillis(), this.lowSecurity));
loginViewModel.addServer(new Server(serverID, this.serverName, this.username, this.server, token, salt, System.currentTimeMillis(), this.lowSecurity));
} }
App.getSubsonicClientInstance(context, true); App.getSubsonicClientInstance(context, true);