Merge pull request #2714 from vector-im/feature/bma/bulk_clean
Feature/bma/bulk clean
This commit is contained in:
commit
25a6b0ddfb
@ -46,47 +46,42 @@ internal class DefaultIdentityBulkLookupTask @Inject constructor(
|
|||||||
@UserId private val userId: String
|
@UserId private val userId: String
|
||||||
) : IdentityBulkLookupTask {
|
) : IdentityBulkLookupTask {
|
||||||
|
|
||||||
private fun getHashedAddresses(threePids: List<ThreePid>, pepper: String): List<String> {
|
|
||||||
return withOlmUtility { olmUtility ->
|
|
||||||
threePids.map { threePid ->
|
|
||||||
base64ToBase64Url(
|
|
||||||
olmUtility.sha256(threePid.value.toLowerCase(Locale.ROOT)
|
|
||||||
+ " " + threePid.toMedium() + " " + pepper)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun execute(params: IdentityBulkLookupTask.Params): List<FoundThreePid> {
|
override suspend fun execute(params: IdentityBulkLookupTask.Params): List<FoundThreePid> {
|
||||||
val identityAPI = getIdentityApiAndEnsureTerms(identityApiProvider, userId)
|
val identityAPI = getIdentityApiAndEnsureTerms(identityApiProvider, userId)
|
||||||
val identityData = identityStore.getIdentityData() ?: throw IdentityServiceError.NoIdentityServerConfigured
|
val identityData = identityStore.getIdentityData() ?: throw IdentityServiceError.NoIdentityServerConfigured
|
||||||
val pepper = identityData.hashLookupPepper
|
val pepper = identityData.hashLookupPepper
|
||||||
val hashDetailResponse = if (pepper == null) {
|
val hashDetailResponse = if (pepper == null) {
|
||||||
// We need to fetch the hash details first
|
// We need to fetch the hash details first
|
||||||
fetchAndStoreHashDetails(identityAPI)
|
fetchHashDetails(identityAPI)
|
||||||
|
.also { identityStore.setHashDetails(it) }
|
||||||
} else {
|
} else {
|
||||||
IdentityHashDetailResponse(pepper, identityData.hashLookupAlgorithm)
|
IdentityHashDetailResponse(pepper, identityData.hashLookupAlgorithm)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hashDetailResponse.algorithms.contains("sha256").not()) {
|
if (hashDetailResponse.algorithms.contains(IdentityHashDetailResponse.ALGORITHM_SHA256).not()) {
|
||||||
// TODO We should ask the user if he is ok to send their 3Pid in clear, but for the moment we do not do it
|
// TODO We should ask the user if he is ok to send their 3Pid in clear, but for the moment we do not do it
|
||||||
// Also, what we have in cache could be outdated, the identity server maybe now supports sha256
|
// Also, what we have in cache could be outdated, the identity server maybe now supports sha256
|
||||||
throw IdentityServiceError.BulkLookupSha256NotSupported
|
throw IdentityServiceError.BulkLookupSha256NotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
val lookupResult = lookUpInternal(identityAPI, params.threePids, hashDetailResponse, true)
|
val lookUpData = lookUpInternal(identityAPI, params.threePids, hashDetailResponse, true)
|
||||||
|
|
||||||
// Convert back to List<FoundThreePid>
|
// Convert back to List<FoundThreePid>
|
||||||
return handleSuccess(params.threePids, lookupResult.first, lookupResult.second)
|
return handleSuccess(params.threePids, lookUpData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data class LookUpData(
|
||||||
|
val hashedAddresses: List<String>,
|
||||||
|
val identityLookUpResponse: IdentityLookUpResponse
|
||||||
|
)
|
||||||
|
|
||||||
private suspend fun lookUpInternal(identityAPI: IdentityAPI,
|
private suspend fun lookUpInternal(identityAPI: IdentityAPI,
|
||||||
threePids: List<ThreePid>,
|
threePids: List<ThreePid>,
|
||||||
hashDetailResponse: IdentityHashDetailResponse,
|
hashDetailResponse: IdentityHashDetailResponse,
|
||||||
canRetry: Boolean): Pair<List<String>, IdentityLookUpResponse> {
|
canRetry: Boolean): LookUpData {
|
||||||
val hashedAddresses = getHashedAddresses(threePids, hashDetailResponse.pepper)
|
val hashedAddresses = getHashedAddresses(threePids, hashDetailResponse.pepper)
|
||||||
return try {
|
return try {
|
||||||
Pair(hashedAddresses,
|
LookUpData(hashedAddresses,
|
||||||
executeRequest(null) {
|
executeRequest(null) {
|
||||||
apiCall = identityAPI.lookup(IdentityLookUpParams(
|
apiCall = identityAPI.lookup(IdentityLookUpParams(
|
||||||
hashedAddresses,
|
hashedAddresses,
|
||||||
@ -98,23 +93,19 @@ internal class DefaultIdentityBulkLookupTask @Inject constructor(
|
|||||||
// Catch invalid hash pepper and retry
|
// Catch invalid hash pepper and retry
|
||||||
if (canRetry && failure is Failure.ServerError && failure.error.code == MatrixError.M_INVALID_PEPPER) {
|
if (canRetry && failure is Failure.ServerError && failure.error.code == MatrixError.M_INVALID_PEPPER) {
|
||||||
// This is not documented, but the error can contain the new pepper!
|
// This is not documented, but the error can contain the new pepper!
|
||||||
if (!failure.error.newLookupPepper.isNullOrEmpty()) {
|
val newHashDetailResponse = if (!failure.error.newLookupPepper.isNullOrEmpty()) {
|
||||||
// Store it and use it right now
|
// Store it and use it right now
|
||||||
hashDetailResponse.copy(pepper = failure.error.newLookupPepper)
|
hashDetailResponse.copy(pepper = failure.error.newLookupPepper)
|
||||||
.also { identityStore.setHashDetails(it) }
|
|
||||||
.let { lookUpInternal(identityAPI, threePids, it, false /* Avoid infinite loop */) }
|
|
||||||
} else {
|
} else {
|
||||||
// Retrieve the new hash details
|
// Retrieve the new hash details
|
||||||
val newHashDetailResponse = fetchAndStoreHashDetails(identityAPI)
|
fetchHashDetails(identityAPI)
|
||||||
|
}
|
||||||
if (hashDetailResponse.algorithms.contains(IdentityHashDetailResponse.ALGORITHM_SHA256).not()) {
|
.also { identityStore.setHashDetails(it) }
|
||||||
|
if (newHashDetailResponse.algorithms.contains(IdentityHashDetailResponse.ALGORITHM_SHA256).not()) {
|
||||||
// TODO We should ask the user if he is ok to send their 3Pid in clear, but for the moment we do not do it
|
// TODO We should ask the user if he is ok to send their 3Pid in clear, but for the moment we do not do it
|
||||||
// Also, what we have in cache is maybe outdated, the identity server maybe now support sha256
|
|
||||||
throw IdentityServiceError.BulkLookupSha256NotSupported
|
throw IdentityServiceError.BulkLookupSha256NotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
lookUpInternal(identityAPI, threePids, newHashDetailResponse, false /* Avoid infinite loop */)
|
lookUpInternal(identityAPI, threePids, newHashDetailResponse, false /* Avoid infinite loop */)
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Other error
|
// Other error
|
||||||
throw failure
|
throw failure
|
||||||
@ -122,16 +113,29 @@ internal class DefaultIdentityBulkLookupTask @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun fetchAndStoreHashDetails(identityAPI: IdentityAPI): IdentityHashDetailResponse {
|
private fun getHashedAddresses(threePids: List<ThreePid>, pepper: String): List<String> {
|
||||||
return executeRequest<IdentityHashDetailResponse>(null) {
|
return withOlmUtility { olmUtility ->
|
||||||
apiCall = identityAPI.hashDetails()
|
threePids.map { threePid ->
|
||||||
|
base64ToBase64Url(
|
||||||
|
olmUtility.sha256(threePid.value.toLowerCase(Locale.ROOT)
|
||||||
|
+ " " + threePid.toMedium() + " " + pepper)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.also { identityStore.setHashDetails(it) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleSuccess(threePids: List<ThreePid>, hashedAddresses: List<String>, identityLookUpResponse: IdentityLookUpResponse): List<FoundThreePid> {
|
private suspend fun fetchHashDetails(identityAPI: IdentityAPI): IdentityHashDetailResponse {
|
||||||
return identityLookUpResponse.mappings.keys.map { hashedAddress ->
|
return executeRequest(null) {
|
||||||
FoundThreePid(threePids[hashedAddresses.indexOf(hashedAddress)], identityLookUpResponse.mappings[hashedAddress] ?: error(""))
|
apiCall = identityAPI.hashDetails()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleSuccess(threePids: List<ThreePid>, lookupData: LookUpData): List<FoundThreePid> {
|
||||||
|
return lookupData.identityLookUpResponse.mappings.keys.map { hashedAddress ->
|
||||||
|
FoundThreePid(
|
||||||
|
threePids[lookupData.hashedAddresses.indexOf(hashedAddress)],
|
||||||
|
lookupData.identityLookUpResponse.mappings[hashedAddress] ?: error("")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user