Add notification action to copy OTP or just inform about the copied OTP (#513)

* Add notification action to copy OTP or just inform about the copied OTP

The notification either shows the current OTP which can be copied by a notification action or it shows just a hint to inform about the copied OTP. This depends on the new option "autoCopyOTP".

* Extract method

* Set type and style one-time
This commit is contained in:
Danny Mösch 2021-10-01 19:32:14 +02:00 committed by GitHub
parent 63e7235978
commit e1cbcb5d7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 155 additions and 76 deletions

View file

@ -57,4 +57,6 @@ public extension DefaultsKeys {
var passwordGenerator: DefaultsKey<PasswordGenerator> { .init("passwordGenerator", defaultValue: PasswordGenerator()) }
var encryptInArmored: DefaultsKey<Bool> { .init("encryptInArmored", defaultValue: false) }
var autoCopyOTP: DefaultsKey<Bool> { .init("autoCopyOTP", defaultValue: false) }
}

View file

@ -47,6 +47,10 @@ public final class Globals {
public static let oneTimePasswordDots = "••••••"
public static let passwordFont = UIFont(name: "Courier-Bold", size: UIFont.labelFontSize - 1)
public static let otpNotification = bundleIdentifier + ".notification.otp"
public static let otpNotificationCategory = bundleIdentifier + ".notification.otp.category"
public static let otpNotificationCopyAction = bundleIdentifier + ".notification.otp.action.copy"
// UI related
public static let tableCellButtonSize = CGFloat(20.0)

View file

@ -0,0 +1,47 @@
//
// NotificationCenterDispatcher.swift
// passKit
//
// Created by Danny Moesch on 29.09.21.
// Copyright © 2021 Bob Sun. All rights reserved.
//
public class NotificationCenterDispatcher: NSObject, UNUserNotificationCenterDelegate {
public static let shared = NotificationCenterDispatcher()
public func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == Globals.otpNotificationCopyAction {
if let otp = response.notification.request.content.userInfo["otp"] as? String {
UIPasteboard.general.string = otp
}
}
completionHandler()
}
public static func showOTPNotification(password: Password) {
guard let otp = password.currentOtp else {
return
}
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.getNotificationSettings { state in
guard state.authorizationStatus == .authorized else {
return
}
let content = UNMutableNotificationContent()
if Defaults.autoCopyOTP {
content.title = "OTPForPasswordCopied".localize(password.name)
} else {
content.title = "OTPForPassword".localize(password.name)
content.body = otp
content.categoryIdentifier = Globals.otpNotificationCategory
content.userInfo = [
"path": password.namePath,
"otp": otp,
]
}
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: Globals.otpNotification, content: content, trigger: trigger)
notificationCenter.add(request)
}
}
}

View file

@ -70,22 +70,4 @@ public enum Utils {
return passphrase
}
}
public static func showNotificationWithOTP(password: Password) {
guard let otp = password.currentOtp else {
return
}
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.getNotificationSettings { state in
guard state.authorizationStatus == .authorized else {
return
}
let content = UNMutableNotificationContent()
content.title = "OTPFor".localize(password.name)
content.body = otp
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "otpNotification", content: content, trigger: trigger)
notificationCenter.add(request)
}
}
}