forked from DagAgren/toot-relay
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDecryptNotification.swift
56 lines (44 loc) · 2.1 KB
/
DecryptNotification.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import UserNotifications
extension UNNotificationContent {
public func decrypt(state: PushNotificationState) throws -> PushNotification {
// TODO: aes128gcm only uses p and x.
guard let payload = (userInfo["p"] as? String)?.decode85(),
let salt = (userInfo["s"] as? String)?.decode85(),
let serverPublicKeyData = (userInfo["k"] as? String)?.decode85() else {
throw DecryptNotificationErrorType.fieldsNotFound
}
let decrypted = try state.receiver.decrypt(payload: payload, salt: salt, serverPublicKeyData: serverPublicKeyData)
return try JSONDecoder().decode(PushNotification.self, from: decrypted)
}
/* func decrypt(instanceRootSettings: SettingsStorage<AnyStringKey>) throws -> PushNotification {
return try decrypt(settings: settings(instanceRootSettings: instanceRootSettings))
}
func decrypt(settings: SettingsStorage<InstanceKeys>) throws -> PushNotification {
// TODO: aes128gcm only uses p and x.
guard let payload = (userInfo["p"] as? String)?.decode85(),
let salt = (userInfo["s"] as? String)?.decode85(),
let serverPublicKeyData = (userInfo["k"] as? String)?.decode85() else {
throw DecryptNotificationErrorType.fieldsNotFound
}
guard let state = settings.object(forKey: .pushNotificationState) as PushNotificationState? else {
throw DecryptNotificationErrorType.subscriptionNotFound
}
let decrypted = try state.receiver.decrypt(payload: payload, salt: salt, serverPublicKeyData: serverPublicKeyData)
return try JSONDecoder.mastodonDecoder.decode(PushNotification.self, from: decrypted)
}
func settings(instanceRootSettings: SettingsStorage<AnyStringKey>) throws -> SettingsStorage<InstanceKeys> {
guard let identifier = userInfo["x"] as? String else {
throw DecryptNotificationErrorType.fieldsNotFound
}
return instanceRootSettings.subSettings(forKey: AnyStringKey(identifier), keyedBy: InstanceKeys.self)
}*/
public func extraField() throws -> String {
guard let extraField = userInfo["x"] as? String else {
throw DecryptNotificationErrorType.fieldsNotFound
}
return extraField
}
}
enum DecryptNotificationErrorType: String, Error {
case fieldsNotFound
}