mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Added authentication via Subsonic API
This commit is contained in:
parent
8a2e2f7000
commit
27cc3b8570
3 changed files with 83 additions and 38 deletions
|
|
@ -1,4 +1,53 @@
|
||||||
package com.cappielloantonio.play.subsonic.api.system;
|
package com.cappielloantonio.play.subsonic.api.system;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.cappielloantonio.play.subsonic.Subsonic;
|
||||||
|
import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
|
||||||
public class SystemClient {
|
public class SystemClient {
|
||||||
|
private static final String TAG = "SystemClient";
|
||||||
|
|
||||||
|
private Subsonic subsonic;
|
||||||
|
private Retrofit retrofit;
|
||||||
|
private SystemService systemService;
|
||||||
|
|
||||||
|
public SystemClient(Subsonic subsonic) {
|
||||||
|
this.subsonic = subsonic;
|
||||||
|
|
||||||
|
this.retrofit = new Retrofit.Builder()
|
||||||
|
.baseUrl(subsonic.getUrl())
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.client(getOkHttpClient())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
this.systemService = retrofit.create(SystemService.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<SubsonicResponse> ping(){
|
||||||
|
Log.d(TAG, "Requesting ping");
|
||||||
|
return systemService.ping(subsonic.getParams());
|
||||||
|
}
|
||||||
|
|
||||||
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
return new OkHttpClient.Builder()
|
||||||
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
|
||||||
|
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
|
||||||
|
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||||
|
|
||||||
|
return loggingInterceptor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,13 @@ package com.cappielloantonio.play.subsonic.api.system;
|
||||||
|
|
||||||
import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
|
import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Map;
|
||||||
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.http.GET;
|
import retrofit2.http.GET;
|
||||||
|
import retrofit2.http.QueryMap;
|
||||||
|
|
||||||
public interface SystemService {
|
public interface SystemService {
|
||||||
@GET("ping")
|
@GET("ping")
|
||||||
Call<List<SubsonicResponse>> ping();
|
Call<SubsonicResponse> ping(@QueryMap Map<String, String> params);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.cappielloantonio.play.ui.fragment;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
@ -13,12 +14,14 @@ import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
import com.cappielloantonio.play.App;
|
import com.cappielloantonio.play.App;
|
||||||
import com.cappielloantonio.play.databinding.FragmentLoginBinding;
|
import com.cappielloantonio.play.databinding.FragmentLoginBinding;
|
||||||
|
import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
|
||||||
import com.cappielloantonio.play.ui.activity.MainActivity;
|
import com.cappielloantonio.play.ui.activity.MainActivity;
|
||||||
import com.cappielloantonio.play.util.PreferenceUtil;
|
import com.cappielloantonio.play.util.PreferenceUtil;
|
||||||
|
|
||||||
import org.jellyfin.apiclient.interaction.Response;
|
import java.util.List;
|
||||||
import org.jellyfin.apiclient.model.system.SystemInfo;
|
|
||||||
import org.jellyfin.apiclient.model.users.AuthenticationResult;
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Callback;
|
||||||
|
|
||||||
public class LoginFragment extends Fragment {
|
public class LoginFragment extends Fragment {
|
||||||
private static final String TAG = "LoginFragment";
|
private static final String TAG = "LoginFragment";
|
||||||
|
|
@ -51,7 +54,7 @@ public class LoginFragment extends Fragment {
|
||||||
private void init() {
|
private void init() {
|
||||||
bind.loginButton.setOnClickListener(v -> {
|
bind.loginButton.setOnClickListener(v -> {
|
||||||
if (validateInput()) {
|
if (validateInput()) {
|
||||||
saveServerPreference(username, server);
|
saveServerPreference(server, username, password);
|
||||||
authenticate();
|
authenticate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -75,44 +78,36 @@ public class LoginFragment extends Fragment {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveServerPreference(String user, String server) {
|
private void saveServerPreference(String server, String user, String password) {
|
||||||
PreferenceUtil.getInstance(requireContext()).setUser(user);
|
PreferenceUtil.getInstance(requireContext()).setUser(user);
|
||||||
PreferenceUtil.getInstance(requireContext()).setServer(server);
|
PreferenceUtil.getInstance(requireContext()).setServer(server);
|
||||||
|
PreferenceUtil.getInstance(requireContext()).setPassword(password);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void authenticate() {
|
private void authenticate() {
|
||||||
App.getApiClientInstance(requireContext()).ChangeServerLocation(server);
|
App.getSubsonicClientInstance(requireContext())
|
||||||
App.getApiClientInstance(requireContext()).AuthenticateUserAsync(username, password, new Response<AuthenticationResult>() {
|
.getSystemClient()
|
||||||
@Override
|
.ping()
|
||||||
public void onResponse(AuthenticationResult result) {
|
.enqueue(new Callback<SubsonicResponse>() {
|
||||||
if (result.getAccessToken() == null) return;
|
@Override
|
||||||
enter(result.getUser().getId(), result.getAccessToken());
|
public void onResponse(Call<SubsonicResponse> call, retrofit2.Response<SubsonicResponse> response) {
|
||||||
}
|
if (!response.isSuccessful()) {
|
||||||
|
Log.d(TAG, "+++ onResponse() unsuccesful");
|
||||||
|
Log.d(TAG, "+++ " + response.message());
|
||||||
|
Toast.makeText(requireContext(), response.message(), Toast.LENGTH_LONG).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
Log.d(TAG, "+++ onResponse() succesful");
|
||||||
public void onError(Exception exception) {
|
Toast.makeText(requireContext(), "Login succesful", Toast.LENGTH_LONG).show();
|
||||||
if (exception.getMessage().contains("AuthFailureError")) {
|
}
|
||||||
Toast.makeText(requireContext(), "Fail to authenticate", Toast.LENGTH_SHORT).show();
|
|
||||||
} else {
|
|
||||||
Toast.makeText(requireContext(), "Server unreachable", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void enter(String user, String token) {
|
@Override
|
||||||
App.getApiClientInstance(requireContext()).GetSystemInfoAsync(new Response<SystemInfo>() {
|
public void onFailure(Call<SubsonicResponse> call, Throwable t) {
|
||||||
@Override
|
Log.d(TAG, "+++ onFailure()");
|
||||||
public void onResponse(SystemInfo result) {
|
Log.d(TAG, "+++ " + t.getMessage());
|
||||||
if (result.getVersion().charAt(0) == '1') {
|
Toast.makeText(requireContext(), t.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
PreferenceUtil.getInstance(requireContext()).setUser(user);
|
}
|
||||||
PreferenceUtil.getInstance(requireContext()).setToken(token);
|
});
|
||||||
|
|
||||||
activity.goFromLogin();
|
|
||||||
} else {
|
|
||||||
Toast.makeText(requireContext(), "Error version", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue