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