remember groupId in session cache
This commit is contained in:
parent
7eb9941f8c
commit
426782a001
@ -65,10 +65,15 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
// The OLM lib utility instance.
|
// The OLM lib utility instance.
|
||||||
private var olmUtility: OlmUtility? = null
|
private var olmUtility: OlmUtility? = null
|
||||||
|
|
||||||
|
private data class GroupSessionCacheItem(
|
||||||
|
val groupId: String,
|
||||||
|
val groupSession: OlmOutboundGroupSession
|
||||||
|
)
|
||||||
|
|
||||||
// The outbound group session.
|
// The outbound group session.
|
||||||
// Caches active outbound session to avoid to sync with DB before read
|
// Caches active outbound session to avoid to sync with DB before read
|
||||||
// The key is the session id, the value the outbound group session.
|
// The key is the session id, the value the <roomID,outbound group session>.
|
||||||
private val outboundGroupSessionCache: MutableMap<String, OlmOutboundGroupSession> = HashMap()
|
private val outboundGroupSessionCache: MutableMap<String, GroupSessionCacheItem> = HashMap()
|
||||||
|
|
||||||
// Store a set of decrypted message indexes for each group session.
|
// Store a set of decrypted message indexes for each group session.
|
||||||
// This partially mitigates a replay attack where a MITM resends a group
|
// This partially mitigates a replay attack where a MITM resends a group
|
||||||
@ -137,7 +142,7 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
fun release() {
|
fun release() {
|
||||||
olmUtility?.releaseUtility()
|
olmUtility?.releaseUtility()
|
||||||
outboundGroupSessionCache.values.forEach {
|
outboundGroupSessionCache.values.forEach {
|
||||||
it.releaseSession()
|
it.groupSession.releaseSession()
|
||||||
}
|
}
|
||||||
outboundGroupSessionCache.clear()
|
outboundGroupSessionCache.clear()
|
||||||
}
|
}
|
||||||
@ -415,7 +420,7 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
var session: OlmOutboundGroupSession? = null
|
var session: OlmOutboundGroupSession? = null
|
||||||
try {
|
try {
|
||||||
session = OlmOutboundGroupSession()
|
session = OlmOutboundGroupSession()
|
||||||
outboundGroupSessionCache[session.sessionIdentifier()] = session
|
outboundGroupSessionCache[session.sessionIdentifier()] = GroupSessionCacheItem(roomId, session)
|
||||||
store.storeCurrentOutboundGroupSessionForRoom(roomId, session)
|
store.storeCurrentOutboundGroupSessionForRoom(roomId, session)
|
||||||
return session.sessionIdentifier()
|
return session.sessionIdentifier()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@ -429,7 +434,7 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
|
|
||||||
fun storeOutboundGroupSessionForRoom(roomId: String, sessionId: String) {
|
fun storeOutboundGroupSessionForRoom(roomId: String, sessionId: String) {
|
||||||
outboundGroupSessionCache[sessionId]?.let {
|
outboundGroupSessionCache[sessionId]?.let {
|
||||||
store.storeCurrentOutboundGroupSessionForRoom(roomId, it)
|
store.storeCurrentOutboundGroupSessionForRoom(roomId, it.groupSession)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -438,7 +443,7 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
if (restoredOutboundGroupSession != null) {
|
if (restoredOutboundGroupSession != null) {
|
||||||
val sessionId = restoredOutboundGroupSession.outboundGroupSession.sessionIdentifier()
|
val sessionId = restoredOutboundGroupSession.outboundGroupSession.sessionIdentifier()
|
||||||
// cache it
|
// cache it
|
||||||
outboundGroupSessionCache[sessionId] = restoredOutboundGroupSession.outboundGroupSession
|
outboundGroupSessionCache[sessionId] = GroupSessionCacheItem(roomId, restoredOutboundGroupSession.outboundGroupSession)
|
||||||
|
|
||||||
return MXOutboundSessionInfo(
|
return MXOutboundSessionInfo(
|
||||||
sessionId = sessionId,
|
sessionId = sessionId,
|
||||||
@ -450,8 +455,12 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun discardOutboundGroupSessionForRoom(roomId: String) {
|
fun discardOutboundGroupSessionForRoom(roomId: String) {
|
||||||
store.getCurrentOutboundGroupSessionForRoom(roomId)?.outboundGroupSession?.sessionIdentifier()?.let { sessionId ->
|
val toDiscard = outboundGroupSessionCache.filter {
|
||||||
outboundGroupSessionCache.remove(sessionId)?.releaseSession()
|
it.value.groupId == roomId
|
||||||
|
}
|
||||||
|
toDiscard.forEach { (sessionId, cacheItem) ->
|
||||||
|
cacheItem.groupSession.releaseSession()
|
||||||
|
outboundGroupSessionCache.remove(sessionId)
|
||||||
}
|
}
|
||||||
store.storeCurrentOutboundGroupSessionForRoom(roomId, null)
|
store.storeCurrentOutboundGroupSessionForRoom(roomId, null)
|
||||||
}
|
}
|
||||||
@ -465,7 +474,7 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
fun getSessionKey(sessionId: String): String? {
|
fun getSessionKey(sessionId: String): String? {
|
||||||
if (sessionId.isNotEmpty()) {
|
if (sessionId.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
return outboundGroupSessionCache[sessionId]!!.sessionKey()
|
return outboundGroupSessionCache[sessionId]!!.groupSession.sessionKey()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e, "## getSessionKey() : failed")
|
Timber.e(e, "## getSessionKey() : failed")
|
||||||
}
|
}
|
||||||
@ -481,7 +490,7 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
*/
|
*/
|
||||||
fun getMessageIndex(sessionId: String): Int {
|
fun getMessageIndex(sessionId: String): Int {
|
||||||
return if (sessionId.isNotEmpty()) {
|
return if (sessionId.isNotEmpty()) {
|
||||||
outboundGroupSessionCache[sessionId]!!.messageIndex()
|
outboundGroupSessionCache[sessionId]!!.groupSession.messageIndex()
|
||||||
} else 0
|
} else 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -495,7 +504,7 @@ internal class MXOlmDevice @Inject constructor(
|
|||||||
fun encryptGroupMessage(sessionId: String, payloadString: String): String? {
|
fun encryptGroupMessage(sessionId: String, payloadString: String): String? {
|
||||||
if (sessionId.isNotEmpty() && payloadString.isNotEmpty()) {
|
if (sessionId.isNotEmpty() && payloadString.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
return outboundGroupSessionCache[sessionId]!!.encryptMessage(payloadString)
|
return outboundGroupSessionCache[sessionId]!!.groupSession.encryptMessage(payloadString)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e, "## encryptGroupMessage() : failed")
|
Timber.e(e, "## encryptGroupMessage() : failed")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user