Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP basic auth #257

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
Expand Down Expand Up @@ -111,7 +113,7 @@ public static String createUrl(String item, int size) {

public static class Builder {
private final RequestManager requestManager;
private Object item;
private String item;

private Builder(Context context, String item, ResourceType type) {
this.requestManager = Glide.with(context);
Expand All @@ -128,9 +130,22 @@ public static Builder from(Context context, String item, ResourceType type) {
}

public RequestBuilder<Drawable> build() {
return requestManager
.load(item)
.transition(DrawableTransitionOptions.withCrossFade());

RequestBuilder<Drawable> requestBuilder;

String basicAuthHeader = App.getSubsonicClientInstance(false).getBasicAuthHeader();
if (basicAuthHeader == null || item == null) {
requestBuilder = requestManager.load(item);
} else {
//Add HTTP basic auth
requestBuilder = requestManager.load(new GlideUrl(item,
new LazyHeaders.Builder()
.addHeader("Authorization", basicAuthHeader)
.build()
));
}

return requestBuilder.transition(DrawableTransitionOptions.withCrossFade());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@ import com.cappielloantonio.tempo.App
import com.cappielloantonio.tempo.subsonic.utils.CacheUtil
import com.google.gson.GsonBuilder
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException
import java.util.concurrent.TimeUnit

class RetrofitClient(subsonic: Subsonic) {
var retrofit: Retrofit

init {

retrofit = Retrofit.Builder()
.baseUrl(subsonic.url)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create()))
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
.client(getOkHttpClient())
.client(getOkHttpClient(subsonic))
.build()
}

private fun getOkHttpClient(): OkHttpClient {
private fun getOkHttpClient(subsonic: Subsonic): OkHttpClient {
val cacheUtil = CacheUtil(60, 60 * 60 * 24 * 30)

// BrowsingClient 60
Expand All @@ -35,7 +40,7 @@ class RetrofitClient(subsonic: Subsonic) {
// SystemClient 60
// AlbumSongListClient 60

return OkHttpClient.Builder()
val okHttpClient = OkHttpClient.Builder()
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
Expand All @@ -44,7 +49,12 @@ class RetrofitClient(subsonic: Subsonic) {
.addInterceptor(cacheUtil.offlineInterceptor)
// .addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build()

if (subsonic.basicAuthHeader != null) {
okHttpClient.addInterceptor(BasicAuthInterceptor(subsonic.basicAuthHeader))
}

return okHttpClient.build();
}

private fun getHttpLoggingInterceptor(): HttpLoggingInterceptor {
Expand All @@ -57,4 +67,15 @@ class RetrofitClient(subsonic: Subsonic) {
val cacheSize = 10 * 1024 * 1024
return Cache(App.getContext().cacheDir, cacheSize.toLong())
}

class BasicAuthInterceptor(private val basicAuthHeader: String) : Interceptor {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request: Request = chain.request()
val authenticatedRequest: Request = request.newBuilder()
.header("Authorization", basicAuthHeader).build()
return chain.proceed(authenticatedRequest)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cappielloantonio.tempo.subsonic;

import android.util.Base64;

import com.cappielloantonio.tempo.subsonic.api.albumsonglist.AlbumSongListClient;
import com.cappielloantonio.tempo.subsonic.api.bookmarks.BookmarksClient;
import com.cappielloantonio.tempo.subsonic.api.browsing.BrowsingClient;
Expand All @@ -15,6 +17,7 @@
import com.cappielloantonio.tempo.subsonic.api.system.SystemClient;
import com.cappielloantonio.tempo.subsonic.base.Version;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -159,4 +162,13 @@ public Map<String, String> getParams() {

return params;
}

public String getBasicAuthHeader() {
URI parsedUri = URI.create(getUrl());
if (parsedUri.getUserInfo() != null) {
return "Basic " + Base64.encodeToString(parsedUri.getUserInfo().getBytes(), Base64.NO_WRAP);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import androidx.media3.exoplayer.offline.DownloadManager;
import androidx.media3.exoplayer.offline.DownloadNotificationHelper;

import com.cappielloantonio.tempo.App;
import com.cappielloantonio.tempo.service.DownloaderManager;

import java.io.File;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;

@UnstableApi
Expand Down Expand Up @@ -74,6 +77,13 @@ public static synchronized DataSource.Factory getHttpDataSourceFactory() {
httpDataSourceFactory = new DefaultHttpDataSource
.Factory()
.setAllowCrossProtocolRedirects(true);

String basicAuthHeader = App.getSubsonicClientInstance(false).getBasicAuthHeader();
if (basicAuthHeader != null) {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", basicAuthHeader);
((DefaultHttpDataSource.Factory) httpDataSourceFactory).setDefaultRequestProperties(headers);
}
}

return httpDataSourceFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ object Preferences {

@JvmStatic
fun isInUseServerAddressLocal(): Boolean {
return getInUseServerAddress() == getLocalAddress()
return getInUseServerAddress() == getLocalAddress() && !getLocalAddress().isNullOrEmpty()
}

@JvmStatic
Expand All @@ -187,7 +187,7 @@ object Preferences {
fun isServerSwitchable(): Boolean {
return App.getInstance().preferences.getLong(
NEXT_SERVER_SWITCH, 0
) + 15000 < System.currentTimeMillis()
) + 15000 < System.currentTimeMillis() && !getServer().isNullOrEmpty() && !getLocalAddress().isNullOrEmpty()
}

@JvmStatic
Expand Down