Files
hapi/ios/HapiWidgets/WidgetKeychain.swift
T
5f511d7684 feat(ios): polish session widgets and live activity surfaces
- Group the large widget into running/active sections, show up to nine
  sessions, and use the last human prompt as the subtitle.
- Show compact status icons plus web-style relative times, right-aligned.
- Mirror the per-install push key into the protected App Group container so
  the Live Activity extension and NSE can decrypt while the data-protection
  Keychain is unavailable on the Lock Screen.
- Redesign the Lock Screen card and Dynamic Island compact/expanded/minimal
  states; Live Activities are now opt-in and recovered activities are ended
  when disabled.
- Rename the app and widget display name to Happy Coding; bump the local
  build to 10.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-07 00:22:34 +08:00

51 lines
2.0 KiB
Swift

import Foundation
import HapiProtocol
import OSLog
import Security
/// Read-only Keychain access to the per-install push key shared with the app
/// and the Notification Service Extension.
///
/// The widget target carries the same `run.hapi.companion.push` Keychain
/// access group. No `kSecAttrAccessGroup` is passed: Foundation reads the
/// first accessible group, matching the app/NSE lookup. Items are stored
/// `kSecAttrAccessibleAfterFirstUnlock`, so this works while the phone is
/// locked after its first unlock since boot.
enum WidgetKeychain {
static let service = "run.hapi.companion.push"
static let pushKeyAccount = "pushKey"
private static let log = Logger(subsystem: "run.hapi.companion.widget", category: "keychain")
static func pushKey() -> Data? {
pushKeyWithStatus().0
}
static func pushKeyWithStatus() -> (Data?, OSStatus) {
// WidgetKit can render a Live Activity on the lock screen where the
// data-protection Keychain is unavailable (-25291). The app mirrors
// the key into the App Group exactly for this path.
if let key = LiveActivityKeyStore.load() {
log.info("pushKey read from app group")
return (key, errSecSuccess)
}
log.error("app group pushKey unavailable; falling back to keychain")
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: pushKeyAccount,
kSecAttrAccessGroup as String: "X75CUV458P.com.wusu.hapi.push",
kSecUseDataProtectionKeychain as String: true,
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne
]
var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)
guard status == errSecSuccess else {
log.error("pushKey read failed: OSStatus \(status, privacy: .public)")
return (nil, status)
}
return (item as? Data, status)
}
}