Skip to content

Commit

Permalink
Fix opening LCP-protected publication in restricted mode (readium#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga authored Apr 15, 2024
1 parent 1f59afa commit 9d18784
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ internal class LcpContentProtection(
asset: ContainerAsset,
license: Try<LcpLicense, LcpError>
): Try<ContentProtection.OpenResult, ContentProtection.OpenError> {
// ContentProtectionService should not expose errors due to user cancellation
val error = license.failureOrNull()
.takeUnless { it is LcpError.MissingPassphrase }

val serviceFactory = LcpContentProtectionService
.createFactory(license.getOrNull(), license.failureOrNull())
.createFactory(license.getOrNull(), error)

val encryptionData =
when {
Expand Down
3 changes: 3 additions & 0 deletions readium/lcp/src/main/java/org/readium/r2/lcp/LcpError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public sealed class LcpError(
override val cause: Error? = null
) : Error {

public object MissingPassphrase :
LcpError("Passphrase is not available.")

/** The interaction is not available with this License. */
public object LicenseInteractionNotAvailable :
LcpError("This interaction is not available.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ internal class LicenseValidation(
transitionTo(State.failure(it.error))
}
on<Event.cancelled> {
if (DEBUG) Timber.d("State.cancelled)")
if (DEBUG) Timber.d("State.cancelled")
transitionTo(State.cancelled)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ internal class LicensesService(
val licenseDocument = LicenseDocument(lcpl)
Timber.d("license ${licenseDocument.json}")
fetchPublication(licenseDocument, destination, onProgress).let { Try.success(it) }
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
tryOrLog { destination.delete() }
Try.failure(LcpError.wrap(e))
Expand Down Expand Up @@ -338,7 +340,7 @@ internal class LicensesService(

// Both error and documents can be null if the user cancelled the passphrase prompt.
if (documents == null) {
throw CancellationException("License validation was interrupted.")
throw LcpException(LcpError.MissingPassphrase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun LcpError.toUserError(): UserError = when (this) {
is LcpError.Network ->
UserError(R.string.lcp_error_network, cause = this)

is LcpError.Runtime ->
is LcpError.Runtime, LcpError.MissingPassphrase ->
UserError(R.string.lcp_error_runtime, cause = this)
is LcpError.Unknown ->
UserError(R.string.lcp_error_unknown, cause = this)
Expand Down

0 comments on commit 9d18784

Please sign in to comment.