Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit 4611197

Browse files
authored
Fix certificate detection
1 parent d7d0407 commit 4611197

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

Sources/prostore/certificates/CertificatesManager.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,37 @@ public final class CertificatesManager: ObservableObject {
2626
public static let shared = CertificatesManager()
2727
private init() {}
2828

29-
// The currently selected certificate identity (nil if none chosen).
30-
@Published public var selectedCertificate: SecIdentity? = nil
29+
// REMOVED: @Published public var selectedCertificate: SecIdentity? = nil
30+
31+
/// Returns the currently selected SecIdentity by loading from the selected folder in UserDefaults
32+
public var selectedIdentity: SecIdentity? {
33+
guard let folderName = UserDefaults.standard.string(forKey: "selectedCertificateFolder"),
34+
!folderName.isEmpty else {
35+
return nil
36+
}
37+
38+
let certDir = CertificateFileManager.shared.certificatesDirectory.appendingPathComponent(folderName)
39+
let p12URL = certDir.appendingPathComponent("certificate.p12")
40+
let pwURL = certDir.appendingPathComponent("password.txt")
41+
42+
guard let p12Data = try? Data(contentsOf: p12URL),
43+
let password = try? String(contentsOf: pwURL, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) else {
44+
return nil
45+
}
46+
47+
var items: CFArray?
48+
let options = [kSecImportExportPassphrase as String: password] as CFDictionary
49+
let status = SecPKCS12Import(p12Data as CFData, options, &items)
50+
51+
guard status == errSecSuccess,
52+
let cfItems = items as? [[String: Any]],
53+
let identityAny = cfItems.first?[kSecImportItemIdentity as String],
54+
CFGetTypeID(identityAny as CFTypeRef) == SecIdentityGetTypeID() else {
55+
return nil
56+
}
57+
58+
return identityAny as! SecIdentity
59+
}
3160

3261
// MARK: - Utility: SHA256 hex
3362
public static func sha256Hex(_ d: Data) -> String {

Sources/prostore/views/AppsDetailView.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public struct AppDetailView: View {
208208
Spacer()
209209
Button(action: {
210210
// Immediate certificate check from CertificatesManager
211-
if certificatesManager.selectedCertificate == nil {
211+
if certificatesManager.selectedIdentity == nil {
212212
showCertError = true
213213
return
214214
}
@@ -226,8 +226,8 @@ public struct AppDetailView: View {
226226
.cornerRadius(25)
227227
.shadow(radius: 5)
228228
}
229-
.disabled(certificatesManager.selectedCertificate == nil)
230-
.opacity(certificatesManager.selectedCertificate == nil ? 0.6 : 1.0)
229+
.disabled(certificatesManager.selectedIdentity == nil)
230+
.opacity(certificatesManager.selectedIdentity == nil ? 0.6 : 1.0)
231231
.padding(.trailing, 20)
232232
.padding(.bottom, 20)
233233
}
@@ -295,7 +295,7 @@ public struct AppDetailView: View {
295295
.foregroundColor(.red)
296296
} else if app.downloadURL != nil {
297297
Button(action: {
298-
if certificatesManager.selectedCertificate == nil {
298+
if certificatesManager.selectedIdentity == nil {
299299
showCertError = true
300300
return
301301
}
@@ -306,9 +306,8 @@ public struct AppDetailView: View {
306306
Text("Download")
307307
}
308308
}
309-
.disabled(certificatesManager.selectedCertificate == nil)
310-
.opacity(certificatesManager.selectedCertificate == nil ? 0.6 : 1.0)
311-
}
309+
.disabled(certificatesManager.selectedIdentity == nil)
310+
.opacity(certificatesManager.selectedIdentity == nil ? 0.6 : 1.0)
312311
}
313312
}
314313
.alert("Please select a certificate first!", isPresented: $showCertError) {

project.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ targets:
3636
properties:
3737
CFBundleDisplayName: "ProStore"
3838
CFBundleName: "prostore"
39-
CFBundleVersion: "49"
40-
CFBundleShortVersionString: "0.12.0d4"
39+
CFBundleVersion: "50"
40+
CFBundleShortVersionString: "0.12.0d5"
4141
UILaunchStoryboardName: "LaunchScreen"
4242
NSPrincipalClass: "UIApplication"
4343
NSAppTransportSecurity:

0 commit comments

Comments
 (0)