Skip to content

Commit 9d18784

Browse files
authored
Fix opening LCP-protected publication in restricted mode (#494)
1 parent 1f59afa commit 9d18784

File tree

5 files changed

+13
-4
lines changed

5 files changed

+13
-4
lines changed

readium/lcp/src/main/java/org/readium/r2/lcp/LcpContentProtection.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ internal class LcpContentProtection(
8282
asset: ContainerAsset,
8383
license: Try<LcpLicense, LcpError>
8484
): Try<ContentProtection.OpenResult, ContentProtection.OpenError> {
85+
// ContentProtectionService should not expose errors due to user cancellation
86+
val error = license.failureOrNull()
87+
.takeUnless { it is LcpError.MissingPassphrase }
88+
8589
val serviceFactory = LcpContentProtectionService
86-
.createFactory(license.getOrNull(), license.failureOrNull())
90+
.createFactory(license.getOrNull(), error)
8791

8892
val encryptionData =
8993
when {

readium/lcp/src/main/java/org/readium/r2/lcp/LcpError.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public sealed class LcpError(
2121
override val cause: Error? = null
2222
) : Error {
2323

24+
public object MissingPassphrase :
25+
LcpError("Passphrase is not available.")
26+
2427
/** The interaction is not available with this License. */
2528
public object LicenseInteractionNotAvailable :
2629
LcpError("This interaction is not available.")

readium/lcp/src/main/java/org/readium/r2/lcp/license/LicenseValidation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ internal class LicenseValidation(
234234
transitionTo(State.failure(it.error))
235235
}
236236
on<Event.cancelled> {
237-
if (DEBUG) Timber.d("State.cancelled)")
237+
if (DEBUG) Timber.d("State.cancelled")
238238
transitionTo(State.cancelled)
239239
}
240240
}

readium/lcp/src/main/java/org/readium/r2/lcp/service/LicensesService.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ internal class LicensesService(
127127
val licenseDocument = LicenseDocument(lcpl)
128128
Timber.d("license ${licenseDocument.json}")
129129
fetchPublication(licenseDocument, destination, onProgress).let { Try.success(it) }
130+
} catch (e: CancellationException) {
131+
throw e
130132
} catch (e: Exception) {
131133
tryOrLog { destination.delete() }
132134
Try.failure(LcpError.wrap(e))
@@ -338,7 +340,7 @@ internal class LicensesService(
338340

339341
// Both error and documents can be null if the user cancelled the passphrase prompt.
340342
if (documents == null) {
341-
throw CancellationException("License validation was interrupted.")
343+
throw LcpException(LcpError.MissingPassphrase)
342344
}
343345
}
344346
}

test-app/src/main/java/org/readium/r2/testapp/domain/LcpUserError.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fun LcpError.toUserError(): UserError = when (this) {
2020
is LcpError.Network ->
2121
UserError(R.string.lcp_error_network, cause = this)
2222

23-
is LcpError.Runtime ->
23+
is LcpError.Runtime, LcpError.MissingPassphrase ->
2424
UserError(R.string.lcp_error_runtime, cause = this)
2525
is LcpError.Unknown ->
2626
UserError(R.string.lcp_error_unknown, cause = this)

0 commit comments

Comments
 (0)