UpdateMonitor: Fix authentication with update monitor
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package net.buzzert.kordophone.backend.server
|
||||
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.Authenticator
|
||||
import okhttp3.HttpUrl
|
||||
@@ -13,14 +12,13 @@ import okhttp3.WebSocket
|
||||
import okhttp3.WebSocketListener
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.create
|
||||
import java.net.URL
|
||||
|
||||
interface APIClient {
|
||||
fun getAPIInterface(): APIInterface
|
||||
fun getWebSocketClient(
|
||||
serverPath: String,
|
||||
authToken: String? = null,
|
||||
queryParams: Map<String, String>?,
|
||||
listener: WebSocketListener
|
||||
): WebSocket
|
||||
}
|
||||
@@ -109,24 +107,35 @@ class RetrofitAPIClient(
|
||||
return retrofit.create(APIInterface::class.java)
|
||||
}
|
||||
|
||||
override fun getWebSocketClient(serverPath: String, authToken: String?, listener: WebSocketListener): WebSocket {
|
||||
val requestURL = baseURL.authenticatedWebSocketURL(serverPath, authToken)
|
||||
override fun getWebSocketClient(
|
||||
serverPath: String,
|
||||
queryParams: Map<String, String>?,
|
||||
listener: WebSocketListener
|
||||
): WebSocket {
|
||||
val params = (queryParams ?: hashMapOf<String, String>()).toMutableMap()
|
||||
|
||||
val authToken = tokenStore.authenticationToken
|
||||
if (authToken != null) {
|
||||
params["token"] = authToken
|
||||
}
|
||||
|
||||
val requestURL = baseURL.authenticatedWebSocketURL(serverPath, params)
|
||||
val request = Request.Builder()
|
||||
.url(requestURL)
|
||||
.build()
|
||||
|
||||
return OkHttpClient().newWebSocket(request, listener)
|
||||
return client.newWebSocket(request, listener)
|
||||
}
|
||||
}
|
||||
|
||||
fun URL.authenticatedWebSocketURL(serverPath: String, authToken: String? = null): URL {
|
||||
fun URL.authenticatedWebSocketURL(serverPath: String, params: Map<String, String>): URL {
|
||||
val baseURI = HttpUrl.parse(this.toString())!!
|
||||
val requestURL = baseURI.newBuilder()
|
||||
.host(baseURI.host())
|
||||
.addEncodedPathSegments(serverPath)
|
||||
|
||||
if (authToken != null) {
|
||||
requestURL.addQueryParameter("token", authToken)
|
||||
params.forEach { (key, value) ->
|
||||
requestURL.addQueryParameter(key, value)
|
||||
}
|
||||
|
||||
return URL(requestURL.build().toString())
|
||||
|
||||
@@ -17,9 +17,7 @@ import okhttp3.Response
|
||||
import okhttp3.WebSocket
|
||||
import okhttp3.WebSocketListener
|
||||
import okio.ByteString
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.lang.reflect.Type
|
||||
import kotlin.time.Duration
|
||||
|
||||
const val UPMON_LOG: String = "UpdateMonitor"
|
||||
|
||||
@@ -36,13 +34,18 @@ class UpdateMonitor(private val client: APIClient) : WebSocketListener() {
|
||||
private val updateItemsType: Type = object : TypeToken<ArrayList<UpdateItem>>() {}.type
|
||||
private var webSocket: WebSocket? = null
|
||||
private var needsSocketReconnect: Boolean = false
|
||||
private var messageSeq: Int = -1
|
||||
|
||||
private val _conversationChanged: MutableSharedFlow<Conversation> = MutableSharedFlow()
|
||||
private val _messageAdded: MutableSharedFlow<Message> = MutableSharedFlow()
|
||||
|
||||
fun beginMonitoringUpdates() {
|
||||
Log.d(UPMON_LOG, "Opening websocket connection")
|
||||
this.webSocket = client.getWebSocketClient("/updates", null, this)
|
||||
this.webSocket = client.getWebSocketClient(
|
||||
serverPath = "/updates",
|
||||
queryParams = mapOf("seq" to messageSeq.toString()),
|
||||
listener = this
|
||||
)
|
||||
}
|
||||
|
||||
fun stopMonitoringForUpdates() {
|
||||
@@ -65,6 +68,10 @@ class UpdateMonitor(private val client: APIClient) : WebSocketListener() {
|
||||
it.conversation = conversationChanged!!
|
||||
})
|
||||
}
|
||||
|
||||
if (updateItem.sequence > messageSeq) {
|
||||
messageSeq = updateItem.sequence
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user