The salt and token are now calculated at each new access starting from the password saved in the database, based on the type of access

This commit is contained in:
CappielloAntonio 2022-02-05 23:33:08 +01:00
parent cc9afb8057
commit d408b82503
4 changed files with 38 additions and 38 deletions

View file

@ -50,12 +50,18 @@ public class App extends Application {
String password = PreferenceUtil.getInstance(context).getPassword(); String password = PreferenceUtil.getInstance(context).getPassword();
String token = PreferenceUtil.getInstance(context).getToken(); String token = PreferenceUtil.getInstance(context).getToken();
String salt = PreferenceUtil.getInstance(context).getSalt(); String salt = PreferenceUtil.getInstance(context).getSalt();
boolean isLowSecurity = PreferenceUtil.getInstance(context).isLowScurity();
SubsonicPreferences preferences = new SubsonicPreferences(); SubsonicPreferences preferences = new SubsonicPreferences();
preferences.setServerUrl(server); preferences.setServerUrl(server);
preferences.setUsername(username); preferences.setUsername(username);
preferences.setPassword(password); preferences.setAuthentication(password, token, salt, isLowSecurity);
preferences.setAuthentication(password, token, salt);
if (preferences.getAuthentication() != null) {
if (preferences.getAuthentication().getPassword() != null) PreferenceUtil.getInstance(context).setPassword(preferences.getAuthentication().getPassword());
if (preferences.getAuthentication().getToken() != null) PreferenceUtil.getInstance(context).setToken(preferences.getAuthentication().getToken());
if (preferences.getAuthentication().getSalt() != null) PreferenceUtil.getInstance(context).setSalt(preferences.getAuthentication().getSalt());
}
return new Subsonic(context, preferences); return new Subsonic(context, preferences);
} }

View file

@ -18,7 +18,6 @@ import com.cappielloantonio.play.model.Server;
import com.cappielloantonio.play.repository.SystemRepository; import com.cappielloantonio.play.repository.SystemRepository;
import com.cappielloantonio.play.ui.activity.MainActivity; import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.ui.dialog.ServerSignupDialog; import com.cappielloantonio.play.ui.dialog.ServerSignupDialog;
import com.cappielloantonio.play.util.MusicUtil;
import com.cappielloantonio.play.util.PreferenceUtil; import com.cappielloantonio.play.util.PreferenceUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -88,8 +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.getServerId(), server.getAddress(), server.getUsername(), server.isLowSecurity() ? server.getPassword() : null, server.getToken(), server.getSalt()); saveServerPreference(server.getServerId(), server.getAddress(), server.getUsername(), server.getPassword(), server.isLowSecurity());
refreshSubsonicClientInstance();
SystemRepository systemRepository = new SystemRepository(App.getInstance()); SystemRepository systemRepository = new SystemRepository(App.getInstance());
systemRepository.checkUserCredential(new SystemCallback() { systemRepository.checkUserCredential(new SystemCallback() {
@ -100,7 +98,6 @@ public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ViewHolder
@Override @Override
public void onSuccess(String password, String token, String salt) { public void onSuccess(String password, String token, String salt) {
saveServerPreference(null, null, null, password, token, salt);
enter(); enter();
} }
}); });
@ -122,21 +119,13 @@ public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ViewHolder
mainActivity.goFromLogin(); mainActivity.goFromLogin();
} }
private void saveServerPreference(String serverId, String server, String user, String password, String token, String salt) { private void saveServerPreference(String serverId, String server, String user, String password, boolean isLowSecurity) {
if (user != null) PreferenceUtil.getInstance(context).setUser(user); PreferenceUtil.getInstance(context).setServerId(serverId);
if (serverId != null) PreferenceUtil.getInstance(context).setServerId(serverId); PreferenceUtil.getInstance(context).setServer(server);
if (server != null) PreferenceUtil.getInstance(context).setServer(server); PreferenceUtil.getInstance(context).setUser(user);
if (password != null) PreferenceUtil.getInstance(context).setPassword(password); PreferenceUtil.getInstance(context).setPassword(password);
PreferenceUtil.getInstance(context).setLowSecurity(isLowSecurity);
if (token != null && salt != null) {
PreferenceUtil.getInstance(context).setPassword(password);
PreferenceUtil.getInstance(context).setToken(token);
PreferenceUtil.getInstance(context).setSalt(salt);
}
}
private void refreshSubsonicClientInstance() {
PreferenceUtil.getInstance(context).setServerId(servers.get(getBindingAdapterPosition()).getServerId());
App.getSubsonicClientInstance(context, true); App.getSubsonicClientInstance(context, true);
} }
} }

View file

@ -115,14 +115,15 @@ public class Subsonic {
public Map<String, String> getParams() { public Map<String, String> getParams() {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("u", preferences.getUsername()); params.put("u", preferences.getUsername());
params.put("s", preferences.getAuthentication().getSalt());
params.put("t", preferences.getAuthentication().getToken()); if (preferences.getAuthentication().getPassword() != null) params.put("p", preferences.getAuthentication().getPassword());
if (preferences.getAuthentication().getSalt() != null) params.put("s", preferences.getAuthentication().getSalt());
if (preferences.getAuthentication().getToken() != null) params.put("t", preferences.getAuthentication().getToken());
params.put("v", getApiVersion().getVersionString()); params.put("v", getApiVersion().getVersionString());
params.put("c", preferences.getClientName()); params.put("c", preferences.getClientName());
params.put("f", "xml"); params.put("f", "xml");
if (preferences.getPassword() != null && !preferences.getPassword().trim().equals("")) params.put("p", preferences.getPassword());
return params; return params;
} }
} }

View file

@ -7,7 +7,6 @@ import java.util.UUID;
public class SubsonicPreferences { public class SubsonicPreferences {
private String serverUrl; private String serverUrl;
private String username; private String username;
private String password;
private String clientName = "Play"; private String clientName = "Play";
private SubsonicAuthentication authentication; private SubsonicAuthentication authentication;
@ -19,10 +18,6 @@ public class SubsonicPreferences {
return username; return username;
} }
public String getPassword() {
return password;
}
public String getClientName() { public String getClientName() {
return clientName; return clientName;
} }
@ -39,26 +34,31 @@ public class SubsonicPreferences {
this.username = username; this.username = username;
} }
public void setPassword(String password) {
this.password = password;
}
public void setClientName(String clientName) { public void setClientName(String clientName) {
this.clientName = clientName; this.clientName = clientName;
} }
public void setAuthentication(String password, String token, String salt) { public void setAuthentication(String password, String token, String salt, boolean isLowSecurity) {
if (password != null) this.authentication = new SubsonicAuthentication(password); if (password != null) {
if (token != null && salt != null) this.authentication = new SubsonicAuthentication(password, isLowSecurity);
}
if (token != null && salt != null) {
this.authentication = new SubsonicAuthentication(token, salt); this.authentication = new SubsonicAuthentication(token, salt);
}
} }
public static class SubsonicAuthentication { public static class SubsonicAuthentication {
private String password;
private String salt; private String salt;
private String token; private String token;
public SubsonicAuthentication(String password) { public SubsonicAuthentication(String password, boolean isLowSecurity) {
update(password); if (isLowSecurity) {
this.password = password;
} else {
update(password);
}
} }
public SubsonicAuthentication(String token, String salt) { public SubsonicAuthentication(String token, String salt) {
@ -66,6 +66,10 @@ public class SubsonicPreferences {
this.salt = salt; this.salt = salt;
} }
public String getPassword() {
return password;
}
public String getSalt() { public String getSalt() {
return salt; return salt;
} }