DecryptNotification.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import UserNotifications
  2. extension UNNotificationContent {
  3. public func decrypt(state: PushNotificationState) throws -> PushNotification {
  4. // TODO: aes128gcm only uses p and x.
  5. guard let payload = (userInfo["p"] as? String)?.decode85(),
  6. let salt = (userInfo["s"] as? String)?.decode85(),
  7. let serverPublicKeyData = (userInfo["k"] as? String)?.decode85() else {
  8. throw DecryptNotificationErrorType.fieldsNotFound
  9. }
  10. let decrypted = try state.receiver.decrypt(payload: payload, salt: salt, serverPublicKeyData: serverPublicKeyData)
  11. return try JSONDecoder().decode(PushNotification.self, from: decrypted)
  12. }
  13. /* func decrypt(instanceRootSettings: SettingsStorage<AnyStringKey>) throws -> PushNotification {
  14. return try decrypt(settings: settings(instanceRootSettings: instanceRootSettings))
  15. }
  16. func decrypt(settings: SettingsStorage<InstanceKeys>) throws -> PushNotification {
  17. // TODO: aes128gcm only uses p and x.
  18. guard let payload = (userInfo["p"] as? String)?.decode85(),
  19. let salt = (userInfo["s"] as? String)?.decode85(),
  20. let serverPublicKeyData = (userInfo["k"] as? String)?.decode85() else {
  21. throw DecryptNotificationErrorType.fieldsNotFound
  22. }
  23. guard let state = settings.object(forKey: .pushNotificationState) as PushNotificationState? else {
  24. throw DecryptNotificationErrorType.subscriptionNotFound
  25. }
  26. let decrypted = try state.receiver.decrypt(payload: payload, salt: salt, serverPublicKeyData: serverPublicKeyData)
  27. return try JSONDecoder.mastodonDecoder.decode(PushNotification.self, from: decrypted)
  28. }
  29. func settings(instanceRootSettings: SettingsStorage<AnyStringKey>) throws -> SettingsStorage<InstanceKeys> {
  30. guard let identifier = userInfo["x"] as? String else {
  31. throw DecryptNotificationErrorType.fieldsNotFound
  32. }
  33. return instanceRootSettings.subSettings(forKey: AnyStringKey(identifier), keyedBy: InstanceKeys.self)
  34. }*/
  35. public func extraField() throws -> String {
  36. guard let extraField = userInfo["x"] as? String else {
  37. throw DecryptNotificationErrorType.fieldsNotFound
  38. }
  39. return extraField
  40. }
  41. }
  42. enum DecryptNotificationErrorType: String, Error {
  43. case fieldsNotFound
  44. }