RemoteSecretManagerImpl.kt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2025 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package ch.threema.localcrypto
  22. import ch.threema.base.ThreemaException
  23. import ch.threema.localcrypto.exceptions.BlockedByAdminException
  24. import ch.threema.localcrypto.exceptions.InvalidCredentialsException
  25. import ch.threema.localcrypto.exceptions.RemoteSecretMonitorException
  26. import ch.threema.localcrypto.models.MasterKeyState
  27. import ch.threema.localcrypto.models.RemoteSecretAuthenticationToken
  28. import ch.threema.localcrypto.models.RemoteSecretClientParameters
  29. import ch.threema.localcrypto.models.RemoteSecretCreationResult
  30. import ch.threema.localcrypto.models.RemoteSecretParameters
  31. import ch.threema.localcrypto.models.RemoteSecretProtectionCheckResult
  32. import java.io.IOException
  33. class RemoteSecretManagerImpl(
  34. private val remoteSecretClient: RemoteSecretClient,
  35. private val shouldUseRemoteSecretProtection: () -> Boolean,
  36. private val remoteSecretMonitor: RemoteSecretMonitor,
  37. private val getWorkServerBaseUrl: () -> String,
  38. ) : RemoteSecretManager {
  39. override fun checkRemoteSecretProtection(lockData: MasterKeyState.WithRemoteSecret?): RemoteSecretProtectionCheckResult {
  40. val shouldUseRemoteSecretProtection = shouldUseRemoteSecretProtection()
  41. val usesRemoteSecretProtection = lockData != null
  42. return when {
  43. shouldUseRemoteSecretProtection && !usesRemoteSecretProtection -> RemoteSecretProtectionCheckResult.SHOULD_ACTIVATE
  44. !shouldUseRemoteSecretProtection && usesRemoteSecretProtection -> RemoteSecretProtectionCheckResult.SHOULD_DEACTIVATE
  45. else -> RemoteSecretProtectionCheckResult.NO_CHANGE_NEEDED
  46. }
  47. }
  48. @Throws(ThreemaException::class, InvalidCredentialsException::class, IOException::class)
  49. override suspend fun createRemoteSecret(clientParameters: RemoteSecretClientParameters): RemoteSecretCreationResult =
  50. try {
  51. remoteSecretClient.createRemoteSecret(
  52. parameters = clientParameters,
  53. )
  54. } catch (e: RemoteSecretClient.RemoteSecretClientException) {
  55. throw ThreemaException("Failed to create remote secret", e)
  56. }
  57. @Throws(ThreemaException::class, InvalidCredentialsException::class, IOException::class)
  58. override suspend fun deleteRemoteSecret(
  59. clientParameters: RemoteSecretClientParameters,
  60. authenticationToken: RemoteSecretAuthenticationToken,
  61. ) {
  62. try {
  63. remoteSecretClient.deleteRemoteSecret(
  64. parameters = clientParameters,
  65. authenticationToken = authenticationToken,
  66. )
  67. } catch (e: RemoteSecretClient.RemoteSecretClientException) {
  68. throw ThreemaException("Failed to delete remote secret", e)
  69. }
  70. }
  71. @Throws(RemoteSecretMonitorException::class, BlockedByAdminException::class)
  72. override suspend fun monitorRemoteSecret(parameters: RemoteSecretParameters) {
  73. remoteSecretMonitor.monitor(
  74. baseUrl = getWorkServerBaseUrl(),
  75. parameters = parameters,
  76. )
  77. }
  78. override suspend fun awaitRemoteSecretAndClear() = remoteSecretMonitor.awaitRemoteSecretAndClear()
  79. }