|
|
@ -1,3 +1,4 @@ |
|
|
|
|
|
|
|
import 'dart:async'; |
|
|
|
import 'dart:convert'; |
|
|
|
import 'dart:convert'; |
|
|
|
import 'dart:ui'; |
|
|
|
import 'dart:ui'; |
|
|
|
|
|
|
|
|
|
|
@ -23,17 +24,17 @@ import 'package:nextcloud/notifications.dart' as notifications; |
|
|
|
class PushUtils { |
|
|
|
class PushUtils { |
|
|
|
const PushUtils._(); |
|
|
|
const PushUtils._(); |
|
|
|
|
|
|
|
|
|
|
|
static Future<notifications.RSAKeypair> loadRSAKeypair() async { |
|
|
|
static notifications.RSAKeypair loadRSAKeypair() { |
|
|
|
const storage = AppStorage(StorageKeys.notifications); |
|
|
|
const storage = AppStorage(StorageKeys.notifications); |
|
|
|
const keyDevicePrivateKey = 'device-private-key'; |
|
|
|
const keyDevicePrivateKey = 'device-private-key'; |
|
|
|
|
|
|
|
|
|
|
|
late notifications.RSAKeypair keypair; |
|
|
|
final notifications.RSAKeypair keypair; |
|
|
|
if (!storage.containsKey(keyDevicePrivateKey) || (storage.getString(keyDevicePrivateKey)?.isEmpty ?? true)) { |
|
|
|
if (!storage.containsKey(keyDevicePrivateKey) || (storage.getString(keyDevicePrivateKey)!.isEmpty)) { |
|
|
|
debugPrint('Generating RSA keys for push notifications'); |
|
|
|
debugPrint('Generating RSA keys for push notifications'); |
|
|
|
// The key size has to be 2048, other sizes are not accepted by Nextcloud (at the moment at least) |
|
|
|
// The key size has to be 2048, other sizes are not accepted by Nextcloud (at the moment at least) |
|
|
|
// ignore: avoid_redundant_argument_values |
|
|
|
// ignore: avoid_redundant_argument_values |
|
|
|
keypair = notifications.RSAKeypair.fromRandom(keySize: 2048); |
|
|
|
keypair = notifications.RSAKeypair.fromRandom(keySize: 2048); |
|
|
|
await storage.setString(keyDevicePrivateKey, keypair.privateKey.toPEM()); |
|
|
|
unawaited(storage.setString(keyDevicePrivateKey, keypair.privateKey.toPEM())); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
keypair = notifications.RSAKeypair(notifications.RSAPrivateKey.fromPEM(storage.getString(keyDevicePrivateKey)!)); |
|
|
|
keypair = notifications.RSAKeypair(notifications.RSAPrivateKey.fromPEM(storage.getString(keyDevicePrivateKey)!)); |
|
|
|
} |
|
|
|
} |
|
|
@ -74,16 +75,10 @@ class PushUtils { |
|
|
|
); |
|
|
|
); |
|
|
|
await NeonStorage.init(); |
|
|
|
await NeonStorage.init(); |
|
|
|
|
|
|
|
|
|
|
|
final keypair = await loadRSAKeypair(); |
|
|
|
final keypair = loadRSAKeypair(); |
|
|
|
|
|
|
|
|
|
|
|
for (final message in Uri(query: utf8.decode(messages)).queryParameters.values) { |
|
|
|
for (final message in Uri(query: utf8.decode(messages)).queryParameters.values) { |
|
|
|
final data = json.decode(message) as Map<String, dynamic>; |
|
|
|
final data = json.decode(message) as Map<String, dynamic>; |
|
|
|
final pushNotification = PushNotification( |
|
|
|
final pushNotification = PushNotification.fromEncrypted(data, keypair.privateKey); |
|
|
|
accountID: instance, |
|
|
|
|
|
|
|
priority: data['priority']! as String, |
|
|
|
|
|
|
|
type: data['type']! as String, |
|
|
|
|
|
|
|
subject: notifications.decryptPushNotificationSubject(keypair.privateKey, data['subject']! as String), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (pushNotification.subject.delete ?? false) { |
|
|
|
if (pushNotification.subject.delete ?? false) { |
|
|
|
await localNotificationsPlugin.cancel(_getNotificationID(instance, pushNotification)); |
|
|
|
await localNotificationsPlugin.cancel(_getNotificationID(instance, pushNotification)); |
|
|
|