Skip to content

Commit

Permalink
Notifications: Fix notification extension using outdated key (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
anian03 authored Feb 15, 2025
1 parent 024124b commit 1653397
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 2 additions & 4 deletions Sources/PushNotifications/PushNotificationCipher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import Common
class PushNotificationCipher {

static func decrypt(payload: String, iv: String) -> PushNotification? {
// Decode PrivateKey from base64 to String
guard let privateKey = UserSessionFactory.shared.getCurrentNotificationDeviceConfiguration()?.notificationsEncryptionKey,
let privateKeyAsData = Data(base64Encoded: privateKey) else {
guard let privateKey = UserSessionFactory.shared.getCurrentNotificationEncryptionKey() else {
return nil
}

Expand All @@ -24,7 +22,7 @@ class PushNotificationCipher {
return nil
}

let uint8Key = [UInt8](privateKeyAsData)
let uint8Key = [UInt8](privateKey)
let uint8Iv = [UInt8](ivAsData)

guard let payloadAsData = Data(base64Encoded: payload) else {
Expand Down
15 changes: 15 additions & 0 deletions Sources/UserStore/UserSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public class UserSession: ObservableObject {
notificationDeviceConfigurations.first(where: { $0.institutionIdentifier == institution && $0.username == username })
}

/// Gets the currently active key for decrypting notifications.
/// This force-loads the latest keychain data because the notification extension can outlive the app,
/// thus the key may have changed while the UserSession is still in memory
public func getCurrentNotificationEncryptionKey() -> Data? {
setupLoginData()
setupInstitutionSelection()
setupNotificationData()
guard let config = getCurrentNotificationDeviceConfiguration(),
let key = config.notificationsEncryptionKey,
let keyAsData = Data(base64Encoded: key) else {
return nil
}
return keyAsData
}

public func saveInstitution(identifier: InstitutionIdentifier?) {
self.institution = identifier

Expand Down

0 comments on commit 1653397

Please sign in to comment.