Skip to content

Commit 1e6e895

Browse files
authored
Allow custom LCP device identifiers (#661)
1 parent 04c35ca commit 1e6e895

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ All notable changes to this project will be documented in this file. Take a look
1111
* Added `VisualNavigatorDelegate.navigatorContentInset(_:)` to customize the content and safe-area insets used by the navigator.
1212
* By default, the navigator uses the window's `safeAreaInsets`, which can cause content to shift when the status bar is shown or hidden (since those insets change). To avoid this, implement `navigatorContentInset(_:)` and return insets that remain stable across status bar visibility changes — for example, a top inset large enough to accommodate the maximum expected status bar height.
1313

14+
#### LCP
15+
16+
* Added an initializer parameter for providing a custom device identifier (contributed by [@dewantawsif](https://github.com/readium/swift-toolkit/pull/661)).
17+
* You must ensure the identifier is unique and stable for the device (persist and reuse across app launches).
18+
* Recommended: generate an app-scoped UUID and store it securely (e.g., in the Keychain); avoid hardware or advertising identifiers.
19+
1420
### Changed
1521

1622
#### Navigator

Sources/LCP/LCPService.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ public final class LCPService: Loggable {
2323

2424
/// - Parameter deviceName: Device name used when registering a license to an LSD server.
2525
/// If not provided, the device name will be the default `UIDevice.current.name`.
26+
/// - Parameter deviceId: Device ID used when registering a license to an LSD server.
27+
/// You must ensure the identifier is unique and stable for the device (persist and
28+
/// reuse across app launches). If not provided, the device ID will be generated as
29+
/// a random UUID.
2630
public init(
2731
client: LCPClient,
2832
licenseRepository: LCPLicenseRepository,
2933
passphraseRepository: LCPPassphraseRepository,
3034
assetRetriever: AssetRetriever,
3135
httpClient: HTTPClient,
32-
deviceName: String? = nil
36+
deviceName: String? = nil,
37+
deviceId: String? = nil
3338
) {
3439
// Determine whether the embedded liblcp.a is in production mode, by attempting to open a production license.
3540
let isProduction: Bool = {
@@ -50,6 +55,7 @@ public final class LCPService: Loggable {
5055
crl: CRLService(httpClient: httpClient),
5156
device: DeviceService(
5257
deviceName: deviceName ?? UIDevice.current.name,
58+
deviceId: deviceId,
5359
repository: licenseRepository,
5460
httpClient: httpClient
5561
),

Sources/LCP/Services/DeviceService.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@ final class DeviceService {
1212
private let httpClient: HTTPClient
1313

1414
/// Returns the device's name.
15-
var name: String
15+
let name: String
16+
/// Returns the device's ID
17+
let id: String
1618

1719
init(
1820
deviceName: String,
21+
deviceId: String?,
1922
repository: LCPLicenseRepository,
2023
httpClient: HTTPClient
2124
) {
2225
name = deviceName
23-
self.repository = repository
24-
self.httpClient = httpClient
25-
}
2626

27-
/// Returns the device ID, creates it if needed.
28-
var id: String {
29-
let defaults = UserDefaults.standard
30-
guard let deviceId = defaults.string(forKey: "lcp_device_id") else {
31-
let deviceId = UUID().description
32-
defaults.set(deviceId.description, forKey: "lcp_device_id")
33-
return deviceId.description
27+
if let providedId = deviceId {
28+
id = providedId
29+
} else if let savedId = UserDefaults.standard.string(forKey: "lcp_device_id") {
30+
id = savedId
31+
} else {
32+
let generatedId = UUID().uuidString
33+
UserDefaults.standard.set(generatedId, forKey: "lcp_device_id")
34+
id = generatedId
3435
}
35-
return deviceId
36+
37+
self.repository = repository
38+
self.httpClient = httpClient
3639
}
3740

3841
// Device ID and name as query parameters for HTTP requests.

0 commit comments

Comments
 (0)