mirror of
https://git.vectorsigma.ru/public/immich.git
synced 2026-07-29 00:18:18 +00:00
handle basic auth
This commit is contained in:
@@ -5,10 +5,12 @@ import android.content.SharedPreferences
|
||||
import android.security.KeyChain
|
||||
import androidx.core.content.edit
|
||||
import app.alextran.immich.BuildConfig
|
||||
import app.alextran.immich.NativeBuffer
|
||||
import okhttp3.Cache
|
||||
import okhttp3.ConnectionPool
|
||||
import okhttp3.Dispatcher
|
||||
import okhttp3.Headers
|
||||
import okhttp3.Credentials
|
||||
import okhttp3.OkHttpClient
|
||||
import org.json.JSONObject
|
||||
import java.io.ByteArrayInputStream
|
||||
@@ -178,9 +180,14 @@ object HttpClientManager {
|
||||
|
||||
return OkHttpClient.Builder()
|
||||
.addInterceptor {
|
||||
val builder = it.request().newBuilder()
|
||||
val request = it.request()
|
||||
val builder = request.newBuilder()
|
||||
builder.header("User-Agent", USER_AGENT)
|
||||
headers.forEach { (key, value) -> builder.header(key, value) }
|
||||
val url = request.url
|
||||
if (url.username.isNotEmpty()) {
|
||||
builder.header("Authorization", Credentials.basic(url.username, url.password))
|
||||
}
|
||||
it.proceed(builder.build())
|
||||
}
|
||||
.connectionPool(connectionPool)
|
||||
|
||||
@@ -15,6 +15,8 @@ import okhttp3.Callback
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import okhttp3.Credentials
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import org.chromium.net.CronetEngine
|
||||
import org.chromium.net.CronetException
|
||||
import org.chromium.net.UrlRequest
|
||||
@@ -190,6 +192,11 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
|
||||
val callback = FetchCallback(onSuccess, onFailure, ::onComplete)
|
||||
val requestBuilder = engine.newUrlRequestBuilder(url, callback, executor)
|
||||
HttpClientManager.headers.forEach { (key, value) -> requestBuilder.addHeader(key, value) }
|
||||
url.toHttpUrlOrNull()?.let { httpUrl ->
|
||||
if (httpUrl.username.isNotEmpty()) {
|
||||
requestBuilder.addHeader("Authorization", Credentials.basic(httpUrl.username, httpUrl.password))
|
||||
}
|
||||
}
|
||||
val request = requestBuilder.build()
|
||||
signal.setOnCancelListener(request::cancel)
|
||||
request.start()
|
||||
|
||||
@@ -68,15 +68,17 @@ class URLSessionManagerDelegate: NSObject, URLSessionTaskDelegate, URLSessionWeb
|
||||
didReceive challenge: URLAuthenticationChallenge,
|
||||
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
|
||||
) {
|
||||
handleChallenge(challenge, completionHandler: completionHandler)
|
||||
handleChallenge(challenge, task: task, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func handleChallenge(
|
||||
_ challenge: URLAuthenticationChallenge,
|
||||
task: URLSessionTask? = nil,
|
||||
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
|
||||
) {
|
||||
switch challenge.protectionSpace.authenticationMethod {
|
||||
case NSURLAuthenticationMethodClientCertificate: handleClientCertificate(completion: completionHandler)
|
||||
case NSURLAuthenticationMethodHTTPBasic: handleBasicAuth(task: task, completion: completionHandler)
|
||||
default: completionHandler(.performDefaultHandling, nil)
|
||||
}
|
||||
}
|
||||
@@ -101,4 +103,18 @@ class URLSessionManagerDelegate: NSObject, URLSessionTaskDelegate, URLSessionWeb
|
||||
}
|
||||
completion(.performDefaultHandling, nil)
|
||||
}
|
||||
|
||||
private func handleBasicAuth(
|
||||
task: URLSessionTask?,
|
||||
completion: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
|
||||
) {
|
||||
guard let url = task?.originalRequest?.url,
|
||||
let user = url.user,
|
||||
let password = url.password
|
||||
else {
|
||||
return completion(.performDefaultHandling, nil)
|
||||
}
|
||||
let credential = URLCredential(user: user, password: password, persistence: .forSession)
|
||||
completion(.useCredential, credential)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user