mirror of
https://github.com/maxgoedjen/secretive.git
synced 2025-04-10 17:47:19 +00:00
Cleanup durations.
This commit is contained in:
parent
6db08bce4b
commit
c69704acb2
@ -15,12 +15,29 @@ class Notifier {
|
|||||||
let updateCategory = UNNotificationCategory(identifier: Constants.updateCategoryIdentitifier, actions: [updateAction, ignoreAction], intentIdentifiers: [], options: [])
|
let updateCategory = UNNotificationCategory(identifier: Constants.updateCategoryIdentitifier, actions: [updateAction, ignoreAction], intentIdentifiers: [], options: [])
|
||||||
let criticalUpdateCategory = UNNotificationCategory(identifier: Constants.criticalUpdateCategoryIdentitifier, actions: [updateAction], intentIdentifiers: [], options: [])
|
let criticalUpdateCategory = UNNotificationCategory(identifier: Constants.criticalUpdateCategoryIdentitifier, actions: [updateAction], intentIdentifiers: [], options: [])
|
||||||
|
|
||||||
let persistForOneMinuteAction = UNNotificationAction(identifier: Constants.persistForOneMinuteActionIdentitifier, title: "1 Minute", options: [])
|
let rawDurations = [Measurement(value: 1, unit: UnitDuration.minutes),
|
||||||
let persistForFiveMinutesAction = UNNotificationAction(identifier: Constants.persistForFiveMinutesActionIdentitifier, title: "5 Minutes", options: [])
|
Measurement(value: 5, unit: UnitDuration.minutes),
|
||||||
let persistForOneHourAction = UNNotificationAction(identifier: Constants.persistForOneHourActionIdentitifier, title: "1 Hour", options: [])
|
Measurement(value: 1, unit: UnitDuration.hours),
|
||||||
let persistForOneDayAction = UNNotificationAction(identifier: Constants.persistForOneDayActionIdentitifier, title: "1 Day", options: [])
|
Measurement(value: 24, unit: UnitDuration.hours)
|
||||||
|
]
|
||||||
|
|
||||||
let persistAuthenticationCategory = UNNotificationCategory(identifier: Constants.persistAuthenticationCategoryIdentitifier, actions: [persistForOneMinuteAction, persistForFiveMinutesAction, persistForOneHourAction, persistForOneDayAction], intentIdentifiers: [], options: [])
|
let doNotPersistAction = UNNotificationAction(identifier: Constants.doNotPersistActionIdentitifier, title: "Do Not Unlock", options: [])
|
||||||
|
var allPersistenceActions = [doNotPersistAction]
|
||||||
|
|
||||||
|
let formatter = DateComponentsFormatter()
|
||||||
|
formatter.unitsStyle = .spellOut
|
||||||
|
formatter.allowedUnits = [.hour, .minute, .day]
|
||||||
|
|
||||||
|
for duration in rawDurations {
|
||||||
|
let seconds = duration.converted(to: .seconds).value
|
||||||
|
guard let string = formatter.string(from: seconds)?.capitalized else { continue }
|
||||||
|
let identifier = Constants.persistAuthenticationCategoryIdentitifier.appending("\(seconds)")
|
||||||
|
let action = UNNotificationAction(identifier: identifier, title: string, options: [])
|
||||||
|
notificationDelegate.persistOptions[identifier] = seconds
|
||||||
|
allPersistenceActions.append(action)
|
||||||
|
}
|
||||||
|
|
||||||
|
let persistAuthenticationCategory = UNNotificationCategory(identifier: Constants.persistAuthenticationCategoryIdentitifier, actions: allPersistenceActions, intentIdentifiers: [], options: [])
|
||||||
if persistAuthenticationCategory.responds(to: Selector(("actionsMenuTitle"))) {
|
if persistAuthenticationCategory.responds(to: Selector(("actionsMenuTitle"))) {
|
||||||
persistAuthenticationCategory.setValue("Leave Unlocked", forKey: "_actionsMenuTitle")
|
persistAuthenticationCategory.setValue("Leave Unlocked", forKey: "_actionsMenuTitle")
|
||||||
}
|
}
|
||||||
@ -101,10 +118,7 @@ extension Notifier {
|
|||||||
// Authorization persistence notificatoins
|
// Authorization persistence notificatoins
|
||||||
static let persistAuthenticationCategoryIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication"
|
static let persistAuthenticationCategoryIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication"
|
||||||
static let doNotPersistActionIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication.donotpersist"
|
static let doNotPersistActionIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication.donotpersist"
|
||||||
static let persistForOneMinuteActionIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication.persist1m"
|
static let persistForActionIdentitifierPrefix = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication.persist."
|
||||||
static let persistForFiveMinutesActionIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication.persist5m"
|
|
||||||
static let persistForOneHourActionIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication.persist1h"
|
|
||||||
static let persistForOneDayActionIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.persistauthentication.persist1d"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -114,6 +128,7 @@ class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
|
|||||||
fileprivate var release: Release?
|
fileprivate var release: Release?
|
||||||
fileprivate var ignore: ((Release) -> Void)?
|
fileprivate var ignore: ((Release) -> Void)?
|
||||||
fileprivate var persistAuthentication: ((TimeInterval?) -> Void)?
|
fileprivate var persistAuthentication: ((TimeInterval?) -> Void)?
|
||||||
|
fileprivate var persistOptions: [String: TimeInterval] = [:]
|
||||||
|
|
||||||
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
|
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
|
||||||
|
|
||||||
@ -146,21 +161,7 @@ class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handlePersistAuthenticationResponse(response: UNNotificationResponse) {
|
func handlePersistAuthenticationResponse(response: UNNotificationResponse) {
|
||||||
switch response.actionIdentifier {
|
persistAuthentication?(persistOptions[response.actionIdentifier])
|
||||||
case Notifier.Constants.doNotPersistActionIdentitifier, UNNotificationDefaultActionIdentifier:
|
|
||||||
break
|
|
||||||
case Notifier.Constants.persistForOneMinuteActionIdentitifier:
|
|
||||||
// TODO: CLEANUP CONSTANTS
|
|
||||||
persistAuthentication?(60)
|
|
||||||
case Notifier.Constants.persistForFiveMinutesActionIdentitifier:
|
|
||||||
persistAuthentication?(60 * 5)
|
|
||||||
case Notifier.Constants.persistForOneHourActionIdentitifier:
|
|
||||||
persistAuthentication?(60 * 60)
|
|
||||||
case Notifier.Constants.persistForOneDayActionIdentitifier:
|
|
||||||
persistAuthentication?(60 * 60 * 24)
|
|
||||||
default:
|
|
||||||
fatalError()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
||||||
|
@ -142,8 +142,18 @@ extension SecureEnclave {
|
|||||||
public func persistAuthentication(secret: Secret, forDuration duration: TimeInterval) throws {
|
public func persistAuthentication(secret: Secret, forDuration duration: TimeInterval) throws {
|
||||||
let newContext = LAContext()
|
let newContext = LAContext()
|
||||||
newContext.localizedCancelTitle = "Deny"
|
newContext.localizedCancelTitle = "Deny"
|
||||||
newContext.localizedReason = "unlock secret \"\(secret.name)\""
|
|
||||||
|
let formatter = DateComponentsFormatter()
|
||||||
|
formatter.unitsStyle = .spellOut
|
||||||
|
formatter.allowedUnits = [.hour, .minute, .day]
|
||||||
|
|
||||||
|
if let durationString = formatter.string(from: duration) {
|
||||||
|
newContext.localizedReason = "unlock secret \"\(secret.name)\" for \(durationString)"
|
||||||
|
} else {
|
||||||
|
newContext.localizedReason = "unlock secret \"\(secret.name)\""
|
||||||
|
}
|
||||||
newContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometricsOrWatch, localizedReason: newContext.localizedReason) { [weak self] success, _ in
|
newContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometricsOrWatch, localizedReason: newContext.localizedReason) { [weak self] success, _ in
|
||||||
|
guard success else { return }
|
||||||
let context = PersistentAuthenticationContext(secret: secret, context: newContext, duration: duration)
|
let context = PersistentAuthenticationContext(secret: secret, context: newContext, duration: duration)
|
||||||
self?.persistedAuthenticationContexts[secret] = context
|
self?.persistedAuthenticationContexts[secret] = context
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user